0

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.

Community
  • 1
  • 1
Xyzk
  • 1,332
  • 2
  • 21
  • 36

1 Answers1

5

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);
Tiny
  • 27,221
  • 105
  • 339
  • 599
Pradheep
  • 3,553
  • 1
  • 27
  • 35
  • Actually, I already edited that part in code. I started with what you are suggesting here, the same problem. Double checked now- except I put wait($status), the same problem. – Xyzk Mar 26 '13 at 17:36
  • can you be more clear in your problem that you are facing now ? – Pradheep Mar 26 '13 at 17:37
  • Okay, let's give an example. Let's say in="echo". The `childFunction()` will exit with `return 0` ,status will be equal to 0, c some number. At the same time, if I were to try in="blabla", `theChildFunction()` will exit with `return 255`. However, status will still be equal to 0! How to get the exit value from wait() – Xyzk Mar 26 '13 at 17:45
  • i think the problem with your code is the system system call. In case of correct execution the system call returns 0 and in case of error it returns -1 .To debug i would first suggest make your code working standalone ie call the function without fork and check if the return from the function is what you really want – Pradheep Mar 26 '13 at 18:25
  • Checked. Yes, the function returns proper value (255 actually, not -1). – Xyzk Mar 26 '13 at 19:13
  • While it did not solve the problem, I found that I have to use waitpid, for system() seems affect the result. – Xyzk Mar 26 '13 at 19:45
  • system() does the wait for you - it's a wrapper around a fork/exec/wait set of calls, so you can't wait for a system() generated process. – alanc Mar 27 '13 at 01:11