Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my program hang when opening a mkfifo-ed pipe?

Tags:

c++

c

mkfifo

I use mkfifo to create a named pipe. Then I use the following program to open it. However, the program hangs at the line "fopen". Is there something wrong here?

int main(int argc, char** argv) {
char* line = "hello, world!";
FILE* fp = fopen("/tmp/myFIFO", "rw");
fprintf(fp, line);
fclose(fp);
return 0;
}
like image 959
flyingbin Avatar asked Dec 14 '11 16:12

flyingbin


People also ask

Why use a named pipe?

Named pipes allow transfer of data between processes in a FIFO manner and synchronization of process execution. Use of a named pipe allows processes to communicate even though they do not know what processes are on the other end of the pipe.

What does mkfifo function returns on successful creation of a named pipe?

If successful, mkfifo() returns 0. If unsuccessful, mkfifo() does not create a FIFO file, returns -1, and sets errno to one of the following values: Error Code.

What does mkfifo create named pipe?

The named pipe is created with the mkfifo system call. A named pipe is much like a traditional pipe, created with the pipe system call. However, while pipe provides access via two file descriptors, the named pipe is accessed via the filesystem at a path. #include <sys/types.


1 Answers

The process blocks until the other end of the pipe gets opened.

like image 71
NPE Avatar answered Sep 20 '22 03:09

NPE