Events and Signals

20 April 2020

Obed N Munoz

Cloud Software Engineer


An event can be defined as a situation or occurence that is being recognized by a program. An event will occur by one or multiple actions that happened in the execution of the program.


File Events

Some applications will require the ability to monitor files or directories in order to determine an event have occured in the specific monitored objects.

A graphical file manager needs to be able to determine when files are added or removed from the directory that is currently being displayed, or a daemon may want to monitor its configuration file in order to know if the file has been changed.


The inotify API: key steps

1. Application calls inotify_init() to create an inotify instance.

2. The application informs the kernel about which files are of interest by using the inotify_add_watch()

3. In order to obtain event notifications, the application performs read() operations on the inotify file descriptor.

4. When the application has finished monitoring, it closes the inotify file descriptor.


The inotify API: events


The inotify API: reading events (1/2)

After events have occurred, each read() returns a buffer.

struct inotify_event {
    int      wd;       /* Watch descriptor */
    uint32_t mask;     /* Mask describing event */
    uint32_t cookie;   /* Unique cookie associating related events (for rename()) */
    uint32_t len;      /* Size of name field */
    char     name[];   /* Optional null-terminated name */

Take a look on inotify/demo_inotify.c from The Linux Programming Interface.

In a first terminal, run:

mkdir dir1 dir2
./demo_inotify dir1 dir2

The inotify API: reading events (2/2)

In a second terminal, run:

cat > dir1/aaa
Hello World

# Type Control-D

mv dir1/aaa dir2/bbb

mkdir dir2/ddd

rmdir dir1

Let's Code: File/Directory Monitor



A signal is a notification to a process that an event has occurred. Signals are sometimes described as software interrupts.

The usual source of many signals sent to a process is the kernel. Below the types of events that cause the kernel to generate a signal:


Process' actions on signal

Depending on the signal, a process may act different:

Alternatively, a program may change the default behavior (also known as disposition) by:


Signal types and default actions


Signal Handlers (changing Signal Dispositions)

A signal handler (also known as signal catcher) is a function, written by the programmer, that performs appropriate tasks in response to the delivery of a signal.


Signal Handler example:

#include <signal.h>
#include "tlpi_hdr.h"
static void

sigHandler(int sig)
   printf("Ouch!\n");   /* UNSAFE (see Section 21.1.2) */

int main(int argc, char *argv[]) {
  int j;

 if (signal(SIGINT, sigHandler) == SIG_ERR) errExit("signal");
   for (j = 0; ; j++) {
       printf("%d\n", j);

Take a look on signals/ouch.c and signals/intquit.c from The Linux Programming Interface.


Sending Signals

One process can send a signal to another process using the kill() system call, which is the analog of the kill shell command.

Sometimes, it is useful for a process to send a signal to itself. In a single-threaded program, a call to raise() is equivalent to the following call to kill():
kill(getpid(), sig);

The killpg() function sends a signal to all of the members of a process group.

Take a look on signals/t_kill.c from The Linux Programming Interface.


Let's Code: Progress Notifier with Signals


Resources and Credits

This material is genereated thanks to some extracts from following resources:


Thank you

Obed N Munoz

Cloud Software Engineer

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)