Chapter08-Exceptional Control Flow(1)Processes
Chapter08-Exceptional Control Flow(1)Processes
SYSTEMS PROGRAMMING
…
exception handler 2
...
n-1
Exception Table Code for
exception handler n-1
oExamples:
• 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
• Faults
• Aborts
Computer Systems: A Programmer’s Perspective 3rd Edition 12
EXAMPLE OF TRAPS: SYSTEM CALLS
• Aborts
Computer Systems: A Programmer’s Perspective 3rd Edition 15
EXAMPLE OF FAULTS: PAGE FAULT
Memory
Stack Stack Stack Stack
Heap Heap Heap Heap
Data Data … Data Data
Code Code Code Code
Saved Saved Saved Saved
registers registers registers registers
CPU
Registers
Memory
Stack Stack Stack Stack
Heap Heap Heap Heap
Data Data … Data Data
Code Code Code Code
Saved Saved Saved Saved
registers registers registers registers
CPU
Registers
Memory
Stack Stack Stack Stack
Heap Heap Heap Heap
Data Data … Data Data
Code Code Code Code
Saved Saved Saved Saved
registers registers registers registers
CPU
Registers
Memory
Stack Stack Stack Stack
Heap Heap Heap Heap
Data Data … Data Data
Code Code Code Code
Saved Saved Saved Saved
registers registers registers registers
CPU
Registers
Memory
Stack Stack Stack Stack
Heap Heap Heap Heap
Data Data … Data Data
Code Code Code Code
Saved Saved Saved Saved
registers registers registers registers
CPU CPU
Registers Registers
o Multicore processors
• 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
Computer Systems: A Programmer’s Perspective 3rd Edition 28
CONCURRENT PROCESSES
• Sequential: B & C
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
a b c d
Parent
• Feasible ordering:
a b e c f d
Ordering depends on the
system and the current situation.
(feasible) a b e f c d
(infeasible) a b c f e d
void fork4()
{
printf("L0\n"); Bye Bye
if (fork() != 0) printf printf
{ L0 L1 L2 Bye
printf("L1\n"); printf fork printf printf
printf fork
if (fork() != 0)
{
printf("L2\n");
}
}
printf("Bye\n"); Linux> ./fork4 Linux> ./fork4
} L0 L0
L1 Bye
Bye L1
Bye Bye
L2 Bye
Bye feasible! L2 infeasible!
o Reaping
• Performed by parent on terminated child (using wait or waitpid)
• Parent is given exit status information
• Kernel then deletes zombie child process
o What if parent doesn’t reap?
• If any parent terminates without reaping a child, then the orphaned child
will be reaped by init process (pid == 1)
• So, only need explicit reaping in long-running processes
e.g., shells and servers
Computer Systems: A Programmer’s Perspective 3rd Edition 47
EXAMPLE ZOMBIE PROCESS
void fork7() {
if (fork() == 0) {
/* Child */
printf("Terminating Child, PID = %d\n", getpid());
exit(0);
} else {
printf("Running Parent, PID = %d\n", getpid());
while (1)
; /* Infinite loop */
}
}
starts,
envp[n] == NULL
envp[n-1] environ
... (global var)
envp[0]
argv[argc] = NULL envp
argv[argc-1] (in %rdx)
...
argv argv[0]
(in %rsi)
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
Computer Systems: A Programmer’s Perspective 3rd Edition 57
SUMMARY
oExceptions
• Events that require nonstandard control flow
• Generated externally (interrupts) or internally (traps and faults)
oProcesses
• 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
oSpawning processes
• Call fork
• One call, two returns
oProcess completion
• Call exit
• One call, no return
oReaping and waiting for processes
• Call wait or waitpid
oLoading and running programs
• Call execve (or variant)
• One call, (normally) no return
Computer Systems: A Programmer’s Perspective 3rd Edition 59