C++ Signal Exchanging Problem
I have to write a program for class of mine, and I need some help. The program has to fork and the two processes have to send signals through the kernel or send bytes through a pipe and time how long it takes to get a response from the other process. Then it has to output the results.
I have the pipes part working, no problem. However, the signal part is giving me a lot of trouble. I can get it to compile and I have the signal handlers responding, but the program never makes it through the test. It fails at different times every time I execute it and I can't figure out why. Here are a few snippets of the code. I still have a few cout's in there to keep track of what's going on.
// Here is the signal handler...
void my_sighandler(int signo)
{
if( signo == SIGUSR1 )
{
if( getpid() == childPID )
{
cout<<"CHILD: Received a signal - "<<SIGUSR1<<endl;
cout<<"CHILD: Responding to parent ("<<parentPID<<")...\n";
kill(parentPID, SIGUSR2);
}
else
{
cout<<"PARENT: Receeived a signal - "<<SIGUSR1<<endl;
cout<<"PARENT: Responding to child ("<<childPID<<")...\n";
kill(childPID, SIGUSR2);
}
}
else
{
cout<<"A SIGUSR2 was received.\n";
}
}
// Here is the part that "installs" the signal handler function above...
struct sigaction myaction;
myaction.sa_handler = my_sighandler;
sigemptyset(&myaction.sa_mask);
myaction.sa_flags=0;
sigaction(SIGUSR1, &myaction, NULL);
sigaction(SIGUSR2, &myaction, NULL);
/* Here is the child's code. It recevies (and replies) for the first set of iterations, and then it sends the signals and waits for replies in the second set. I haven't set up the timing part yet. */
for(int z=0; z<iterations; z++)
{
pause();
}
cout<<"child starts sending..\n";
for(int z=0; z<iterations; z++)
{
kill(parentPID, SIGUSR1); //send
pause(); // wait for response
}
cout<<"child exiting..\n";
/* Here is the parent's code. It sends signals and waits for replies first. Then it receives them and replies. Again, I haven't set up the timing part yet. */
for(int z=0; z<iterations; z++)
{
kill(childPID, SIGUSR1);
pause();
}
cout<<"Parent done sending..\n";
for(int z=0; z<iterations; z++)
{
pause();
}
cout<<"Parent exiting..\n";
Any help would really be appreciated. Any additional info needed, just tell me.
|