+ -
当前位置:首页 → 问答吧 → 请各位大侠看看为什么这个程序没有正常退出

请各位大侠看看为什么这个程序没有正常退出

时间: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

In UNIX System terminology, a process that has terminated, but whose parent has not yet waited for it, is called a zombie.

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

楼主发上来的时候可以把#if 0 #endif中间的东西都去掉。

作者: davelv   发布时间: 2010-07-26

wait(0); ???

怎么能Wait 0呢?

Parent:Wait(X)

作者: folklore   发布时间: 2010-07-26

回复 folklore
我现在改成如下,怎么还是卡在那不动呢,为什么啊

    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 13:53 编辑

呵呵。刚才看错了!

作者: zhangsuozhu   发布时间: 2010-07-26

  1. int main()
  2. {
  3.         int x = 0, fd[2];
  4.         char buf[30] = {0}, s[30] = {0};
  5.         pipe(fd);
  6.         while (-1 == (x = fork()));
  7.         if (0 == x) {
  8.             close(fd[1]);
  9.             read(fd[0], s, 30);
  10.             printf("childPID is %d, message%s", x, s);
  11.             exit(0);               
  12.         } else {
  13.             close(fd[0]);
  14.             sprintf(buf, "This is message from Father\r\n");
  15.             write(fd[1], buf, 30);
  16.         }
  17.         wait(x);
  18.         exit(0);
  19. }
复制代码
手上没有Unix系统,没发现什么错误,试试

作者: folklore   发布时间: 2010-07-26

相关阅读 更多