ECE344 Fall 2026 (Sec 1) Lec 8 - Event Loops and Process Groups
Overview
Jon Eyolfson shows how Linux programs can wait efficiently for multiple events without busy polling or unsafe signal handlers: block SIGCHLD, expose it through signalfd, and use poll alongside standard input. A working event loop then reaps all terminated children, while the lecture also explains how process groups let shells signal related processes together and how systemd cgroups provide stronger grouping.
Key takeaways
- A blocked SIGCHLD remains pending without running a handler, and because standard signals can coalesce, one notification should trigger a waitpid(-1, ..., WNOHANG) loop that reaps all available children.
- Linux signalfd turns blocked pending signals into readable file descriptors, allowing signal handling to happen in normal program flow rather than an asynchronous handler.
- poll can sleep until any monitored descriptor is ready, making it suitable for a single-threaded event loop that must respond to both standard input and child exits.
- A child inherits its parent's signal mask across fork, and blocked signals remain blocked across execve; restore the desired mask in the child before execvp.
- A negative process-group ID passed to kill targets all processes in that group, while systemd cgroups provide stronger membership control because processes cannot simply leave the group.
Chapters
- Repeated waitpid(..., WNOHANG) calls waste CPU; adding sleeps saves CPU at the cost of response time.
- A SIGCHLD handler can react to child termination, but asynchronous handlers introduce concurrency and can interrupt code at unsafe points.
- Lab 2's subprocess daemon must handle both client requests and child exits without blocking on either one.
- sigprocmask blocks selected signals so their handlers do not run; blocked signals remain pending until unblocked.
- Standard signals are tracked as pending rather than counted, so several child exits can coalesce into one SIGCHLD notification.
- Linux signalfd exposes blocked, pending signals as a readable file descriptor, letting ordinary code handle them when it chooses.
- poll waits for readiness across an array of file descriptors and sleeps until an event or timeout, rather than repeatedly checking them.
- A descriptor is ready for reading when a read will not block; POLLIN signals available input, while POLLOUT indicates writing can proceed.
- The pollfd revents field reports returned events, including POLLHUP or POLLERR even when those events were not requested.
- The example program must accept numbers from standard input to launch sleep children and report child exits promptly.
- Blocking on standard input alone can delay child cleanup; handling both input and exits in a signal handler risks unsafe operations such as printf.
- The design monitors standard input and a SIGCHLD signalfd together, then checks both descriptors because both may become ready.
- The program initializes a signal set with SIGCHLD, blocks it with sigprocmask, and creates a close-on-exec signalfd.
- Its poll array watches file descriptor 0 and the signal FD for POLLIN; poll waits indefinitely and retries if interrupted with EINTR.
- When standard input is ready, the program reads a line and forks a child that uses execvp to run sleep with the supplied duration.
- The child inherits its parent's blocked signal mask, so it unblocks SIGCHLD before execvp to avoid passing an unexpected blocked state to the new program.
- The close-on-exec flag prevents the signal FD from leaking into the executed sleep process.
- After reading signalfd's siginfo structure, the parent loops on waitpid(-1, ..., WNOHANG) to reap every exited child because one SIGCHLD may represent multiple exits.
- waitpid returning 0 means children remain but none has exited; ECHILD means there are no children left to reap.
- The example is an event loop: one thread waits for input and child events, then handles ready events without asynchronous signal-handler code.
- Jon Eyolfson compares this structure to Node.js, where a single JavaScript thread can handle I/O-heavy workloads without application-level concurrency.
- The approach avoids busy polling and uses poll to sleep until the kernel reports an event.
- After execve, caught signal handlers reset to their default behavior because the old program's memory and handler code are replaced.
- Blocked and ignored signal states survive execve, so a child should restore the intended mask before launching another program.
- The previous mask can be saved with sigprocmask and restored later; Linux also exposes blocked-signal information in /proc process status.
- Every process belongs to a process group; setpgid(0, 0) places the calling process in a new group identified by its PID.
- Passing a negative process-group ID to kill sends the signal to every process in that group, rather than to one PID.
- Shells commonly group commands and their subprocesses together, allowing one signal to affect the related processes; processes can still leave a process group.
- Linux cgroups group processes for services and, unlike process groups, prevent a process from leaving its assigned group through ordinary process-group operations.
- Jon Eyolfson explains that killing a systemd user service can bring down the desktop because its processes share a cgroup.
- The lecture's core pattern is to block SIGCHLD, read it through signalfd, wait with poll, and reap all exited children; child processes should unblock signals before exec.
Transcript excerpt
3:26 All righty, welcome back to operating systems. Definitely a Friday. So, you should be able to start, if you haven't looked at lab two yet, everyone's looked and finished lab two, right? Yeah, no. All right, so when it was posted, you could have done all the week one tasks that I separated out for you. You couldn't have done week two. After this lecture, you should be able to do the week two stuff and complete the lab, and understand it more importantly. So, we were talking about event loops and process groups. So, recall we were waiting for a child. If I want to like in lecture six, we were want to react to the child exiting. So,
4:13 there were few options we had. We could just poll. We could constantly call waitpid with that no hang and just ask over and over again. That's bad. That will keep the CPU busy. If we try and fix it by sleeping, then we're sacrificing response time for actually just burning CPU cycles. And then we saw the interrupt version of that, which was waitpid in that sigchild handler, because we know now that our operating system, when our child terminates, it will send us a signal. We can react to it. And we can handle it there if we want. But, writing code in a signal handler or an interrupt handler is notoriously difficult to get right. Like, for example, that signal close example, it
5:01 accidentally closed its file twice, because with signal handlers, well, the operating system might just run another signal handler in the middle of your first signal handler, introducing concurrency, which I'm sorry to tell you you are not good enough programmers to handle yet. So, you might have tried that in another course. Have How many How many of you
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Jon Eyolfson.