- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在学习 Linux 中的命名管道,并编写了以下程序来尝试一下。
以下程序创建一个名为“test.fifo”的 FIFO 特殊文件,然后创建两个子进程以从该命名管道读取和写入。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
int main() {
pid_t pid[2];
// Create FIFO special file
mkfifo("test.fifo", 0644);
pid[0] = fork();
if (pid[0] < 0) {
printf("Fail to fork child process #0\n");
return 1;
} else if (pid[0] == 0) {
// Open FIFO special file in read mode
int in = open("test.fifo", O_RDONLY);
// Read data
char buf[256];
read(in, buf, 256);
printf("Child process #0 received: %s\n", buf);
// Close pipe
close(in);
return 0;
}
pid[1] = fork();
if (pid[1] < 0) {
printf("Fail to fork child process #1\n");
return 1;
} else if (pid[1] == 0) {
// Open FIFO special file in write mode
int out = open("test.fifo", O_WRONLY);
// Write data
char buf[256] = {0};
strcpy(buf, "Hello world");
write(out, buf, 256);
// Close pipe
close(out);
return 0;
}
return 0;
}
有时这个程序运行正常。但有时在这个程序运行后,我的 shell 提示消失了。
MyUserName@MyHostName:~$ ./a.out
MyUserName@MyHostName:~$ Child process #0 received: Hello world
(This is an empty line, no shell prompt occurs)
我尝试在空行中键入并运行命令 ls
,该命令有效。所以看起来程序已经成功退出了,只是没有出现shell提示。
如果我运行命令 cat test.fifo
,同样的事情也会发生。
MyUserName@MyHostName:~$ cat test.fifo
(This is an empty line, no shell prompt occurs)
发生了什么,为什么会这样?
最佳答案
由于父程序不wait(2)
子程序完成,当它执行exit(2)
时(在最后一个返回0 ;
in main) shell 显示提示符。但是您无法控制(甚至不知道) children 是否完成了他们的输出打印,因此 shell 提示符可能被其中一个 child 的输出覆盖。但是 shell 总是输出提示,但由于它没有换行(光标保持在同一行),所以它可能会被子级的输出覆盖。
尝试将以下内容放在最后一个 return 0;
之前:
wait(NULL);
wait(NULL); /* as you did two fork()s you can make up to two wait()s */
然后,程序终止将与 child 同步。
关于c - 为什么在我的程序中 "cat"一个FIFO特殊文件或创建一个FIFO特殊文件后没有出现shell提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49080052/
如何从表中选择结果,其中 cat = '$cat',如果不相等,则从 cat 中选择所有结果,无论 $cat 是什么。这是我的查询 $sql = mysql_query("SELECT * FROM
如标题中的示例,我想要结果: 目录:home/tom/cat 第一次剪辑:tom/cat 第二次切割:/cat 最后一刀:/ 如何在 python 中实现? 最佳答案 这是您要找的吗: In [101
使用循环,我可以创建 My cat is: Cat1 ... My cat is: Cat1 但是,当我尝试使用 Cat ("cat"+i) = new Cat("Cat" + i); 我犯错了.
我目前正在用 C 和 unix 开发一个 cat 命令。这次我的任务是设计 cat -b 和 cat -s 命令。但是我已经创建了其他命令,例如 -t、-v、-n、-e。我卡在了命令 -b 中,因为每
我理解这一点的方式是命令 cat file 将显示文件的内容并 cat < file 将把文件的内容作为输入。我正在尝试这个并创建了两个文件。一个名为 file1.txt,另一个名为 file2.tx
我理解这一点的方式是命令 cat file 将显示文件的内容并 cat < file 将把文件的内容作为输入。我正在尝试这个并创建了两个文件。一个名为 file1.txt,另一个名为 file2.tx
我有一个程序可以同时读取两个输入文件。我想从标准输入中读取这个程序。我以为我会使用这样的东西: $program1 <(cat) <($program2) 但我刚刚发现 cat <(cat) 产生 .
管道的正式定义指出左侧文件的 STDOUT 将立即通过管道传输到右侧文件的 STDIN。我有两个文件,hello.txt 和 human.txt 。 cat hello.txt 返回 Hello 并且
我想编写一个 shell 脚本,它接受来自标准输入的数据,将它写入一个文件,然后用它做一些事情。 出于这个问题的目的,让我们假设我的脚本应该接受标准输入,将其写入 in.txt ,然后 grep 一个
有没有办法让cat , less等打印制表符而不是制表符被转换为空格?当我将代码从终端复制到编辑器时,我对此很恼火。 最佳答案 我在这里看到两个问题。 首先,目标编辑器可以将 TAB 转换为空格数。一
我刚刚发现执行 find . 比执行 find 慢。 |猫。这是在我的主目录中执行 time find . 3 次的结果: First: real 0m4.385s user 0m0.54
我相信该程序适用于除一个以外的大多数情况。我在 indexOF() 中添加了空格,所以像 cathrine 和 dogsuit lammas 这样的词都会被认为是非亵渎的。我看到的唯一问题是用户是否以
解决方案: 不支持 data-* 属性。您可以定义自己的属性并使用 getAttribute 调用它们。就我而言, this.dataset.cat 变为 this.getAttribute("cat
这是让我“我该怎么办?”的问题之一 这是我遇到的编译器错误: 在“SKFootmanSprite”类型上使用实例成员“getAttackUPSequence_Frames”您是否打算改用“SKFoot
我想将 2 个不同的 cat 语句合并为一个 cat 语句 cat /dir1/file1.cfg | grep -q "linux" cat /dir1/file2.cfg | grep -q "l
我已经在 C 中使用系统调用(打开、读取和写入)来模拟 Linux 系统中的“猫”功能,并且它比真实的慢... 我正在使用与真正的“cat”相同的缓冲区大小,并使用“strace”我认为它进行相同数量
我在不同的示例、教程、博客等中看到过这两种格式,但就我的生活而言,我找不到对差异的解释。有什么区别 ICriteria crit = session.CreateCriteria(typeof(Cat
我在不同的示例、教程、博客等中看到过这两种格式,但就我的生活而言,我找不到对差异的解释。有什么区别 ICriteria crit = session.CreateCriteria(typeof(Cat
我有一个包含列的表:(project_id, name) 这是一个人员列表,每个人都有其所属的项目。如果一个人在两个项目中,则重复。 我想提取一个包含以下列的表:(project_id, people
我想提取 git 存储库中保存的文件的最新版本的副本,并将其传递到脚本中进行某些处理。对于 svn 或 hg,我只使用“cat”命令: Print the specified files as the
我是一名优秀的程序员,十分优秀!