Lucian Gabriel
Lucian Gabriel

Reputation: 321

C program with fifo is not working, Unix console waits for input

I want to make a simple program that uses fifo. I compiled this code and when I run it the console is waiting for an input. I tried to put a printf on first line and it doesn t appear on console.

int main(){
char* fifo = "./f"; 
int x = mkfifo(fifo, 0700);
if ( x == -1){
    perror("error open");
    exit(EXIT_FAILURE);
}
int f = open (fifo, O_WRONLY);
if ( f == -1){
    perror("error open");
    exit(EXIT_FAILURE);
}
close(f);
unlink(fifo);
return 0;
}

In console I run it like this

./x

and nothing happens, just the cursor is going next line and is waiting for input.

Why is my program not running?

Upvotes: 1

Views: 636

Answers (1)

Julien Thierry
Julien Thierry

Reputation: 661

From the mkfifo() man page:

Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa. See fifo(7) for nonblocking handling of FIFO special files.

So after your call to open(), your process is put on hold until another process opens the fifo with read access. Which in your case never happens.

Upvotes: 4

Related Questions