The good ol' alarm clock

The alarm(2) system call allows a process to set up a simple timeout mechanism; its signature is as follows:

#include <unistd.h>unsigned int alarm(unsigned int seconds);

It is, indeed, quite self-explanatory. Let's take a simple example: A process wants to set up a timer that will expire in three seconds from now, so alarm(3) is essentially the code to use to do this.

What exactly happens in the aforementioned code? Three seconds after the alarm system call is issued—that is, after the timer has been armed—the kernel will send the signal SIGALRM to the process.

The default action of SIGALRM (signal # 14 on x86) is to terminate the process.

Thus, we expect the developer to catch the signal (via the sigaction(2) system ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.