CS444/CS544 Operating Systems Processes 1/24/2007 Prof. Searleman [email protected]
Transcript
Slide 1
CS444/CS544 Operating Systems Processes 1/24/2007 Prof.
Searleman [email protected]
Slide 2
CS444/CS544 Spring 2007 More on Processes Introduction to
Threads Course webpage: http://www.clarkson.edu/class/cs444 Reading
assignment: SGG: Chapter 3
Slide 3
Recap: Programs vs Processes A program is passive Sequence of
commands waiting to be run A process is active An instance of
program being executed There may be many processes running the same
program What makes up a process: Address space Code Data Stack
(nesting of procedure calls made) Register values (including the
PC) Resources allocated to the process Memory, open files, network
connections
Slide 4
Recap: Process States New Ready Running Waiting Terminated
admit dispatch exit, abort interrupt block for I/O or wait for
event I/O completed or event occurs
Slide 5
State Queues OSs often maintain a number of queues of processes
that represent the state of the processes All the runnable
processes are linked together into one queue All the processes
blocked (or perhaps blocked for a particular class of event) are
linked together As a process changes state, it is unlinked from one
queue and linked into another
Slide 6
Context Switch When a process is running, some of its state is
stored directly in the CPU (register values, etc.) When the OS
stops a process, it must save all of this hardware state somewhere
(PCB) so that it can be restored again The act of saving one
process hardware state and restoring anothers is called a context
switch 100s or 1000s per second!
Slide 7
Context Switch
Slide 8
UNIX process creation fork() system call Creates a new PCB and
a new address space Initializes the new address space with a *copy*
of the parents address space Initializes many other resources to
copies of the parents (e.g. same open files) Places new process on
the queue of runnable processes fork gives two processes exactly
alike fork() returns twice: to parent and child Returns childs
process ID to the parent Returns 0 to the child
Slide 9
Example Code Snippet int main (int argc, char **argv) { int
childPid; childPid = fork(); if (childPid == 0){ printf(Child
running\n); } else { printf(Parent running: my child is %d\n,
childPid); } PC parent PC child
Slide 10
Output %./tryfork Parent running: my child is 707 Child running
%
Slide 11
Process creation Question: How many processes will be created
in order to run the following code? int main (int argc, char
**argv) { fork(); }
Slide 12
Process hierarchy int main () { 1: fork(); 2: fork(); 3: } p0
PC p0 Running process: p0 State: before p0 executes statement
1
Slide 13
Process hierarchy int main () { 1: fork(); 2: fork(); 3: } p0
PC p0 p1 PC p1 State: after p0 executes statement 1 Who will run
next? Depends on the scheduler. Suppose its process p1 (requires a
context switch from p0 to p1)
Slide 14
Process hierarchy int main () { 1: fork(); 2: fork(); 3: } p0
PC p0 p1 PC p1 Running process: p1 State: before p1 executes
statement 2
Slide 15
Process hierarchy int main () { 1: fork(); 2: fork(); 3: } p0
PC p0 p1 PC p1 Running process: p1 p2 PC p2 State: after p1
executes statement 2 Suppose that process p0 is selected to run
next (requires a context switch from p1 to p0)
Slide 16
Process hierarchy int main () { 1: fork(); 2: fork(); 3: } p0
PC p0 p1 PC p1 Running process: p0 p2 PC p2 p3 PC p3 State: after
p0 executes statement 2 How many processes were created? Answer: 4
(NOTE: other execution sequences are possible.) Question: what
happens to the 4 processes?
Slide 17
Experiments Be Careful! Try putting an infinite loop in the
childs portion ( do you return to the command shell?) and then
looking for it in the ps output Try putting an infinite loop in the
parents portion (do you return to the command shell?) Put an
infinite loop in both try killing the child (look in the ps output
for the child and the parent) Try killing the parent what happens
to the child?
Slide 18
Exec How do we get a brand new process, not just a copy of the
parent? exec() system call int exec (char * prog, char ** argv)
exec() Stops the current process Loads the program, prog, into the
address space Passes the arguments specified in argv Places the PCB
back on the ready queue exec() takes over the process There is no
going back to it when it returns Try to exec something in your
shell (example: exec ls) when ls is done your shell is gone because
ls replaced it! Note: execvp() will search users path
automatically
Slide 19
Better way? Dont fork/exec seem a bit wasteful to you Why make
a full copy of the parents virtual memory space if we are just
going to overwrite it anyway? Function vfork creates a new process
without fully copying the parents address space Parent suspended
while child temporarily borrows its memory and thread of control
(unlike fork) Man vfork for warnings about _exit vs exit
Experiment: Time N iterations of fork or vfork
Slide 20
Init process In last stage of boot process, kernel creates a
user level process, init() init() is the parent (or grandparent) of
all other processes init() does various important housecleaning
activities checks and mounts the filesystems, sets hostname,
timezones, etc. init() reads various resource configuration files
(/etc/rc.conf, etc) and spawns off processes to provide various
services In multi-user mode, init() maintains processes for each
terminal port (tty) Usually runs getty which executes the login
program
Slide 21
Foreground vs Background int main (int argc, char **argv){
while (1){ int childPid; char* cmdLine = readCommandLine(); if
(userChooseExit(cmdLine)){ wait for all background jobs } childPid
= fork(); if (childPid == 0){ setSTDOUT_STDIN_STDERR(cmdLine);
exec( getCommand(cmdLine)); } else if (runInForeground(cmdLine)){
wait(childPid); } else { Record childPid In list of background
jobs} }
Slide 22
Schedulers Technically two kinds of schedulers Short-term
scheduler (or CPU scheduler) Selects which process should be
executed next and allocates CPU. Short-term scheduler is invoked
very frequently (milliseconds) (must be fast). Long-term scheduler
(or job scheduler) Selects which processes should be brought into
the ready queue. Determines the degree of multiprogramming In
reality this is usually you the human user
Slide 23
What kinds of processes are there? Compute bound/ IO bound
Long-running/short-running Interactive/batch Large/small memory
footprint Cooperating with other processes? How do we get all these
different kinds of processes?
Slide 24
Family Tree Age old questions where do new processes come from?
New processes are created when an existing process requests it
Creating process called the parent; created called the child
Children of same parent called siblings Children often inherit
privileges/attributes from their parent Working directory, Clone of
address space
Redirecting I/O Three default file streams in each process
Stdout, stdin, stderr In a shell, child process may need a
different stdout, stdin or stderr than the parent Before call exec
If there is a < infile then set stdin to the infile If there is
a > outfile then set the stdout the outfile
Slide 27
Dup2 //after fork, in the child if (command line contains <
infile){ int fd = open (infileName, O_RDONLY); if (fd == -1){
redirection failed } else { dup2 (fd, STDIN_FILENO); } //exec
Slide 28
Cooperating Processes Processes can run independently of each
other or processes can coordinate their activities with other
processes To cooperate, processes must use OS facilities to
communicate One example: parent process waits for child Many others
Files (Youve Used) Sockets (Networks) Pipes (Like Sockets for local
machine; Pair of files) Signals (Today) Shared Memory Events Remote
Procedure Call
Slide 29
Processes: Summary A process includes Address space (Code,
Data, Heap, Stack) Register values (including the PC) Resources
allocated to the process Memory, open files, network connections
How to create a process Initializing the PCB and the address space
(page tables) takes a significant amount of time Interprocess
communication IPC is costly also Communication must go through OS
(OS has to guard any doors in the walls it builds around processes
for their protection)