简体   繁体   中英

How to best structure inter-process communication bewteen 4 processes with pipes/execl?

I'm trying to set up a program that uses interprocess communication to communicate between four processes using pipes/execl. This is part of a homework problem to demonstrate the use of pipes, but I'm having a bit of difficulty wrapping my head around it (ie how the process tree grows). These processes are to be instances of binaries on my system, hence the use of execl. To give a brief overview of what I'm trying to accomplish, I want to redirect stdin from the overall parent process/program to a sub-process called "s" (which is a binary on my system called scan), which interprets words. In "s"/scan, it processes the words from its stdin and then depending on the word, it will then send/write the word to either one process ("e") or another process ("o"). The e/o processes are actually the same binary that just have symbolic links to them- it basically does the same thing.

How would I go about structuring my calling/parent program to structure this? I have to create all the processes in this main program, otherwise I'd just create the two sub-processes in the scan/"s" process. I have some code below that I wrote that redirect stdin of the main program to the "s" process, but I am unsure where to fork the two sub-processes that will connect to it. I think it would be best to get the pids of the other two sub processes that I need to create in addition to the "s"/scan process and call the respective binaries with the different pids as arguments, but I'm not sure. Any help would be much appreciated!

 int s_to_e[2]; int s_to_o[2];
 int e_to_s[2]; int o_to_e[2];
 pid_t e_pid; pid_t o_pid; pid_t s_pid;

 if (pipe(s_to_e)) {          
      exit(1);
 }

 if (pipe(s_to_o)) {          
      exit(1);
 }

 if ( (s_pid = fork()) == -1) {
      fprintf(stderr, "Fork error! \n");
      exit(1);
 }

 int status;

 //Child progress of scan
 if (s_pid == 0) {
      //Redirect stdin to s
      //This probably isn't the best way to go about doing this
      dup2(0, s_to_e[0]);
      close(s_to_e[1]);

      //We need to replace the child fork with a new process
      if(execl("./scan", "./scan", NULL ) == -1) {
           printf("execl Error!");
           exit(1);
      }

 } else {
      printf("I am parent\n");
      wait(&status);
      printf("Done\n");
 }  

Two mistakes in your question are:

  • I want to redirect stdin from the overall parent process/program to a sub-process

The parent's standard input is inherited by the child and not to be redirected.

  • I think it would be best to get the pids of the other two sub processes that I need to create in addition to the "s"/scan process and call the respective binaries with the different pids as arguments

There is no need to pass its own id to a process; it can always obtain it by getpid() .

The following program should meet your requirements, except error checking.

 #define _GNU_SOURCE #include <fcntl.h> #include <unistd.h> #include <stdlib.h> main() { int s_to_o[2]; pipe2(s_to_o, O_CLOEXEC); int s_to_e[2]; pipe2(s_to_e, O_CLOEXEC); if (fork() == 0) dup2(s_to_o[1], 1), dup2(s_to_e[1], 2), exit(execl("scan", "scan", NULL)); close(s_to_o[1]); close(s_to_e[1]); if (fork() == 0) dup2(s_to_o[0], 0), exit(execl("o", "o", NULL)); close(s_to_o[0]); if (fork() == 0) dup2(s_to_e[0], 0), exit(execl("e", "e", NULL)); close(s_to_e[0]); wait(NULL); return 0; } 

You could set a dynamic variable to pass in the currently process's number, and use non-blocked IO to read a value which would be written by parent to the pipe.

It means that after fork, the processes should do these:

  1. echo process close the useless pipe.
  2. parent to read a value (if failed, set it into 0).
  3. parent write the increased value into the pipe.
  4. child execl to the same program.

That should be all right then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM