14 Ecf Procs
14 Ecf Procs
Instructors:
Randal E. Bryant and David R. O’Hallaron
Today
Exceptional Control Flow
Exceptions
Processes
Process Control
Control Flow
Processors do only one thing:
From startup to shutdown, a CPU simply reads and executes
(interprets) a sequence of instructions, one at a time
This sequence is the CPU’s control flow (or flow of control)
Today
Exceptional Control Flow
Exceptions
Processes
Process Control
Exceptions
An exception is a transfer of control to the OS kernel in response
to some event (i.e., change in processor state)
Kernel is the memory-resident part of the OS
Examples of events: Divide by 0, arithmetic overflow, page fault, I/O
request completes, typing Ctrl-C
Exception Tables
Exception
numbers
Examples:
Timer interrupt
Every few ms, an external timer chip triggers an interrupt
Used by the kernel to take back control from user programs
I/O interrupt from external device
Hitting Ctrl-C at the keyboard
Arrival of a packet from a network
Arrival of data from a disk
Synchronous Exceptions
Caused by events that occur as a result of executing an
instruction:
Traps
Intentional
Examples: system calls, breakpoint traps, special instructions
Returns control to “next” instruction
Faults
Unintentional but possibly recoverable
Examples: page faults (recoverable), protection faults
(unrecoverable), floating point exceptions
Either re-executes faulting (“current”) instruction or aborts
Aborts
Unintentional and unrecoverable
Examples: illegal instruction, parity error, machine check
Aborts current program
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10
Carnegie Mellon
System Calls
Each x86-64 system call has a unique ID number
Examples:
Number Name Description
0 read Read file
1 write Write file
2 open Open file
3 close Close file
4 stat Get info about file
57 fork Create process
59 execve Execute a program
60 _exit Terminate process
62 kill Send signal to process
00000000000e5d70 <__open>:
...
e5d79: b8 02 00 00 00 mov $0x2,%eax # open is syscall #2
e5d7e: 0f 05 syscall # Return value in %rax
e5d80: 48 3d 01 f0 ff ff cmp $0xfffffffffffff001,%rax
...
e5dfa: c3 retq
Today
Exceptional Control Flow
Exceptions
Processes
Process Control
Processes
Definition: A process is an instance of a running
program.
One of the most profound ideas in computer science
Not the same as “program” or “processor”
Multiprocessing Example
CPU
Registers
CPU
Registers
CPU
Registers
CPU
Registers
CPU CPU
Multicore processors
Registers Registers Multiple CPUs on single chip
Share main memory (and some of
the caches)
Each can execute a separate process
Scheduling of processors onto
cores done by kernel
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 23
Carnegie Mellon
Concurrent Processes
Each process is a logical control flow.
Two processes run concurrently (are concurrent) if their
flows overlap in time
Otherwise, they are sequential
Examples (running on single core):
Concurrent: A & B, A & C
Sequential: B & C
Time
Time
Context Switching
Processes are managed by a shared chunk of memory-
resident OS code called the kernel
Important: the kernel is not a separate process, but rather runs as part
of some existing process.
Control flow passes from one process to another via a
context switch
Process A Process B
user code
user code
Today
Exceptional Control Flow
Exceptions
Processes
Process Control
Error-reporting functions
Can simplify somewhat using an error-reporting function:
void unix_error(char *msg) /* Unix-style error */
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(0);
}
Error-handling Wrappers
We simplify the code we present to you even further by
using Stevens-style error-handling wrappers:
pid_t Fork(void)
{
pid_t pid;
pid = Fork();
pid_t getppid(void)
Returns PID of parent process
Running
Process is either executing, or waiting to be executed and will
eventually be scheduled (i.e., chosen to execute) by the kernel
Stopped
Process execution is suspended and will not be scheduled until
further notice (next lecture when we study signals)
Terminated
Process is stopped permanently
Terminating Processes
Process becomes terminated for one of three reasons:
Receiving a signal whose default action is to terminate (next
lecture)
Returning from the main routine
Calling the exit function
Creating Processes
Parent process creates a new running child process by
calling fork
int fork(void)
Returns 0 to the child process, child’s PID to parent process
Child is almost identical to parent:
Child get an identical (but separate) copy of the parent’s virtual
address space.
Child gets identical copies of the parent’s open file descriptors
Child has a different PID than the parent
fork Example
int main()
Call once, return twice
{
pid_t pid;
Concurrent execution
int x = 1; Can’t predict execution
order of parent and child
pid = Fork();
if (pid == 0) { /* Child */ Duplicate but separate
printf("child : x=%d\n", ++x);
exit(0);
address space
} x has a value of 1 when
fork returns in parent and
/* Parent */
child
printf("parent: x=%d\n", --x);
exit(0); Subsequent changes to x
} fork.c are independent
Shared open files
linux> ./fork
parent: x=0 stdout is the same in
child : x=2 both parent and child
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35
Carnegie Mellon
/* Parent */
printf("parent: x=%d\n", --x);
exit(0);
} fork.c
a b e c f d
a b c d
a b f c e d
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 38
Carnegie Mellon
Zombie
void fork7() {
if (fork() == 0) {
/* Child */
Non-
void fork8()
{
if (fork() == 0) {
terminating /* Child */
printf("Running Child, PID = %d\n",
void fork10() {
pid_t pid[N];
int i, child_status;
execve Example
Executes “/bin/ls –lt /usr/include” in child process
using current environment:
myargv[argc] = NULL
(argc == 3) myargv[2] “/usr/include”
myargv[1] “-lt”
myargv myargv[0] “/bin/ls”
envp[n] = NULL
envp[n-1] “PWD=/usr/droh”
…
envp[0] “USER=droh”
environ
Summary
Exceptions
Events that require nonstandard control flow
Generated externally (interrupts) or internally (traps and faults)
Processes
At any given time, system has multiple active processes
Only one can execute at a time on a single core, though
Each process appears to have total control of
processor + private memory space
Summary (cont.)
Spawning processes
Call fork
One call, two returns
Process completion
Call exit
One call, no return
Reaping and waiting for processes
Call wait or waitpid
Loading and running programs
Call execve (or variant)
One call, (normally) no return