Thread: forks and child processes

  1. #1
    Registered User
    Join Date
    May 2008

    forks and child processes


    I have a requirement to write a C program, in which a parent process creates 3 child processes. Each of these three child processes are required to run individual programs and once complete, the parent will then exit.

    I've managed to create the parent, and 1 child, which does execute a program called average.

    I'm stuck at changing this so that I have 3 childs created and each having a different program run. Any help would be appreciated.


    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/wait.h>
    int main(void) {
    	pid_t child_pid;
    	int status;
    	pid_t wait_result;
    	child_pid = fork();
    	if (child_pid == 0)
    		printf("I am a child and my pid = %d\n", getpid());
    		execlp("average", "./average", NULL);
    		printf("Could not execl file average\n");
            else if (child_pid > 0)
    		printf("I am the parent and my pid = %d\n", getpid());
    		printf("My child has pid = %d\n", child_pid);
    		printf("The fork system call failed to create a new process\n");
    	printf("I am a happy, healthy process and my pid = %d\n", getpid());
    	if (child_pid == 0)
     	printf("This code will never be executed!\n");
    		printf("I am a parent and I am going to wait for my child.\n");
    		do {
    			wait_result = wait(&status);
    		} while (wait_result != child_pid);
    		printf("I am a parent and I am quitting.\n");
    	return 0;
    and the average.c file - which works perfectly fine!
    /* average.c */
      Program to calculate the average of n numbers
    #include <stdio.h>
    #include <stdlib.h>
    #define NMAX 100
    int main() 
      int n, count;
      double avg, d, sum = 0.;
      double list[NMAX];
      /* Read in value for n */
      printf("\nHow many numbers do you want to average? ");
      scanf("%d", &n);
      /* Check that n is not too large or too small */
      if ((n > NMAX) || (n <= 0)) 
        printf("\nPlease enter an integer only... program will terminate\n");
      /* Read in the numbers and calculate their sum */
      for (count = 0; count < n; ++count) 
        printf("Enter %d number: ", count + 1);
        scanf("%lf", &list[count]);
        sum += list[count];
      /* Calculate and display the average */
      avg = sum / (double) n;
      printf("\nAverage is %5.2f\n\n", avg);
    Appreciate any advice. I'm very new to the concept of programming and C, so I apologise in advance if this is a stupid question.


  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Creating more children is essentially just a case of repeating the process of creating the first fork.

    As you will be starting different children, so you would have to use different arguments to exec(). But other than that, it's pretty much the same.

    The other thing that you need to track is which children PID's you get, and keep track of that ALL of them have exited before you exit the parent process.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Processes not dying
    By Elkvis in forum Linux Programming
    Replies: 12
    Last Post: 04-23-2008, 08:59 AM
  2. multiple forks, pipes?
    By alcaseltz in forum C Programming
    Replies: 2
    Last Post: 10-26-2007, 07:07 AM
  3. Forks v's threads
    By clancyPC in forum C Programming
    Replies: 7
    Last Post: 11-11-2005, 06:29 AM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. Forks and Processes
    By osal in forum C Programming
    Replies: 4
    Last Post: 09-26-2004, 05:47 PM