🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.next.programmer
Article #470

Re: signals and the appkit

#470
From: Josh Doenias
Date: Mon, 03 Aug 1992 20:40
59 lines
2443 bytes
In article <Bs4G4M.19M@oceania.com>  writes:

>We are using sigvec to set a handler to catch SIGCHLD. (wait?(2) is no  
>good as this application has other tasks to perform.)  Since the  
handler's  
>stack is finite, we want to post an event to notify the application and  
>that there is work to be done and get off the stack.  

First of all, I would recommend avoiding signals and signal handling in an  
application at all costs.  Besides the inherent problems of protecting  
yourself against unwanted signals at the appropriate times, they are also  
really bad news if your program ever becomes multi-threaded.  You have no  
guarantee as to which thread will receive the signal, and things can get  
ugly.

In your case, where you're interested in child processes completing, I  
would recommend the following solution.

1.  Create a DPSTimedEntry function that gets called at some regular  
interval (using DPSAddTimedEntry)
2.  In that function, use the wait3() system call.  You said that wait()  
is no good, since it is blocking, but wait3() can be passed parameters  
making it non-blocking.
3.  If the wait3() call tells you a process has finished, send a message  
to whoever cares.  You can queue the messages up or handle them  
immediately.

Something like the following:

  void myHandlerFunction(DPSTimedEntry tenum, double now, MyObject *self)
  {
    union wait status;
    int pid = wait3(&status,WNOHANG,NULL);
    
    if (pid > 0) [self processFinished:pid withStatus:&status];
  }

  @interface MyObject
  
  #define CHILD_CHECKING_PERIOD 2.0    /* Check every two seconds */

  - init
  {
   [super init];
   childhandler = DPSAddTimedEntry(CHILD_CHECKING_PERIOD, 
			(DPSTimedEntryProc)myHandlerFunction,
			(void *)self, NX_BASETHRESHOLD);
  }

  - processFinished:(int)pid withStatus:(union wait *)status
  {
  /* do something interesting here */
  }

..

Hope this helps.  Again, I would recommend against using signals.

-- josh
Technical Details
Message-ID: <4536@rosie.NeXT.COM>
Path: arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!mips!sdd.hp.com!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!unixhub!ditka!ntg!rosie!next.com
References: <Bs4G4M.19M@oceania.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!spool.mu.edu!mips!sdd.hp.com!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!unixhub!ditka!ntg!rosie!next.com
From: jdoenias@next.com (Josh Doenias)
Newsgroups: comp.sys.next.programmer
Subject: Re: signals and the appkit
Message-ID: <4536@rosie.NeXT.COM>
Date: 3 Aug 92 20:40:34 GMT
Article-I.D.: rosie.4536
References: <Bs4G4M.19M@oceania.com>
Sender: news@NeXT.COM
Lines: 59