Week 6 - June 10-14
Lecture recording
here.
Introduction
This week we will look at how to fork a process
and how to exec a process. We will also look at signals (a software interrupt); how to generate them,
how to handle them, and where they are generally used.
Videos
Quiz
Midterm
- Mid-Term Review. If you can answer the questions in MidTermReview, you should get 100%.
Lecture Material
Labs
Assignment(s)
None.
Sample Code
Processes
- Before we look at process creation and termination, we have to understand the memory space of a process. A document describing the memory layout of a process can be found
here. The linux command pmap can be used to report the memory map of a process.
- We study process creation and termination using
fork(),
vfork(),
exit(),
atexit(),
on_exit(),
wait(), waitpid(), and
execve().
A document summarizing these functionalities can be found here.
- Code that demonstrates a simple fork() can be found in fork1.cpp.
- Code that demonstrates the wait() function can be found in fork2.cpp
- The makefile for fork1.cpp and fork2.cpp can be found in Makefile.
- Code demonstrating exec() can be found in exec1.cpp. Here is its Makefile.
- Code demonstrating the on_exit() functionality can be found in openTest.cpp and fopenTest.cpp, with its Makefile.
- System monitoring using fork can be found in sysmonFork.cpp with it's Makefile.
sysmonFork.cpp spawns two child processes using fork(), where each child process monitors a network interface.
- System monitoring using fork and exec can be found in sysmonExec.cpp and
intfMonitor.cpp. Here is the Makefile.
sysmonExec.cpp spawns two child processes using fork() and exec(), where intfMonitor.cpp contains the code for the child process. Each child process monitors a network
interface.
Signals
- Lecture: Introduction to Signals. Signals are a form of software interrupt, in that they interrupt what a program is doing.
These signals can be handled by the program or they can perform their default behaviour.
If a program is written to handle signals, then when a signal is received the program will interrupt what it is doing,
go to the handler, perform tasks in the handler, then return to what it was doing. See Signals.png.
For a general discussion on software interrupts, see software interrupt.
You might want to look at hardware interrupts as well, but this is out of scope for this course.
For a general discussion on hardware interrupts, see Interrupts and Interrupt Handlers.
- For a complete list of signals, see List of Signals.
- Signals can be sent via the kill function either from the command line or from within a program.
To send kill from within a program, see kill.
- To send kill from the command line, see Sending signals to Processes.
- Signal handlers can be registered from within a C/CPP program with signal or
sigaction. sigaction() is the preferred way. One advantage of sigaction is that
it will block handling other signals until the handling of the current signal has been completed.
- A program that sends signals to a particular process is sendsig.cpp.
A program that does not handle signals is recvsig.cpp and a program that handles signals is recvsig2.cpp.
Their Makefile can be found here.
- A program that forks 4 child processes, where the parent shuts each down one by one can be found in sigact.cpp.
Its Makefile can be found here.
- A program that cannot be interrupted by ctrl-C or ctrl-Z is shown in uninterrupt.cpp.