请各位大侠看看为什么这个程序没有正常退出
时间:2010-07-26
来源:互联网
#include <stdio.h>
#include <unistd.h>
int main()
{
int x = 0, fd[2] = {0};
char buf[30] = {0}, s[30] = {0};
pipe(fd);
while (-1 == (x = fork()));
if (0 == x) {
#if 0
sprintf(buf, "This is message from child\r\n");
write(fd[1], buf, 30);
#endif
} else {
#if 0
wait(0);
read(fd[0], s, 30);
printf("childPID is %d, message%s", x, s);
#endif
sprintf(buf, "This is message from Father\r\n");
write(fd[1], buf, 30);
}
if (0 == x) {
wait(0);
read(fd[0], s, 30);
printf("childPID is %d, message%s", x, s);
//exit(0);
}
exit(0);
}
为什么在linux编译运行后,没有正常退出呢?
[root@localhost c_testy]# childPID is 0, messageThis is message from Father//程序卡在这个地方,没有出现下面的命令行提示符呢
[root@localhost c_testy]#
作者: h_virus 发布时间: 2010-07-26
俺看不懂啊..
作者: pandaiam 发布时间: 2010-07-26
作者: mirnshi 发布时间: 2010-07-26
If we write a long-running program that forks many child processes, they become zombies unless we wait for them and fetch their termination status.
請教樓上.apue上是這麼說的.
但是,父進程終止后,子進程不是被init領養了嗎...
作者: pandaiam 发布时间: 2010-07-26
作者: davelv 发布时间: 2010-07-26
怎么能Wait 0呢?
Parent:Wait(X)
作者: folklore 发布时间: 2010-07-26
我现在改成如下,怎么还是卡在那不动呢,为什么啊
int main()
{
int x = 0, fd[2] = {0};
char buf[30] = {0}, s[30] = {0};
pipe(fd);
while (-1 == (x = fork()));
if (0 == x) {
} else {
sprintf(buf, "This is message from Father\r\n");
write(fd[1], buf, 30);
}
if (0 == x) {
//wait(0);
read(fd[0], s, 30);
printf("childPID is %d, message%s", x, s);
exit(0);
}
exit(0);
}
作者: h_virus 发布时间: 2010-07-26
呵呵。刚才看错了!
作者: zhangsuozhu 发布时间: 2010-07-26
- int main()
- {
- int x = 0, fd[2];
- char buf[30] = {0}, s[30] = {0};
- pipe(fd);
- while (-1 == (x = fork()));
- if (0 == x) {
- close(fd[1]);
- read(fd[0], s, 30);
- printf("childPID is %d, message%s", x, s);
- exit(0);
- } else {
- close(fd[0]);
- sprintf(buf, "This is message from Father\r\n");
- write(fd[1], buf, 30);
- }
- wait(x);
- exit(0);
- }
作者: folklore 发布时间: 2010-07-26
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28