LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2004, 05:56 PM   #1
beginner_84
LQ Newbie
 
Registered: Aug 2004
Posts: 11

Rep: Reputation: 0
fork


how to create multiple child process and make parent process wait till all child procesas have exitted
Code:
if(fork()==0)
..
else if(fork()==0)
........
...............
.................
else wait();
doesnt work
 
Old 08-19-2004, 06:23 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Code:
int pids[27];
int i=0;
for(i=0;i<27;i++) {
  if((pids[i]=fork())!=0) {
  } else {
    printf("Hi, I'm child number %d\n", i);
    break;
  }
}
// Now the parent has started 27 childs.
// At this point, i contains values 0..27, inclusive. 0-26 with the childs
// 27 is the parent.
switch(i) {
   case 0: ....
    ... //Whatever the childs do..
   case 26: 
   case 27:
    { int j;    
       for(j=0;j<27;j++) waitpid(pids[i],NULL,0);
    }
}
 
Old 08-20-2004, 04:53 AM   #3
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Hi,
This is one way I could think of -
Code:
#include <stdio.h>
#include <pthread.h>
#include <stdio.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>



pid_t pid;


pthread_t t1, t2;

void *
workthread (void *arg)
{
  pid = fork ();

  if (pid == 0)
    {
      fprintf (stderr, "\nchild pid : %d  and it's parent is %d\n", getpid (),
               getppid ());
      sleep (5);
    }
}

int
main ()
{
  int i = 0;

  for (i = 0; i < 5; ++i)
    {
      pthread_create (&t2, NULL, &workthread, NULL);
    }
  for (i = 0; i < 5; ++i)
    {
      pid = wait (NULL);
      fprintf (stderr, "\nchild %d terminated", pid);
    }
  return 0;
}
The code can be changed a bit if you want every child to perform different functions.

Last edited by Kumar; 08-20-2004 at 04:56 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
more on fork() feetyouwell Programming 6 09-17-2004 11:18 AM
fork() vibhory2j Linux - Software 1 05-24-2004 04:11 AM
Fork again Avatar33 Programming 13 08-22-2003 01:41 PM
fork() lowlifeish Programming 3 11-04-2002 10:50 AM
Fork Ztyx Linux - General 1 08-31-2002 11:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration