Thread: Sighandler questions with child/parent processes

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    14

    Sighandler questions with child/parent processes

    Hello all, I currently have the following program which uses fork to create two child processes which exit normally and return back to the parents and give the following output:

    child 2916 terminated normally with exit status=199
    child 2917 terminated normally with exit status=200

    However, I'm trying to induce a segmentation error by making the child write to a read-only segment of memory, which I tried to do right after the fork() command in the child section, which only made the program not give any output? I'm trying to alter the program to be able to handle segmentation faults so that it will print the following output:

    child 2916 terminated by signal 11: Segmentation fault
    child 2917 terminated by signal 11: Segmentation fault

    What am I doing wrong? And what is the best way of going about setting up a sig handler? How about psignal() ?

    Any help would be appreciated, thanks!

    Code:
    #include <string.h>
    #include <errno.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <signal.h>
    
    
    #define N 2
    
    
    pid_t Fork(void)
    {
            pid_t pid;
            if ((pid = fork()) < 0)
                printf("Fork error");
            return pid;
    }
    
    int main()
    {
            int status, i;
            pid_t pid;
    
            for (i = 0; i < N; i++)
                if ((pid = Fork()) == 0)   /* child */
                    memset((char *)0x0, 42, 200);    
                    /*should cause seg fault */
    
                    exit(199+i);
    
    
            /* parent waits for all of its children to terminate */
            while ((pid = waitpid(-1, &status, 0)) > 0) {
                if (WIFEXITED(status))
                printf("child %d terminated normally with exit status=d\n",
                           pid, WEXITSTATUS(status));
                else
                    printf("child %d terminated abnormally\n", pid);
            }
            if (errno != ECHILD)
       /*      unix_error("waitpid error"); */
    
            exit(0);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps
    WTERMSIG-Determine Which Signal Caused the Child Process to Exit
    WIFSIGNALED-Query Status to See If a Child Process Ended Abnormally

    > memset((char *)0x0, 42, 200);
    This would be better, it's more controlled and more flexible (you can try other causes apart from segfault)
    raise( SIGSEG );
    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.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by ninjacookies
    Code:
    memset((char *)0x0, 42, 200);  

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. Processes not dying
    By Elkvis in forum Linux Programming
    Replies: 12
    Last Post: 04-23-2008, 08:59 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM