Link to home
Start Free TrialLog in
Avatar of kuganesh
kuganesh

asked on

modify the program code using 'for' loop to create the 4 processes

the code should be changed so that the 4 child processes are created using the 'for ' loop:
PLS HELP! thanx!

[create wait process]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

void ShowStatus( int stat )
{
  if (WIFEXITED(stat))
    fprintf(stderr, "Normal termination: Exit status %d\n", WEXITSTATUS(stat) );
  else if (WIFSIGNALED(stat))
    fprintf( stderr, "Abnoral termination: Signal number %d\n", WTERMSIG(stat) );
  else
    fprintf(stderr,  "Unknown problem detected\n" );
}

int main(int argc, int **argv)
{
  pid_t retFork;
  int status;
  int up = 200;
  int down = 0;
  int * pointer = null;

   retFork= fork();  /*Create first child process*/
   switch (retFork){
   case -1:
     perror( "fork error" );
     exit (1);
   case 0:         /* Child process */
     exit( 1 );    /* Terminate normally */
   default:        /* Parent process */
    if (wait( &status ) != retFork){  /* wait for child */
      perror( "wait error" );
    }
    ShowStatus ( status );
  }
   retFork = fork();  /*Create second child process*/
   switch (retFork){
   case -1:
     perror( "fork error" );
     exit (1);
   case 0:         /* Child process */
     abort(  );    /* Generate SIGABRT */
     exit( 2 );     /*Terminate abnormally*/
   default:        /* Parent process */
    if (wait( &status ) != retFork ){  /* wait for child */
      perror( "wait error" );
    }
    ShowStatus ( status );
  }
   retFork = fork();  /*Create third child process*/
   switch (retFork){
   case -1:
     perror( "fork error" );
     exit (1);
   case 0:         /* Child process */
     up = up / down;    /* Generate SIGFPE (i.e., division by zero)*/
     exit(2);     /*Terminate abnormally*/
   default:        /* Parent process */
    if (wait( &status ) != retFork ){  /* wait for child */
      perror( "wait error" );
    }
    ShowStatus (status);
  }
     retFork = fork();  /*Create four child process*/
   switch (retFork){
   case -1:
     perror( "fork error" );
     exit (1);
   case 0:         /* Child process */
     *pointer = 100;    /* Generate SIGFPE (i.e., division by zero)*/
     exit(2);     /*Terminate abnormally*/
   default:        /* Parent process */
    if (wait( &status ) != retFork ){  /* wait for child */
      perror( "wait error" );
    }
    ShowStatus (status);
  }
return 0;
}



Avatar of pulupul
pulupul

What for loop? I don't see any. If you mean where to put the for, explain what you want to do first.
Avatar of kuganesh

ASKER

yes. just change all the child processes into a for structure... not case switch..
its quite basic.. just have to change the structure of the code so that it executes in a for loop..
You want all the child processes to execute at the same time, and then wait for them? or you want to never stop creating processes and waiting for them?. What should be the condition of the for loop?
the condition for the loop should be :
for(i=0; i<4; i++)
from what i understand i should put this infront of retfork
but i dont know how... can you please code it so that the whole thing is able to use 'for' execution ... thank you
Ok, I wans't getting the point, here it goes the code:

int main(int argc, int **argv)
{
      pid_t retFork;
      int status;
      int up = 200;
      int down = 0;
      int * pointer = null;


      for (int i=0; i < 4; i++)
      {
            retFork= fork();  /*Create first child process*/
            switch (retFork)
            {
            case -1:
                  perror( "fork error" );
                  exit (1);
            case 0:         /* Child process */
                  switch(i) {
                  case 0:
                        exit( 1 );    /* Terminate normally */
                        break;
                  case 1:
                        abort(  );    /* Generate SIGABRT */
                        exit( 2 );     /*Terminate abnormally*/
                        break;
                  case 2:
                        up = up / down;    /* Generate SIGFPE (i.e., division by zero)*/
                        exit( 2 );     /*Terminate abnormally*/
                        break;
                  case 3:
                        *pointer = 100;    /* Generate SIGFPE (i.e., division by zero)*/
                        exit( 2 );     /*Terminate abnormally*/
                        break;
                  }
            default:        /* Parent process */
                  if (wait( &status ) != retFork){  /* wait for child */
                  perror( "wait error" );
            }
            ShowStatus ( status );
      }

      return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of lebuihung
lebuihung

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial