Handling SIGCHLD, how to record the return values of children as they die

后端 未结 2 494
感动是毒
感动是毒 2021-01-03 12:07
void childSignalHandler(int signo) {
    int status;

    pid_t pid = wait(&status);

    struct PIDList* record = getRecordForPID(childlist, pid);
    if (recor         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 12:50

    Note signals are not queued. If two children die in quick succession, you may only recieve one SIGCHLD. So, you should actually loop around calling waitpid() until there are no more exiting processes to handle:

    int status;
    pid_t pid;
    
    while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
        if (WIFEXITED(status)) {
            struct PIDList *record = getRecordForPID(childlist, pid);
    
            if (record != NULL)
                record->returnValue = WEXITSTATUS(status);
        }
    }
    

提交回复
热议问题