-
January 19th, 2010, 04:37 PM
#1
Problems designing a signal handler in UNIX
I'm trying to create a signal handler thread to handle SIGALRM signals.
The main thread calls the following function before creating any other threads
Code:
pthread_t Timer::Init(long _clock_interval_musec)
{
// Set interval.
clock_interval = _clock_interval_musec;
// This is called while in a single threaded environment.
// Main thread masks alarm.
sigset_t alarm_off_mask;
if ((sigemptyset(&alarm_off_mask) == -1) || (sigaddset(&alarm_off_mask, SIGALRM) == -1))
perror("Failed to initialize the signal set");
if (pthread_sigmask(SIG_BLOCK, &alarm_off_mask, NULL) == -1)
perror("Failed to block SIGALRM");
// Now call a new thread which handles alarm signals.
pthread_t retID;
pthread_create(&retID, NULL, TimerHandlerThread, NULL);
// Return id of thread responsible for managing alarm signals.
return retID;
}
As you can see, I block the alarm signal.
The spawned signal handler thread has the following Run function
Code:
void* Timer::TimerHandlerThread(void* args)
{
// Change granualirity of alarm.
itimerval tv;
tv.it_interval.tv_sec = clock_interval / USECS_IN_SECS;
tv.it_interval.tv_usec = clock_interval % USECS_IN_SECS;
setitimer(ITIMER_REAL, &tv, NULL);
// Unmask alarm signals in this thread.
sigset_t sigset;
if ((sigemptyset(&sigset) == -1) || (sigaddset(&sigset, SIGALRM) == -1))
perror("Failed to add signal to wait for");
Timer currentTime;
int sig;
while (1)
{
/* wait for a signal to arrive */
sigwait(&sigset, &sig);
std::cout << "we are here\n";
if (sig == SIGALRM && g_timerQueue.size() > 0)
{
currentTime.Set(0);
// Handle if needed
g_mutex.P();
if (*(g_timerQueue.front()) < currentTime)
{
g_timerQueue.front()->Event_Handler();
std::pop_heap(g_timerQueue.begin(), g_timerQueue.end());
g_timerQueue.pop_back();
}
g_mutex.V();
}
}
return NULL;
}
Sigwait never returns ("we are here" never prints).
clock_interval is 50.
Is the timer not firing?
Did I not set my interval correctly?
Did I not proper block the signals ensuring the that the default handler would not take care of them?
Any help is welcome.
-
January 19th, 2010, 07:49 PM
#2
Re: Problems designing a signal handler in UNIX
If the lack of replies is due to the question not being clear, please do say so.
-
January 20th, 2010, 08:40 PM
#3
Re: Problems designing a signal handler in UNIX
Try setting both tv.it_interval and tv.it_value.
You should flush cout if you want to see the output immediately.
gg
-
January 21st, 2010, 04:12 AM
#4
Re: Problems designing a signal handler in UNIX
I'm not familiar with Unix style threading (or the Posix pthreads library) - so I'm probably not the person to help, but don't you need to add the timer signal to your signal set? (sigaddset)
-
January 21st, 2010, 10:02 AM
#5
Re: Problems designing a signal handler in UNIX
Signal handlers and threads don't mix well. There's little you can do in a signal handler, and there's no control of which thread (if any) the signal handler is executed on.
To have a thread deal with particular signals, you have to block that signal in all threads, then a single thread can use sigwait() to determine if the signal is pending and act on it.
See Examples section here: http://www.opengroup.org/onlinepubs/...d_sigmask.html
gg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|