Solved

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

Posted on 2004-08-15
6
199 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Retrun object in plist format 5 55
port mapped I/o query 10 166
Computer slow / BSOD 10 43
Memory going from 12gb to 64gb or 96gb. worth it? 15 138
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to create, access, and change arrays in the C programming language.

895 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

11 Experts available now in Live!

Get 1:1 Help Now