Professional Documents
Culture Documents
Systems:
Internals
and Design
Principles Chapter 4
Process and Threads
Eighth Edition
By William Stallings
1
OS Management of
Application Execution
Resources are made available to multiple
applications
Theprocessor is switched among multiple
applications so all will appear to be
progressing
Theprocessor and I/O devices can be
used efficiently
2
Process
Fundamental to the structure of operating systems
a program in execution
an instance of a running program
the entity that can be assigned to, and executed on, a processor
3
Process Elements
Two essential elements of a process are:
Program code
which may be shared with other processes that are executing
the same program
identifier
program
state priority
counter
Context data
6
Process States
Trace Dispatcher
the behavior of an
individual process
by listing the
sequence of small program
instructions that that switches the
execute for that processor from
process one process to
another
7
Address Main Memory Program Counter
0
8000
100
Process
Dispatcher
5000
Execution 8000
Process A
Process B
12000
Process C
8
5000 8000 12000
5001 8001 12001
5002 8002 12002
5003 8003 12003
5004 12004
5005 12005
5006 12006
5007 12007
5008 12008
5009 12009
5010 12010
5011 12011
(a) Trace of Process A (b) Trace of Process B (c) Trace of Process C
10
Two-State Process Model
A process may be in one of two states:
o Running
o Not-running
Dispatch
Pause
11
Pause
Queue
Enter Dispatch Exit
Processor
Pause
12
Process Creation
Occurs when a new process is to be added to those
currently being managed processes.
The operating system builds the data structures that are
used to manage the process and allocates address space
in main memory to the process.
13
Table 3.1 Reasons for Process
Creation
14
Process Creation
Process
spawning Parent process Child process
15
Process Termination
There must be a means for a process to indicate its
completion
A batch job should include a HALT instruction or an
explicit OS service call for termination
For an interactive application, the action of the user will
indicate when the process is completed (e.g. log off,
quitting an application)
16
Normal completion The process executes an OS service call to indicate that it has completed
running.
Table 3.2 Time limit exceeded The process has run longer than the specified total time limit. There are a
number of possibilities for the type of time that is measured. These include total
elapsed time ("wall clock time"), amount of time spent executing, and, in the
case of an interactive process, the amount of time since the user last provided
any input.
Reasons for Memory unavailable The process requires more memory than the system can provide.
Protection error
The process tries to access a memory location that it is not allowed to access.
The process attempts to use a resource such as a file that it is not allowed to use,
Arithmetic error The process tries a prohibited computation, such as division by zero, or tries to
store numbers larger than the hardware can accommodate.
Time overrun The process has waited longer than a specified maximum for a certain event to
occur.
I/O failure An error occurs during input or output, such as inability to find a file, failure to
read or write after a specified maximum number of tries (when, for example, a
defective area is encountered on a tape), or invalid operation (such as reading
from the line printer).
Invalid instruction The process attempts to execute a nonexistent instruction (often a result of
branching into a data area and attempting to execute the data).
Privileged instruction The process attempts to use an instruction reserved for the operating system.
Operator or OS intervention For some reason, the operator or the operating system has terminated the process
(e.g., if a deadlock exists).
Parent termination When a parent terminates, the operating system may automatically terminate all
of the offspring of that parent.
(Table is located on page 115 Parent request A parent process typically has the authority to terminate any of its offspring.
in the textbook)
17
Five-State Process Model
Dispatch
Admit Release
New Ready Running Exit
Timeout
Event
Occurs Event
Wait
Blocked
18
Suspended Processes
Swapping
involves moving part of all of a process from main memory to disk
when none of the processes in main memory is in the Ready state, the
OS swaps one of the blocked processes out on to disk into a suspend
queue
19
(a) With One Suspend State
New
Ad
t
Dispatch
mi
Admit Release
mi
Ad
New Ready Suspend Running Exit
t
Timeout
Activate Dispatch
Ready/ Release
it
Ready Running Exit
Occurs Occurs
a
e
Event Event
at
tW
Suspend
iv
Suspend Timeout
ct
t W ven
A
t
ai
Occurs
Event
Suspend
en
Suspend Blocked
Ev
Activate
Blocked/
Suspend (a)Blocked
With One Suspend State
Suspend
mi
Ad
Suspe
t
nd
20
Characteristics of a
Suspended Process
The process is not The process may or may
immediately available not be waiting on an
for execution event
21
Table 3.3 Reasons for Process Suspension
22
P1 P2 Pn
Virtual
Memory
Computer
Resources
Main
Processor I/O I/O I/O
Memory
Figure 3.10 Processes and Resources (resource allocation at one snapshot in time)
23
Processes and Threads
24
Processes and Threads
The unit of dispatching is referred to as a thread or
lightweight process
The unit of resource ownership is referred to as a
process or task
Multithreading - The ability of an OS to support
multiple, concurrent paths of execution within a
single process
25
Single Threaded Approaches
A single thread of
execution per process,
in which the concept one process
one thread
one process
multiple threads
of a thread is not
recognized, is referred
to as a single-threaded
approach multiple processes
one thread per process
multiple processes
multiple threads per process
26
Multithreaded Approaches
The right half of
Figure 4.1 depicts
multithreaded one process
one thread
one process
multiple threads
approaches
A Java run-time
environment is an multiple processes multiple processes
example of a system one thread per process multiple threads per process
27
Processes
The unit of resource allocation and a unit of
protection
A virtual address space that holds the process image
Protected access to:
processors
other processes
files
I/O resources
28
One or More Threads
in a Process
30
Benefits of Threads
31
Operating System
Control Structures
To manage processes and resources, OS must have
information about the current status of each process and
resource.
The OS constructs and maintains tables of information
about each entity that it is managing.
Four different types of tables maintained by the OS:
o Memory tables
o IO Tables
o File Tables
o Process Tables
32
Process
Image
Memory Tables
Process
Memory 1
Files
Process 1
Process 2
Process
Process 3 Image
Process
n
Process n
Process states
Two-state process model
Creation and termination
Five-state model
Suspended processes
34