I've been working on this code for 2 days now and i finally think i got the while loop running correctly but now i cant seem to print the child process id's. The code starts with the parent process forking 3 child processes. Then the parent must wait till a child terminates to print out that child's PID. Any tips to fix my output is greatly appreciated.

#include <stdio.h>
//exit function
#include <stdlib.h>
//unix functions
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SHMKEY ((key_t) 7890)
typedef struct
{
  int value;
} shared_mem;

shared_mem *total;




/*----------------------------------------------------------------------*
 * This function increases the value of shared variable "total"
 *  by one all the way to 120000
 *----------------------------------------------------------------------*/

void process1 ()
{
  int k = 0;

  while (k < 120000)

{
      k++;
      total->value = total->value + 1;
    }
  printf ("From process1 total = %d\n", total->value);

}


/*----------------------------------------------------------------------*
 * This function increases the vlaue of shared memory variable "total"
 *  by one all the way to 170000
 *----------------------------------------------------------------------*/

void process2 ()
{
  int k = 0;

  while (k < 170000)

{
      k++;

     total->value = total->value + 1;
    }

  printf ("From process2 total = %d\n", total->value);

}

/*----------------------------------------------------------------------*
 * This function increases the vlaue of shared memory variable "total"
 *  by one all the way to 200000
 *----------------------------------------------------------------------*/
void process3 ()
{
  int k = 0;

  while(k < 200000)
{
    k++;

    total->value = total->value + 1;
  }

  printf ("From process3 total = %d\n", total->value);

}

/*----------------------------------------------------------------------*
 * MAIN()
 *----------------------------------------------------------------------*/

main()
{
  int   shmid;
  int   pid1;
  int   pid2;
  int   pid3;
  int   ID;
  int   status;

  char *shmadd;
  shmadd = (char *) 0;

/* Create and connect to a shared memory segmentt*/

  if ((shmid = shmget (SHMKEY, sizeof(int), IPC_CREAT | 0666)) < 0)
    {
      perror ("shmget");
      exit (1);
    }


 if ((total = (shared_mem *) shmat (shmid, shmadd, 0)) == (shared_mem *) -1)
    {
      perror ("shmat");
      exit (0);
    }

  //intialize shared memory variable

total->value = 0;

  //if variable pid1 is a child when forked
  if ((pid1 = fork()) == 0)
    process1();

  //if variable pid2 is a child when forked
  if ((pid1 != 0) && (pid2 = fork()) == 0)
    process2();

  //if variable pid3 is a child when forked
  if((pid1 != 0) && (pid2 != 0) && (pid3 = fork()) == 0)
    process3();

  //If this is a parent process
  if ((pid1 != 0) && (pid2 != 0) && (pid3 != 0))
    {

      if ((shmctl (shmid, IPC_RMID, (struct shmid_ds *) 0)) == -1)
        {
          perror ("shmctl");
          exit (-1);
        }

     while((pid1 = wait(&status) > 0) && (pid2 = wait(&status) > 0) && (pid3 = wait(&status) > 0)){
        ID = pid1;
        if(status > 0)
          printf("Process 1 with PID %d\n", ID);
        ID = pid2;
        if(status > 0)
          printf("Process 2 with PID %d\n", ID);
        ID = pid3;
        if(status > 0)
          printf("Process 3 with PID %d\n", ID);
     }

      printf ("\t\t  End of Program.\n");
    }

}

output:
From process1 total = 120000
From process2 total = 290000
From process3 total = 490000
Process 1 with PID 1
Process 2 with PID 1
Process 3 with PID 1

//Note: loop for parent to wait for child processes to finish and print ID of each child*****/

Just do one wait() in the parent process in a loop - not one for each child pid - until it receives the SIGCHLD signal for each forked child process. Waiting 3 times in the same while() loop is counter-productive. Alternatively, you can use waitpid() instead, to wait for a specific process to die.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.