c - Need to access variables from child during SIGCHLD handler -


i'm prefacing stating homework assignment , i'm stuck while writing sigchld handler. need access variables within child process.

#include <stdlib.h> #include <stdio.h> #include <signal.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h>   #define text_len 10 #define num_rescue_plows 4 #define num_victims_to_rescue 40   /*  purpose:  keep trap of number of victims have been rescued.  */ int numrescuedvictims = 0;    /*  purpose:  keep track of process id's of rescuing snow plows.  */ pid_t plowpid[num_rescue_plows];   /*  purpose:  note @ least 1 child plow has finished.  reports  *process id , number of victims rescued each child plow.  *'signum' tells signal number.  no return value  */ //  may want define sigchld listener here //  should have loop wait()s children , //  , prints how many victims each has rescued. void listensigchld(int sig) {   int status;   pid_t pidwait;   while((pidwait = wait(&status)) > 0)     printf("plow %d rescued %d victim(s)", plowid, numvictimsrescued); }   /*  purpose:  handle being informed of rescued victim.  'signum' tells  *signal number.  no return value.  */ //  may want define sigusr1 listener here //  increments 'numrescuedvictims' , prints total number rescued victims void listensigusr1(int sig) {   numrescuedvictims += &numvictimsrescued;   printf("we've rescued %d victims!" numrescuedvictims); }   /*  purpose:  make 'num_rescue_plows' processes run 'rescuingplow'  *rescue stuck victims, , tell them quit after  *'num_victims_to_rescue' victims have been rescued.  ignores parameters.  *returns 'exit_success' os.  */ int main () {    //  i.  applicability validity check:     //  ii.  rescue victims:    //  ii.a.  install 'sigusr1' handler:   signal(sigusr1, listensigusr1);    // install 'sigusr1' handler   //  install sigchld handler here   signal(sigchld, listensigchld);    //  ii.b.  tell num_rescue_plows plows start rescuing victims:    int i;   int mypid= getpid();     (i = 0;  < num_rescue_plows;  i++)     {       //  fork() , save in plowpid[i]       plowpid[i] = fork();       //  if plowpid[i] less 0 do:       if(plowpid[i] < 0)     {       fprintf(stderr,"dude, system way busy play rescuer!\n");       return(exit_failure);     }        //  if plowpid[i] equal 0 do:       else if(plowpid[i] == 0)     {       char pidtext[text_len];       char indextext[text_len];     }       snprintf(pidtext,text_len,"%d",mypid);       snprintf(indextext,text_len,"%d",i);       execl("./rescuingplow","rescuingplow",pidtext,indextext,null);       fprintf(stderr,"dude, stole plow!!\n");       return(exit_failure);     }     //  ii.c.  wait until victims have been rescued:    while  (numrescuedvictims < num_victims_to_rescue)     {       sleep(1);       printf("searching victims . . .\n");     }     //  iii.  finished:    //  loop send sigterm num_rescue_plows plow processes   (int = 0; < num_rescue_plows; i++)     {       kill(plowpid[i], sigterm);     }   int tosleep= num_rescue_plows;    //  sleep() can interrupted sigchld.  whenever interrupted   //  returns number of seconds still remain on alarm   //  clock.  let's wait until has slept full amount incase   //  prematured interrupted sigchld.       {       tosleep= sleep(tosleep);     }   while  (tosleep > 0);    printf("ready next snow storm!\n");   return(exit_success); } 

`

and here's what's going end being spawned processes. although haven't worked through yet.

/*  *  rescuingplow.c  *  *  compile $ gcc rescuingplow.c -o rescuingplow  */ #include <stdlib.h> #include <stdio.h> #include <signal.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h>   /*  purpose:  keep track of number of victims process  *  rescued.  */ int numvictimsrescued   = 0;    /*  purpose:  return number of victims rescued os.  'signum'  *  tells signal number.  no return value.  */ //  may want write sigterm handling function //  returns os 'numvictimsrescued'.    /*  purpose:  rescue victims @ random intervals , inform parent process  *  sending sigusr1 until receiving sigterm.  first parameter (after  *  program name) tells parent's process id.  second parameter tells  *  plow's index.  */ int main    (int argc, char* argv[]) {    //  i.  applicability validity check:    pid_t parentpid;   int   plowid;    if  (argc < 3)   {     fprintf(stderr,"usage: rescuingplow <parentpid> <plowid>\n");     return(exit_failure);   }    parentpid = atoi(argv[1]);   plowid    = atoi(argv[2]);     //  ii.  rescuing victims until told stop:    //  ii.a.  install signal handler:    //  install sigterm handler here   srand(plowid);  //  uniquely initialize random number generator act independently of each other     //  ii.b.  rescue victims:    //  write endless loop that:   //  (1) 'sleep((rand() % 6) + 1);'   //  (2) increments 'numvictimsrescued'   //  (3) 'printf("plow %d rescued %d victim(s)!\n",plowid,numvictimsrescued);'   //  (4) send 'sigusr1' 'parentpid'     //  iii.  finished:    return(exit_success); } 

i'm not sure i'm going or how deal that. i'm pretty confident can deal of other problems exist.

without using ipc mechanics child can pass value of 8 bits parent process.

those 8 bits send child parameter call exit() , receive parent applying macro wexitstatus() value of status returned successful call wait() or waitpid(). please see man 2 exit , man 2 wait details.

if remember correctly 8 bits minimum defined standard. implementations might allow more bits.

update:

example on how use wait():

int child_exit_code = -1; int status = -1; pid_t pid = wait(&status); if (-1 != pid)   child_exit_code = wexitstatus(status); 

Comments