简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM