PDF Ccccsss PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 245

Chapter 1: Introduction

Operating System Concepts – 9th Edit9on Silberschatz, Galvin and Gagne ©2013
What is an Operating System?

 A program that acts as an intermediary between a user of a


computer and the computer hardware
 An operating system is a program that acts as an interface
between the user and the computer hardware and controls the
execution of all kinds of programs.

Operating System Concepts – 9th Edition 1.2 Silberschatz, Galvin and Gagne ©2013
Computer System Structure

 Computer system can be divided into four components:


 Hardware – provides basic computing resources
 CPU, memory, I/O devices
 Operating system
 Controls and coordinates use of hardware among various
applications and users
 Application programs – define the ways in which the system
resources are used to solve the computing problems of the
users
 Word processors, compilers, web browsers, database
systems, video games
 Users
 People, machines, other computers

Operating System Concepts – 9th Edition 1.3 Silberschatz, Galvin and Gagne ©2013
Four Components of a Computer System

Operating System Concepts – 9th Edition 1.4 Silberschatz, Galvin and Gagne ©2013
Operating System Definition

No universally accepted definition

 OS is a resource allocator
 Manages all resources
 Decides between conflicting requests for efficient and
fair resource use
 OS is a control program
 Controls execution of programs to prevent errors and
improper use of the computer
 OS is a Command Executer
 Provides an environment for running user commands

Operating System Concepts – 9th Edition 1.5 Silberschatz, Galvin and Gagne ©2013
Computer Startup
 bootstrap program is loaded at power-up or reboot
 Typically stored in ROM or EPROM, generally known as firmware
 Initializes all aspects of system
 Loads operating system kernel and starts execution

Operating System Concepts – 9th Edition 1.6 Silberschatz, Galvin and Gagne ©2013
Computer System Organization

 Computer-system operation
 One or more CPUs, device controllers connect through common
bus providing access to shared memory
 Concurrent execution of CPUs and devices competing for
memory cycles

Operating System Concepts – 9th Edition 1.7 Silberschatz, Galvin and Gagne ©2013
Computer-System Operation

 I/O devices and the CPU can execute concurrently


 Each device controller is in charge of a particular device type
 Each device controller has a local buffer
 CPU moves data from/to main memory to/from local buffers
 I/O is from the device to local buffer of controller
 Device controller informs CPU that it has finished its operation by
causing an interrupt

Operating System Concepts – 9th Edition 1.8 Silberschatz, Galvin and Gagne ©2013
Common Functions of Interrupts

 Interrupt transfers control to the interrupt service


routine generally, through the interrupt vector, which
contains the addresses of all the service routines
 Interrupt architecture must save the address of the
interrupted instruction
 A trap or exception is a software-generated interrupt
caused either by an error or a user request
 An operating system is interrupt driven

Operating System Concepts – 9th Edition 1.9 Silberschatz, Galvin and Gagne ©2013
Storage Definitions and Notation Review – self read

The basic unit of computer storage is the bit. A bit can contain one of two
values, 0 and 1. All other storage in a computer is based on collections of bits.
Given enough bits, it is amazing how many things a computer can represent:
numbers, letters, images, movies, sounds, documents, and programs, to name
a few. A byte is 8 bits, and on most computers it is the smallest convenient
chunk of storage. For example, most computers don’t have an instruction to
move a bit but do have one to move a byte. A less common term is word,
which is a given computer architecture’s native unit of data. A word is made up
of one or more bytes. For example, a computer that has 64-bit registers and 64-
bit memory addressing typically has 64-bit (8-byte) words. A computer executes
many operations in its native word size rather than a byte at a time.
Computer storage, along with most computer throughput, is generally measured
and manipulated in bytes and collections of bytes.
A kilobyte, or KB, is 1,024 bytes
a megabyte, or MB, is 1,0242 bytes
a gigabyte, or GB, is 1,0243 bytes
a terabyte, or TB, is 1,0244 bytes
a petabyte, or PB, is 1,0245 bytes

Computer manufacturers often round off these numbers and say that a
megabyte is 1 million bytes and a gigabyte is 1 billion bytes. Networking
measurements are an exception to this general rule; they are given in bits
(because networks move data a bit at a time).

Operating System Concepts – 9th Edition 1.10 Silberschatz, Galvin and Gagne ©2013
Storage Structure
 Main memory – only large storage media that the CPU can access
directly
 Random access
 Typically volatile
 Secondary storage – extension of main memory that provides large
nonvolatile storage capacity
 Hard disks – rigid metal or glass platters covered with magnetic
recording material
 Disk surface is logically divided into tracks, which are subdivided into
sectors
 The disk controller determines the logical interaction between the device
and the computer
 Solid-state disks – faster than hard disks, nonvolatile
 Various technologies
 Becoming more popular

Operating System Concepts – 9th Edition 1.11 Silberschatz, Galvin and Gagne ©2013
Storage Hierarchy

 Storage systems organized in hierarchy


 Speed
 Cost
 Volatility
 Caching – copying information into faster storage system; main
memory can be viewed as a cache for secondary storage
 Device Driver for each device controller to manage I/O
 Provides uniform interface between controller and kernel

Operating System Concepts – 9th Edition 1.12 Silberschatz, Galvin and Gagne ©2013
Storage-Device Hierarchy

Operating System Concepts – 9th Edition 1.13 Silberschatz, Galvin and Gagne ©2013
Caching

 Important principle, performed at many levels in a computer


(in hardware, operating system, software)
 Information in use copied from slower to faster storage
temporarily
 Faster storage (cache) checked first to determine if
information is there
 If it is, information used directly from the cache (fast)
 If not, data copied to cache and used there
 Cache smaller than storage being cached
 Cache management important design problem
 Cache size and replacement policy

Operating System Concepts – 9th Edition 1.14 Silberschatz, Galvin and Gagne ©2013
Question

 Why we need different types of memory in


the computer system, why not have only
one type with the best properties?

Operating System Concepts – 9th Edition 1.15 Silberschatz, Galvin and Gagne ©2013
Operating System Structure
 Multiprogramming (Batch system) needed for efficiency
 Goal  keep CPU and I/O devices busy at all times
 Multiprogramming organizes jobs (code and data) so CPU always has one
to execute
 A subset of total jobs in system is kept in memory

 One job selected and run via job scheduling


 When it has to wait (for I/O for example), OS switches to another job

Memory Layout for Multiprogrammed System

Operating System Concepts – 9th Edition 1.16 Silberschatz, Galvin and Gagne ©2013
Operating System Structure
 Timesharing (multitasking) is logical extension of multiprogramming.

 Several users can share the computer simultaneously.


 CPU switches jobs so frequently
 users can interact with each job while it is running (interactive computing)

Operating System Concepts – 9th Edition 1.17 Silberschatz, Galvin and Gagne ©2013
Computer-System Architecture

 Multiprocessors
 Also known as parallel systems, tightly-coupled systems
 Advantages include:
1. Increased throughput
2. Economy of scale
3. Increased reliability – graceful degradation or fault tolerance

Operating System Concepts – 9th Edition 1.18 Silberschatz, Galvin and Gagne ©2013
Clustered Systems

 Like multiprocessor systems, but multiple systems working together


 Usually sharing storage via a storage-area network (SAN)
 Provides a high-availability service which survives failures
 Asymmetric clustering has one machine in hot-standby
mode
 Symmetric clustering has multiple nodes running
applications, monitoring each other
 Some clusters are for high-performance computing (HPC)
 Applications must be written to use parallelization
 Some have distributed lock manager (DLM) to avoid
conflicting operations

Operating System Concepts – 9th Edition 1.19 Silberschatz, Galvin and Gagne ©2013
Clustered Systems

Operating System Concepts – 9th Edition 1.20 Silberschatz, Galvin and Gagne ©2013
Question

 What kind of security problems


encountered in multiprogramming and
time-sharing systems?

Operating System Concepts – 9th Edition 1.21 Silberschatz, Galvin and Gagne ©2013
Transition from User to Kernel Mode
 Timer to prevent infinite loop / process hogging resources
 Timer is set to interrupt the computer after some time period
 Keep a counter that is decremented by the physical clock.
 Operating system set the counter (privileged instruction)
 When counter zero generate an interrupt
 Set up before scheduling process to regain control or terminate
program that exceeds allotted time

Operating System Concepts – 9th Edition 1.22 Silberschatz, Galvin and Gagne ©2013
Process Management
 A process is a program in execution. It is a unit of work within the
system. Program is a passive entity, process is an active entity.
 Process needs resources to accomplish its task
 CPU, memory, I/O, files
 Initialization data
 Process termination requires reclaim of any reusable resources
 Each process has program counter specifying location of next
instruction to execute
 Process executes instructions sequentially, one at a time,
until completion
 Typically system has many processes, some user, some
operating system running concurrently on one or more CPUs
 Concurrency by multiplexing the CPUs among the processes

Operating System Concepts – 9th Edition 1.23 Silberschatz, Galvin and Gagne ©2013
Process Management Activities

The operating system is responsible for the following activities in


connection with process management:
 Creating and deleting both user and system processes
 Suspending and resuming processes
 Providing mechanisms for process synchronization
 Providing mechanisms for process communication
 Providing mechanisms for deadlock handling

Operating System Concepts – 9th Edition 1.24 Silberschatz, Galvin and Gagne ©2013
Memory Management

 To execute a program all (or part) of the instructions must be in


memory
 All (or part) of the data that is needed by the program must be in
memory.
 Memory management determines what is in memory and when
 Optimizing CPU utilization and computer response to users
 Memory management activities
 Keeping track of which parts of memory are currently being
used and by whom
 Deciding which processes (or parts thereof) and data to
move into and out of memory
 Allocating and deallocating memory space as needed

Operating System Concepts – 9th Edition 1.25 Silberschatz, Galvin and Gagne ©2013
Storage Management
 OS provides uniform, logical view of information storage
 Abstracts physical properties to logical storage unit - file
 Each medium is controlled by device (i.e., disk drive, tape drive)
 Varying properties include access speed, capacity, data-
transfer rate, access method (sequential or random)

 File-System management
 Files usually organized into directories
 Access control on most systems to determine who can access
what
 OS activities include
 Creating and deleting files and directories

 Primitives to manipulate files and directories


 Mapping files onto secondary storage

 Backup files onto stable (non-volatile) storage media

Operating System Concepts – 9th Edition 1.26 Silberschatz, Galvin and Gagne ©2013
Mass-Storage Management
 Usually disks used to store data that does not fit in main memory or
data that must be kept for a “long” period of time
 Proper management is of central importance
 Entire speed of computer operation hinges on disk subsystem and its
algorithms
 OS activities
 Free-space management
 Storage allocation
 Disk scheduling
 Some storage need not be fast
 Tertiary storage includes optical storage, magnetic tape
 Still must be managed – by OS or applications
 Varies between WORM (write-once, read-many-times) and RW
(read-write)

Operating System Concepts – 9th Edition 1.27 Silberschatz, Galvin and Gagne ©2013
Protection and Security

 Protection – any mechanism for controlling access of processes or


users to resources defined by the OS
 Security – defense of the system against internal and external attacks
 Huge range, including denial-of-service, worms, viruses, identity
theft, theft of service
 Systems generally first distinguish among users, to determine who
can do what
 User identities (user IDs, security IDs) include name and
associated number, one per user
 User ID then associated with all files, processes of that user to
determine access control
 Group identifier (group ID) allows set of users to be defined and
controls managed, then also associated with each process, file
 Privilege escalation allows user to change to effective ID with
more rights

Operating System Concepts – 9th Edition 1.28 Silberschatz, Galvin and Gagne ©2013
Question

 What are the challenges when designing


operating system for mobile devices?

Operating System Concepts – 9th Edition 1.29 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Mobile

 Handheld smartphones, tablets, etc


 What is the functional difference between them and a
“traditional” laptop?
 Extra feature – more OS features (GPS, gyroscope)
 Allows new types of apps like augmented reality
 Use IEEE 802.11 wireless, or cellular data networks for
connectivity
 Leaders are Apple iOS and Google Android

Operating System Concepts – 9th Edition 1.30 Silberschatz, Galvin and Gagne ©2013
Computing Environments – Distributed

 Distributed computiing
 Collection of separate, possibly heterogeneous, systems
networked together
 Network is a communications path, TCP/IP most
common
– Local Area Network (LAN)
– Wide Area Network (WAN)
– Metropolitan Area Network (MAN)
– Personal Area Network (PAN)
 Network Operating System provides features between
systems across network
 Communication scheme allows systems to exchange
messages
 Illusion of a single system
Operating System Concepts – 9th Edition 1.31 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Virtualization

 Allows operating systems to run applications within other OSes


 Vast and growing industry
 Emulation used when source CPU type different from target
type (i.e. PowerPC to Intel x86)
 Generally slowest method
 When computer language not compiled to native code –
Interpretation
 Virtualization – OS natively compiled for CPU, running guest
OSes also natively compiled
 Consider VMware running WinXP guests, each running
applications, all on native WinXP host OS
 VMM (virtual machine Manager) provides virtualization
services

Operating System Concepts – 9th Edition 1.32 Silberschatz, Galvin and Gagne ©2013
Computing Environments - Virtualization

Operating System Concepts – 9th Edition 1.33 Silberschatz, Galvin and Gagne ©2013
Computing Environments – Cloud Computing

 Delivers computing, storage, even apps as a service across a network


 Logical extension of virtualization because it uses virtualization as the base
for it functionality.
 Amazon EC2 has thousands of servers, millions of virtual machines,
petabytes of storage available across the Internet, pay based on usage
 Many types
 Public cloud – available via Internet to anyone willing to pay
 Private cloud – run by a company for the company’s own use
 Hybrid cloud – includes both public and private cloud components
 Software as a Service (SaaS) – one or more applications available via
the Internet (i.e., word processor)
 Platform as a Service (PaaS) – software stack ready for application use
via the Internet (i.e., a database server)
 Infrastructure as a Service (IaaS) – servers or storage available over
Internet (i.e., storage available for backup use)

Operating System Concepts – 9th Edition 1.34 Silberschatz, Galvin and Gagne ©2013
Computing Environments – Real-Time Embedded Systems

 Real-time embedded systems most prevalent form of


computers
 Vary considerable, special purpose, limited purpose
OS, real-time OS
 Use expanding
 Many other special computing environments as well
 Some have OSes, some perform tasks without an OS
 Real-time OS has well-defined fixed time constraints
 Processing must be done within constraint
 Correct operation only if constraints met

Operating System Concepts – 9th Edition 1.35 Silberschatz, Galvin and Gagne ©2013
Open-Source Operating Systems

 Operating systems made available in source-code format rather


than just binary closed-source
 Counter to the copy protection and Digital Rights
Management (DRM) movement
 Started by Free Software Foundation (FSF), which has
“copyleft” GNU Public License (GPL)
 Examples include GNU/Linux and BSD UNIX (including core of
Mac OS X), and many more
 Can use VMM like VMware Player (Free on Windows), Virtualbox
(open source and free on many platforms -
http://www.virtualbox.com)
 Use to run guest operating systems for exploration

Operating System Concepts – 9th Edition 1.36 Silberschatz, Galvin and Gagne ©2013
Chapter 6: Process
Synchronization

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Background

 Concurrent access to shared data may result in data


inconsistency
 Maintaining data consistency requires mechanisms to
ensure the orderly execution of cooperating processes
 Suppose that we wanted to provide a solution to the
consumer-producer problem that fills all the buffers. We
can do so by having an integer count that keeps track of
the number of full buffers. Initially, count is set to 0. It is
incremented by the producer after it produces a new
buffer and is decremented by the consumer after it
consumes a buffer.

Save Load

6.2
The concept of producer-consumer

Producer Consumer
in out

count

6.3
 What to do in the producer if the
buffer is full?
 What to do in the consumer if the
buffer is empty?

6.4
Producer

while (true) {

/* produce an item and put in nextProduced */


while (count == BUFFER_SIZE) Stuck in infinite loop until one item is
; // do nothing consumes

buffer [in] = nextProduced;


in = (in + 1) % BUFFER_SIZE;
count++;
}

count is shared by consumer and producer.


count holds how many data are still not consumed until now.

6.5
Consumer

while (true) {
while (count == 0) Stuck in infinite loop until one item is in
; // do nothing the buffer

nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;

/* consume the item in nextConsumed


}

6.6
Race Condition

 The output of the software depends on a sequence of events, if they


occurs in a wrong order a fault happened.
 Happened if multiple threads operate on a shared data.

Process 1 Process 2

6.7
Race Condition
 count++ could be implemented as

register1 = count Machine language


register1 = register1 + 1
count = register1
 count-- could be implemented as

register2 = count
register2 = register2 - 1
count = register2
 Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = count {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = count {register2 = 5}
S3: consumer execute register2 = register2 - 1 {register2 = 4}
S4: producer execute count = register1 {count = 6 }
S5: consumer execute count = register2 {count = 4}

6.8
Question

 Can you think of a program that may


have a race condition?

6.9
Question

 Can you think of a program that may


have a race condition?

6.10
 General structure of a typical process P

accessing a resource (file,


input or output port, global
data, etc.).

6.11
Solution to Critical-Section Problem

1. Mutual Exclusion - If process Pi is executing in its critical section,


then no other processes can be executing in their critical sections
2. Progress - If no process is executing in its critical section and
there exist some processes that wish to enter their critical section,
then the selection of the processes that will enter the critical
section next cannot be postponed indefinitely
3. Bounded Waiting - A bound must exist on the number of times
that other processes are allowed to enter their critical sections
after a process has made a request to enter its critical section and
before that request is granted

6.12
Peterson’s Solution

 Only two process solution


 Assume that the LOAD and STORE instructions are atomic;
that is, cannot be interrupted.
 The two processes share two variables:
 int turn;
 Boolean flag[2]
 The variable turn indicates whose turn it is to enter the
critical section. if turn == i, then process Pi is allowed to
execute in its critical section.
 The flag array is used to indicate if a process is ready to
enter the critical section. flag[i] = true implies that process Pi
is ready to enter its critical section.

6.13
Algorithm for Process Pi

while (true) {
flag[i] = TRUE;
Give chance to each P; enters its critical section only if either flag[j] == false
another process first turn = j; or turn == i
while ( flag[j] && turn == j);

a process P, can be prevented from entering the


CRITICAL SECTION critical section only if it is stuck in the while loop
with the condition flag [j] == true and turn == j

flag[i] = FALSE;

REMAINDER SECTION

6.14
turn flag

6.15
 How this algorithm satisfies the 3
conditions of critical section solution?

6.16
Synchronization Hardware

 Many systems provide hardware support for critical section


code
 Uniprocessors (single microprocessor used to execute
tasks)– could disable interrupts
 Currently running code would execute without
preemption
 Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
 Modern machines provide special atomic hardware
instructions
 Atomic = non-interruptable When dealing with shared memory
variables we can use these
 Either test memory and set value instructions to execute the instructions
 Or swap contents of two memory as atomic.

6.17
TestAndndSet Instruction

 Definition:

boolean TestAndSet (boolean *target) Pointer which points to the boolean variable

{
boolean rv = *target;
*target = TRUE;
return rv:
}

6.18
Solution using TestAndSet

 All processes shared boolean variable lock., initialized to false which


implies critical section is free now
 Solution:
Make lock true, so all
lock = false;
processes know that there
while (true) {/* for all processes from 1.. n is a process in the CS

while ( TestAndSet (&lock ))


; /* do nothing as another process is in the CS

// critical section

lock = FALSE; /* let another process execute CS

// remainder section

} 6.19
3- while is false, means exit the loop
and enter CS 1-Pi start with testandset(false) 4- Pj testandset(true

lock

2- When target is true, lock is also true 6.20


Swap Instruction

 Definition:

void Swap (boolean *a, boolean *b)


{
boolean temp = *a;
*a = *b;
*b = temp:
}

6.21
Solution using Swap
 Shared Boolean variable lock initialized to FALSE; Each
process has a local Boolean variable key.
 Solution:
while (true) {
key = TRUE;
while ( key == TRUE) Stuck in infinite loop until key is false
Swap (&lock, &key );

// critical section

lock = FALSE;

// remainder section

}
6.22
5- stuck in infinite loop, key will remain true
2- while is true, then swap lock and key.
1- lock is initially false for all processes

a is lock and b is key

4- Pj key is true
and lock is true

lock

3- Then key becomes false and we enter CS 6.23


Chapter 7: Deadlocks

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.2 Silberschatz, Galvin and Gagne ©2013
System Model

 System consists of resources

 Resource types R1, R2, . . ., Rm


CPU cycles, memory space, I/O devices

 Each resource type Ri has W i instances.

 Each process utilizes a resource as follows:


 request
 use
 release

Operating System Concepts – 9th Edition 7.3 Silberschatz, Galvin and Gagne ©2013
Deadlock Characterization

Deadlock can arise if four conditions hold simultaneously.

 Mutual exclusion: only one process at a time can use a


resource

 Hold and wait: a process holding at least one resource is


waiting to acquire additional resources held by other processes

 No preemption: a resource can be released only voluntarily by


the process holding it, after that process has completed its task

 Circular wait: there exists a set {P0, P1, …, Pn} of waiting


processes such that P0 is waiting for a resource that is held by P1,
P1 is waiting for a resource that is held by
P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is
waiting for a resource that is held by P0.

Operating System Concepts – 9th Edition 7.4 Silberschatz, Galvin and Gagne ©2013
 We need a way to present the processes
and the resources in the system.

Operating System Concepts – 9th Edition 7.5 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph

A set of vertices V and a set of edges E.


 V is partitioned into two types:
 P = {P1, P2, …, Pn}, the set consisting of all the processes in
the system

 R = {R1, R2, …, Rm}, the set consisting of all resource types in


the system

 request edge – directed edge Pi  Rj

 assignment edge – directed edge Rj  Pi

Operating System Concepts – 9th Edition 7.6 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph (Cont.)
 Process

 Resource Type with 4 instances

 Pi requests instance of Rj

Pi
Rj
 Pi is holding an instance of Rj

Pi
Rj

Operating System Concepts – 9th Edition 7.7 Silberschatz, Galvin and Gagne ©2013
Question

 Convert the following description into a


resource allocation graph:
 P1 is holding R2 and waiting for R1
 P2 is holding {R1, R2} and waiting for R3
 P3 is holding R3

Operating System Concepts – 9th Edition 7.8 Silberschatz, Galvin and Gagne ©2013
Example of a Resource Allocation Graph

Operating System Concepts – 9th Edition 7.9 Silberschatz, Galvin and Gagne ©2013
How to know if the system is deadlock free?

 Resource-allocation graph contains no cycles, then


the system is not deadlocked.
 Resource category contains more than one instance,
then the presence of a cycle in the resource-allocation
graph indicates the possibility of a deadlock.
 Resource-allocation graph does contain
cycles AND each resource category contains only a
single instance, then a deadlock exists.

Operating System Concepts – 9th Edition 7.10 Silberschatz, Galvin and Gagne ©2013
Summary

 no cycles  no deadlock

 If graph contains a cycle 


 if only one instance per resource type, then deadlock
 if several instances per resource type, possibility of deadlock

Operating System Concepts – 9th Edition 7.11 Silberschatz, Galvin and Gagne ©2013
Resource Allocation Graph With A Deadlock

Is this system
deadlock free?

Operating System Concepts – 9th Edition 7.12 Silberschatz, Galvin and Gagne ©2013
Graph With A Cycle But No Deadlock

Is this system
deadlock free?

Operating System Concepts – 9th Edition 7.13 Silberschatz, Galvin and Gagne ©2013
Methods for Handling Deadlocks

 Ensure that the system will never enter a deadlock state

 Allow the system to enter a deadlock state and then recover, how?

 Ignore the problem and pretend that deadlocks never occur in the
system; used by most operating systems, including Windows and
UNIX. What to do if deadlock happened?

Operating System Concepts – 9th Edition 7.14 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention

Restrain the ways request can be made

 Mutual Exclusion – not required for sharable resources; must


hold for non-sharable resources (e.g., printers).

 Hold and Wait – must guarantee that whenever a process


requests a resource, it does not hold any other resources
 Require process to request and be allocated all its resources
before it begins execution, or allow process to request
resources only when the process has none
 Low resource utilization; starvation possible

Operating System Concepts – 9th Edition 7.15 Silberschatz, Galvin and Gagne ©2013
Deadlock Prevention (Cont.)
 No Preemption – Steps to ensure that
 If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released
 Preempted resources are added to the list of resources for which
the process is waiting
 Process will be restarted only when it can regain its old resources,
as well as the new ones that it is requesting

 Circular Wait – impose a total ordering of all resource types, and


require that each process requests resources in an increasing order of
enumeration

Operating System Concepts – 9th Edition 7.16 Silberschatz, Galvin and Gagne ©2013
 Circular wait
For example: If the set of resource types R includes tape drives, disk
drives, and printers, then a function F might be defined as follows:
F(tape drive)= 1; F(disk drive)= 5; F(Printer)= 12.

A protocol to prevent deadlocks: Each process can request resources


only in an increasing order of enumeration.

Operating System Concepts – 9th Edition 7.17 Silberschatz, Galvin and Gagne ©2013
 Circular wait
For example: If the set of resource types R includes tape drives, disk
drives, and printers, then a function F might be defined as follows:
F(tape drive)= 1; F(disk drive)= 5; F(Printer)= 12.

Assume that a circular wait exists (proof by contradiction).


Let the set of processes involved in the circular wait be {P0, P1, ..., Pn},
where Pi is waiting for a resource Ri , which is held by process Pi+1.
Since process Pi+1 is holding resource Ri while requesting resource Ri+1,
we must have F(Ri) < F(Ri+1) for all i.
But this condition means that F(R0) < F(R1) < ... < F(Rn) < F(R0).
By transitivity, F(R0) < F(R0), which is impossible.
Therefore, there can be no circular wait.
Operating System Concepts – 9th Edition 7.18 Silberschatz, Galvin and Gagne ©2013
Deadlock Avoidance

Requires that the system has some additional a priori information


available

 Simplest and most useful model requires that each process declare
the maximum number of resources of each type that it may need

 The deadlock-avoidance algorithm dynamically examines the


resource-allocation state to ensure that there can never be a
circular-wait condition

 Resource-allocation state is defined by the number of available and


allocated resources, and the maximum demands of the processes

Operating System Concepts – 9th Edition 7.19 Silberschatz, Galvin and Gagne ©2013
Safe State

 When a process requests an available resource, system must decide


if immediate allocation leaves the system in a safe state

 System is in safe state if there exists a sequence <P1, P2, …, Pn> of


ALL the processes in the systems such that for each P i, the
resources that Pi can still request can be satisfied by currently
available resources + resources held by all the Pj, with j < i

 That is:
 If Pi resource needs are not immediately available, then Pi can
wait until all Pj have finished
 When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate
 When Pi terminates, Pi +1 can obtain its needed resources, and so
on

Operating System Concepts – 9th Edition 7.20 Silberschatz, Galvin and Gagne ©2013
 The system knows each process:
 resources max need
 resources currently held.
 Check if there is a safe state by allocating the
needed resources in the future :
Possible future need = Max Need - Current Hold
 Free resources = total resources – total currently held

Operating System Concepts – 9th Edition 7.21 Silberschatz, Galvin and Gagne ©2013
 Is the current allocation safe, if yes what is
the safe sequence?

Operating System Concepts – 9th Edition 7.22 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.23 Silberschatz, Galvin and Gagne ©2013
Total 12 resources available
Is the current allocation safe, if yes
what is the safe sequence?

Operating System Concepts – 9th Edition 7.24 Silberschatz, Galvin and Gagne ©2013
 Even if P1 take the remaining free resources (2) and completes its
execution (release all its 4 resources). P0 and P2 Future need is 5
and 6 which cannot be fullfilled.

Operating System Concepts – 9th Edition 7.25 Silberschatz, Galvin and Gagne ©2013
Basic Facts

 If a system is in safe state  no deadlocks

 If a system is in unsafe state  possibility of deadlock

 Avoidance  ensure that a system will never enter an unsafe state.

Operating System Concepts – 9th Edition 7.26 Silberschatz, Galvin and Gagne ©2013
Avoidance algorithms

 Single instance of a resource type


 Use a resource-allocation graph

 Multiple instances of a resource type


 Use the banker’s algorithm

Operating System Concepts – 9th Edition 7.27 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph Scheme

 Claim edge Pi  Rj indicated that process Pj may request resource


Rj; represented by a dashed line

 Claim edge converts to request edge when a process requests a


resource

 Request edge converted to an assignment edge when the resource


is allocated to the process

 When a resource is released by a process, assignment edge


reconverts to a claim edge

 Resources must be claimed a priori in the system

Operating System Concepts – 9th Edition 7.28 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph

Operating System Concepts – 9th Edition 7.29 Silberschatz, Galvin and Gagne ©2013
Unsafe State In Resource-Allocation Graph

Operating System Concepts – 9th Edition 7.30 Silberschatz, Galvin and Gagne ©2013
Resource-Allocation Graph Algorithm

 Suppose that process Pi requests a resource Rj

 The request can be granted only if converting the request edge to an


assignment edge does not result in the formation of a cycle in the
resource allocation graph

Operating System Concepts – 9th Edition 7.31 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 7.32 Silberschatz, Galvin and Gagne ©2013
Question

 Consider a system consisting of one


resource with four instances that are
shared by three processes, each of which
needs at most two resources. Show that
the system is deadlock free.

Operating System Concepts – 9th Edition 7.33 Silberschatz, Galvin and Gagne ©2013
Chapter 4: Threads

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Single and Multithreaded Processes

Operating System Concepts – 9th Edition 4.2 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 4.3 Silberschatz, Galvin and Gagne ©2013
Benefits
 Responsiveness

 Resource Sharing

 Economy

 Scalability

Operating System Concepts – 9th Edition 4.4 Silberschatz, Galvin and Gagne ©2013
Question
 What is the difference between fork and thread?

Operating System Concepts – 9th Edition 4.5 Silberschatz, Galvin and Gagne ©2013
Multicore Programming
 Multicore systems putting pressure on programmers, challenges include
 Dividing activities
 Balance
 Data splitting
 Data dependency
 Testing and debugging

Operating System Concepts – 9th Edition 4.6 Silberschatz, Galvin and Gagne ©2013
Multithreaded Server Architecture

Operating System Concepts – 9th Edition 4.7 Silberschatz, Galvin and Gagne ©2013
Concurrent Execution on a Single-core System

Operating System Concepts – 9th Edition 4.8 Silberschatz, Galvin and Gagne ©2013
Parallel Execution on a Multicore System

Operating System Concepts – 9th Edition 4.9 Silberschatz, Galvin and Gagne ©2013
User Threads
 Thread management done by user-level threads library

 Three primary thread libraries:


 POSIX Pthreads
 Win32 threads
 Java threads

Operating System Concepts – 9th Edition 4.10 Silberschatz, Galvin and Gagne ©2013
Multithreading Models
 Many-to-One

 One-to-One

 Many-to-Many

Operating System Concepts – 9th Edition 4.11 Silberschatz, Galvin and Gagne ©2013
Many-to-One
 Many user-level threads mapped to single kernel thread
 Examples:
 Solaris Green Threads
 GNU Portable Threads

Operating System Concepts – 9th Edition 4.12 Silberschatz, Galvin and Gagne ©2013
Many-to-One Model

Operating System Concepts – 9th Edition 4.13 Silberschatz, Galvin and Gagne ©2013
One-to-One
 Each user-level thread maps to kernel thread
 Examples
 Windows NT/XP/2000
 Linux
 Solaris 9 and later

Operating System Concepts – 9th Edition 4.14 Silberschatz, Galvin and Gagne ©2013
One-to-one Model

Operating System Concepts – 9th Edition 4.15 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model

 Allows many user level threads to be mapped to many kernel threads


 Allows the operating system to create a sufficient number of kernel threads
 Solaris prior to version 9
 Windows NT/2000 with the ThreadFiber package

Operating System Concepts – 9th Edition 4.16 Silberschatz, Galvin and Gagne ©2013
Many-to-Many Model

Operating System Concepts – 9th Edition 4.17 Silberschatz, Galvin and Gagne ©2013
Question
 Under what circumstances do we use multithreading?

Operating System Concepts – 9th Edition 4.18 Silberschatz, Galvin and Gagne ©2013
Pthreads
 May be provided either as user-level or kernel-level
 A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization
 API specifies behavior of the thread library, implementation is up to development of the
library
 Common in UNIX operating systems (Solaris, Linux, Mac OS X)

Operating System Concepts – 9th Edition 4.19 Silberschatz, Galvin and Gagne ©2013
Java Threads
 Java threads are managed by the JVM

 Typically implemented using the threads model provided by underlying OS

 Java threads may be created by:

 Extending Thread class


 Implementing the Runnable interface

Operating System Concepts – 9th Edition 4.20 Silberschatz, Galvin and Gagne ©2013
Threading Issues

 Semantics of fork() and exec() system calls


 Thread cancellation of target thread
 Asynchronous or deferred
 Signal handling

Operating System Concepts – 9th Edition 4.21 Silberschatz, Galvin and Gagne ©2013
Semantics of fork() and exec()
 Does fork() duplicate only the calling thread or all threads?

Operating System Concepts – 9th Edition 4.22 Silberschatz, Galvin and Gagne ©2013
Thread Cancellation

 Terminating a thread before it has finished


 Two general approaches:
 Asynchronous cancellation terminates the target thread immediately
 Deferred cancellation allows the target thread to periodically check if it should be
cancelled

Operating System Concepts – 9th Edition 4.23 Silberschatz, Galvin and Gagne ©2013
Signal Handling

 Signals are used in UNIX systems to notify a process that a particular event has occurred
 A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled
 Options:
 Deliver the signal to the thread to which the signal applies
 Deliver the signal to every thread in the process
 Deliver the signal to certain threads in the process
 Assign a specific thread to receive all signals for the process

Operating System Concepts – 9th Edition 4.24 Silberschatz, Galvin and Gagne ©2013
 Online compiler that support threads
https://rextester.com/AWD84754

Operating System Concepts – 9th Edition 4.25 Silberschatz, Galvin and Gagne ©2013
Chapter 8: Main Memory

Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Background

 Program must be brought (from disk) into memory and placed within a
process for it to be run

 Main memory and registers are only storage CPU can access directly

 Register access in one CPU clock (or less)

 Main memory can take many cycles

 Cache sits between main memory and CPU registers

 Protection of memory required to ensure correct operation

Operating System Concepts – 8th Edition 8.2 Silberschatz, Galvin and Gagne ©2009
Base and Limit Registers
 A pair of base and limit registers define the logical address space

Operating System Concepts – 8th Edition 8.3 Silberschatz, Galvin and Gagne ©2009
Logical vs. Physical Address Space

 The concept of a logical address space that is bound to a separate


physical address space is central to proper memory management
 Logical address – generated by the CPU; also referred to as
virtual address
 Physical address – address seen by the memory unit

 Logical and physical addresses are the same in compile-time and load-
time address-binding schemes; logical (virtual) and physical addresses
differ in execution-time address-binding scheme

Operating System Concepts – 8th Edition 8.4 Silberschatz, Galvin and Gagne ©2009
Question

 Why we have logical and physical


addresses?

Operating System Concepts – 8th Edition 8.5 Silberschatz, Galvin and Gagne ©2009
Memory-Management Unit (MMU)

 Hardware device that maps virtual to physical address

 In MMU scheme, the value in the relocation register is added to every


address generated by a user process at the time it is sent to memory

 The user program deals with logical addresses; it never sees the real
physical addresses

Operating System Concepts – 8th Edition 8.6 Silberschatz, Galvin and Gagne ©2009
Dynamic relocation using a
relocation register

Operating System Concepts – 8th Edition 8.7 Silberschatz, Galvin and Gagne ©2009
Swapping
 A process can be swapped temporarily out of memory to a backing
store, and then brought back into memory for continued execution

 Backing store – fast disk large enough to accommodate copies of all


memory images for all users; must provide direct access to these
memory images

 Roll out, roll in – swapping variant used for priority-based scheduling


algorithms; lower-priority process is swapped out so higher-priority
process can be loaded and executed

 Major part of swap time is transfer time; total transfer time is directly
proportional to the amount of memory swapped

 Modified versions of swapping are found on many systems (i.e., UNIX,


Linux, and Windows)

 System maintains a ready queue of ready-to-run processes which


have memory images on disk

Operating System Concepts – 8th Edition 8.8 Silberschatz, Galvin and Gagne ©2009
Schematic View of Swapping

Operating System Concepts – 8th Edition 8.9 Silberschatz, Galvin and Gagne ©2009
Contiguous Allocation

 Main memory usually into two partitions:


 Resident operating system, usually held in low memory with
interrupt vector
 User processes then held in high memory

 Relocation registers used to protect user processes from each other,


and from changing operating-system code and data
 Base register contains value of smallest physical address
 Limit register contains range of logical addresses – each logical
address must be less than the limit register
 MMU maps logical address dynamically

Operating System Concepts – 8th Edition 8.10 Silberschatz, Galvin and Gagne ©2009
Hardware Support for Relocation
and Limit Registers

Operating System Concepts – 8th Edition 8.11 Silberschatz, Galvin and Gagne ©2009
Contiguous Allocation (Cont.)

 Multiple-partition allocation
 Hole – block of available memory; holes of various size are
scattered throughout memory
 When a process arrives, it is allocated memory from a hole large
enough to accommodate it
 Operating system maintains information about:
a) allocated partitions b) free partitions (hole)

OS OS OS OS

process 5 process 5 process 5 process 5


process 9 process 9

process 8 process 10

process 2 process 2 process 2 process 2

Operating System Concepts – 8th Edition 8.12 Silberschatz, Galvin and Gagne ©2009
Dynamic Storage-Allocation Problem

How to satisfy a request of size n from a list of free holes

 First-fit: Allocate the first hole that is big enough

 Best-fit: Allocate the smallest hole that is big enough; must search
entire list, unless ordered by size
 Produces the smallest leftover hole

 Worst-fit: Allocate the largest hole; must also search entire list
 Produces the largest leftover hole

First-fit and best-fit better than worst-fit in terms of speed and storage
utilization

Operating System Concepts – 8th Edition 8.13 Silberschatz, Galvin and Gagne ©2009
Dynamic Storage-Allocation Problem
(cont.)
 Exercise:
Given memory partitions of 100kb, 500kb, 200kb,
300kb and 600kb (in order), how would each of first
fit, best fit and worst fit algorithms place processes
of 212kb, 417kb, 112kb, and 426kb (in order)? Which
algorithm makes the most efficient use of memory?

Operating System Concepts – 8th Edition 8.14 Silberschatz, Galvin and Gagne ©2009
Fragmentation

 External Fragmentation – total memory space exists to satisfy a


request, but it is not contiguous

 Internal Fragmentation – allocated memory may be slightly larger


than requested memory; this size difference is memory internal to a
partition, but not being used

 Reduce external fragmentation by compaction


 Shuffle memory contents to place all free memory together in one
large block
 Compaction is possible only if relocation is dynamic, and is done at
execution time

Operating System Concepts – 8th Edition 8.15 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 8.16 Silberschatz, Galvin and Gagne ©2009
Paging
 Logical address space of a process can be noncontiguous; process is
allocated physical memory whenever the latter is available

 Divide physical memory into fixed-sized blocks called frames (size is


power of 2, between 512 bytes and 8,192 bytes)

 Divide logical memory into blocks of same size called pages

 Keep track of all free frames

 To run a program of size n pages, need to find n free frames and load
program

 Set up a page table to translate logical to physical addresses

 Internal fragmentation

Operating System Concepts – 8th Edition 8.17 Silberschatz, Galvin and Gagne ©2009
Address Translation Scheme

 Address generated by CPU is divided into:


 Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
 Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit
page number page offset
p d
m-n n

 For given logical address space 2m and page size 2n

Operating System Concepts – 8th Edition 8.18 Silberschatz, Galvin and Gagne ©2009
You need to know

 How to calculate page size?


 How to calculate frame size?
 How do we know the number of pages
needed for a process?
 How do we know how many bits needed
for offset?
 How do we know how many bits needed
for page number?

Operating System Concepts – 8th Edition 8.19 Silberschatz, Galvin and Gagne ©2009
Paging Hardware

Operating System Concepts – 8th Edition 8.20 Silberschatz, Galvin and Gagne ©2009
Paging Model of Logical and
Physical Memory

How the page table is created

Operating System Concepts – 8th Edition 8.21 Silberschatz, Galvin and Gagne ©2009
Example
 4-byte page size.
 32-byte physical memory.
 logical-address space is 16.

 Convert the Logical address into physical address


 Logical address 0
 Logical address 3
 Logical address 13

Operating System Concepts – 8th Edition 8.22 Silberschatz, Galvin and Gagne ©2009
Paging Example

32-byte memory and 4-byte pages


Operating System Concepts – 8th Edition 8.23 Silberschatz, Galvin and Gagne ©2009
Example
 m= 4 (logical address bits), n= 2 (page size bits).
 Physical = Frame # * page Size + Offset
 Page number p = m-n = 4-2 = 2, Offset d = n = 2
 Logical address 0 (0000) is page 0, offset 0.
By page table we find that page 0 is in frame 5.
Thus logical address 0 maps to physical address 5*4+0 = 20
 Logical address 3 (0011) is page 0, offset 3 maps to physical
address 5*4 + 3 = 23
 Logical address 13 (1101) is page 3 (is in frame 2), offset 1 maps
to physical address 2*4+1 = 9

Operating System Concepts – 8th Edition 8.24 Silberschatz, Galvin and Gagne ©2009
Operating System Concepts – 8th Edition 8.25 Silberschatz, Galvin and Gagne ©2009
Questions

 What is the physical address of the


following logical addresses:
b (1)
g (6)
k (10)
O (14)

Operating System Concepts – 8th Edition 8.26 Silberschatz, Galvin and Gagne ©2009
Questions

 A certain computer provides its users with


a virtual memory space of 232 bytes. The
computer has 218 bytes of physical
memory. The virtual memory is
implemented by paging, and the page size
is 4K bytes. A user process generated the
virtual address 11123456.
Explain how the system establishes the
corresponding physical location.

Operating System Concepts – 8th Edition 8.27 Silberschatz, Galvin and Gagne ©2009
Free Frames

Before allocation After allocation

Operating System Concepts – 8th Edition 8.28 Silberschatz, Galvin and Gagne ©2009
Implementation of Page Table

 Page table is kept in main memory

 Page-table base register (PTBR) points to the page table

 Page-table length register (PRLR) indicates size of the page table

 In this scheme every data/instruction access requires two memory


accesses. One for the page table and one for the data/instruction.

 The two memory access problem can be solved by the use of a special
fast-lookup hardware cache called associative memory or translation
look-aside buffers (TLBs)

 Some TLBs store address-space identifiers (ASIDs) in each TLB


entry – uniquely identifies each process to provide address-space
protection for that process

Operating System Concepts – 8th Edition 8.29 Silberschatz, Galvin and Gagne ©2009
Associative Memory

 Associative memory – parallel search


Page # Frame #

Address translation (p, d)


 If p is in associative register, get frame # out
 Otherwise get frame # from page table in memory

Operating System Concepts – 8th Edition 8.30 Silberschatz, Galvin and Gagne ©2009
Paging Hardware With TLB

Operating System Concepts – 8th Edition 8.31 Silberschatz, Galvin and Gagne ©2009
TLB Example
 TLB size =2 entries, assume
TLB is empty
 Page Size = 4 bytes
 Logical address space =16 bytes
 Physical address space = 64 bytes
 Convert the following logical addresses into physical
addresses, and identify if we have a TLB hit or a TLB
miss:
 10
 14
 1
Operating System Concepts – 8th Edition 8.32 Silberschatz, Galvin and Gagne ©2009
Question
 Suppose a logical address space is 1KB, and the page-size is 16 bytes.
Suppose TLB has two entries. Both TLB and page table are empty initially.
FIFO is used as TLB replacement algorithm. Suppose the following logical
address is accessed in order: 0x3d,0x30,0xe5,0x7d, 0x33, 0xef. For each of
the following address access:
 specify the physical address that is mapped to it.
 Suppose it takes 10 ns to access TLB, and 100ns to access memory,
and 8ms to handle page fault. How long time it takes to access this
address?

Operating System Concepts – 8th Edition 8.33 Silberschatz, Galvin and Gagne ©2009
Segmentation

 Memory-management scheme that supports user view of memory

 A program is a collection of segments


 A segment is a logical unit such as:
main program
procedure
function
method
object
local variables, global variables
common block
stack
symbol table
arrays

Operating System Concepts – 8th Edition 8.34 Silberschatz, Galvin and Gagne ©2009
User’s View of a Program

Operating System Concepts – 8th Edition 8.35 Silberschatz, Galvin and Gagne ©2009
Logical View of Segmentation

4
1

3 2
4

user space physical memory space

Operating System Concepts – 8th Edition 8.36 Silberschatz, Galvin and Gagne ©2009
Segmentation Architecture
 Logical address consists of a two tuple:
<segment-number, offset>,

 Segment table – maps two-dimensional physical addresses; each


table entry has:
 base – contains the starting physical address where the segments
reside in memory
 limit – specifies the length of the segment

 Segment-table base register (STBR) points to the segment table’s


location in memory

 Segment-table length register (STLR) indicates number of segments


used by a program;
segment number s is legal if s < STLR

Operating System Concepts – 8th Edition 8.37 Silberschatz, Galvin and Gagne ©2009
Example of Segmentation

Operating System Concepts – 8th Edition 8.38 Silberschatz, Galvin and Gagne ©2009
Segmentation Hardware

Operating System Concepts – 8th Edition 8.39 Silberschatz, Galvin and Gagne ©2009
Question

 What is the physical address for the


following logical addresses:
 <0, 100>
 <3, 1300>
 <10, 300>

Operating System Concepts – 8th Edition 8.40 Silberschatz, Galvin and Gagne ©2009
Chapter 9: Virtual-Memory
Management

Operating System Concepts Essentials – 9th Edition Silberschatz, Galvin and Gagne ©2013
Background
 Code needs to be in memory to execute, but entire program rarely used
 Error code, unusual routines, large data structures
 Entire program code not needed at same time
 Consider ability to execute partially-loaded program
 Program no longer constrained by limits of physical memory
 Program and programs could be larger than physical memory

Operating System Concepts Essentials – 9th Edition 9.2 Silberschatz, Galvin and Gagne ©2013
Background
 Virtual memory – separation of user logical memory from physical memory
 Only part of the program needs to be in memory for execution
 Logical address space can therefore be much larger than physical address
space
 Allows address spaces to be shared by several processes
 Allows for more efficient process creation
 More programs running concurrently
 Less I/O needed to load or swap processes

 Virtual memory can be implemented via:


 Demand paging
 Demand segmentation

Operating System Concepts Essentials – 9th Edition 9.3 Silberschatz, Galvin and Gagne ©2013
Question

 What is virtual, logical, and physical memory?

Operating System Concepts Essentials – 9th Edition 9.4 Silberschatz, Galvin and Gagne ©2013
Virtual Memory That is
Larger Than Physical Memory

Operating System Concepts Essentials – 9th Edition 9.5 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts Essentials – 9th Edition 9.6 Silberschatz, Galvin and Gagne ©2013
Stack and Heap

Image source: vikashazrati.wordpress.com

Operating System Concepts Essentials – 9th Edition 9.7 Silberschatz, Galvin and Gagne ©2013
Virtual-address Space

Operating System Concepts Essentials – 9th Edition 9.8 Silberschatz, Galvin and Gagne ©2013
Shared Library Using Virtual Memory

Operating System Concepts Essentials – 9th Edition 9.9 Silberschatz, Galvin and Gagne ©2013
Demand Paging
 Could bring entire process into memory at load time
 Or bring a page into memory only when it is needed
 Less I/O needed, no unnecessary I/O
 Less memory needed
 Faster response
 More users

 Page is needed  reference to it


 invalid reference  abort
 not-in-memory  bring to memory

 Lazy swapper – never swaps a page into memory unless page will be needed
 Swapper that deals with pages is a pager

Operating System Concepts Essentials – 9th Edition 9.10 Silberschatz, Galvin and Gagne ©2013
Transfer of a Paged Memory to
Contiguous Disk Space

Operating System Concepts Essentials – 9th Edition 9.11 Silberschatz, Galvin and Gagne ©2013
Valid-Invalid Bit
 With each page table entry a valid–invalid bit is associated
(v  in-memory – memory resident, i  not-in-memory)
 Initially valid–invalid bit is set to i on all entries
 Example of a page table snapshot:
Frame # valid-invalid bit
v
v
v
v
i

….

i
i
page table

 During address translation, if valid–invalid bit in page table entry


is i  page fault
Operating System Concepts Essentials – 9th Edition 9.12 Silberschatz, Galvin and Gagne ©2013
Page Table When Some Pages
Are Not in Main Memory

Disk

Main memory
Operating System Concepts Essentials – 9th Edition 9.13 Silberschatz, Galvin and Gagne ©2013
Page Fault

 If there is a reference to a page, first reference to that page will trap to operating
system:
page fault
1. Operating system looks at another table to decide:
 Invalid reference  abort
Just not in memory

2. Get empty frame
3. Swap page into frame via scheduled disk operation
4. Reset tables to indicate page now in memory
Set validation bit = v
5. Restart the instruction that caused the page fault

Operating System Concepts Essentials – 9th Edition 9.14 Silberschatz, Galvin and Gagne ©2013
Aspects of Demand Paging
 Extreme case – start process with no pages in memory
 OS sets instruction pointer to first instruction of process, non-memory-
resident -> page fault
 And for every other process pages on first access
 Pure demand paging
 Actually, a given instruction could access multiple pages ->
multiple page faults
 Pain decreased because of locality of reference
 Hardware support needed for demand paging
 Page table with valid / invalid bit
 Secondary memory (swap device with swap space)
 Instruction restart
Operating System Concepts Essentials – 9th Edition 9.15 Silberschatz, Galvin and Gagne ©2013
Instruction Restart
 Example
 C= A + B
 Instructions:
 Fetch and decode instruction Add
 Fetch A
 Fetch B
 Add A and B
 Store the sum in C

Operating System Concepts Essentials – 9th Edition 9.16 Silberschatz, Galvin and Gagne ©2013
Question

 Why a page fault happened?

Operating System Concepts Essentials – 9th Edition 9.17 Silberschatz, Galvin and Gagne ©2013
Steps in Handling a Page Fault

Operating System Concepts Essentials – 9th Edition 9.18 Silberschatz, Galvin and Gagne ©2013
Performance of Demand Paging
 Stages in Demand Paging
1. Trap to the operating system
2. Save the user registers and process state
3. Determine that the interrupt was a page fault
4. Check that the page reference was legal and determine the location of the page on the disk
5. Issue a read from the disk to a free frame in main memory:
1. Wait in a queue for this device until the read request is serviced
2. Wait for the device seek and/or latency time
3. Begin the transfer of the page to a free frame
6. While waiting, allocate the CPU to some other user
7. Receive an interrupt from the disk I/O subsystem (I/O completed)
8. Save the registers and process state for the other user
9. Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in memory
11. Wait for the CPU to be allocated to this process again
12. Restore the user registers, process state, and new page table, and then resume the interrupted
instruction
Operating System Concepts Essentials – 9th Edition 9.19 Silberschatz, Galvin and Gagne ©2013
Performance of Demand Paging (Cont.)

 Page Fault Rate 0p1


 if p = 0 no page faults
 if p = 1, every reference is a fault

 Effective Access Time (EAT)


EAT = (1 – p) x memory access
+ p (page fault overhead
+ swap page out
+ swap page in
+ restart overhead
)
Operating System Concepts Essentials – 9th Edition 9.20 Silberschatz, Galvin and Gagne ©2013
Demand Paging Example
 Memory access time = 200 nanoseconds
 Average page-fault service time = 8 milliseconds

 EAT = (1 – p) x 200 + p (8 milliseconds)


= (1 – p x 200 + p x 8,000,000
= 200 + p x 7,999,800
 If one access out of 1,000 causes a page fault, then
EAT = 8.2 microseconds.
This is a slowdown by a factor of 40!!
 If want performance degradation < 10 percent
 220 > 200 + 7,999,800 x p
20 > 7,999,800 x p
 p < .0000025
 < one page fault in every 400,000 memory accesses

Operating System Concepts Essentials – 9th Edition 9.21 Silberschatz, Galvin and Gagne ©2013
Question
 Consider the page table below (all numbers are in hex) and
assume 1 KB page size. Assume 16-bit logical address and 20-
bit physical address.
 What are the page numbers and offsets for the following logical
addresses:
 1CC4
 0A98
 17FC
 Provide the physical address for each address

Operating System Concepts Essentials – 9th Edition 9.22 Silberschatz, Galvin and Gagne ©2013
What Happens if There is no Free Frame?

 Used up by process pages


 Also in demand from the kernel, I/O buffers, etc
 How much to allocate to each?

 Page replacement – find some page in memory, but not really in use, page it out
 Algorithm – terminate? swap out? replace the page?
 Performance – want an algorithm which will result in minimum number of
page faults

 Same page may be brought into memory several times

Operating System Concepts Essentials – 9th Edition 9.23 Silberschatz, Galvin and Gagne ©2013
Page Replacement
 Prevent over-allocation of memory by modifying page-fault service routine to
include page replacement

 Use modify (dirty) bit to reduce overhead of page transfers – only modified
pages are written to disk

 Page replacement completes separation between logical memory and physical


memory – large virtual memory can be provided on a smaller physical memory

Operating System Concepts Essentials – 9th Edition 9.24 Silberschatz, Galvin and Gagne ©2013
Need For Page Replacement

Operating System Concepts Essentials – 9th Edition 9.25 Silberschatz, Galvin and Gagne ©2013
Basic Page Replacement

1. Find the location of the desired page on disk

2. Find a free frame:


- If there is a free frame, use it
- If there is no free frame, use a page replacement algorithm to select a
victim frame
- Write victim frame to disk if dirty

3. Bring the desired page into the (newly) free frame; update the page and frame
tables

4. Continue the process by restarting the instruction that caused the trap

Note now potentially 2 page transfers for page fault – increasing EAT

Operating System Concepts Essentials – 9th Edition 9.26 Silberschatz, Galvin and Gagne ©2013
Page Replacement

Operating System Concepts Essentials – 9th Edition 9.27 Silberschatz, Galvin and Gagne ©2013
Page and Frame Replacement Algorithms

 Frame-allocation algorithm determines


 How many frames to give each process
 Which frames to replace
 Page-replacement algorithm
 Want lowest page-fault rate on both first access and re-access

 Evaluate algorithm by running it on a particular string of memory references


(reference string) and computing the number of page faults on that string
 String is just page numbers, not full addresses
 Repeated access to the same page does not cause a page fault
 In all our examples, the reference string is
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1

Operating System Concepts Essentials – 9th Edition 9.28 Silberschatz, Galvin and Gagne ©2013
First-In-First-Out (FIFO) Algorithm
 Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
 3 frames (3 pages can be in memory at a time per process)

1 7 2 4 0 7

2 0 3 2 1 0 15 page faults

3 1 0 3 2 1

 How to track ages of pages?


 Just use a FIFO queue

Operating System Concepts Essentials – 9th Edition 9.29 Silberschatz, Galvin and Gagne ©2013
FIFO Page Replacement

Operating System Concepts Essentials – 9th Edition 9.30 Silberschatz, Galvin and Gagne ©2013
First-In-First-Out (FIFO) Algorithm
 Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5
 Adding more frames can cause more page faults!
 Belady’s Anomaly
3 frames (9 page faults)

4 frames (10 page faults)

Operating System Concepts Essentials – 9th Edition 9.31 Silberschatz, Galvin and Gagne ©2013
Optimal Algorithm
 Replace page that will not be used for longest period of time
 9 is optimal for the example on the next slide

 How do you know this?


 Can’t read the future

 Used for measuring how well your algorithm performs

Operating System Concepts Essentials – 9th Edition 9.32 Silberschatz, Galvin and Gagne ©2013
Optimal Page Replacement

Operating System Concepts Essentials – 9th Edition 9.33 Silberschatz, Galvin and Gagne ©2013
Least Recently Used (LRU) Algorithm

 Use past knowledge rather than future


 Replace page that has not been used in the most amount of time
 Associate time of last use with each page
In this example we will be looking to the last 3 different accesses and
replace the oldest one

 12 faults – better than FIFO but worse than OPT


 Generally good algorithm and frequently used
 But how to implement?
Operating System Concepts Essentials – 9th Edition 9.34 Silberschatz, Galvin and Gagne ©2013
LRU Algorithm (Cont.)
 Counter implementation
 Every page entry has a counter; every time page is referenced through this
entry, copy the clock into the counter
 When a page needs to be changed, look at the counters to find smallest
value
 Search through table needed
 Stack implementation
 Keep a stack of page numbers in a double link form:
 Page referenced:
 move it to the top
 requires 6 pointers to be changed
 But each update more expensive
 No search for replacement
 LRU and OPT are cases of stack algorithms that don’t have Belady’s
Anomaly
Operating System Concepts Essentials – 9th Edition 9.35 Silberschatz, Galvin and Gagne ©2013
Use Of A Stack to Record The
Most Recent Page References

Move 7 to the
top of stack

Operating System Concepts Essentials – 9th Edition 9.36 Silberschatz, Galvin and Gagne ©2013
Chapter 5: Process
Scheduling

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 5: Process Scheduling
 Basic Concepts
 Scheduling Criteria
 Scheduling Algorithms
 Thread Scheduling
 Multiple-Processor Scheduling
 Real-Time CPU Scheduling
 Operating Systems Examples

Operating System Concepts – 9th Edition 6.2 Silberschatz, Galvin and Gagne ©2013
Basic Concepts
 Maximum CPU utilization obtained with
multiprogramming
 CPU–I/O Burst Cycle – Process execution consists
of a cycle of CPU execution and I/O wait
 CPU burst followed by I/O burst
 CPU burst distribution is of main concern

Operating System Concepts – 9th Edition 6.3 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
 Short-term scheduler selects from among the processes in ready queue, and allocates the CPU
to one of them
 Queue may be ordered in various ways
 CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
 Scheduling under 1 and 4 is nonpreemptive
 Scheduling under 2 and 3 is preemptive

Operating System Concepts – 9th Edition 6.4 Silberschatz, Galvin and Gagne ©2013
Dispatcher
 Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves:
 switching context
 switching to user mode
 jumping to the proper location in the user program to restart that program
 Dispatch latency – time it takes for the dispatcher to stop one process and start another
running

Operating System Concepts – 9th Edition 6.5 Silberschatz, Galvin and Gagne ©2013
Scheduling Criteria
 CPU utilization – keep the CPU as busy as possible
 Throughput – # of processes that complete their execution per time unit
 Turnaround time – amount of time to execute a particular process
 Waiting time – amount of time a process has been waiting in the ready queue
 Response time – amount of time it takes from when a request was submitted until the first
response is produced, not output (for time-sharing environment)

Operating System Concepts – 9th Edition 6.6 Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm Optimization Criteria

 Max CPU utilization


 Max throughput
 Min turnaround time
 Min waiting time
 Min response time

Operating System Concepts – 9th Edition 6.7 Silberschatz, Galvin and Gagne ©2013
Question
 Why we need to use scheduling algorithm in the OS, while we
are executing only one process in the CPU?

Operating System Concepts – 9th Edition 6.8 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3
0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17

Operating System Concepts – 9th Edition 6.9 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
 The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30
 Waiting time for P1 = 6; P2 = 0; P3 = 3
 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Convoy effect - short process behind long process

Operating System Concepts – 9th Edition 6.10 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF) Scheduling
 Associate with each process the length of its next CPU burst
 Use these lengths to schedule the process with the shortest time
 SJF is optimal – gives minimum average waiting time for a given set of processes
 The difficulty is knowing the length of the next CPU request

Operating System Concepts – 9th Edition 6.11 Silberschatz, Galvin and Gagne ©2013
Example of SJF
ProcessArriva l Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

Operating System Concepts – 9th Edition 6.12 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling
 A priority number (integer) is associated with each process

 The CPU is allocated to the process with the highest priority (smallest integer  highest priority)
 Preemptive
 Nonpreemptive

 SJF is priority scheduling where priority is the inverse of predicted next CPU burst time

 Problem  Starvation – low priority processes may never execute

 Solution  Aging – as time progresses increase the priority of the process

Operating System Concepts – 9th Edition 6.13 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

 Priority scheduling Gantt Chart

P1 P2 P1 P3 P4
0 1 6 16 18 19

 Average waiting time = 8.2 msec

Operating System Concepts – 9th Edition 6.14 Silberschatz, Galvin and Gagne ©2013
Round Robin (RR)
 Each process gets a small unit of CPU time (time quantum q), usually 10-100 milliseconds. After
this time has elapsed, the process is preempted and added to the end of the ready queue.
 If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n
of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time
units.
 Timer interrupts every quantum to schedule next process
 Performance
 q large  FIFO
 q small  q must be large with respect to context switch, otherwise overhead is too high

Operating System Concepts – 9th Edition 6.15 Silberschatz, Galvin and Gagne ©2013
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
 The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

 Typically, higher average turnaround than SJF, but better response


 q should be large compared to context switch time
 q usually 10ms to 100ms, context switch < 10 usec

Operating System Concepts – 9th Edition 6.16 Silberschatz, Galvin and Gagne ©2013
Time Quantum and Context Switch Time

Operating System Concepts – 9th Edition 6.17 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With
The Time Quantum

80% of CPU bursts should


be shorter than q

Operating System Concepts – 9th Edition 6.18 Silberschatz, Galvin and Gagne ©2013
Question
 What is the best value for the quantum time; very large or very
small?

Operating System Concepts – 9th Edition 6.19 Silberschatz, Galvin and Gagne ©2013
Thread Scheduling

 Distinction between user-level and kernel-level threads

 When threads supported, threads scheduled, not processes

 Many-to-one and many-to-many models, thread library schedules user-level threads to run on Lightweight
processes (LWP)
 Known as process-contention scope (PCS) since scheduling competition is within the process
 Typically done via priority set by programmer

 Kernel thread scheduled onto available CPU is system-contention scope (SCS) – competition among all
threads in system

Operating System Concepts – 9th Edition 6.20 Silberschatz, Galvin and Gagne ©2013
Chapter 3: Processes

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 3: Processes
 Process Concept
 Process Scheduling
 Operations on Processes
 Interprocess Communication

Operating System Concepts – 9th Edition 3.2 Silberschatz, Galvin and Gagne ©2013
Process Concept

 Process – a program in execution; process execution must


progress in sequential fashion
 Process Multiple parts
 text section: The program code.
 Current activity Section: including program counter,
processor registers
 Stack section: containing temporary data
 Function parameters, return addresses, local
variables
 Data section: containing global variables
 Heap section: containing memory dynamically allocated
during run time

Operating System Concepts – 9th Edition 3.3 Silberschatz, Galvin and Gagne ©2013
Process Concept (Cont.)
 Program is passive entity stored on disk (executable file),
process is active
 Program becomes process when executable file loaded into
memory
 One program can be several processes
 Consider multiple users executing the same program

Operating System Concepts – 9th Edition 3.4 Silberschatz, Galvin and Gagne ©2013
Process in Memory

Operating System Concepts – 9th Edition 3.5 Silberschatz, Galvin and Gagne ©2013
Process State
 As a process executes, it changes state
 new: The process is being created
 running: Instructions are being executed
 waiting: The process is waiting for some event to occur
 ready: The process is waiting to be assigned to a processor
 terminated: The process has finished execution

Diagram of Process State

Operating System Concepts – 9th Edition 3.6 Silberschatz, Galvin and Gagne ©2013
Process Control Block (PCB)
Information associated with each process
(also called task control block)
 Process state – running, waiting, etc
 Program counter – location of
instruction to next execute
 CPU registers – contents of all process-
centric registers
 CPU scheduling information- priorities,
scheduling queue pointers
 Memory-management information –
memory allocated to the process
 Accounting information – CPU used,
clock time elapsed since start, time
limits
 I/O status information – I/O devices
allocated to process, list of open files

Operating System Concepts – 9th Edition 3.7 Silberschatz, Galvin and Gagne ©2013
CPU Switch From Process to Process

Operating System Concepts – 9th Edition 3.8 Silberschatz, Galvin and Gagne ©2013
Question

 What to we mean by save and reload the


process state in the case of context
switch?

Operating System Concepts – 9th Edition 3.9 Silberschatz, Galvin and Gagne ©2013
Process Scheduling

 Maximize CPU use, quickly switch processes onto CPU for


time sharing
 Process scheduler selects among available processes for
next execution on CPU
 Maintains scheduling queues of processes
 Job queue – set of all processes in the system
 Ready queue – set of all processes residing in main
memory, ready and waiting to execute
 Device queues – set of processes waiting for an I/O device
 Processes migrate among the various queues

Operating System Concepts – 9th Edition 3.10 Silberschatz, Galvin and Gagne ©2013
Ready Queue And Various I/O Device Queues

Operating System Concepts – 9th Edition 3.11 Silberschatz, Galvin and Gagne ©2013
Representation of Process Scheduling

 Queueing diagram represents queues, resources, flows

Operating System Concepts – 9th Edition 3.12 Silberschatz, Galvin and Gagne ©2013
Schedulers
 Short-term scheduler (or CPU scheduler) – selects which process should
be executed next and allocates CPU
 Sometimes the only scheduler in a system
 Short-term scheduler is invoked frequently (milliseconds)  (must be
fast)
 Long-term scheduler (or job scheduler) – selects which processes should
be brought into the ready queue
 Long-term scheduler is invoked infrequently (seconds, minutes) 
(may be slow)
 The long-term scheduler controls the degree of multiprogramming
 Processes can be described as either:
 I/O-bound process – spends more time doing I/O than computations,
many short CPU bursts
 CPU-bound process – spends more time doing computations; few very
long CPU bursts
 Long-term scheduler strives for good process mix

Operating System Concepts – 9th Edition 3.13 Silberschatz, Galvin and Gagne ©2013
Addition of Medium Term Scheduling
 Medium-term scheduler can be added if degree of multiple
programming needs to decrease
 Remove process from memory, store on disk, bring back in
from disk to continue execution: swapping

Operating System Concepts – 9th Edition 3.14 Silberschatz, Galvin and Gagne ©2013
Context Switch
 When CPU switches to another process, the system must save
the state of the old process and load the saved state for the
new process via a context switch
 Context of a process represented in the PCB
 Context-switch time is overhead; the system does no useful
work while switching
 The more complex the OS and the PCB  the longer the
context switch
 Time dependent on hardware support
 Some hardware provides multiple sets of registers per CPU
 multiple contexts loaded at once

Operating System Concepts – 9th Edition 3.15 Silberschatz, Galvin and Gagne ©2013
Operations on Processes

 System must provide mechanisms for:


 process creation.
 process execution.
 process termination.

Operating System Concepts – 9th Edition 3.16 Silberschatz, Galvin and Gagne ©2013
Process Creation & Exec.
 Parent process create children processes, which, in turn create
other processes, forming a tree of processes
 Generally, process identified and managed via a process
identifier (pid)
 Resource sharing options
 Parent and children share all resources
 Children share subset of parent’s resources
 Parent and child share no resources
 Execution options
 Parent and children execute concurrently
 Parent waits until children terminate
 Address space
 Child duplicate of parent
 Child has a program loaded into it

Operating System Concepts – 9th Edition 3.17 Silberschatz, Galvin and Gagne ©2013
Process Creation & exec. (Cont.)
 UNIX examples
 fork() creates new process
 exec() used after a fork() to execute the process code.

Operating System Concepts – 9th Edition 3.18 Silberschatz, Galvin and Gagne ©2013
C Programming intro

Online C compiler supports fork

https://www.onlinegdb.com/fork/r1rLnO-kb

Operating System Concepts – 9th Edition 3.19 Silberschatz, Galvin and Gagne ©2013
Hello World

#include<stdio.h>
int main()
{
printf("Hello, World!");
return0;
}

Operating System Concepts – 9th Edition 3.20 Silberschatz, Galvin and Gagne ©2013
C Program Forking Separate Process
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}

Operating System Concepts – 9th Edition 3.21 Silberschatz, Galvin and Gagne ©2013
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
printf( "The process identifier (pid) of the parent process is %d\n", (int)getpid() );
/* fork another process */
int pid = fork();
if ( pid == 0 ) {
printf( "After the fork, the process identifier (pid) " "of the child is %d\n", (int)getpid() );
} else {
printf( "After the fork, the process identifier (pid) " "of the parent is still %d\n - fork()
returned %d\n", (int)getpid(), pid );
}
return 0;
}

Operating System Concepts – 9th Edition 3.22 Silberschatz, Galvin and Gagne ©2013
A tree of processes on a typical Solaris

Operating System Concepts – 9th Edition 3.23 Silberschatz, Galvin and Gagne ©2013
Question

 Why do we need fork to create new


processes?

Operating System Concepts – 9th Edition 3.24 Silberschatz, Galvin and Gagne ©2013
Process Termination

 Process executes last statement and then asks the operating


system to delete it using the exit().
 Returns status data from child to parent .
 Process’ resources are deallocated by operating system
 Parent may terminate the execution of children processes using
the abort(). Some reasons for doing so:
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 The parent is exiting and the operating systems does not
allow a child to continue if its parent terminates

Operating System Concepts – 9th Edition 3.25 Silberschatz, Galvin and Gagne ©2013
Process Termination

 Some operating systems do not allow child to exists if its parent


has terminated. If a process terminates, then all its children must
also be terminated.
 cascading termination. All children, grandchildren, etc. are
terminated.

Operating System Concepts – 9th Edition 3.26 Silberschatz, Galvin and Gagne ©2013
 How processes communicate?

Operating System Concepts – 9th Edition 3.27 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication
 Processes within a system may be independent or cooperating.
 Independent process cannot affect or be affected by the execution
of another process
 Cooperating process can affect or be affected by other processes,
including sharing data
 Reasons for cooperating processes:
 Information sharing
 Computation speedup
 Modularity
 Convenience
 Cooperating processes need interprocess communication (IPC)
 Two models of IPC
 Shared memory
 Message passing

Operating System Concepts – 9th Edition 3.28 Silberschatz, Galvin and Gagne ©2013
Communications Models

(a) Message passing. (b) shared memory.

Operating System Concepts – 9th Edition 3.29 Silberschatz, Galvin and Gagne ©2013
Question

 What are the strengths and weaknesses of


the two communication models?

Operating System Concepts – 9th Edition 3.30 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Shared Memory

 Interprocess Communication – Shared Memory


 An area of memory shared among the processes that wish
to communicate
 The communication is under the control of the users
processes not the operating system.

 Interprocess Communication – Message Passing


 IPC facility provides two operations:
 send(message)

 receive(message)
 If processes P and Q wish to communicate, they need to:
 Establish a communication link between them
 Exchange messages via send/receive

Operating System Concepts – 9th Edition 3.31 Silberschatz, Galvin and Gagne ©2013
Interprocess Communication – Message Passing

 Mechanism for processes to communicate and to synchronize their


actions
 Message system – processes communicate with each other without
resorting to shared variables
 IPC facility provides two operations:
 send(message) – message size fixed or variable
 receive(message)
 If P and Q wish to communicate, they need to:
 establish a communication link between them
 exchange messages via send/receive
 Implementation of communication link
 physical (e.g., shared memory, hardware bus)
 logical (e.g., logical properties)

Operating System Concepts – 9th Edition 3.32 Silberschatz, Galvin and Gagne ©2013

You might also like