Signals and forks

This is a discussion on Signals and forks within the C Programming forums, part of the General Programming Boards category; i am asked to find all the possible outputs in this question: Code: #define N 4 int val = 9; ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    Signals and forks

    i am asked to find all the possible outputs in this question:
    Code:
    
    #define N 4
    int val = 9;
    void handler(sig) {   val += 3;   return;}
    int main() {  
    pid_t pid;  
    int i;  
    signal(SIGCHLD,handler);  
    for (i=0;i<N;i++) {   
     if ((pid =fork()) == 0) {        
    val -= 3;        
    exit(0);    }  } 
    for (i=0;i<N;i++) {   
     waitpid(-1,NULL,0);  }  
    printf("val = %d\n",val); }
    


    I do not know what the line signal(SIGCHLD, handler) does. I only found the following:


    SIGABRT - abnormal termination.
    SIGFPE - floating point exception.
    SIGILL - invalid instruction.
    SIGINT - interactive attention request sent to the program.
    SIGSEGV - invalid memory access.
    SIGTERM - termination request sent to the program.

    what does SIGCHLD do? Can you also explain the for loop in this question as well?

    and what necessary libraries do I need to compile and run this code?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    If I google for "SIGCHLD" the first sentence in the results is literally this: "The SIGCHLD signal is sent to the parent of a child process when it exits, is interrupted, or resumes after being interrupted."

    Basically the program is forking N times -- that's what the first for loop is doing -- and then waiting on each of the N processes to exit. In addition, the first program installs a signal handler for SIGCHLD so that whenever that signal is received, 3 is added to val...

    If you want to compile and run this, all you need is some header files. printf needs stdio.h, SIGCHLD and signal are in signal.h, and exit is in stdlib.h. You may need others as well, e.g. waitpid is supposed to require sys/types.h and sys/wait.h, and fork is supposed to require unistd.h, etc. The first three were all I needed on my particular machine since some headers include others, but might as well be safe:
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    #define N 4
    int val = 9;
    void handler(sig) {   val += 3;   return;}
    int main() {  
    pid_t pid;  
    int i;  
    signal(SIGCHLD,handler);  
    for (i=0;i<N;i++) {   
     if ((pid =fork()) == 0) {        
    val -= 3;        
    exit(0);    }  } 
    for (i=0;i<N;i++) {   
     waitpid(-1,NULL,0);  }  
    printf("val = %d\n",val); }
    $ gcc foo.c -o foo
    $ ./foo
    val = 21
    $
    Looks like 21 is one possible output.
    Last edited by dwks; 12-09-2012 at 05:01 PM. Reason: mention other headers
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. many forks
    By quo in forum C++ Programming
    Replies: 1
    Last Post: 06-07-2012, 02:34 PM
  2. communication between forks
    By kaijuu in forum C Programming
    Replies: 6
    Last Post: 05-07-2008, 09:37 AM
  3. Forks v's threads
    By clancyPC in forum C Programming
    Replies: 7
    Last Post: 11-11-2005, 05:29 AM
  4. Forks and Processes
    By osal in forum C Programming
    Replies: 4
    Last Post: 09-26-2004, 05:47 PM
  5. Pipes/Forks
    By Alexisa in forum C Programming
    Replies: 6
    Last Post: 11-06-2003, 06:31 PM

Tags for this Thread


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