Operating Systems 2026F Lecture 6
Video
Video from the lecture given on October 2, 2026 is now available:
Notes
Lecture 6
Lecture 6
---------
Signals
What is a signal?
- message from the kernel to a process
- can originate from the kernel or from another process
Idea: interrupt the process to handle an event
- so a simple form of event handling, or more exception handling
When a process receives a signal
- it stops executing the current code (function)
- it instead starts running the appropriate signal handler
- when handling is complete, it then returns to where it was, continuing the function
This means signals can interrupt almost ANY part of your program
- need to be prepared!
What sort of signals are there?
- user defined (SIGUSR1, SIGUSR2)
- memory access errors (SIGSEGV, SIGBUS)
- some floating point errors
But mainly:
- when child processes terminate (SIGCHLD)
- when the user wants to terminate the process (SIGTERM)
or suspend (SIGSTOP)
And many other conditions
Signal handlers are defined by default by the C library
you can change signals with sigaction()
(don't use signal)
So what bad things can happen to your program because of a signal handler?
- could interrupt critical code
- can even happen while doing a system call
- handler could mess up state of your code
When a process receives a signal while in a system call, we have two choices:
- continue the system call after the signal is handled
- that doesn't work because of how the kernel operates
- but we can do the system call from the beginning (restart it)
- cancel the system call after the signal is handled
Standard I/O file descriptors
- when you type "ls", its output goes to the terminal
- but all ls was doing was writing to standard out
- how did standard out end up in the terminal?
C standard in = file descriptor 0 in Linux/UNIX
C standard out = file descriptor 1 in Linux/UNIX
C standard error = file descriptor 2 in Linux/UNIX
What is a file descriptor?
- small number
- refers to an open file
A file descriptor is what UNIX uses instead of the FILE struct for C.
But what is that small number, really?
- index into an array of open files the kernel maintains
for the currently running process
When a program starts, 0, 1, and 2 file descriptors are expected to be open and ready to go, with 0 ready for reads and 1 and 2 ready for writes.
Who gets these file descriptors set up? Whomever did the execve!
- they could inherit them from who execve'd them, but they are responsible
When you're using a shell, the shell is responsible for setting up the standard I/O file descriptors.
This is good, because it lets us do I/O redirection
So what does ">" do in most shells?
- redirects standard output to the given file
Remember that "files" in UNIX can be stored on disk, but they can be many other things as well
- tty's/pseudo tty's
- another process (IPC, inter-process communication)