Using named pipes in a single process

I am trying to use a named pipe for communication within a process. Here is the code

#include <stdio.h>
#include <fcntl.h>
#include <signal.h>

void sigint(int num)
{
    int fd = open("np", O_WRONLY);
    write(fd, "y", 1);
    close(fd);
}

main()
{
    char ch[1];
    int fd;

    mkfifo("np", 0666);

    signal(SIGINT, sigint);

    fd = open("np", O_RDONLY);

    read(fd, ch, 1);

    close(fd);

    printf("%c\n", ch[0]);
    return;
}

I want the main unit to lock until something is written to the handset. The problem is that after opening the pipe, it blocks the sigint () signal. Is it assumed that the pipe is already open for reading earlier in main ()?

+3
source share
2 answers

You block open () by opening fifo to read blocks until someone opens it for writing.

And opening fifo to write blocks until someone opens it for reading.

the signal handler runs in the same thread as your main (), so you get a dead end. None of them will be able to open fifo.

, , strace.

+3

man:

FIFO , - FIFO , .

FIFO . , ; ENXIO ( ), .

Linux, FIFO . POSIX undefined. FIFO . , .

+1

Source: https://habr.com/ru/post/1732533/


All Articles