Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-08-2009, 06:14 PM   #1
LQ Newbie
Registered: Jul 2009
Posts: 8

Rep: Reputation: 1
SIGIO sigaction wakes sleep up early

I am trying to do asynchronous i/o using the SIGEV_SIGNAL notify method. This is occurring in one thread and I have the main thread set to sleep for some number of seconds. When the first call to my signal handler happens, the sleep ends prematurely.

I've added a small sample that should get the idea what I'm trying to do. The printf of the remaining time from the sleep call always shows sleep exited before the time should have been up. I'm hoping I'm doing something wrong. Any help is much appreciated. Thanks!

/* sample code, most unimportant parts removed */

aio_completion_sig(int signo, signinfo_t* info, void* context)
/*... sleep wakes up after this ...*/

void* doThread(void* arg)

struct sigaction sig_act;
struct aiocb aio_req;

/* setup signal handler */
sig_act.sa_flags = SA_SIGINFO;
sig_act.sa_sigaction = aio_completion_sig;

various aoicb initialization here

/* link request for signal handler */
aio_req.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
aio_req.aio_sigevent.sigev_signo = SIGIO;

/* map handler for SIGIO */
sigaction(SIGIO, &sig_act, NULL);

if(aio_read(&aio_req) < 0)

pthread_exit((void*) 0);

int main()

pthread_t tid;
pthread_create(&tid , NULL, doThread, NULL);


printf("sleep remaining:%d\n", sleep(10));


Old 07-08-2009, 09:20 PM   #2
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
I have some good news and some bad news.
  1. The good news:

    As the man page for sleep() indicates, that function returns after the specified time interval or after a caught signal, whichever occurs first. The signal is caught by another thread? Too bad; you still have to plan on it. Merely wrap a loop around that sleep() function, and call sleep() with whatever time remains to sleep, until there is no remaining time to sleep. Problem solved.

    Use nanosleep() if this modification means you're suddently interested in sleep intervals with greater precision.

  2. The bad news:

    POSIX threads and signals get along, but only if you're extremely careful.

    1. Example: A signal catcher shouldn't mess with semaphores or condition variables; another thread might be in the middle of modifying a semaphore or condition variable when a signal arrives.

    2. Example: As Butenhof recommends:
      Always use sigwait to work with asynchronous signals within threaded programs.
      Don't know why? Read the book I'm about to mention.

    3. Example: If you're using aio_read() or aio_write() in your POSIX threads program, you'll be interested in the sigev_notify_function and possibly the sigev_notify_attributes fields in the struct sigevent structure.

    Ok, now it's time to make the pitch. There are far more details involved here than I have time to type from a book (I have a life :), but you probably want to run (not walk) to your nearest bookseller and buy David R. Butenhof's book Programming with POSIX Threads, published by Addison Wesley. Don't get the O'Reilly book on the same subject; in my view, it doesn't even begin to compare.

    Focus on Section 6.6, which covers signals, and dwells a bit on the whole aio_read()/aio_write() situation.
Hope this helps.
Old 07-09-2009, 09:56 AM   #3
LQ Newbie
Registered: Jul 2009
Posts: 8

Original Poster
Rep: Reputation: 1
Cool, thanks for the information; that does help. I thought sleep would only wait for SIGALRM, but it sounds like it will awake on any signal handled by the program. I'll look into getting the recommened book.


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
LXer: Midnight Commander wakes from deep sleep LXer Syndicated Linux News 0 02-02-2009 01:50 PM
Who wakes up the hard drive? shturm Fedora 3 01-09-2007 11:34 PM
Searching for Server (BOOTP/DHCP) <sleep> <sleep> .. .. Eileen Linux - Networking 12 10-21-2005 02:14 AM
Standby/Sleep immediately wakes up again bsander Linux - Laptop and Netbook 2 05-07-2005 04:29 PM
SuSE 9.1 Pro goes into early sleep mode bad_andy Linux - Distributions 4 10-09-2004 04:25 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:56 PM.

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