Lecture07 Processes 1 Proc
Lecture07 Processes 1 Proc
Lecture07 Processes 1 Proc
and Signals
Computer Systems Organization (Spring 2016)
CSCI-UA 201, Section 2
Control Flow
Time
<startup>
inst1
inst2
inst3
instn
<shutdown>
2
Exceptions
Exceptions
User code
Event
I_current
I_next
Kernel code
Exception
Exception processing
by exception handler
Exception Tables
Exception
numbers
Code for
exception handler 0
Exception
Table
0
1
2
n-1
...
Code for
exception handler 1
Code for
exception handler 2
...
Code for
exception handler n-1
Examples:
Timer interrupt
Every few ms, an external timer chip triggers an interrupt
Used by the kernel to take back control from user programs
Synchronous Exceptions
System Calls
Name
Description
read
Read file
write
Write file
open
Open file
close
Close file
stat
57
fork
Create process
59
execve
Execute a program
60
_exit
Terminate process
62
kill
User code
Kernel code
syscall
cmp
Exception
Open file
Returns
User code
movl
c7 05 10 9d 04 08 0d
int a[1000];
main ()
{
a[500] = 13;
}
movl
$0xd,0x8049d10
Kernel code
Exception: page fault
Return and
reexecute movl
12
intSIGSEGV
a[1000]; signal to user process
Sends
main ()
User
{ process exits with segmentation fault
a[5000] = 13;
}
80483b7:
c7 05 60 e3 04 08 0d
User code
movl
movl
$0xd,0x804e360
Kernel code
13
Processes
14
Processes
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
Memory
Stack
Heap
Data
Code
CPU
Registers
15
Memory
Stack
Heap
Data
Code
Memory
Stack
Heap
Data
Code
CPU
CPU
CPU
Registers
Registers
Registers
16
Multiprocessing Example
17
Stack
Heap
Data
Code
Saved
registers
Saved
registers
Stack
Heap
Data
Code
Saved
registers
CPU
Registers
Stack
Heap
Data
Code
Saved
registers
Saved
registers
Stack
Heap
Data
Code
Saved
registers
CPU
Registers
19
Stack
Heap
Data
Code
Saved
registers
Saved
registers
Stack
Heap
Data
Code
Saved
registers
CPU
Registers
20
Stack
Heap
Data
Code
Saved
registers
Saved
registers
Stack
Heap
Data
Code
Saved
registers
CPU
Registers
21
Stack
Heap
Data
Code
Saved
registers
Saved
registers
CPU
CPU
Registers
Registers
Stack
Heap
Data
Code
Saved
registers
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
(But we still will have more processes running
than there are cores on a machine.)
22
Concurrent Processes
Process A
Process B
Process C
Time
23
Process A
Process B
Process C
Time
24
Context Switching
Control flow passes from one process to another via a context switch
Process A
Process B
user code
kernel code
Time
context switch
user code
kernel code
context switch
user code
25
Process Control
(ways of manipulating processes)
26
Example:
27
Error-reporting functions
28
Error-handling Wrappers
pid_t Fork(void)
{
pid_t pid;
if ((pid = fork()) < 0)
unix_error("Fork error");
return pid;
}
pid = Fork();
29
pid_t getpid(void)
Returns PID of current process
pid_t getppid(void)
Returns PID of parent process
30
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
31
Terminating Processes
32
Creating Processes
Parent process creates a new running child process by
calling fork
int fork(void)
Returns 0 to the child process, childs PID to parent process
Child is almost identical to parent:
Child get an identical (but separate) copy of the parents virtual address space.
Child gets identical copies of the parents open file descriptors
Child has a different PID than the parent
33
fork Example
int main()
{
pid_t pid;
int x = 1;
pid = Fork();
if (pid == 0) { /* Child */
printf("child : x=%d\n", ++x);
exit(0);
}
Concurrent execution
Cant predict execution
order of parent and child
/* Parent */
printf("parent: x=%d\n", --x);
exit(0);
}
linux> ./fork
parent: x=0
child : x=2
34
child: x=2
printf
exit
x==1
main
fork
parent: x=0
printf
exit
Child
Parent
35
child: x=2
printf
exit
x==1
main
parent: x=0
fork
printf
exit
Child
Parent
/* Parent */
printf("parent: x=%d\n", --x);
exit(0);
}
36
Original graph:
child: x=2
printf
parent: x=0
x==1
main
exit
fork
printf
exit
Relabled graph:
37
void fork2()
{
printf("L0\n");
fork();
printf("L1\n");
fork();
printf("Bye\n");
}
printf
L1
printf
L0
printf
Bye
fork
L1
fork
Feasible output:
L0
L1
Bye
Bye
L1
Bye
Bye
printf
printf
Bye
printf
Bye
fork
printf
Infeasible output:
L0
Bye
L1
Bye
L1
Bye
Bye
38
Bye
printf
L0
printf
Bye
printf
L1
fork
L2
printf fork
Feasible output:
L0
L1
Bye
Bye
L2
Bye
Bye
printf printf
Infeasible output:
L0
Bye
L1
Bye
Bye
L2
39
L2
Bye
printf printf
L1
printf
L0
printf
fork
Bye
printf
Bye
fork
printf
Feasible output:
L0
Bye
L1
L2
Bye
Bye
Infeasible output:
L0
Bye
L1
Bye
Bye
L2
40
Idea
When process terminates, it still consumes system resources
Examples: Exit status, various OS tables
Called a zombie
Living corpse, half alive and half dead
Reaping
Performed by parent on terminated child (using wait or waitpid)
Parent is given exit status information
Kernel then deletes zombie child process
41
Zombie
Example
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 */
}
}
6639 ttyp9
00:00:03 forks
6640 ttyp9
00:00:00 forks <defunct>
6641 ttyp9
00:00:00 ps
linux> kill 6639
[1]
Terminated
linux> ps
PID TTY
TIME CMD
6585 ttyp9
00:00:00 tcsh
6642 ttyp9
00:00:00 ps
42
Non-terminating
Child Example
void fork8()
{
if (fork() == 0) {
/* Child */
printf("Running Child, PID = %d\n",
getpid());
while (1)
; /* Infinite loop */
} else {
printf("Terminating Parent, PID = %d\n",
getpid());
exit(0);
}
}
linux> ./forks 8
Terminating Parent, PID = 6675
Running Child, PID = 6676
linux> ps
PID TTY
TIME CMD
6585 ttyp9
00:00:00 tcsh
6676 ttyp9
00:00:06 forks
6677 ttyp9
00:00:00 ps
linux> kill 6676
linux> ps
PID TTY
TIME CMD
6585 ttyp9
00:00:00 tcsh
6678 ttyp9
00:00:00 ps
44
HC
printf
HP
fork printf
exit
CT
Bye
wait printf
Feasible output:
HC
HP
CT
Bye
Infeasible output:
HP
CT
Bye
HC
45
46
Structure of
the stack when
a new program
starts
argv
(in %rsi)
argc
(in %rdi)
Null-terminated
environment variable strings
Bottom of stack
Null-terminated
command-line arg strings
envp[n] == NULL
envp[n-1]
...
environ
(global var)
envp[0]
argv[argc] = NULL
argv[argc-1]
envp
(in %rdx)
...
argv[0]
Stack frame for
libc_start_main
Top of stack
execve Example
(argc == 3)
myargv
environ
myargv[2]
myargv[1]
myargv[0]
envp[n] = NULL
envp[n-1]
envp[0]
"/usr/include"
"-lt"
"/bin/ls"
"PWD=/usr/droh"
"USER=droh"
if ((pid = Fork()) == 0) {
/* Child runs program */
if (execve(myargv[0], myargv, environ) < 0) {
printf("%s: Command not found.\n", myargv[0]);
exit(1);
}
}
49
Summary
Exceptions
Call fork
One call, two returns
Process completion
Call exit
One call, no return
Processes
At any given time, system has multiple
active processes
Spawning processes
50