Professional Documents
Culture Documents
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 0 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 1
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 2 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 3
Process states Process state diagram
There are a number of states that can be attributed to a process: state transition
indeed, the operation of a multiprogramming system can be described internal
external
by a state transition diagram on the process states. The states of a
stopped kill
process include:
• New—a process being created but not yet included in the pool of resume suspend
executable processes (resource acquisition).
create
new time-out exit
• Ready—processes that are prepared to execute when given the
opportunity.
ready active exiting
• Active—the process that is currently being executed by the CPU.
• Blocked—a process that cannot execute until some event occurs. dispatch kill
• Stopped—a special case of blocked where the process is suspended event
event or
by the operator or the user. occurs or
resource
resource
available wait
• Exiting—a process that is about to be removed from the pool of blocked error
executable processes (resource release).
Jan’0 1
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 4 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 5
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 6 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 7
Dispatcher at work Control of the CPU
process The CPU can only do one thing at a time. While a user
process
2 process is running, dispatcher cannot run, thus the
1 process operating system may lose control.
n
How does the dispatcher regain control (of the CPU)?
CPU
• Trust the process to wake up the dispatcher when done
(sleeping beauty approach) .
• Provide a mechanism to wake up the dispatcher (alarm
Operating dispatcher
clock).
System
The problem with the first approach is that
sometimes processes loop indefinitely. Therefore, the
Ready Queue
alarm clock interrupt approach is better.
Jan’0 1
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 8 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 9
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 10 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 11
Context switch implementation Creating a new process
The mechanism of context switching is the most There are two practical ways of creating a new process:
important part of an operating system, and needs • Build one from scratch:
special care during the implementation, because: – Load code and data into memory.
• It is tricky. – Create (empty) a dynamic memory workspace (heap).
– Create and initialize the process control block.
Saving the state of a user process is problematic
because the operating system must execute code to – Make process known to dispatcher.
save the state without changing the process’ current • Clone an existing one:
state! – Stop current process and save its state.
• Machine dependent. – Make a copy of code, data, dynamic memory workspace
Thanks to technology; each CPU provides some and process control block.
special support to ease the implementation. – Make process known to dispatcher.
Jan’0 1
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 12 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 13
parent until the child has finished execution, so the parent before fork()
child
resumes
and child do not run concurrently. here
after fork()
Jan’0 1
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 14 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 15
A typical use of fork() UNIX system initialization
process 0
parent bootstrap wait until system shutdown
resumes swapper init exits
here
if ( fork() )
{
fork process 1
} wait until all
else init children exit
{
exec(…)
}
if ( fork() ) exit
{ fork
exec exec
child getty login SHELL
}
else resumes .
{ here .
}
exec(…)
. fork
if ( fork() ) user
{ commands
} exit
before fork() else
{ as many as
child exec(…) available
}
resumes terminals
here user
environment
after exec()
after fork()
Jan’0 1
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 16 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 17
TEXT
text : executable code
Jan’0 1
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 18 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 19
Threads Threads: an example
Unit of execution (unit of dispatching) and a A good example of an application that could make use of
collection of resources, with which the unit of threads is a file server on a local area network (LAN).
execution is associated, characterize the notion of a
process. process controller
file server
A thread
clients
A thread is the abstraction of a unit of execution. It is
also referred to as a light-weight process (LWP) .
process
B worker
As a basic unit of CPU utilization, a thread consists of threads
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 20 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 21
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 22 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 23
Thread implementations Fly in a bottle
• User level:
implemented as a set of library functions; cannot be
scheduled independently; each thread gets partial time
quantum of a process; a system call by a thread blocks the
entire set of threads of a process; less costly (thread)
operations
• Kernel level:
implemented as system calls; can be scheduled directly by
the OS; independent operation of threads in a single
process; more expensive (thread) operations.
• Hybrid approach:
combines the advantages of the above two; e.g., Solaris A traditional A modern
threads.
UNIX process UNIX process
Jan’0 1
Jan’0 1
Copyright © 1998-2001 by Eskicioglu & Marsland Processes 24 Copyright © 1998-2001 by Eskicioglu & Marsland Processes 25