You are on page 1of 34

Operating

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 process can be defined as:

a program in execution
an instance of a running program

the entity that can be assigned to, and executed on, a processor

a unit of activity characterized by a single sequential thread of execution, a


current state, and an associated set of system resources

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

A set of data associated with that code


 when the processor begins to execute the program code, we refer to
this executing entity as a process
4
Process Elements
 While the program is executing, this process can be uniquely
characterized by a number of elements, including:

identifier

program
state priority
counter

memory I/O status accounting


context data
pointers information information
5
Identifier
State
Process Control Priority
Program counter
Block Memory pointers

Context data

Contains the process elements I/O status


information
It is possible to interrupt a running
Accounting
process and later resume execution as
information
if the interruption had not occurred

Created and managed by the


operating system

Key tool that allows support for


multiple processes

Figure 3.1 Simplified Process Control Block

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

the behavior of the processor


can be characterized by
showing how the traces of
the various processes are
interleaved

7
Address Main Memory Program Counter
0
8000
100

Process
Dispatcher

5000

Execution 8000
Process A

Process B

12000

Process C

Figure 3.2 Snapshot of Example Execution (Figure 3.4)


at Instruction Cycle 13

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

5000 = Starting address of program of Process A


8000 = Starting address of program of Process B
12000 = Starting address of program of Process C

Figure 3.3 Traces of Processes of Figure 3.2


9
1 5000 27 12004
2 5001 28 12005
3 5002 -------------------- Timeout
4 5003 29 100
5 5004 30 101
6 5005 31 102
-------------------- Timeout 32 103
7 100 33 104
8 101 34 105
9 102 35 5006
10 103 36 5007
11 104 37 5008
12 105 38 5009
13 8000 39 5010
14 8001 40 5011
15 8002 -------------------- Timeout
16 8003 41 100
----------------I/O Request 42 101
17 100 43 102
18 101 44 103
19 102 45 104
20 103 46 105
21 104 47 12006
22 105 48 12007
23 12000 49 12008
24 12001 50 12009
25 12002 51 12010
26 12003 52 12011
-------------------- Timeout

100 = Starting address of dispatcher program


Shaded areas indicate execution of dispatcher process;
first and third columns count instruction cycles;
second and fourth columns show address of instruction being executed

Figure 3.4 Combined Trace of Processes of Figure 3.2

10
Two-State Process Model
 A process may be in one of two states:
o Running

o Not-running
Dispatch

Enter Not Exit


Running Running

Pause

(a) State transition diagram

11
Pause

(a) State transition diagram

Queue
Enter Dispatch Exit
Processor

Pause

(b) Queuing diagram

Figure 3.5 Two-State Process Model

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

• when the • is the • is the new


OS creates a original, process
process at creating,
the explicit process
request of
another
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.

Process Bounds violation

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,

Termination or it tries to use it in an improper fashion, such as writing to a read-only file.

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.

Data misuse A piece of data is of the wrong type or is not initialized.

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

Figure 3.6 Five-State Process Model

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

(b) With Two Suspend States


New

Figure 3.9 Process State Transition Diagram with Suspend States


Ad
t
mi

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

 The process was placed


 The process may not be
in a suspended state by removed from this state
an agent: either itself, a until the agent explicitly
parent process, or the orders the removal
OS, for the purpose of
preventing its execution

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

Resource Ownership Scheduling/Execution


Process includes a Follows an execution path
virtual address space that may be interleaved
to hold the process with other processes
image  a process has an execution state
 the OS performs a (Running, Ready, etc.) and a
protection function to dispatching priority and is
prevent unwanted scheduled and dispatched by
interference between the OS
processes with respect to
resources

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

 MS-DOS is an = instruction trace

example Figure 4.1 Threads and Processes

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

of one process with = instruction trace

multiple threads Figure 4.1 Threads and Processes

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

Each thread has:

• an execution state (Running, Ready, etc.)


• saved thread context when not running
• an execution stack
• some per-thread static storage for local
variables
• access to the memory and resources of its
process (all threads of a process share this)
29
Single-Threaded Multithreaded
Process Model Process Model
Thread Thread Thread
Thread Thread Thread
Process User Control Control Control
Control Stack Block Block Block
Block

Process User User User


User Kernel Stack Stack Stack
Control
Address Stack
Block
Space

User Kernel Kernel Kernel


Address Stack Stack Stack
Space

Figure 4.2 Single Threaded and Multithreaded Process Models

30
Benefits of Threads

Less time to Threads enhance


terminate a efficiency in
thread than a Switching communication
Takes less process between two between programs
time to threads takes less
create a new time than
thread than a switching between
process processes

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

Devices I/O Tables

Files

Processes File Tables

Primary Process Table

Process 1

Process 2
Process
Process 3 Image
Process
n

Process n

Figure 3.11 General Structure of Operating System Control Tables


33
Summary
 What is a process?  Processes and threads
 Background
 Operating system control
 Processes and process control structures
blocks

 Process states
 Two-state process model
 Creation and termination
 Five-state model
 Suspended processes

34

You might also like