首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >_exit()、叉()和waitpid()系统调用

_exit()、叉()和waitpid()系统调用
EN

Stack Overflow用户
提问于 2014-02-18 16:38:57
回答 2查看 1.6K关注 0票数 0

所以,我要退出子线程回到父线程。我使用的是_exit()系统调用。我想知道几件事。一个是我的孩子的_exit参数是什么。下面是我的子进程正在执行的代码:

代码语言:javascript
复制
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()系统调用中使用它。以下是在子进程完成后执行的父进程的代码。

代码语言:javascript
复制
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()系统调用,它确保了子程序的退出,然后使用状态检查它是否正确退出。我在想,我是不是在用正确的方式来创造这个孩子,然后离开这个孩子。

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

谢谢你的帮忙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-18 18:30:58

来自“等待linux手册”。

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

您不需要付出等待的返回值来检查子程序是否失败。您需要检查状态值。有几个宏需要检查状态。

代码语言:javascript
复制
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被传递给等待,而不是状态,所以我认为您做得很好)。

代码语言:javascript
复制
/* 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;
票数 0
EN

Stack Overflow用户

发布于 2014-02-18 17:19:13

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

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

要记住的一件事是,您可能希望在尝试/捕捉中包围您的子方法的肉。对于线程,您通常不希望有一个异常来破坏您的主流。

对于多个进程,您不会遇到这个问题,但请考虑是否希望在出现异常时调用_exit,以及如何(与父进程或用户)通信发生异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21859954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档