- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是制作人。
// speak.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for readers...\n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuff\n");
while (gets(s), !feof(stdin)) {
if ((num = write(fd, s, strlen(s))) == -1)
perror("write");
else
printf("speak: wrote %d bytes\n", num);
}
return 0;
}
这就是消费者。
//tick.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "american_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for writers...\n");
fd = open(FIFO_NAME, O_RDONLY);
printf("got a writer\n");
do {
if ((num = read(fd, s, 300)) == -1)
perror("read");
else {
s[num] = '\0';
printf("tick: read %d bytes: \"%s\"\n", num, s);
}
} while (num > 0);
return 0;
}
当我运行它们时,Producer 输出,
waiting for readers...
和消费者产出,
waiting for writers...
speak 找不到读者,打勾。来自 theory here我明白了,open() (speak.c) 将一直被阻塞,直到 open() (tick.c) 打开。反之亦然。所以我猜想有一个僵局或发生了什么事。我需要一个解决方案。
最佳答案
看起来您在读者和作者之间存在竞争条件。
要解决此问题,您需要一种在编写器处于“事件状态”之前不启动读取器的方法。为此,我建议制作一个管道并在编写器准备好时写入它。然后,当从 fork 的读取端读取成功时,fifo 准备就绪,读取器应该工作。
您需要在此处使用 fork,因为协调父进程和子进程之间的互斥量非常重要,而且正确完成管道更容易。
此外,您调用了两次 mknod()
。当然,它会返回 -1 和 errno == EEXIST
,但要更加小心。为避免这种情况,让读取器和写入器成为一个以路径作为参数的函数。
将 writer 重写为 int speak(const char *fifo, int pipefd)
,将 reader 重写为 int tick(const char *fifo)
。
然后像这样制作一个包装器:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
const char fifo_name[] /* = ... */;
int speak(const char *fifo, int pipefd);
int tick(const char *fifo);
int main() {
int pipefd[2];
pipe(pipefd);
mknod(fifo_name, S_IFIFO | 0666, 0);
if (fork() == 0) {
close(pipefd[0]);
return speak(fifo_name, pipefd[1]);
} else {
close(pipefd[1]);
char foo;
read(pipefd[0], &foo, 1);
return tick(fifo_name);
}
}
修改您的编写器以在创建 fifo 后(即在调用 open(..., O_WRONLY)
之后)向传递的 fd 打印一个字节(任何内容)。
不要逐字使用我的代码,因为为了简洁起见,我省略了错误检查。
关于c - IPC FIFO 生产者-消费者死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10570184/
Chisel 是否支持设计中的多个时钟?如果我想实现一个异步 fifo,如何在 Chisel 中实现呢? 最佳答案 是的,Chisel 支持设计中的多个时钟。如果你想使用异步fifo,你可以导入模块C
当在单个进程上使用 FIFO 时,看起来两端都打开然后关闭,不可能重用 FIFO。任何重新打开封闭端的尝试都会失败,或者返回的文件描述符无用。 是否有可能解决此问题,还是我们必须保持 FIFO 的两端
考虑以下场景: 名为 test 的 FIFO被 build 。在一个终端窗口 (A) 中,我运行 cat test 。现在可以在窗口 B 中写入并在窗口 A 中获取输出。也可以终止进程 A 并重新启动
是否有某种方法可用于在真正的 FIFO 中使用 Amazon SQS 消息?我尝试从配置延迟消息传递,但这没有帮助。 最佳答案 亚马逊SQS刚刚获得FIFO Queues with Exactly-O
我有一个非常简单的基本程序,它有两个进程,第一个是parent,第二个是child。 子进程应该向 FIFO 写入一些内容。在所有写作工作完成后(在 child 被终止后)。然后父进程应该读取所有
在 C 中使用 epoll,有什么方法可以让从 FIFO 读取的程序检测到写入同一 FIFO 的程序已将其关闭? 我以为这会生成一个 EPOLLHUP 事件,但显然它不会。 例子:我有一个读者和一个作
在 bash 脚本中,我使用管道将命令的输出传递给另一个命令的输入: $ output_com | input_com output_com 比 input_com 慢。在我的脚本中,在input_c
我试图在 3.12 内核中自己实现 FIFO 调度程序,我无法找到 FIFO 文件的位置...就像 rt.c 位于/kernel/sched/rt.c 最佳答案 您在寻找哪个调度程序? Linux 有
这个问题在这里已经有了答案: Implementing FIFO using LIFO (2 个答案) 关闭 6 年前。 我了解使 Push 操作昂贵或 pop 操作昂贵的传统方法。 如何让push
我正在学习 Linux 中的管道编程,但无法理解管道/FIFO 管理。 我写了一个小程序来打开我创建的 FIFO(在执行程序之前,我在终端中执行了 mkfifo newfifo)。然后我反复读取并转储
我有一个写入标准输出的进程,我希望能够在运行 tail -f 时通过 grepping 查找各种字符串来监视输出。执行此操作的一种方法是写入一个普通文件,然后对文件进行尾部搜索一个字符串,然后对另一个
我想将一些数据输出到管道并让其他进程逐行处理数据。这是一个玩具示例: mkfifo pipe cat pipe& cat >pipe 现在我可以输入我想要的任何内容,按下回车后我立即看到同一行。但是如
我正在尝试开发一个程序来播放和录制一些 rtmp 流。该程序是在Qt中开发的。 我正在使用 rtmpdump 和 mplayer。因为两者都在单独的进程中运行,所以我使用 fifo 将流从 rtmpd
所以我开始构建我的本地 iOS 设备环境。我已经完全设置并运行了我的模拟器环境。我已经通过 iOS 开发人员为应用程序正确设置了我的分发和配置文件。在尝试运行该应用程序时,我遵循以下步骤:使用 USB
S3-FIFO 本文作为下一篇缓存文章的预备知识。 背景 基于LRU和FIFO的驱逐 FIFO和LRU都是经典的缓存驱逐算法,在过去几十年中也出现了很多追求更高效率的驱逐算法,如ARC, 2Q
我可以为单向通信创建一个 FIFO。但我该如何进行双向沟通呢?我可以创建一个可以充当发送方的 C 文件,并打开另一个充当接收方的终端。但如何建立双向通信呢?谢谢! 最佳答案 你不能。尝试一个套接字。考
考虑下表: Id Verb Qty Price `1 Buy 6 10.0 `2 Sell 5 11.0 `3 Buy 4 10.0 `4 Sell 3 11.0 `5 Sell 8 9.0 `6
我有两个进程(“发送者”和“接收者”)需要通过 transient 单向 FIFO 通信管道在单台机器上本地进行通信。这是我想要发生的事情(使用更接近 Unix 域套接字的语言): 发送者在已知地址“
好的,所以我正在使用 FIFO,并且我正在尝试构建一个小型库以在 future 的程序中使用。 它应该能够创建一个命名管道,读取它并在其中写入。 我能够完成所有这些功能,但它没有正确读取。问题如下:
数组有循环缓冲区版本吗?假设一次最大推送元素的数量已知,我是否必须派生自己的 FIFO 队列来提高性能? 这是我尝试过的: 循环实现: function CBuf(n) { var ctrPu
我是一名优秀的程序员,十分优秀!