LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-09-2006, 01:29 PM   #1
aral
Member
 
Registered: Aug 2005
Posts: 115

Rep: Reputation: 15
signal interrupts reading from queue


I have a process who's main thread reads messages from a SystemV message queue every 5 seconds (because the messages arive in the queue every 5 seconds ) . Between two readings from the queue the main thread is blocked in a read( msgrcv call) from the queue , because there are no messages in the queue for 5 seconds. This process is supposed to be stoped by a signal sent from another process . So it should exit the reading loop when the signal arrives, destroy all resources and end properly. But the signal ,which is caught by the signal handler I installed ( this handler only changes a flag ), interrupts the read (msgrcv call) from the queue so the program stops failing at the msgrcv call and thus it doesn't have time to destroy resources and end properly. How could I fix this? I want the process which catches the signal to end properly, and destroy all resources .. How can I do this ?
 
Old 10-10-2006, 10:57 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by aral
But the signal ,which is caught by the signal handler I installed ( this handler only changes a flag ), interrupts the read (msgrcv call) from the queue so the program stops failing at the msgrcv call and thus it doesn't have time to destroy resources and end properly. How could I fix this? I want the process which catches the signal to end properly, and destroy all resources .. How can I do this ?
I suggest trying something like this:
Code:
/* Other #include's */

#include <errno.h>

/* ... */

int result;

/* ... */

result = msgrcv( ... );
if (result == -1) {
    if (errno == EINTR) {  /* msgrcv() was interrupted by a signal */
        if (flag_set_in_signal_handler) {
            your_resources_cleanup();
            exit(0);
        }
        exit(1);
     }

     /* Other error-handling / printing / logging */
}

/* msgrcv() was successful. 
 * Do somthing with the message just fetched */
Hope this helps.
 
Old 10-10-2006, 08:24 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I solved a similar issue in Perl ie blocking listen plus clean exit.
Waht I did was make the sighandler close (actually shutdown) the listen socket. this causes the listen loop to then continue and check for an exit/cleanup flag.
HTH
Actually, this was multi-threaded, but I think(?) the same trick would work in single thr.
 
  


Reply



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 01:05 AM
Signal Strength Meter shows low signal? falcon56215 Linux - Networking 0 09-15-2006 07:39 AM
Deleted qmail queue /var/qmail/queue tris Linux - Software 0 10-24-2003 11:52 PM
Signal 6: Unknown Signal <=> Error with MAYA4.X under Linux SOLVED!!!! Faeroon Linux - Software 9 05-09-2003 01:57 PM
Signal: 6 (Unknown Signal) - Problems running a program Faeroon Linux - Software 86 01-14-2003 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 AM.

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
Open Source Consulting | Domain Registration