带有 fifo 的 C 程序不工作,Unix 控制台等待输入

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

我想制作一个使用 fifo 的简单程序。我编译了这段代码,当我 运行 它时,控制台正在等待输入。我试图将 printf 放在第一行,但它没有出现在控制台上。

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;
}

在控制台我运行它是这样的

./x

没有任何反应,只是光标移动到下一行并等待输入。

为什么我的程序不是运行ning?

来自 mkfifo() 手册页:

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.

因此,在您调用 open() 之后,您的进程将暂停,直到另一个进程以读取权限打开 fifo。在你的情况下,这永远不会发生。