Solved

Can't catch SIGCHLD

Posted on 2000-03-05
5
368 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:MokoWebworks
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:ufolk123
ID: 2586764
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
 

Author Comment

by:MokoWebworks
ID: 2589732
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
 
LVL 3

Accepted Solution

by:
ufolk123 earned 50 total points
ID: 2590400
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
 

Author Comment

by:MokoWebworks
ID: 2590506
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
 
LVL 3

Expert Comment

by:ufolk123
ID: 2590646
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question