문제

I have the following code.

My question is in the code

     int main() {

            ....

         if ((uproc.pid = fork()) == -1) {
            return -1;
        }

        if (uproc.pid == 0) {
            /* child */

            const char *argv[3];
            int i = 0;
            argv[i++] = "/bin/sh";
            argv[i++] =  "/my/script.sh";
            argv[i++] = NULL;

            execvp(argv[0], (char **) argv);
            exit(ESRCH);

        } else if (uproc.pid < 0)
            return -1;

        /* parent */
        int status;
        while (wait(&status) != uproc.pid) {
            DD(DEBUG,"waiting for child to exit");
        }

           // If /my/script.sh exit accidentally in some place with error. 
           // can I catch this error right here?
          ......
    }
도움이 되었습니까?

해결책

The exit status of the child is provided by the wait function, in the status variable.

You get the exit status by using the WEXITSTATUS macro, but only if the program exited normally (i.e. called exit or returned from its main function):

if (WIFEXITED(status))
    printf("Child exit status: %d\n", WEXITSTATUS(status));
else
    printf("Child exited abnormally\n");

Read the manual page for wait for more information.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top