0

所以,我从子线程退出回到父线程。我正在使用 _exit() 系统调用。我想知道一些事情。一个是我孩子的 _exit 参数。这是我的子进程正在执行的代码:

printf("\n****Child process.****\n\nSquence: ");

 do{
        //Print the integer in the sequence.
        printf("%d\t",inputInteger);
        if((inputInteger%2) == 0){
            //printf("inputInteger = %d\n", inputInteger);
            inputInteger = inputInteger / 2;
        }else{
            inputInteger = 3*inputInteger +1;
            //printf("%d\t",inputInteger);
        }

    }while(inputInteger != 1);

    //Makes sure we print off 1!
    printf("%d\n\n", inputInteger);

    //Properly exit
    _exit(status);

我使用 status 是因为在我的父线程中我在 waitpid() 系统调用中使用它。这是子进程完成后执行的父进程的代码。

waitpid_check = waitpid(processID, &status, 0);
        printf("\n****Parent process.****\n");

        if(waitpid_check == -1){
            printf("Error in waitpid.\n");
            exit(EXIT_FAILURE);
        }

        if(WIFEXITED(status)){
            printf("Child process terminated normally!\n");
        }

在这里,我使用 waitpid() 系统调用来确保孩子已退出,然后使用 status 检查它是否已正确退出。我想知道我是否以正确的方式来创建孩子并退出它。

然后我还想知道我是否正确地检查了父母中孩子的退出情况。

谢谢你的帮助!

4

2 回答 2

0

我很乐意帮忙,但我对这些电话真的很生疏。如果您已经阅读了有关这些 API 调用的文档,并且到处检查错误返回,那么您应该处于良好状态。

这个想法在高层次上似乎很好。

要记住的一件事是,您可能希望在 try/catch 中包围您的子方法的内容。使用线程,您通常不希望异常扰乱您的主要流程。

多进程不会有这个问题,但要考虑是否要_exit在遇到异常时被调用,以及如何(与父级或用户)通信(与父级或用户)发生异常。

于 2014-02-18T17:19:13.570 回答
0

来自waitpid linux手册。

“如果 status 不为 NULL,wait() 和 waitpid() 将状态信息存储在它指向的 int 中。”

您不需要等待支付的返回值来检查孩子是否失败。您需要检查状态值。有一些宏可以检查状态。

WIFEXITED(status)
returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main().
WEXITSTATUS(status)
returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.
WIFSIGNALED(status)
returns true if the child process was terminated by a signal.
WTERMSIG(status)
returns the number of the signal that caused the child process to terminate. This macro should only be employed if WIFSIGNALED returned true.
WCOREDUMP(status)
returns true if the child produced a core dump. This macro should only be employed if WIFSIGNALED returned true. This macro is not specified in POSIX.1-2001 and is not available on some UNIX implementations (e.g., AIX, SunOS). Only use this enclosed in #ifdef WCOREDUMP ... #endif.
WIFSTOPPED(status)
returns true if the child process was stopped by delivery of a signal; this is only possible if the call was done using WUNTRACED or when the child is being traced (see ptrace(2)).
WSTOPSIG(status)
returns the number of the signal which caused the child to stop. This macro should only be employed if WIFSTOPPED returned true.
WIFCONTINUED(status)
(since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT.

至于您是否退出子进程,这真的取决于。您会像在任何其他程序中一样退出,因为当您分叉一个进程时,您实际上只是在复制一个地址空间,而当子程序作为其自己的独立程序运行时(当然使用相同的打开 FD、已声明的值等作为父程序) . 下面是这个问题的典型实现(虽然 NULL 被传递给等待而不是状态,所以我认为你做对了。)

/* fork a child process */
pid = fork();

if (pid < 0) { /* error occurred */
    fprintf(stderr, "Fork Failed\n");
    return 1;
}
else if (pid == 0) { /* child process */
    printf("I am the child %d\n",pid);
    execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
    /* parent will wait for the child to complete */
    printf("I am the parent %d\n",pid);
    wait(NULL);

    printf("Child Complete\n");
}

return 0;
于 2014-02-18T18:30:58.423 回答