Which one to choose waitpid/wait/waitid?

假如想象 提交于 2019-12-08 07:32:56

问题


I want to use execl in child process after doing fork. execl will execute the script which will take around 120 seconds of time. I tried almost all the combination with waitpid, wait and waitid with different argument (0, WNOHANG, etc.,) but in all case I am getting -1 return value. So I want to know in which wait function I need to use when? So I can concentrate in one wait function to make it work.

One more interesting thing which I observed from my logs is that when I am doing nothing in child process it is showing my parent thread as orphaned. I don't know how it is possible? How can my parent thread become orphaned?

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    pid_t Checksum_pid = fork();

    if (Checksum_pid < 0)
        printf("Fork Failed\n");
    else if (Checksum_pid == 0)
    {
        execl("/bin/ls","ls",(char *)NULL) ;
        exit(EXIT_FAILURE);
    }
    else
    {
        int childStatus;
        pid_t returnValue = waitpid(Checksum_pid, &childStatus, 0);

        if (returnValue > 0)
        {
            if (WIFEXITED(childStatus))
                printf("Exit Code: %d\n", WEXITSTATUS(childStatus));

        }
        else if (returnValue == 0)
            printf("Child process still running\n");
        else
        {
            if (errno == ECHILD)
                printf(" Error ECHILD!!\n");
            else if (errno == EINTR)
                printf(" Error EINTR!!\n");
            else
                printf("Error EINVAL!!\n");
        }
    }

    return 0;
}

回答1:


As I commented: your last else should just be

 else perror("waitpid");

But you are getting ECHILD. So please read waitpid(2) man page:

   ECHILD (for wait()) The calling process does not have any unwaited-
          for children.

  ECHILD (for waitpid() or waitid()) The process specified by pid
          (waitpid()) or idtype and id (waitid()) does not exist or is
          not a child of the calling process.  (This can happen for
          one's own child if the action for SIGCHLD is set to SIG_IGN.
          See also the Linux Notes section about threads.)

BTW, I cannot reproduce your error. Check also your limits as given by ulimit -a on bash.

Maybe your execl fails (especially if you execute some script instead of /bin/ls). Add a call to perror after it.

Also, compile with gcc -Wall -g and use strace and gdb



来源:https://stackoverflow.com/questions/22933500/which-one-to-choose-waitpid-wait-waitid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!