You are on page 1of 40

Slide 6-1

Copyri ght 2004 Pearson Educati on, Inc.


Operating Systems: A Modern Perspective, Chapter 6
Slide 6-2
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Implementing
Processes, Threads,
and Resources
6
Slide 6-3
Copyri ght 2004 Pearson Educati on, Inc.
Introduction
A Process -- a program in execution
-- a schedulable unit of computation
Processes and the threads are the active computational
elements in a modern computer system
Operate on passive resources i.e. primary memory and
devices
Process manager provides spectrum of services to define,
support and administer the systems processes, threads
and resources
OS creates a collection of abstraction abstraction
machine, each of which can execute an application programs.
Slide 6-4
Copyri ght 2004 Pearson Educati on, Inc.
Slide 6-5
Copyri ght 2004 Pearson Educati on, Inc.
Classic vs Modern Process
Classic : one thread (execution engine) per one
process represent the concept of a program in
execution
New abstraction : divide the aspects of classic process
into 2 parts : modern process & thread
Modern process Modern process : defines a customized
computation framework in which a program
execution.
Thread Thread : keeps track of code execution within that
framework.
Slide 6-6
Copyri ght 2004 Pearson Educati on, Inc.
Classic vs Modern Process (cont)
Modern model : enables programmer to design
software so that various parts of the
computation can work together as a set of
threads within a single process
Analogy :
Modern process: is like a studio, thread is like a
musician who uses the studio to make music.
Classic process: each musician has a private
studio.
Slide 6-7
Copyri ght 2004 Pearson Educati on, Inc.
Abstract Machine for Classic
Processes
Modern OS use multiprogrammingto provide application
program with the illusion that they each have their own
exclusive machine on which to execute the code
Idea: OS allocate blocks of executable memory using
space-multiplex, then it allocates the processor to
different classic processes using time-multiplex sharing
Abstract machine is fundamental concepts in
multiprogramming : defines the logical computing
environment in which the classic process executes.
Slide 6-8
Copyri ght 2004 Pearson Educati on, Inc.
Implementing the process
abstraction
A
b
s
t
r
a
c
t

M
a
c
h
i
n
e
Slide 6-9
Copyri ght 2004 Pearson Educati on, Inc.
The external view of the Process Manager
Application program view the OS as an abstract machine that is
controlled by making calls on the functions on its exported API.
Slide 6-10
Copyri ght 2004 Pearson Educati on, Inc.
Modern Processes and Threads
Threads are multi-
programmed entities that
execute on a single
abstract machine
Threads Threads
Abstract Abstract
machines machines
Slide 6-11
Copyri ght 2004 Pearson Educati on, Inc.
Process Address Space
Collection of addresses that a thread can reference.
Addresses refer to an executable memory location or can be
associated with other abstract machine elements
Memory-mapped resources : abstract machine resource whose
interface can be specified as a collections of byte addresses
i.e. if the OS exports a device interface as a set of byte-
addressable registers, then the device can be bound into a
processs address space
Binding addresses : responsibility of resource manager to bind
addresses with addressable elements of the resource
Slide 6-12
Copyri ght 2004 Pearson Educati on, Inc.
The Address Space
Any resource that can be references as a collection of bytes can have
each of its byte-addressable elements bound to an address in the address
space.
Slide 6-13
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Process Manager
In a multi-programmed OS, several processes can be
executed at the same time.
The Process Manager is that part of the OS that is
responsible for managing all the processes on the
system.
When the computer is powered on, there is only one
program in execution: the initial process.
The initial process creates the OS, which can then
create other processes as needed.
A process can create another process with a system
call (e.g. forkin UNIX).
The created process is called a child process
Slide 6-14
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Process Manager
Slide 6-15
Copyri ght 2004 Pearson Educati on, Inc.
Process manager responsibilities
Process manager :
is responsible for implementing the process, thread and
resource abstraction using the underlying hardware
Must control the activity of the processor and other
resources so they provide the abstract machine functions :
Process creation & deletion
Thread creation & deletion
Process/thread synchronization
Resource allocation
Resource protection
Cooperation with device manager to implement I/O
Implementation of address space
Slide 6-16
Copyri ght 2004 Pearson Educati on, Inc.
A modern process
Composed of the elements :
An An address space address space
A A program program define behavior of the process
Data Data used by the process
Resources Resources required for thread execution
Process identifier Process identifier to uniquely reference a
process during its existence. to keep track of the
progress of each process
Slide 6-17
Copyri ght 2004 Pearson Educati on, Inc.
Threads characteristics
Threads are the active element of computation
that occurs within a modern process
framework.
Each threads has :
A host process environment environment (framework)
Thread specific data data
Thread identifier identifier
Slide 6-18
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Processes&Threads
A
d
d
r
e
s
s

S
p
a
c
e
Map
S
t
a
c
k
State
P
r
o
g
r
a
m
S
t
a
t
i
c

d
a
t
a
R
e
s
o
u
r
c
e
s
S
t
a
c
k
State
Map
Slide 6-19
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Tracing the Hardware Process
Bootstap
Loader
Process
Manager
Interrupt
Handler
P
1
P
,2
P
n

Machine is
Powered up
Initialization
Load the kernel
Service an interrupt
H
a
r
d
w
a
r
e

p
r
o
c
e
s
s

p
r
o
g
r
e
s
s
Execute a thread
Schedule
The hardware process represents the sequence of machine instructions
executed by CPU
Slide 6-20
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Process Manager
Slide 6-21
Copyri ght 2004 Pearson Educati on, Inc.
The Abstract Machine Interface
Abstract machine interface has 2 kinds of
instruction :
1. User mode hardware instructions
2. OS functions
To make sure that when a process executes an
abstract machine instruction that is
implemented by an OS function, the system
intercepts the hardware execution and calls an
OS function instead.
Slide 6-22
Copyri ght 2004 Pearson Educati on, Inc.
The Abstract Machine Interface
The complete abstract machine interface is defined by the set of instructions
that the hardware can execute when the processor is in user mode.
The trap trap instruction instruction extends the
abstract machine interface so that the
abstract machine instructions include
all thefunctions ontheOS systemcall
interface.
Slide 6-23
Copyri ght 2004 Pearson Educati on, Inc.
The Process Abstraction
The process manager creates the environment in which
multiple processes coexists; each with its own abstract
machine.
When hardware process begins to execute OS code, it will
execute an algorithm that switches the hardware process
from one context (abstract machine) to another.
Context switches Context switches occur whenever the OS gets control of
the processor
whenever a process/thread makes a system call
(execute trap instruction)
whenever device interrupts occur
Slide 6-24
Copyri ght 2004 Pearson Educati on, Inc.
Context Switching
A context switch is the action taken by the hardware process when it
suspends execution on one program and begin execution on another.
Hardware process Hardware process Hardware process Hardware process Context Switching Context Switching Context Switching Context Switching
Slide 6-25
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Context switching
Slide 6-26
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Context Switch
Context switching
the action taken by the hardware process
when it suspends execution on one program
and begins execution on another.
Slide 6-27
Copyri ght 2004 Pearson Educati on, Inc.
Process Descriptor
OS creates/manages process abstraction
When a process is created, the process manager
algorithm creates a data structure called the process process
descriptor descriptor
to keep all details for managing the process.
Register values
Logical state
Type & location of resources it holds
List of resources it needs
Security keys
The process descriptor will de-allocated when the
process terminates
Slide 6-28
Copyri ght 2004 Pearson Educati on, Inc.
The Thread Abstraction
The thread management algorithm is the part of the
process manager that creates and manages threads.
The major task in managing a thread are :
Create / destroy a thread
Allocate thread-specific resources
Manage thread context switching and scheduling
Process manager contains algorithm to accomplish
these tasks as well as a central thread descriptor thread descriptor data
structure
Slide 6-29
Copyri ght 2004 Pearson Educati on, Inc.
The Thread Abstraction
The thread descriptor thread descriptor is the data structure where the OS will
keep all information it needs to manage that thread.
The fields that are incorporated into a generic thread
descriptor:
State
Execution statistics
Process
List of related threads
Stack
Other resources
Slide 6-30
Copyri ght 2004 Pearson Educati on, Inc.
State Diagram
After a process/thread has been created, OS has
descriptors that it can use to keep track of the
process/thread.
The OS will need to know in which linked list a
process is stored if it is to move the process.
In process managers, use a state state variable as a
summary status of the process/thread.
The state variable summarizes the process/thread
status by individual values.
Slide 6-31
Copyri ght 2004 Pearson Educati on, Inc.
State Diagram
A state diagram state diagram represents the different states in which
a thread can be at different times, along with the
transitions transitions from one state to another that are possible in
the OS.
The process manager causes the process/thread to
change state by administering the process.
Slide 6-32
Copyri ght 2004 Pearson Educati on, Inc.
Process State
The most basic process/thread diagram has three states : running, ready,
blocked.
Running : using the processor
Ready : wait for the processor
Blocked : a blocked process was running but request for resource that
unavailable. It will remain blocked until it is allocated the resource.
Slide 6-33
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Process State
Slide 6-34
Copyri ght 2004 Pearson Educati on, Inc.
Resource Manager
The generic part of resource manager is a mechanism mechanism
for allocating resources
The resource-specific behaviour is determined by a
policy policy
Every resource manager accepts requests accepts requests to allocate
units of its resource to a process as OS functions (the
request() request() function)
The request() request() function will execute a resource-
specific policy algorithm to determine the criteria under
which it should allocate resources to processes
Slide 6-35
Copyri ght 2004 Pearson Educati on, Inc.
Resource Manager
The behavior of a resource manager using formal
model:
There are many, m, different types of resources
Each type can have multiple units
Processes can requestvariable numbers of units of
the resource
At any given moment, some number of units of each
of the resource type are available, hence it would be
allocated
Slide 6-36
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Using the Model
There is a resource manager, Mgr(R
j
) for every R
j
Mgr(R
j
)
Process Process
p
i
can only request n
i
c
j
units of reusable R
j
p
i
can request unbounded #of units of consumable R
j
Process p
i
can request units of R
j
if it is currently running
request
Mgr(R
j
) can allocate units of R
j
to p
i
allocate
Slide 6-37
Copyri ght 2004 Pearson Educati on, Inc.
Resource Manager
Process Process
Resource Manager
Process Process
Process Process
Process Process
Blocked Processes
Resource Pool
r equest ( )
r el ease( )
Policy
Await allocation of
resource
Slide 6-38
Copyri ght 2004 Pearson Educati on, Inc.
Resource Manager
Resource desriptor Resource desriptor:
Internal resource name
Total units
Available units
List of available units
List of blocked processes
Slide 6-39
Copyri ght 2004 Pearson Educati on, Inc.
Resource Manager
CPU,
primary
memory,
disk space
Message,
input data
Slide 6-40
Copyri ght 2004 Pearson Educati on, Inc.
Operating Systems: A Modern Perspective, Chapter 6
Process Manager Overview
Program
Process
Abstract Computing Environment
File
Manager
Memory
Manager
Device
Manager
Protection
Deadlock
Synchronization
Description
Process
Description
CPU Other H/W
Scheduler
Resource
Manager
Resource
Manager
Resource
Manager
Memory Devices

You might also like