Subject: Re: Handling signals
To: Benjamin Walkenhorst <krylon@gmx.net>
From: David S. <davids@idiom.com>
List: netbsd-users
Date: 12/24/2003 23:20:44
> 
> I have written a program that writes output to a log file (via 
> fprintf()). When I stop the program (which runs in background) via 
> kill, the last line of output sometimes is not written completely into 
> the file.
> I use a perl-script to control the program which starts the program by 
> reading the logfile's last line and passing part of it as a 
> command-line-argument to the program.
> When reading such an incomplete line, sometimes processing the line 
> fails or the program is started with an incorrect parameter. So I would 
> like to make sure, the program does not quit while writing a line to 
> the logfile.
> I think I remember you can do this in shell-scripts via the 
> "trap"-command. With trap, if I remember correctly, you can catch a 
> signal and define an action to handle it. Can I do such a thing in C? 

Sure.  The explanation of how is a bit involved.  I recommend reading
chapter 10 of Steven's _Advanced_Programming_in_the_UNIX_Environment_.
If you don't have that book, then you should get a copy, especially
if you're going to do the sort of programming you describe.

David S.

>