C Academy · Lesson

Signals

Asynchronous notifications.

Lesson 4 of 413 steps

Signals is a free C Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.

What Is a Signal

A signal is an asynchronous notification sent to a process to tell it that an event occurred, such as a user pressing Ctrl-C or a child process exiting. Signals are a lightweight form of IPC.

Common Signals

A few you will meet often:

  • SIGINT interrupt (Ctrl-C)
  • SIGTERM polite termination request
  • SIGKILL forced kill (cannot be caught)
  • SIGCHLD a child changed state
  • SIGSEGV invalid memory access

Default Dispositions

Each signal has a default action: terminate, terminate with core dump, ignore, stop, or continue. You can override most of these with a handler.

Installing a Handler

The modern, portable way to set a handler is sigaction. The handler is a function taking the signal number.

#include <signal.h>
#include <stdio.h>

void on_signal(int sig) {
    /* note: printf is not async-signal-safe; for demo only */
    write(1, "caught\n", 7);
}

int install(void) {
    struct sigaction sa = {0};
    sa.sa_handler = on_signal;
    return sigaction(SIGINT, &sa, NULL);
}

Why Not signal()

The old signal() function has inconsistent behavior across systems (handler reset, no SA_RESTART control). Prefer sigaction, which is well-defined and portable.

Async-Signal-Safe Functions

A handler can interrupt your code at any point, so it may only call async-signal-safe functions like write. Calling printf or malloc from a handler risks deadlock or corruption.

Using a volatile flag

A safe pattern: the handler sets a volatile sig_atomic_t flag, and the main loop checks it. This keeps the handler tiny and safe.

#include <signal.h>

volatile sig_atomic_t stop = 0;

void handler(int sig) {
    stop = 1; /* only set a flag */
}
/* main loop: while (!stop) { do_work(); } */

Sending Signals

Use kill(pid, sig) to send a signal to another process, or raise(sig) to send one to yourself. Despite the name, kill sends any signal, not just lethal ones.

#include <signal.h>

int notify(int pid) {
    return kill(pid, SIGTERM); /* ask process to terminate */
}

Blocking Signals

To protect a critical section, temporarily block signals with sigprocmask and a signal set. Blocked signals stay pending and are delivered once unblocked.

#include <signal.h>

void block_int(void) {
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGINT);
    sigprocmask(SIG_BLOCK, &set, NULL);
}

Reaping Children With SIGCHLD

When a child exits, the kernel sends SIGCHLD. A handler calling waitpid reaps the child, preventing zombie processes that linger in the process table.

#include <signal.h>
#include <sys/wait.h>

void reap(int sig) {
    while (waitpid(-1, NULL, WNOHANG) > 0)
        ; /* reap all finished children */
}

Signals That Cannot Be Caught

Two signals are absolute: SIGKILL always terminates and SIGSTOP always stops the process. They cannot be caught, blocked, or ignored, giving the system a guaranteed way to control processes.

Quick Check

Test your understanding of signals.

Recap

You learned signals for asynchronous notification.

  • Install handlers with sigaction, not the old signal
  • Handlers must be async-signal-safe; prefer setting a flag
  • Send with kill/raise; block with sigprocmask
  • Reap children on SIGCHLD; SIGKILL and SIGSTOP cannot be caught
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Signals” lesson free?

Yes — the full text of “Signals” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.

What will I learn in “Signals”?

Asynchronous notifications. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Signals” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Pipes
  2. Shared Memory
  3. Message Queues
  4. Signals
← Back to C Academy