LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-16-2004, 07:14 PM   #1
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Rep: Reputation: 30
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.
 
Old 02-16-2004, 07:28 PM   #2
gauge73
Member
 
Registered: Jan 2003
Location: Dallas, TX
Distribution: Fedora Core 4
Posts: 420

Original Poster
Rep: Reputation: 30
I've actually gotten the program to finish a couple of times (both processes). I am assuming this is some sort of synchronization problem. I guess perhaps something along the lines of the receiving process replying, and the sending process receiving that reply and sending another signal before the receiving process gets back to the next pause() in its for loop or something like that. Any suggestions about how I can make this work?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
squid stops due to signal 6 and signal 25 simplyrahul Linux - Software 3 05-28-2011 02:05 AM
exchanging data between 2 computer using serial port blackzone Linux - General 1 07-24-2004 03:54 AM
pthread signal problem xinwang00 Programming 12 11-11-2003 03:30 PM
Signal 6: Unknown Signal <=> Error with MAYA4.X under Linux SOLVED!!!! Faeroon Linux - Software 9 05-09-2003 02:57 PM
Signal: 6 (Unknown Signal) - Problems running a program Faeroon Linux - Software 86 01-14-2003 01:08 AM


All times are GMT -5. The time now is 01:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration