Can't catch SIGCHLD

I have a process that spawns another process.  In case 1 process A spawns process B by simply calling spawnv() on it's binary.  In case 2 process A calls a script that unzips process B, once process B is uncompressed, process A then calls spawnv() to run process B.

My question is, when I run process A using case 1 I am able to catch SIGCHLD with my signal handler, but when I run process A using case 2 I am no longer able to catch SIGCHLD with my signal handler.  Any ideas on why this is happening.  There is no recoding done to process A to do the 2 cases, I just change the flags to either run the binary or call the decompressing script first.
MokoWebworksAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ufolk123Connect With a Mentor Commented:
Hi MokoWebworks ,

I have some doubts regarding your environment.spawnxxx() family of calls are not supported on most of the Unix falimy OS.What exactly is your work environment ?
You code flow seems OK.From this it looks like may be implementation of spawnl() on your system is disabling the signal handling ( as you are using P_WAIT option) .Actually it should not completely change the signal handler and properly restore the SIGCHLD handler before return but from your example seeems that is the root of your problem.

So try to do the following.
Add
signal(SIGCHLD, signalhandler);
signal(SIGTERM, signalhandler);
as
startup_apps()
{
..
..
if (running_in_zipmode)
{
call_script(name_of_zipfile)
}
..
signal(SIGCHLD, signalhandler);
signal(SIGTERM, signalhandler);
..
rval = spawnv(P_NOWAIT, programpath, args_list);
setprio(rval, 29);
..
..
return;
}


Also try to use the system() instead of spawn as a second option.
Try and check if you still get the problem.

ufolk123

0
 
ufolk123Commented:
Hi MokoWebWorks.

In case 2 , Are you using fork etc to run the unzip script ?
If that is the case then your main process (A) will not get the signal as signal will be sent to forked child of A.
Can you give me some snapshot of your code ?

ufolk123
0
 
MokoWebworksAuthor Commented:
Hello ufolk123,

Thank's for responding.  Here is a snapshot of my code:

/*---------------------------------*/int
main()
      {
      .
      .
      signal(SIGCHLD, signalhandler);
      signal(SIGTERM, signalhandler);
      .
      .
      startup_apps();
      .
      .
      while(1)
            {
            }
      return 1;
      }

/*---------------------------------*/void
startup_apps()
      {
      .
      .
      if (running_in_zipmode)
            {
            call_script(name_of_zipfile)
            }
      .
      .
      rval = spawnv(P_NOWAIT, programpath, args_list);
      setprio(rval, 29);
      .
      .
      return;
      }      

/*---------------------------------*/void
call_script(char *name_of_zipfile)
      {
      .
      .
      rval2 = spawnl(P_WAIT,             scriptpath, scriptname, name_of_zipfile, programpath, NULL);
      .
      .
      return;
      }

/*---------------------------------*/void
signalhandler(int signo)
      {
      switch (signo)
            {
            .
            .
            case SIGCHLD:
                  pid = wait(NULL);

                  printf("Process %d terminated!\n", pid);
                  break;
            .
            .
            }
      return;
      }
      
I tried to debug the code and in case 2 it didn't even entered the signal handler as opposed to when the running_in_zipmode flag is unset.

In case you wan't to know what's in the scriptfile, here it is:

/*---------------------------------*/#!/bin/sh

if gzip -t $1
then
      gzip -dc $1 > $2
      chmod 777 $2
else
      echo gzipfile corrupted
      exit 1
fi

exit 0

Hope you could figure out what's wrong with it.  Thanks in advance!
0
 
MokoWebworksAuthor Commented:
Hello again ufolk123,

Thanks for the advice,I managed to get it working by placing the signal handler right before spawning, though, I do have a few concerns.  The program is to run as a spawning program, meaning it'll call spawn in more than one occasion (in fact, in a lot of occassion),  won't this screw up the system by assigning the same signal to the signal handler more than once?  Otherwise, the program works and I accept your answer.

BTW, my working environment is QNX, an OS specializing for embedded systems, same as UNIX only slightly different architecture.
0
 
ufolk123Commented:
won't this screw up the system by assigning the same signal to the signal handler more than once?  Otherwise, the program works and I accept your answer.

Do not worry about that.Ideally the signal call should replace the signal handler to the new one.So mulitple signal() calls are never a problem ( As par the standard implementation).


my working environment is QNX, an OS specializing for embedded systems, same as UNIX only slightly different architecture.


Hmmm, Thats why you have some DOS based C library calls supported.As spawn() etc are C library calls orginally in DOS ( & now in windows also).

Anyway thanks for this session.Please feel free if you have anything to share in future.

ufolk123
0
All Courses

From novice to tech pro — start learning today.