【问题标题】:How to run 2 processes at the same time in C?如何在 C 中同时运行 2 个进程?
【发布时间】:2015-09-29 17:13:22
【问题描述】:

我正在用 C 语言创建一个程序,例如终端 shell,这是我在课堂上的练习。如果您在句末添加“&”,该程序允许同时运行多个进程。 (例如:$ gedit &);但是,我的代码只是在子进程退出时运行父进程。

#include  <stdio.h>
#include  <sys/types.h>
#include <string.h>

void  parse(char *line, char **argv)
{
     while (*line != '\0') {       /* if not the end of line ....... */ 
          while (*line == ' ' || *line == '\t' || *line == '\n')
              *line++ = '\0';     /* replace white spaces with 0    */

          *argv++ = line;          /* save the argument position     */
          while (*line != '\0' && *line != ' ' && 
                 *line != '\t' && *line != '\n') 
               line++;             /* skip the argument until ...    */
     }
     *argv = '\0';                 /* mark the end of argument list  */
}


void  execute(char **argv)
{
     pid_t  pid;
     int    status;

     if ((pid = fork()) < 0) {     /* fork a child process           */
          printf("*** ERROR: forking child process failed\n");
          exit(1);
     }
     else if (pid == 0) {          /* for the child process:         */
          if (execvp(*argv, argv) < 0) {     /* execute the command  */
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
     }
     else {                                  /* for the parent:      */
            wait(NULL);                          /* wait for completion  */

     }
}

void  main(void)
{
     char  line[1024];             /* the input line                 */
     char  *argv[64];              /* the command line argument      */

     while (1) {                   /* repeat until done ....         */
          printf("COMMAND -> ");     /*   display a prompt             */
          gets(line);              /*   read in the command line     */
          printf("\n");
          parse(line, argv);       /*   parse the line               */
          if (strcmp(argv[0], "exit") == 0)  /* is it an "exit"?     */
               exit(0);            /*   exit if it is                */
          execute(argv);           /* otherwise, execute the command */
     }
}

我想要这样的结果:

例如:$ gedit & // 程序 gedit 以 pid = 4789 运行

$ emacs // 程序 emacs 以 pid = 5123 ppid = 4789 运行

【问题讨论】:

  • 想必父进程正在等待,因为它调用了wait函数。
  • @Adrian:我知道;但我不知道如何执行多个进程。
  • 我不明白你在问什么。您是在问如何运行多个 child 进程吗?然后您需要多次致电fork。当然,你不能在父进程等待的时候调用fork,所以你需要想办法解决这个问题。

标签: c linux bash


【解决方案1】:

如果您看到 & 符号,请省略 wait()

void  execute(char **argv, int block)
{
     pid_t  pid;
     int    status;

     if ((pid = fork()) < 0) {
          printf("*** ERROR: forking child process failed\n");
          exit(1);
     }
     else if (pid == 0) {
          if (execvp(*argv, argv) < 0) {
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
     }
     else if (block) {
            wait(NULL);
     }
}

【讨论】:

  • 当你有多个孩子时,你会想要使用waitpid而不是wait,所以你等待正确的。
  • 我如何知道进程是否在没有等待的情况下完成?在我的编程任务中,我有一个提示,不断询问用户要执行的命令(这个过程永远不必等待)。但是,它只能同时运行 2 个进程,否则它应该被添加到队列中。因此,我需要一种在两个进程之一完成后分叉下一个进程的方法。
  • 我在 StackOverflow 上提出了我的问题,但到目前为止还没有得到答案。我会很感激你的帮助! stackoverflow.com/questions/58887417/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 2020-06-11
  • 2020-11-01
  • 2013-10-21
  • 1970-01-01
相关资源
最近更新 更多