我需要创建一个程序来创建n个进程并显示信息。当每个进程结束时,我要打印它的PID和退出状态。按照我的方法,父程序将等待创建下一个进程,直到当前进程结束。我需要它,以便它始终创建子进程,并且只显示一个进程结束时的退出信息,而不阻止父进程继续。我想不出该把等待放在哪里以确保这一点。以下是我的代码:

int main (int argc, char *argv[])
{
  if (argc < 2)
  {
     printf("\n\nUsage: %s <enter a number (12 or less)>\n\n", argv[0]);
     exit (-1);
  }
  else
  {
    int *processNum = (int *)malloc(sizeof(12));
    int processNumTemp;

    processNumTemp = atoi(argv[1]);
    processNum = &processNumTemp;

  if(*processNum > 12 || *processNum < 1)
    {
      printf("\n\nUsage: %s <enter a number (12 or lrss)>\n\n", argv[0]);
    }
  else
    {
      parentInfo(processNum);
      createChildProcess(processNum);
    }


  }

 return 0;
}


//Name:  parentInfo
//Description:  Displays information about the parent process
//Parameters:  processNum - stores the number of child processes to create
//             (entered at the command line).
//Return:  none
void parentInfo(int *processNum)
{
 printf("Parent process ID:  %d\n", getppid());
 printf("Number of processes to create:  %d\n", *processNum);
}


//Name:  createChildProcess
//Description:  Creates n number of child processes.
//              For each child process, it says its a child process and it
//              displays its PID.
//              After each child process closes, the parent displays info.
//Parameters:  processNum - stores the number of child processes to create
//             (entered at the command line).
//Return:  none
void createChildProcess(int *processNum)
{
 int i;
 int childStatus;
 pid_t childpid;

 /*The for loop will create n number of processes based on the value of            processNum.*/
 for(i = 1; i <= *processNum; i++)
 childpid = fork();

  //Executes if fork didn't work
  if(childpid < 0)
    {
      perror("fork");
      exit(1);
    }

  //Executes if the fork worked
  else if( childpid == 0)
    {
      int pid = getpid();

      //Prints a message and the child processe's PID
      printf("\nHello I am a child process.\n");
      printf("My PID is %d. \n", getpid());

      for(int x = 1; x <= pid; x ++);

      exit(15);
    }

}
      //Executes after the child process has ended
      //Checks the child process's exit status

      waitpid(childpid, &childStatus, WUNTRACED);
      printf("\nPID of the child process that was just created:  %d.\n", childpid);

      if(WIFEXITED(childStatus))
        {
          printf("PID %d exited normally.  Exit number:  %d\n", childpid, WEXITSTATUS(childStatus));
        }
      else if(WIFSTOPPED(childStatus))
        {
          printf("PID %d was stopped by %d\n", childpid, WSTOPSIG(childStatus));
        }
      else if(WIFSIGNALED(childStatus))
        {
          printf("PID %d exited due to signal %d\n.", childpid, WTERMSIG(childStatus));
        }
      else
        {
          perror("waitpid");
        }
}

最佳答案

代码前

signal(SIGCHLD, childHandler);

在childHandler中输入fork代码。
void childHandler(int signum)
{

    pid_t childpid;
    int childstatus;

    while ((childpid = waitpid( -1, &childstatus, WNOHANG)) > 0)
    {
        if (WIFEXITED(childStatus))
        {
            printf("PID %d exited normally.  Exit number:  %d\n", childpid, WEXITSTATUS(childStatus));
        }
        else
            if (WIFSTOPPED(childStatus))
            {
                printf("PID %d was stopped by %d\n", childpid, WSTOPSIG(childStatus));
            }
            else
                if (WIFSIGNALED(childStatus))
                {
                    printf("PID %d exited due to signal %d\n.", childpid, WTERMSIG(childStatus));
                }
                else
                {
                    perror("waitpid");
                }
        }
    }
}

不应在信号处理程序内部使用异步不安全调用,如waitpid,因此请更改代码以将状态保存在全局或堆分配的数组中(您知道要从processNum创建的大小),并在处理程序外部打印状态信息。
而且,按照目前的结构,你的父母可能会在收获所有孩子之前结束。为孩子添加一个计数器,以便在家长退出之前等待所有的计数器。

关于c - 如何不使用waitpid阻止父对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22939431/

10-09 13:46