简体   繁体   English

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

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

I want to make a simple program that uses fifo.我想制作一个使用 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.我试图将 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;
}

In console I run it like this在控制台中我像这样运行它

./x

and nothing happens, just the cursor is going next line and is waiting for input.什么也没有发生,只是 cursor 正在下一行等待输入。

Why is my program not running?为什么我的程序没有运行?

From the mkfifo() man page:mkfifo()手册页:

Opening a FIFO for reading normally blocks until some other process opens the same FIFO for writing, and vice versa.打开一个 FIFO 进行读取通常会阻塞,直到某个其他进程打开同一个 FIFO 进行写入,反之亦然。 See fifo(7) for nonblocking handling of FIFO special files.有关 FIFO 特殊文件的非阻塞处理,请参见 fifo(7)。

So after your call to open(), your process is put on hold until another process opens the fifo with read access.因此,在您调用 open() 之后,您的进程将被暂停,直到另一个进程打开具有读取访问权限的 fifo。 Which in your case never happens.在你的情况下,这永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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