ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ...*/
}
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.
The bad news:
POSIX threads and signals get along, but only if you're extremely careful.
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.
Example: As Butenhof recommends:
Quote:
Always use sigwait to work with asynchronous signals within threaded programs.
Don't know why? Read the book I'm about to mention.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.