Professional Documents
Culture Documents
OS Structure,
Processes & Process Management
ProgramtoProcess
ProcessinMemory
Program to process.
void X (int b) {
if(b == 1) {
int main() {
int a = 2;
X(a);
}
void X (int b) {
if(b == 1) {
int main() {
int a = 2;
X(a);
}
What is in memory.
main; a = 2
X; b = 2
Stack
Heap
void X (int b) {
if(b == 1) {
int main() {
int a = 2;
X(a);
Code
}
4
ProcessesandProcessManagement
Keepingtrackofaprocess
Detailsforrunningaprogram
E.g.,
g each p
process has an identifier (p
(process
identifier, or PID)
ProcessContexts
ProcessLifeCycle
Example:Multiprogramming
Start
User Program n
main{
a {
...
Program 1
k: read()
save
state
User Program 2
Ready
User Program 1
Running
OS
I/O
Device
Program 2
read{
startIO()
schedule()
}
main{
System
System Software
Software
endio{
schedule()
Operating System
Blocked
k+1:
Memory
restore
state
interrupt
save
state
}
7
SchedulingProcesses
WhenaprocessiswaitingforI/Owhatisits
schedulingstate?
1.
2.
3.
4.
5.
Ready
Running
Blocked
Zombie
Exited
10
WhyUseProcesses?
WhyUseProcesses?
Consider a Web server
get network message (URL) from client
create child process, send it URL
Child
fetch URL data from disk
compose response
send response
11
12
TheGeniusofSeparatingFork/Exec
TheGeniusofSeparatingFork/Exec
Decoupling fork and exec lets you do anything to the
childs process environment without adding it to the
CreateProcess API.
identical
de ca copy o
of a
all pa
parents
e s variables
a ab es & memory
e oy
identical copy of all parents CPU registers (except one)
Parent and child execute at the same point after fork() returns:
by convention, for the child, fork() returns 0
by convention, for the parent, fork() returns the process identifier of
the child
fork() return code a convenience, could always use getpid()
13
14
ProgramLoading:exec()
Whatcreatesaprocess?
1
1.
2.
3.
Fork
Exec
Both
15
16
GeneralPurposeProcessCreation
Ashellforksandthenexecsacalculator
int pid = fork();
if(pid == 0) {
close(.history);
exec(/bin/calc);
} else {
wait(pid);
child_status = wait(pid);
}
int calc_main(){
pid = fork();
if(pid
int q==
= 0)
7; {
close(.history);
do_init();
exec(/bin/calc);
ln = get_input();
} else
exec_in(ln);
{
wait(pid);
USER
OS
p = 128
pid
127
open files = .history
last_cpu = 0
Process Control
Blocks (PCBs)
pid = 128
open files =
last_cpu = 0
17
18
Ashellforksandthenexecsacalculator
main; a = 2
Stack
Atwhatcost,fork()?
Stack
Simple implementation of fork():
USER
OS
Heap
0xFC0933CA
int shell_main() {
int a = 2;
Code
pid = 128
p
127
open files = .history
last_cpu = 0
Heap
0x43178050
int calc_main() {
int q = 7;
Code
vfork()
a system call that creates a process without creating an identical
memory image
child process should call exec() almost immediately
Unfortunate example of implementation influence on interface
Process Control
Blocks (PCBs)
pid = 128
open files =
last_cpu = 0
20
OrderlyTermination:exit()
Thewait()SystemCall
If so, it holds the result value until parent requests it; in this case,
process does not really die, but it enters the zombie/defunct state
If not, it deallocates all data structures, the process is dead
21
22
TyingitAllTogether:TheUnixShell
ProcessControl
while(! EOF) {
read input
handle regular expressions
pid = fork();
()
// create a child
int p
if(pid == 0) {
// child continues here
exec(program, argc, argv0, argv1, );
}
else {
// parent continues here
Debugging support:
ptrace(), allows a process to be put under control of another
process
The other process can set breakpoints, examine registers, etc.
23
24
Anatomy of a Process
ADoseofReality:SchedulinginSolaris
mapped segments
DLLs
Header
Code
Processs
address space
Stack
Initialized data
Process Control
Block
Heap
Executable File
Close to our scheduling diagram, but more complicated
25
PC
Stack Pointer
Registers
PID
UID
Scheduling Priority
List of open files
Initialized data
Code
26
Unixfork()example
The execution context for the child process is a copy of the
parents context at the time of the call
fork() returns child PID in parent, and 0 in child
main {
int childPID;
S1;
fork()
childPID = fork();
if(childPID == 0)
p
<code ffor child process>
else {
<code for parent process>
wait();
}
S2;
}
Code
childPID
=0
Data
Stack
Parent
Code
Data
childPID
= xxx
Stack
Child
27