Solved

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

Posted on 2004-08-15
6
201 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

820 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