Solved

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

Posted on 2004-08-15
6
198 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:kuganesh
Comment Utility
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
Comment Utility
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
Comment Utility
#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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now