Multiple fork() Concurrency
Asked Answered
L

4

6

How do you use the fork() command in such a way that you can spawn 10 processes and have them do a small task concurrently.

Concurrent is the operative word, many places that show how to use fork only use one call to fork() in their demos. I thought you would use some kind of for loop but i tried and it seems in my tests that the fork()'s are spawning a new process, doing work, then spawning a new process. So they appear to be running sequentially but how can I fork concurrently and have 10 processes do the work simultaneously if that makes sense?

Thanks.

Update: Thanks for the answers guys, I think I just misunderstood some aspects of fork() initially but i understand it now. Cheers.

Lichi answered 4/9, 2009 at 19:23 Comment(1)
BTW-- Do you really want processes (as in your text) or threads (as in your tag). If processes the tag you want is [multiprocessing]Cajolery
A
15

Call fork() in a loop:

Adding code to wait for children per comments:

int numberOfChildren = 10;
pid_t *childPids = NULL;
pid_t p;

/* Allocate array of child PIDs: error handling omitted for brevity */
childPids = malloc(numberOfChildren * sizeof(pid_t));

/* Start up children */
for (int ii = 0; ii < numberOfChildren; ++ii) {
   if ((p = fork()) == 0) {
      // Child process: do your work here
      exit(0);
   }
   else {
      childPids[ii] = p;
   }
}

/* Wait for children to exit */
int stillWaiting;
do {
   stillWaiting = 0;
    for (int ii = 0; ii < numberOfChildren; ++ii) {
       if (childPids[ii] > 0) {
          if (waitpid(childPids[ii], NULL, WNOHANG) != 0) {
             /* Child is done */
             childPids[ii] = 0;
          }
          else {
             /* Still waiting on this child */
             stillWaiting = 1;
          }
       }
       /* Give up timeslice and prevent hard loop: this may not work on all flavors of Unix */
       sleep(0);
    }
} while (stillWaiting);

/* Cleanup */
free(childPids);
Anal answered 4/9, 2009 at 19:33 Comment(5)
oh most definitely. You're inviting a limited fork bomb otherwise.Higley
Look out for the horde of zombies. You need to wait(2) for these processes in the parent.Supersensual
I ran this code and there is some problem with it, the processes never complete and wait forever...Lichi
What are the child processes doing?Anal
Waitpid returns 0 with WNOHANG if childPids[ii] is NOT done; it returns childPids[ii] else. In fact with 0 value, loop never ends.Wantage
J
4

When you fork off processes the WILL be running concurrently. But note that unless you have enough available idle processors, they might not actually be executing concurrently, which shouldn't really matter...

Your second paragraph makes it seem like you aren't understanding how fork works, you have to check the return code to see if you are in the parent or in the forked process. So you would have the parent run a loop to fork off 10 processes, and in the children you do whatever you wanted to do concurrently.

Judaism answered 4/9, 2009 at 19:32 Comment(0)
C
3

Just loop in the "main" process spawning one child after another with each assign a particular task.

Cajolery answered 4/9, 2009 at 19:31 Comment(0)
A
0

You might also want to look into POSIX Threads (or pthreads). Here is a tutorial:

https://computing.llnl.gov/tutorials/pthreads/

Acidimeter answered 5/9, 2009 at 3:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.