ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
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.