- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我使用 mkfifo 创建命名管道。然后我使用下面的程序打开它。但是,程序卡在“fopen”行。这里有什么问题吗?
int main(int argc, char** argv) {
char* line = "hello, world!";
FILE* fp = fopen("/tmp/myFIFO", "rw");
fprintf(fp, line);
fclose(fp);
return 0;
}
最佳答案
尝试将 "w"
作为模式传递给 fopen。 "rw"
不是 fopen
的有效模式参数,即使是,您也可能不希望在同一进程中同时读取和写入 FIFO (虽然有可能,见下文)。
<支持>顺便说一句,打开文件进行读取和写入的正确模式参数是 "r+"
或 "w+"
(参见 this question for the differences 的答案)。
此程序将正确写入 FIFO:
#include <stdio.h>
int main(int argc, char** argv) {
FILE* fp = fopen("/tmp/myFIFO", "w");
fprintf(fp, "Hello, world!\n");
fclose(fp);
return 0;
}
请注意,上述程序中的fopen
将阻塞,直到打开FIFO 进行读取。当它阻塞时,在另一个终端中运行它:
$ cat /tmp/myFIFO
Hello, world!
$
之所以阻塞是因为fopen
没有把O_NONBLOCK
传给open
:
$ strace -P /tmp/myFIFO ./a.out
open("/tmp/myFIFO", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
...
只读,没有 O_NONBLOCK
:open
阻塞,直到另一个进程打开 FIFO 进行写入。这是将 fopen
与模式参数 "r"
一起使用时的行为。
只写,没有 O_NONBLOCK
:open
阻塞,直到另一个进程打开 FIFO 进行读取。这是将 fopen
与模式参数 "w"
一起使用时的行为。
只读,带有 O_NONBLOCK
:open
立即返回。
只写,使用 O_NONBLOCK
:open
返回错误,errno
设置为 ENXIO
,除非另一个进程已打开 FIFO 以供读取。
<支持>来自 W. Richard Stevens 的“UNIX 环境中的高级编程”的信息。
在 Linux 中也可以在同一进程中打开 FIFO 进行读取和写入。 Linux FIFO man page状态:
Under Linux, opening a FIFO for read and write will succeed both in blocking and nonblocking mode. POSIX leaves this behavior undefined. This can be used to open a FIFO for writing while there are no readers available. A process that uses both ends of the connection in order to communicate with itself should be very careful to avoid deadlocks.
这是一个写入和读取同一个 FIFO 的程序:
#include <stdio.h>
int main(int argc, const char *argv[]) {
char buf[100] = {0};
FILE* fp = fopen("/tmp/myFIFO", "r+");
fprintf(fp, "Hello, world!\n");
fgets(buf, sizeof(buf), fp);
printf("%s", buf);
fclose(fp);
return 0;
}
不阻塞,立即返回:
$ gcc fifo.c && ./a.out
Hello, world!
请注意,这是不可移植的,可能无法在 Linux 以外的操作系统上运行。
关于c++ - 为什么我的程序在打开 mkfifo 管道时会挂起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8507810/
我遇到了以下代码的问题: #include #include #include #include #include int main(int argc, char*argv[]){ FILE
我有一个不断需要写入信息的进程。此外,还有第二个进程有时会连接到写入进程的“信息 channel ”,并且应该读取连接后写入的信息。此过程也可能会再次断开连接并重新连接几次。 我目前通过在我的 C++
我的 C 程序中的以下行应提供 All/Group/Owner 读写权限 mkfifo("/tmp/dumbPipe", 0666) 但是一旦我执行了代码并检查了未设置写入位的权限,我最终得到了 pr
我正在尝试使用 mkfifo 命令在 linux 下的 c 上创建一个命名管道。但是当我运行该程序时,我要么收到“没有这样的文件或目录”错误,要么什么也没有(控制台不显示任何内容) 这是我的代码: #
#include #include #include #include #include #include #include int main(void) { if(mkfifo
据我了解,根据https://linux.die.net/man/3/mkfifo , 我得到一个暗示,我必须有读取器和写入器文件,才能 利用管道文件。下面的来源是编写器文件, #include #
我有以下代码,我从 Xcode 中的 Swift 主程序调用它,例如在虚拟 iPhone 的模拟器中运行它时,它可以工作。它创建/tmp/MYFIFO。 int32_t init_udpC(void)
我在命名 Linux BASH、命名管道等方面相当陌生。我正在关注这篇文章中的一个例子: https://www.linuxjournal.com/content/using-named-pipes-
我正在编写一个脚本来设置机器环境。我有能力在机器上放置一个启动脚本,所以我试图在我的“工作站”机器上使用 unix 中的命名管道在线。 问题是,我无法控制这些机器何时可以联机。总共有 20 多台机器同
gcc (GCC) 4.7.2 c89 我正在使用管道 mkfifo。我有一个读者和一个作家。 我希望读者阻塞,直到文件中有内容。 有一个标志可以设置为非阻塞模式的 O_NONBLOCK。所以默认情况
我使用 mkfifo 创建命名管道。然后我使用下面的程序打开它。但是,程序卡在“fopen”行。这里有什么问题吗? int main(int argc, char** argv) { char* li
我想执行一个 bash 脚本来执行以下操作: 应用程序“ffmpeg”生成一个实时传输流 (.ts) 文件。我需要处理这个实时流(执行解复用等)。 现在我知道这必须通过 FIFO 来完成;但这是我的任
我正在使用命名管道来捕获另一个程序 (MATLAB) 中的外部程序 (wgrib2) 的输出。 MATLAB 代码如下,system()访问命令行以制作管道: system('mkfifo myfif
我已经尝试调试这个问题几个小时了,但我仍然卡住了...... 我在这段代码中遇到了“mkfifo”调用的段错误(它只是我整个代码的一部分,因为我认为其余部分与这里无关): #include "mark
我有一个动态加载的库,可以计算一个特殊值。我的程序需要访问该值(在 C 或 C++ 中)。我想到了命名管道。但他们对于这个目的似乎有些奇怪。如果通过管道实现,我的库会在文件中写入一个值,然后我的程序会
我正在尝试用 C 创建命名管道,但没有成功。 这是我的代码: pid_t pid = getpid() ; char * pipeNameo = malloc( sizeof(char) * 100
我正在尝试用 C 创建命名管道,但没有成功。 这是我的代码: #define FIFO_NAME "/tmp/myfifo" int main(){ int fd; fd = mkfi
我正在尝试编写两个使用 fifo 管道相互通信的程序。我使用了例子here (第 5.2 节),但我将那里的 mknod 更改为 mkfifo 并尝试将 gets 更改为 fgets。这是代码(写入
我试图用 FIFO 做一些简单的事情:读取行,但不是一次全部读取,结果出乎意料地“不起作用”。 没关系: $ f=$(mktemp -u) $ mkfifo $f $ { seq 5 > $f; }
我使用 mkfifo 创建命名管道。然后我用下面的程序打开它。但是,程序在“fopen”行挂起。这里有什么问题吗? int main(int argc, char** argv) { char* li
我是一名优秀的程序员,十分优秀!