fork()、pipe() 和 exec() 进程创建和通信 [英] fork(), pipe() and exec() process creation and communication

查看:20
本文介绍了fork()、pipe() 和 exec() 进程创建和通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写使用 pipe() 创建进程的程序.

I have to write program that create process using pipe().

我的第一个任务是编写一个父进程,它使用 fork() 函数生成四个子进程.

My first task is to write a parent process that generates four child processes using the fork() function.

fork() 成功后,将子进程替换为另一个进程 rover1、rover2、rover3 和 rover4,尽管它们的代码相同.

Once the fork() is successful, replace the child process with another process rover1, rover2, rover3, and rover4, though all of them have the same code.

进程的功能如下.

  1. 每个子进程最初都有自己的编号.它从父母那里收到一个新号码.使用以下公式,它会创建自己的新号码,如下所示并将其转发给父级:

  1. Each child process is initially given its own number. It receives a new number from the parent. Using the following formula it creates its own new number as follows and forwards it to the parent:

mynumber = (3 * mynumber + 4 * numberreceived)/7

这个过程一直持续到父母发送系统稳定的消息.父级也有其初始编号.它接收所有孩子的数量并计算其新数量如下:

This process continues until the parent sends the message that the system is stable. The parent also has its initial number. It receives numbers of all the children and computes its new number as follows:

mynumber = (3 * mynumber + 所有孩子发送的号码)/7

父母会将此号码发送给它的所有孩子.这个过程将一直持续到父母发现它的号码不再变化.到时候它会告诉孩子系统已经稳定了.

The parent will send this number to all its children. This process will continue until the parent finds that its number is not changing anymore. At that time it will tell the children the system has become stable.

这就是我所做的,但我的教授说我必须使用 exec() 来执行子进程并将子进程替换为另一个子进程.我不确定如何使用 exec().你能帮我解决这个问题吗?

This is what I did but my professor said I have to use exec() to execute the child and replace child process with another child process. I am not sure how to use exec(). Could you please help me with this.

我只附加第一个子代.

// I included stdio.h, unistd.h stdlib.h and errno.h 
int main(void)
{
  // Values returned from the four fork() calls
  pid_t rover1, rover2, rover3, rover4;

  int parentnumber, mynumber1, mynumber2, mynumber3, mynumber4;

  int childownnumber1 = 0, status = 1, childownnumber2 = 0,
      childownnumber3 = 0, childownnumber4 = 0, numberreceived = 0;

  printf("Enter parent number: ");
  printf("%d", parentnumber);
  printf("Enter each children number");
  printf("%d %d %d %d", mynumber1, mynumber2, mynumber3, mynumber4);

  // Create pipes for communication between child and parent
  int p1[2], p2[2];
  // Attempt to open pipe
  if (pipe(p1) == -1) {
    perror("pipe call error");
    exit(1);
  }
  // Attempt to open pipe
  if (pipe(p2) == -1) {
    perror("pipe call error");
    exit(1);
  }

  // Parent process generates 4 child processes
  rover1 = fork();

  // if fork() returns 0, we're in the child process;
  // call exec() for each child to replace itself with another process
  if (rover1 == 0) {
    for(; numberreceived != 1; ) {  
      close(p1[1]); // Close write end of pipe
      close(p2[0]); // Close read end of second pipe

      // Read parent's number from pipe
      read(p1[0], &numberreceived, sizeof(int));

      if (numberreceived == 1) {
        // System stable, end child process
        close(p1[0]);
        close(p2[1]);
        _exit(0); // End child process
      }

      mynumber1 = (int)((3*mynumber1 + 4*numberreceived)/7.0);

      printf("
rover1 number: ");
      printf("%i", mynumber1);

      // Write to pipe
      write(p2[1], &mynumber1, sizeof(int));    
    }       
  }
  /* Error:
   * If fork() returns a negative number, an error happened;
   * output error message
   */
  if (rover1 < 0) {
    fprintf(stderr,
            "can't fork, child process 1 not created, error %d
",
            errno);
    exit(EXIT_FAILURE);
  }
}

推荐答案

exec 系列函数用于用新进程替换当前进程.注意replace这个词的使用.一旦调用了 exec,当前进程就消失了,新进程开始了.如果要创建一个单独的进程,必须先fork,然后exec子进程内的新二进制文件.

The exec family of functions is used to replace the current process with a new process. Note the use of the word replace. Once exec is called, the current process is gone and the new process starts. If you want to create a separate process, you must first fork, and then exec the new binary within the child process.

使用 exec 函数类似于从命令行执行程序.要执行的程序以及传递给程序的参数在对 exec 函数的调用中提供.

Using the exec functions is similar to executing a program from the command line. The program to execute as well as the arguments passed to the program are provided in the call to the exec function.

例如,下面的execcommand*等价于后面的shell命令:

For example, the following execcommand* is the equivalent to the subsequent shell command:

execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);

/bin/ls -r -t -l

* 注意arg0"是要执行的命令/文件名

由于这是家庭作业,因此对这个过程有一个很好的了解很重要.您可以先阅读关于 pipe 的文档、forkexec 结合一些教程来更好地理解每个一步.

Since this is homework, it is important to have a good understanding of this process. You could start by reading documentation on pipe, fork, and exec combined with a few tutorials to gain a better understanding each step.

以下链接应该可以帮助您入门:

The following links should help to get you started:

这篇关于fork()、pipe() 和 exec() 进程创建和通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆