I have a program that I need to use signals and handlers with. I have two child processes that are supposed to receive a user defined signal and one is supposed to do nothing with the signal, while the other is supposed to print out some generic message, sleep and then print out another message indicating that the halt is over. The one child process that’s supposed to do nothing with the signal is supposed to be the one to raise the signal.
I am pretty sure the signal is rose with the notation signal(SIGINT, handler); so I would simply put that within the function that is defined by the one child process, but how do I “send” the signal to both processes? How can I say under a set of conditions that the one process receives the signal and does what it is told by the handler while the other receives the signal and does nothing?
I was reading about how kill(pid, handler) would be able to send the designated process to the handler, but whenever I try it, it tells me that argument 1 of kill makes integer from pointer without a cast.
My program is a reader/writer program that pretty much tests concepts without a definite purpose, and right now, is all within one source doc, but will be split off into a reader source to define the reader process’ functions, a writer source, which will define the writer process’ function and then the main source that creates the two child processes. I will also have a header file that will define all the necessary constants and function prototypes, but right now, if I can just get it working from within this one source, I can figure out later how to separate it into multiple sources.
This answer is based off of my reading of the GNU C Manual’s chapter on signals.
Like Kerrek says, signal() allows processes to register with the OS that there is a signal of interest that the process would like the OS to deliver to it. Once the signal has been generated, the sighandler_t argument to signal() specifies what the process will do, either default handling (SIG_DFL), ignore the signal (SIG_IGN), or something custom as defined by the handler function that is passed to signal(). That handler function is where you define the behaviors of one process to do something with it when the signal is delivered to it.
The second processes job is to raise the specified signal after a period of sleep() so it’ll be using the kill(pid, signum) function to do so which takes the PID of the intended recipient and signal to deliver to said recipient.