Solved

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

Posted on 2004-08-15
6
200 Views
Last Modified: 2010-04-15
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;
}



0
Comment
Question by:kuganesh
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:pulupul
ID: 11803653
What for loop? I don't see any. If you mean where to put the for, explain what you want to do first.
0
 

Author Comment

by:kuganesh
ID: 11803720
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..
0
 
LVL 3

Expert Comment

by:pulupul
ID: 11803753
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?
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:kuganesh
ID: 11803882
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
0
 
LVL 3

Expert Comment

by:pulupul
ID: 11803923
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;
}
0
 

Accepted Solution

by:
lebuihung earned 300 total points
ID: 11867725
#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;
  int i;
 
 
  for(i=0;i<4;i++)
  {
        retFork=fork();
      
      if(retFork==0) /* if its a child process */
      {
            if(i==0) /* first child process */
            {
                  exit(1);
            }
            else if(i==1)
            {
                     abort(  );    /* Generate SIGABRT */
                   exit( 2 );     /*Terminate abnormally*/
            }
            else if(i==2)
            {
                    up = up / down;    /* Generate SIGFPE (i.e., division by zero)*/
                    exit(2);     /*Terminate abnormally*/
            }
            else if(i==3)
            {
                    *pointer = 100  ;  /* Generate SIGFPE (i.e., division by zero)*/
                    exit(2);     /*Terminate abnormally*/
            }
      }
      else if(retFork<0)
      {      
            perror( "fork error" );
            exit (1);
      }
          else
      {
            if (wait( &status ) != retFork)
            {  /* wait for child */
                        perror( "wait error" );
                }
      
            ShowStatus ( status );
      }
   }
   return 0;
 }
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to understand recursion 12 226
xamarin c# deserialize Json containing nested object 2 93
Raspberry Pi 3 to send text message 9 115
outlook office 365 8 122
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question