plz help!!!

This is a discussion on plz help!!! within the Linux Programming forums, part of the Platform Specific Boards category; THE CODE : Code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/wait.h> void ShowStatus( int stat ) { if (WIFEXITED(stat)) ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    plz help!!!

    THE CODE :
    Code:
    #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;
    
       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 fourth 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);
      }
      return 0;
    }

    I have been given this code and told to:

    i. Modify the createwaitprocess.c program to create a fourth child process which terminates due to a segmentation fault. Does the addition of the fourth process change the output produced from the first three processes? Explain.

    So i added in a fourth child process but then i got stuck!!! Does anyone know wat a segmentation fault is?? Or how I am suppose to cause one in this program?

    thnx

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    seg fault is when the program tries to read memory not allocated to it...
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i=0;
    	char str[5]={'\0'};
    	for(;;)
    	{
    		printf("%s",str[i++]);
    	}
    	return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    A far more predictable way is to just dereference a NULL pointer

    int *p = NULL;
    *p = 0; // go boom
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 10:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 08:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21