Fork and execute many different processes and get results from each

I managed to unlock and run another program from my application. I am currently working on how to wait until a process called by exec returns a result via a pipe or stdout. However, can I have a group of processes using one fork, or do I need to deploy and repeat the same program many times? Can I get a PID for every other process? I want my application to call the same program that I now call many times, but with different parameters: I want a group of 8 processes of the same program to work and return results through channels. Can someone please point me in the right direction please? I looked at the linux.die man pages, but they are pretty spartan and cryptic in my description. Is there an e-book or pdf,Which can I find for more information? Thank!

pid_t pID = fork();
 if (pID == 0){
  int proc = execl(BOLDAGENT,BOLDAGENT,"-u","2","-c","walkevo.xml",NULL);
  std::cout << strerror(errno) << std::endl;
}

For example, how can I control the PID, which child element (according to the xml file) received the result (by pipe or stdout), and thus act accordingly? Do I have to encapsulate child processes in an object and work from there, or can I group them at all?

+3
source share
3 answers

One syscall Forkdoes only one new process (one PID). You have to organize some data structures (for example, an array of pids, an array of parent pipe ends, etc.), Make 8 fork from the main program (each child will do exec), and then wait for the children.

After each fork (), it will return you the child's PID. You can save this pid and related information as follows:

#define MAX_CHILD=8
pid_t pids[MAX_CHILD];
int pipe_fd[MAX_CHILD];
for(int child=0;child<MAX_CHILD;child++) {
 int pipe[2];
 /* create a pipe; save one of pipe fd to the pipe_fd[child] */
 int ret;
 ret = fork();
 if(ret) { /* parent */ 
  /* close alien half of pipe */
  pids[child] = ret; /* save the pid */
 } else { /* child */
  /* close alien half of pipe */
  /* We are child #child, exec needed program */
  exec(...);
  /* here can be no more code in the child, as `exec` will not return if there is no error! */
 }
} 

/* there you can do a `select` to wait data from several pipes; select will give you number of fd with data waiting, you can find a pid from two arrays */
+6
source

, , , , fork():

  • ( "" ) , "", PID, ,

  • fork() : 0, 1 , , 0, ""; PID ;

  • . .

(0 .xor. -0) fork(), , - 0 , - ; , .

, , , () ; , , fork().

.

, : , , () , . , - , , ; fork().


1 : fork() pid_t, .

+5

It has been some time since I worked in C / C ++, but a few points:

  • The Wikipedia fork-exec page provides a starting point to learn about forking and execing. Google is also your friend.

  • As the osgx answer says, fork () can only provide one subprocess, so you will need to call it 8 times to get 8 processes, and then each one must execute another program.

  • fork () returns the PID of the child process in the main process and 0 in the subprocess, so you should be able to do something like:

int pid = fork();
if (pid == 0) {
  /* exec new program here */
} else {
  /* continue with parent process stuff */
}
+1
source

All Articles