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.
See man 7 signal, section Async-signal-safe functions for the list of functions you can use in a signal handler. All other functions may or may not work. In particular, printf() et al. can sometimes silently fail, mangle, or discard data. (You can use low-level I/O from unistd.h, though; just not the stdio.h interface.)
You're right. glibc implements sleep(3) using nanosleep(2); in fact, it's the only sleep syscall (well, along with clock_nanosleep()) the Linux kernel provides. Apparently, the *BSDs also use nanosleep().
While this does mean that on Linux and BSDs sleep() will actually not interfere with signals, including SIGALRM used by alarm(), it is only because the C library uses nanosleep() behind your back on these architectures. Do not expect sleep() and alarm() to work simultaneously on other architectures.
I used to have an account on a Tru64 Unix box, and its documentation states alarm() is deactivated by a sleep() call. I seem to recall a test program to check this, from years ago; perhaps it was on that machine. Also, the relevant man pages explicitly mention that POSIX says using alarm() and sleep() at the same time yields undefined results. The rare cases where I've used a sleep in my real programs (with alarm() or any signals) I've used nanosleep() explicitly. In almost all cases there are much better ways to wait/block than sleep()/usleep()/nanosleep(); sleeping is only really useful when testing.
So, while NevemTeve is certainly right, I stand by my two points.
My latter point does not affect the execution of the code on knightmare1980's Ubuntu box, but using sleep() and alarm() at the same time is undefined by all the relevant standards. Better use nanosleep() explicitly instead: design your code, applying current knowledge, instead of just throwing together something that seems to work. Please.
Please use [code] and [/code] tags.
Please don't put actual code into a signal-handler. In a signal-handler, setting a (volatile!) variable and/or writing a pipe is enough, the rest should be done in the main program.
Also don't use printf in signal-handler (sprintf+write is ok).