liveBook · Manning

Chapter 12: Signals, interrupts, and exceptions

What you will learn in chapter 12 of Rust in Action: Systems programming concepts and techniques

The chapter introduces how outside events interrupt program execution and how signals let running programs communicate.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces how outside events interrupt program execution and how signals let running programs communicate.

Key terms: interrupts, OS, program execution, signals, UNIX

12.1 Glossary

Defines the chapter’s terminology for CPU-level interruptions and OS-level signals, including the kinds of exception.

Key terms: abort, fault, trap, exception, hardware interrupt, software interrupt, interrupt, signal

12.1.1 Signals vs. interrupts

This section distinguishes OS-level signals from CPU-related interrupts, then explains why the chapter practices interrupt-handling ideas with signals.

Key terms: signal, interrupt, interprocess communication, synchronous interrupt, exception, asynchronous interrupt, trap, fault, control flow, handler code

12.2 How interrupts affect applications

A simple addition program shows that an interrupt diverts the CPU without changing the program’s calculation, though it can change how long the program takes.

Key terms: interrupt, interrupt handler, control flow, hardware interrupts, wall clock time, nondeterministic

12.3 Software interrupts

This section explains that programs generate software interrupts with CPU instructions and briefly identifies Rust’s asm! macro and its compiler requirement.

Key terms: software interrupts, CPU, asm! macro, nightly Rust compiler, rustup

12.4 Hardware interrupts

This section identifies the Programmable Interrupt Controller as the link between hardware devices and the CPU in a hardware interrupt.

Key terms: hardware interrupts, Programmable Interrupt Controller (PIC)

12.5 Signal handling

12.5.1 Default behavior

This section explains why an application can often rely on default signal handling and distinguishes three common termination signals.

Key terms: signal, default behavior, handler function, SIGINT, SIGTERM, SIGKILL

12.5.2 Suspend and resume a program’s operation

This section explains how SIGSTOP and SIGCONT suspend and resume a UNIX process, then demonstrates sending those signals to a running program.

Key terms: SIGSTOP, SIGCONT, suspended, job control, sixty, PID, kill

12.5.3 Listing all signals supported by the OS

The section points readers to the kill command for signal information and explains why most applications need only consider a small subset, with SIGKILL and SIGSTOP as exceptions.

Key terms: kill command, signals, default handlers, SIGKILL, SIGSTOP, handled, blocked

12.6 Handling signals with custom actions

This section explains why applications use signal handlers for clean shutdown and how a minimal handler can request that shutdown through a global flag.

Key terms: default actions, signal handlers, shut down cleanly, signal handler, f(i32) -> (), signals, Boolean flag, global variable, race conditions

12.6.1 Global variables in Rust

This subsection introduces Rust global variables through a mutable shutdown flag and explains why accessing global state calls for caution.

Key terms: global variables, static, static mut, SHUT_DOWN, unsafe blocks, global state

12.6.2 Using a global variable to indicate that shutdown has been initiated

This section shows how a minimal signal handler can indicate that shutdown has begun, explains how handlers are registered, and distinguishes static values from const values.

Key terms: signal handler, global variable, shut down, libc::signal(), signal name, function pointer, usize, static, const, data locality

12.7 Sending application-defined signals

This section explains how application-defined meanings for SIGUSR1 and SIGUSR2 let signals serve as limited messages.

Key terms: signals, SIGUSR1, SIGUSR2, custom signals

12.7.1 Understanding function pointers and their syntax

Explains how Rust function pointers represent function addresses, how they can be cast to and from usize, and how to read their type syntax.

Key terms: function pointer, usize, address, *const fn() -> (), const pointer, unit

12.8 Ignoring signals

This section shows how to ignore selected signals with libc::SIG_IGN and later restore their default behavior with SIG_DFL.

Key terms: libc::signal(), libc::SIG_IGN, SIG_DFL, SIGSTOP, SIGKILL

12.9 Shutting down from deeply nested call stacks

This section explains why a program might jump out of a deep call stack, how setjmp and longjmp change control flow, and why Rust needs compiler-provided support for the technique.

Key terms: nonlocal control transfer, setjmp, longjmp, call stack, error-handling code, intrinsics, compiler’s toolchain

12.9.1 Introducing the sjlj project

Introduces sjlj as a project that alters normal function control flow to bypass layers of the call stack.

Key terms: sjlj project, control flow, call stack

12.9.2 Setting up intrinsics in a program

This section explains how the sjlj program makes LLVM intrinsics available to Rust and what intrinsics and LLVM do.

Key terms: setjmp(), longjmp(), unsafe block, extern "C", link_name, intrinsic functions, intrinsics, target environment, atomic operations, exception handling

12.9.3 Casting a pointer to another type

This section explains why a jump-buffer reference must be cast to a mutable byte pointer for setjmp() and longjmp(), and walks through the conversion used in the example.

Key terms: jmp_buf, setjmp(), longjmp(), *mut i8, RETURN_HERE, *const i8, unsafe block

12.9.4 Compiling the sjlj project

This subsection explains how to compile the sjlj project with Rust’s nightly toolchain.

Key terms: sjlj project, rustc, nightly channel, rustup install nightly, +nightly, cargo

12.9.5 sjlj project source code

This subsection identifies how the sjlj source code uses LLVM to access longjmp for escaping a stack frame.

Key terms: LLVM, longjmp, stack frame, address space

12.10 A note on applying these techniques to platforms without signals

The section explains how the chapter’s interrupt-handling approach can apply on platforms that do not use UNIX signals.

Key terms: signals, SetConsoleCtrlHandler, handler function, interrupts, callback system, function pointer

12.11 Revising exceptions

This closing section revisits how exceptions relate to interrupts and signals, and summarizes the role of Linux signals in OS-to-application communication.

Key terms: exceptions, interrupts, signals, Linux, OS, applications

12.12 Summary

Concepts explained in this chapter

← Chapter 11: Kernel

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.