家长想读的孩子退出状态(或返回值),叉和等待 [英] Parent trying to read children exit status (or return value), fork and wait

查看:116
本文介绍了家长想读的孩子退出状态(或返回值),叉和等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑。据称,立足于人,和许多其他来源,像这样:
<一href=\"http://stackoverflow.com/questions/742413/return-$c$c-when-os-kills-your-process/747819#747819\">Return当OS杀死你的进程 code
等待(安培;状态)应该有可能使我得到的退出状态,或者返回一个子进程的值

2的那些片段code的,应该让我看到了退出的价值它的孩子,然后再打印出来。
子进程的功能:

  INT childFunction(字符[]中,CHAR LOGPATH []){
        FILE * LOGFILE = FOPEN(日志路径,A);
        如果(日志文件== NULL)
        返回1;
        INT C =系统(中);
        FCLOSE(日志文件);
        返回(三);
    }

和主要的:

  {...在此之前一些不重要code}
        结果= fork()的;
        如果(结果== 0){
            出口(childFunction(内联,LOGPATH));
        }
        否则如果(结果大于0){
            INT状态= 0 ;;
            诠释三=(int)的waitpid函数(-1,&放大器;状态,0);
            的printf(A:%D B数:%d \\ n,地位,WIFEXITED(状态));
        其他
            返回-1;
        I = 0;

我试图去等待,睡了一段时间,退出,返回,并阅读手册页几次。无论是有在我这个功能的认识基本的错误,或4小时的寻找,我只是以后不再能看到它。

解决
出于某种原因,这是我绝对不明白,如果更改childFunction的收益率(C)如果(C!=),回报(1 );否则返回(0)它会工作。不知道为什么。

2求解
好了,现在我想我知道为什么。看,似乎要么返回呼叫,或者等待,减少后的状态8最显著位(256)。这意味着什么?这意味着,如果发送正常int,则第一比特被使用,而最后8被丢弃。与此同时,当我指定收益率(1)编译器自动缩短的int到short int类型。解决方法是返回一个短号码,或做它就像我在previous没有解决的评论。


解决方案

问题是你是不是在等待孩子退出


  INT C =(int)的waitpid函数(-1,&安培;状态,WNOHANG);


下面父进程检查子进程已经退出,如果孩子没有退出返回。

在上述情况下,因为你是在子进程打开一个文件时,有更大​​的概率有一个IO等待的孩子,因此父进程就会被执行。

现在父进程将检查状态,自 WNOHANG 使用它不会在waitpid函数被阻止它继续,这是你没有得到的原因正确的状态

我会建议你使用


  INT C =(int)的waitpid函数(安培;状态);


I'm confused. Supposedly, basing on man, and many other sources, like this: Return code when OS kills your process wait(&status) should make it possible for me to get the exit status or return value of a child process?

Those 2 snippets of code, should allow me to see the exit value of it's child, and then print it. child process function:

    int childFunction(char in[],char logPath[]){
        FILE *logFile= fopen( logPath, "a" );
        if(logFile==NULL)
        return 1;
        int c=system(in);
        fclose(logFile);
        return(c);
    }

and main:

        {...some unimportant code before this}
        result= fork();
        if(result==0){
            exit(childFunction(inLine,logPath));
        }
        else if(result>0){
            int status=0;;
            int c=(int)waitpid(-1,&status,0);
            printf("a:%d b:%d\n",status, WIFEXITED(status));
        else
            return -1;
        i=0;

I tried going with wait, sleeping for some time, exiting, returning, and read man page a few times. There either is a basic error in my understanding of this function, or after 4 hours of looking I simply no longer can see it.

SOLVED For some reason, which i absolutely do not understand, if you change the return(c) in childFunction to if(c!=)return(1);else return(0) it will work. No idea why.

SOLVED 2 Okay, now I think I know why. See, it appears that either return call, or wait, reduces status to 8 most significant bits (256). What does that mean? That means, if you send normal int, the first bits are used, while the last 8 are discarded. At the same time, when I specify return (1) compiler automatically "shortens" the int to short int. Solution is to either return a short number, or do it like I did in previous "Solved" comment.

解决方案

the problem is you are not waiting for the child to exit

int c=(int)waitpid(-1,&status,WNOHANG);

Here the parent process checks if the child process has exited and if the child has not exited it returns.

In the above case, since you are opening a file in child process,there is a greater probability that there is a IO wait at child and so the parent process would be executed.

Now the parent process would check the status and since WNOHANG is used it will not be blocked at the waitpid and it continues and this is the reason you are not getting the correct status

I would suggest you use

int c=(int)waitpid(&status);

这篇关于家长想读的孩子退出状态(或返回值),叉和等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆