C 使用 exec 和管道创建许多 child 进程

C create many child process using exec and pipe

我想创建与用户在参数中发送的一样多的 child 进程。我成功了。但是,我必须使用 exec 函数创建 child 进程,我不知道该怎么做。 Childs 进程将作为单独的程序创建并且 运行 通过执行。此外,我希望每个 child 进程都使用 pipe 与主进程 (parent) 通信。我不知道怎么做。到目前为止,我设法写了这样的东西:

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


   int main(int argc, char **argv)
   {
        pid_t pid;

        if(argc < 3)
        {
                printf("Not enought arguments");
                exit(0);
        }

        int number = atoi(argv[2]); // number of childern
        pid_t pids[number],pid_s;
        int i,n=number,status;
        int pipes[number*2];
        char buff[512];

        if(strcmp("-p", argv[1]) == 0)
        {
                //
        }

        if(strcmp("-f", argv[1]) == 0)
        {
                //
        }


        switch (pid = fork()) {
           case -1:
                        perror("Error in fork");
                        exit(0);

                        break;
           case 0:

                   for(i=0; i < n; i++)
                   {
                           if((pids[i] = fork()) < 0)
                           {
                                   perror("error fork");
                           }
                           else if(pids[i] == 0)
                           {
                                close(pipes[0]);
                                char *reply = "message";
                                write(pipes[1], reply, strlen(reply)+1);
                                execvp(argv[0],NULL);
                           }
                   }

                   while(n > 0)
                   {
                           pid_s = wait(&status);
                           --n;
                   }
                          break;
            default:

               close(pipes[1]);
               read(pipes[0],buff,80);
               printf("Message: %s", buff);

                   if(wait(0) == -1)
                   {

                   }

                          break;
         }

        return 0;
}

我更正了代码,child 由 exec 创建了一个新进程。我想 child 通过管道与主进程通信。最好循环执行吗?

此示例代码摘自:http://www.cs.ecu.edu/karl/4630/sum01/example1.html 展示了如何使用 execvp() 函数,由我稍作修改。

它使用函数 parsecmd(cmd,argv),这里没有写,但是它在空格处断开 cmd 并将片段存储到(本地数组)argv[],后跟一个空指针。例如,parsecmd("eat the banana", argv) 将设置 argv 如下。

   argv[0] = "eat"
   argv[1] = "the"
   argv[2] = "banana"
   argv[3] = NULL

This example also presumes that there might be other child processes running in background, and that they might terminate while the shell is waiting for the current command to stop. A function called process_terminated is use to handle the termination of a background process. It is not written here.

int runcmd(char *cmd)
{
  char* argv[MAX_ARGS];
  pid_t child_pid;
  int child_status;

  parsecmd(cmd,argv);
  child_pid = fork();
  if(child_pid == 0) {
    /* This is done by the child process. */

    execvp(argv[0], argv);

    /* If execvp returns, it must have failed. */

    fprintf( stderr, "execvp failed due to %s\n", strerror(errno) );
    exit(0);
  }
  else {
     /* This is run by the parent.  Wait for the child
        to terminate. */

     do {
       pid_t tpid = wait(&child_status);
       if(tpid != child_pid) process_terminated(tpid);
     } while(tpid != child_pid);

     return child_status;
  }
}