|
Home > Archive > Unix Programming > February 2004 > A signal race condition
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
A signal race condition
|
|
| pat saunders 2004-02-18, 10:33 pm |
| Hi,
I am wondering whether anyone can help me with fixing a signal race condition.
Basically I have two processes, one reading from a shared queue and the other
writing message onto the message queue.
The reading process goes to sleep (pause) if no messages are on the queue and
the writing process after writing a message to the queue , sends a SIGCONT
signal to the reading / sleeping process to wakeup and read message.
Here is a snipet of code from reading process :
Ev_get_event(boolean wait)
{
if event queue is empty
{
if (wait == FALSE)
{
return no_event;
}
else (sleep until SIGCONT signal recieved)
{
pause();
}
} process message on event queue;
}
The Race condition can happen between the (sleep until SIGCONT) and the pause().
If a SIGCONT is received , the process will still pause() and miss the message.
I have come up with a solution and wonder if it will work ???
Ev_get_event(boolean wait)
{
/* Block SIGCONT */
Sigemptyset(&zeromask);
Sigemptyset(&oldmask);
Sigemptyset(&newmask);
Sigaddset(&newmask, SIGCONT);
Sigprocmask(SIG_BLOCK, &newmask, &oldmask);
if event queue is empty
{
if (wait == FALSE)
{
/* unblock SIGCONT */
Sigprocmask(SIG_SETMASK,&oldmask,NULL);
return no_event;
}
else (sleep until SIGCONT received)
{
/* Replace Pause with Sigsuspend */
Sigsuspend(&zeromask);
/* Unblock SIGCONT */
Sigprocmask(SIG_SET_SETMASK, &oldmask, NULL);
}
} process message on event queue;
/* unblock SIGCONT */
Sigprocmask(SIG_SET_SETMASK, &oldmask, NULL);
}
Any Help appreciated
Pat
| |
| Loic Domaigne 2004-02-19, 2:34 am |
| Hi Pat,
> I am wondering whether anyone can help me with fixing a signal race
> condition.
Unfortunately, I won't answer your question directly...
> Basically I have two processes, one reading from a shared queue and the
> other writing message onto the message queue.
Do you mean, you are using shared memory? Because, if you need a mechanism
for sleeping as long as no data are available, then I would maybe consider
another form of IPC (message queue / FIFO / Unix Socket Domain etc.).
The major advantages of those, is that the synchronization mechnism is
built-in. The reading process performs a blocking read on the queue, and
is put (resp. awaken) by the kernel when no data are available (resp. data
has arrived).
Otherwise, if you want to stay with this "shared queue", then consider
rather semaphores (Posix or SysV) for synchronizing. And if you really want
to synchronize your processes with signal, then maybe you should consider
Real-Time signals (AFAIK, SIGCONT isn't queued. This might be a problem).
Regards,
Loic.
--
Article posté via l'accès Usenet http://www.mes-news.com
Accès par Nnrp ou Web
|
|
|
|
|