Professional Documents
Culture Documents
Unit 2
Unit 2
Processes management
Operating System Concepts – 10th Edition 3.2 Silberschatz, Galvin and Gagne
Process Concept
An operating system executes a variety of programs that
run as a process.
Process – a program in execution; process execution must
progress in sequential fashion. No parallel execution of
instructions of a single process
Multiple parts
• The program code, also called text section
• Current activity including program counter, processor
registers
• Stack containing temporary data
Function parameters, return addresses, local
variables
• Data section containing global variables
• Heap containing memory dynamically allocated during
run time
Operating System Concepts – 10th Edition 3.3 Silberschatz, Galvin and Gagne
Process Concept (Cont.)
Operating System Concepts – 10th Edition 3.4 Silberschatz, Galvin and Gagne
Process in Memory
Operating System Concepts – 10th Edition 3.5 Silberschatz, Galvin and Gagne
Memory Layout of a C Program
Operating System Concepts – 10th Edition 3.6 Silberschatz, Galvin and Gagne
Process State
Operating System Concepts – 10th Edition 3.7 Silberschatz, Galvin and Gagne
Diagram of Process State
Operating System Concepts – 10th Edition 3.8 Silberschatz, Galvin and Gagne
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 – 10th Edition 3.9 Silberschatz, Galvin and Gagne
Process Representation in Linux
Operating System Concepts – 10th Edition 3.10 Silberschatz, Galvin and Gagne
Process Scheduling
Operating System Concepts – 10th Edition 3.11 Silberschatz, Galvin and Gagne
Ready and Wait Queues
Operating System Concepts – 10th Edition 3.12 Silberschatz, Galvin and Gagne
Representation of Process Scheduling
Operating System Concepts – 10th Edition 3.13 Silberschatz, Galvin and Gagne
CPU Switch From Process to Process
A context switch occurs when the CPU switches from
one process to another.
Operating System Concepts – 10th Edition 3.14 Silberschatz, Galvin and Gagne
Context Switch
Operating System Concepts – 10th Edition 3.15 Silberschatz, Galvin and Gagne
Basic Concepts
Operating System Concepts – 10th Edition 3.16 Silberschatz, Galvin and Gagne
Histogram of CPU-burst Times
Operating System Concepts – 10th Edition 3.17 Silberschatz, Galvin and Gagne
CPU Scheduler
The CPU scheduler selects from among the processes
in ready queue, and allocates a CPU core 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
For situations 1 and 4, there is no choice in terms of
scheduling. A new process (if one exists in the ready
queue) must be selected for execution.
For situations 2 and 3, however, there is a choice.
Operating System Concepts – 10th Edition 3.18 Silberschatz, Galvin and Gagne
Preemptive and Nonpreemptive Scheduling
Otherwise, it is preemptive.
Under Nonpreemptive scheduling, once the CPU has
been allocated to a process, the process keeps the CPU
until it releases it either by terminating or by switching
to the waiting state.
Operating System Concepts – 10th Edition 3.19 Silberschatz, Galvin and Gagne
Preemptive Scheduling and Race Conditions
Operating System Concepts – 10th Edition 3.20 Silberschatz, Galvin and Gagne
Dispatcher
Dispatcher module gives control
of the CPU to the process
selected by the CPU 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 – 10th Edition 3.21 Silberschatz, Galvin and Gagne
Scheduling Criteria
Operating System Concepts – 10th Edition 3.22 Silberschatz, Galvin and Gagne
Scheduling Algorithm Optimization Criteria
Operating System Concepts – 10th Edition 3.23 Silberschatz, Galvin and Gagne
First- Come, First-Served (FCFS) Scheduling
Operating System Concepts – 10th Edition 3.24 Silberschatz, Galvin and Gagne
FCFS Scheduling (Cont.)
P2 P3 P1
0 3 6 30
Operating System Concepts – 10th Edition 3.25 Silberschatz, Galvin and Gagne
Shortest-Job-First (SJF) Scheduling
Operating System Concepts – 10th Edition 3.26 Silberschatz, Galvin and Gagne
Example of SJF
P4 P1 P3 P2
0 3 9 16 24
Operating System Concepts – 10th Edition 3.27 Silberschatz, Galvin and Gagne
Determining Length of Next CPU Burst
Commonly, α set to ½
Operating System Concepts – 10th Edition 3.28 Silberschatz, Galvin and Gagne
Shortest Remaining Time First Scheduling
Operating System Concepts – 10th Edition 3.29 Silberschatz, Galvin and Gagne
Example of Shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating System Concepts – 10th Edition 3.30 Silberschatz, Galvin and Gagne
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.
Operating System Concepts – 10th Edition 3.32 Silberschatz, Galvin and Gagne
Time Quantum and Context Switch Time
Operating System Concepts – 10th Edition 3.33 Silberschatz, Galvin and Gagne
Turnaround Time Varies With The Time Quantum
Operating System Concepts – 10th Edition 3.34 Silberschatz, Galvin and Gagne
Priority Scheduling
Operating System Concepts – 10th Edition 3.35 Silberschatz, Galvin and Gagne
Example of Priority Scheduling
Operating System Concepts – 10th Edition 3.36 Silberschatz, Galvin and Gagne
Priority Scheduling w/ Round-Robin
Operating System Concepts – 10th Edition 3.37 Silberschatz, Galvin and Gagne
Multilevel Queue
The ready queue consists of multiple queues
Multilevel queue scheduler defined by the following
parameters:
• Number of queues
• Scheduling algorithms for each queue
• Method used to determine which queue a
process will enter when that process needs
service
• Scheduling among the queues
Operating System Concepts – 10th Edition 3.38 Silberschatz, Galvin and Gagne
Multilevel Queue
With priority scheduling, have separate queues for each
priority.
Schedule the process in the highest-priority queue!
Operating System Concepts – 10th Edition 3.39 Silberschatz, Galvin and Gagne
Multilevel Queue
Operating System Concepts – 10th Edition 3.40 Silberschatz, Galvin and Gagne
Multilevel Feedback Queue
A process can move between the various queues.
Multilevel-feedback-queue scheduler defined by the
following parameters:
• Number of queues
• Scheduling algorithms for each queue
• Method used to determine when to upgrade a
process
• Method used to determine when to demote a
process
• Method used to determine which queue a process
will enter when that process needs service
Aging can be implemented using multilevel feedback
queue
Operating System Concepts – 10th Edition 3.41 Silberschatz, Galvin and Gagne
Example of Multilevel Feedback Queue
Three queues:
• Q0 – RR with time quantum 8
milliseconds
• Q1 – RR time quantum 16
milliseconds
• Q2 – FCFS
Scheduling
• A new process enters queue Q0
which is served in RR
When it gains CPU, the process
receives 8 milliseconds
If it does not finish in 8
milliseconds, the process is moved
to queue Q1
• At Q1 job is again served in RR
and receives 16 additional
milliseconds
If it still does not complete, it is
preempted and moved to queue Q2
Operating System Concepts – 10th Edition 3.42 Silberschatz, Galvin and Gagne
Threads Motivation
Operating System Concepts – 10th Edition 3.43 Silberschatz, Galvin and Gagne
Single and Multithreaded Processes
Operating System Concepts – 10th Edition 3.44 Silberschatz, Galvin and Gagne
Multithreaded Server Architecture
Operating System Concepts – 10th Edition 3.45 Silberschatz, Galvin and Gagne
Benefits
Operating System Concepts – 10th Edition 3.46 Silberschatz, Galvin and Gagne
Thread Scheduling
Operating System Concepts – 10th Edition 3.47 Silberschatz, Galvin and Gagne
Pthread Scheduling
API allows specifying either PCS or SCS during thread
creation
• PTHREAD_SCOPE_PROCESS schedules threads using
PCS scheduling
Operating System Concepts – 10th Edition 3.48 Silberschatz, Galvin and Gagne
Multithreading Models
• Many-to-One
• One-to-One
• Many-to-Many
Operating System Concepts – 10th Edition 3.49 Silberschatz, Galvin and Gagne
Many-to-One
In this model, many user threads are mapped to only
one kernel thread.
All user-level threads share a single kernel-level thread.
Advantages:
• Easy to implement since it requires minimal kernel
support.
• Fast context switching between user threads.
Disadvantages:
• Blocking system calls can block the entire process.
• Not suitable for multicore processors due to single
kernel thread.
Examples:
• Solaris Green Threads
• GNU Portable Threads
Operating System Concepts – 10th Edition 3.50 Silberschatz, Galvin and Gagne
One-to-One
Each user thread corresponds to exactly one kernel thread.
Provides true parallelism on multicore systems.
Advantages:
• Efficient due to parallel execution.
• Blocking system calls do not affect other threads.
Disadvantages:
• Higher overhead (more kernel threads).
• Increased context switching time.
Examples
• Windows
• Linux
Operating System Concepts – 10th Edition 3.51 Silberschatz, Galvin and Gagne
Many-to-Many Model
Allows multiple user threads to be mapped to multiple
kernel threads.
Provides a balance between the previous models.
Advantages:
• Flexibility in managing user and kernel threads.
• Can exploit multicore processors effectively.
Disadvantages:
• Complex implementation with synchronization
challenges.
Example: Windows threads
(user threads mapped to kernel
threads).
Operating System Concepts – 10th Edition 3.52 Silberschatz, Galvin and Gagne
Process Synchronization
Types of Processes:
Independent Processes: These processes do not affect
each other’s execution. Their actions are isolated and
independent.
Operating System Concepts – 10th Edition 3.54 Silberschatz, Galvin and Gagne
Race Condition
counter++ could be implemented as (P1)
register1 = counter
register1 = register1 + 1
counter = register1
counter-- could be implemented as (p2)
register2 = counter
register2 = register2 - 1
counter = register2
Operating System Concepts – 10th Edition 3.55 Silberschatz, Galvin and Gagne
Critical Section Problem
A critical section refers to a part of a program where
shared resources are accessed, and where concurrent
access by multiple processes could lead to undesirable
behavior such as data inconsistency.
Consider system of n processes {p0, p1, … pn-1}
Each process has critical section segment of code
• Process may be changing common variables, updating
table, writing file, etc
• When one process in critical section, no other may be
in its critical section
Critical section problem is to design protocol to solve
this
Each process must ask permission to enter critical
section in entry section, may follow critical section with
exit section, then remainder section
Operating System Concepts – 10th Edition 3.56 Silberschatz, Galvin and Gagne
Critical Section
Operating System Concepts – 10th Edition 3.57 Silberschatz, Galvin and Gagne
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
Operating System Concepts – 10th Edition 3.59 Silberschatz, Galvin and Gagne
Algorithm for Process Pi
do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j);
critical section
flag[i] = false;
remainder section
} while (true);
Operating System Concepts – 10th Edition 3.60 Silberschatz, Galvin and Gagne
Peterson’s Solution (Cont.)
Provable that the three CS requirement are met:
1. Mutual exclusion is preserved
Pi enters CS only if:
either flag[j] = false or turn = i
2. Progress requirement is satisfied
3. Bounded-waiting requirement is met
Operating System Concepts – 10th Edition 3.61 Silberschatz, Galvin and Gagne
Synchronization Hardware
Many systems provide hardware support for critical
section code
Operating System Concepts – 10th Edition 3.62 Silberschatz, Galvin and Gagne
Solution to Critical-section
Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
Operating System Concepts – 10th Edition 3.63 Silberschatz, Galvin and Gagne
TestAndSet Instruction
Definition:
Operating System Concepts – 10th Edition 3.64 Silberschatz, Galvin and Gagne
Solution using TestAndSet
do {
while ( TestAndSet (&lock ))
; // do nothing
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Operating System Concepts – 10th Edition 3.65 Silberschatz, Galvin and Gagne
Swap Instruction
Definition:
Operating System Concepts – 10th Edition 3.66 Silberschatz, Galvin and Gagne
Solution using Swap
Shared Boolean variable lock initialized to FALSE; Each
process has a local Boolean variable key
Solution:
do {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
// critical section
lock = FALSE;
// remainder section
} while (TRUE);
Operating System Concepts – 10th Edition 3.67 Silberschatz, Galvin and Gagne
Bounded-waiting Mutual Exclusion
with TestandSet()
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key)
key = TestAndSet(&lock);
waiting[i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);
Operating System Concepts – 10th Edition 3.68 Silberschatz, Galvin and Gagne
Semaphore
Operating System Concepts – 10th Edition 3.69 Silberschatz, Galvin and Gagne
Semaphore as
General Synchronization Tool
Counting semaphore – integer value can range over an
unrestricted domain
Binary semaphore – integer value can range only between 0
and 1; can be simpler to implement
• Also known as mutex locks
Can implement a counting semaphore S as a binary semaphore
Provides mutual exclusion
Semaphore mutex; // initialized to 1
do {
wait (mutex);
// Critical Section
signal (mutex);
// remainder section
} while (TRUE);
Operating System Concepts – 10th Edition 3.70 Silberschatz, Galvin and Gagne
Semaphore Implementation
Operating System Concepts – 10th Edition 3.71 Silberschatz, Galvin and Gagne
Semaphore Implementation
with no Busy waiting
Two operations:
• block – place the process invoking the operation on
the appropriate waiting queue
• wakeup – remove one of processes in the waiting
queue and place it in the ready queue
Operating System Concepts – 10th Edition 3.72 Silberschatz, Galvin and Gagne
Semaphore Implementation with
no Busy waiting (Cont.)
Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
Operating System Concepts – 10th Edition 3.73 Silberschatz, Galvin and Gagne
Deadlock and Starvation
Deadlock – two or more processes are waiting
indefinitely for an event that can be caused by only one
of the waiting processes
Let S and Q be two semaphores initialized to 1
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. .
. .
. .
signal (S); signal (Q);
signal (Q); signal (S);
Starvation – indefinite blocking
• A process may never be removed from the
semaphore queue in which it is suspended
Priority Inversion – Scheduling problem when lower-
priority process holds a lock needed by higher-priority
process
Operating System•Concepts
Solved via priority-inheritance
– 10 Edition
th
3.74 protocol Silberschatz, Galvin and Gagne
Classical Problems of Synchronization
Classical problems used to test newly-proposed
synchronization schemes
• Bounded-Buffer Problem
• Dining-Philosophers Problem
Operating System Concepts – 10th Edition 3.75 Silberschatz, Galvin and Gagne
Bounded Buffer Problem (Cont.)
The structure of the producer process
do {
wait (empty);
wait (mutex);
signal (mutex);
signal (full);
} while (TRUE);
Operating System Concepts – 10th Edition 3.76 Silberschatz, Galvin and Gagne
Bounded Buffer Problem (Cont.)
The structure of the consumer process
do {
wait (full);
wait (mutex);
signal (mutex);
signal (empty);
} while (TRUE);
Operating System Concepts – 10th Edition 3.77 Silberschatz, Galvin and Gagne
Readers-Writers Problem
do {
wait (wrt) ;
// writing is performed
signal (wrt) ;
} while (TRUE);
Operating System Concepts – 10th Edition 3.79 Silberschatz, Galvin and Gagne
Readers-Writers Problem (Cont.)
The structure of a reader process
do {
wait (mutex) ;
readcount ++ ;
if (readcount == 1)
wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0)
signal (wrt) ;
signal (mutex) ;
} while (TRUE);
Operating System Concepts – 10th Edition 3.80 Silberschatz, Galvin and Gagne
Readers-Writers Problem Variations
Operating System Concepts – 10th Edition 3.81 Silberschatz, Galvin and Gagne
Dining-Philosophers Problem
Operating System Concepts – 10th Edition 3.82 Silberschatz, Galvin and Gagne
Dining-Philosophers Problem
Algorithm
The structure of Philosopher i:
do {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
Operating System Concepts – 10th Edition 3.83 Silberschatz, Galvin and Gagne
Problems with Semaphores
Operating System Concepts – 10th Edition 3.84 Silberschatz, Galvin and Gagne
Monitors
A high-level abstraction that provides a convenient and
effective mechanism for process synchronization
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
Operating System Concepts – 10th Edition 3.86 Silberschatz, Galvin and Gagne
Condition Variables
condition x, y;
Operating System Concepts – 10th Edition 3.87 Silberschatz, Galvin and Gagne
Monitor with Condition Variables
Operating System Concepts – 10th Edition 3.88 Silberschatz, Galvin and Gagne
Condition Variables Choices
Options include
• Signal and wait – P waits until Q leaves monitor or
waits for another condition
• Signal and continue – Q waits until P leaves the
monitor or waits for another condition
Operating System Concepts – 10th Edition 3.90 Silberschatz, Galvin and Gagne
Solution to Dining Philosophers (Cont.)
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
Operating System Concepts – 10th Edition 3.91 Silberschatz, Galvin and Gagne
Solution to Dining Philosophers (Cont.)
DiningPhilosophers.pickup (i);
EAT
DiningPhilosophers.putdown (i);
Operating System Concepts – 10th Edition 3.92 Silberschatz, Galvin and Gagne
Monitor Implementation Using Semaphores
Variables
semaphore mutex; // (initially = 1)
semaphore next; // (initially = 0)
int next_count = 0;
wait(mutex);
…
body of F;
…
if (next_count > 0)
signal(next)
else
signal(mutex);
Operating System Concepts – 10th Edition 3.93 Silberschatz, Galvin and Gagne
Monitor Implementation – Condition
Variables
x-count++;
if (next_count > 0)
signal(next);
else
signal(mutex);
wait(x_sem);
x-count--;
Operating System Concepts – 10th Edition 3.94 Silberschatz, Galvin and Gagne
Monitor Implementation (Cont.)
The operation x.signal can be implemented as:
if (x-count > 0) {
next_count++;
signal(x_sem);
wait(next);
next_count--;
}
Operating System Concepts – 10th Edition 3.95 Silberschatz, Galvin and Gagne
Resuming Processes within a Monitor
If several processes queued on condition x, and x.signal()
executed, which should be resumed?
Operating System Concepts – 10th Edition 3.96 Silberschatz, Galvin and Gagne
iv) Increment full value by 1and decrement empty value by 1
monitor ResourceAllocator
{
boolean busy;
condition x;
void acquire(int time) {
if (busy)
x.wait(time);
busy = TRUE;
}
void release() {
busy = FALSE;
x.signal();
}
initialization code() {
busy = FALSE;
}
}
Operating System Concepts – 10th Edition 3.97 Silberschatz, Galvin and Gagne
Synchronization Examples
Solaris
Windows XP
Linux
Pthreads
Operating System Concepts – 10th Edition 3.98 Silberschatz, Galvin and Gagne
Solaris Synchronization
Implements a variety of locks to support multitasking,
multithreading (including real-time threads), and
multiprocessing
Uses adaptive mutexes for efficiency when protecting
data from short code segments
• Starts as a standard semaphore spin-lock
• If lock held, and by a thread running on another CPU,
spins
• If lock held by non-run-state thread, block and sleep
waiting for signal of lock being released
Uses condition variables
Uses readers-writers locks when longer sections of code
need access to data
Uses turnstiles to order the list of threads waiting to
acquire either an adaptive mutex or reader-writer lock
• Turnstiles are per-lock-holding-thread, not per-object
Priority-inheritance per-turnstile gives the running
Operating System Concepts – 10th Edition 3.99 Silberschatz, Galvin and Gagne
Windows XP Synchronization
Uses interrupt masks to protect access to global
resources on uniprocessor systems
• Events
An event acts much like a condition variable
• Timers notify one or more thread when time expired
• Dispatcher objects either signaled-state (object
available) or non-signaled state (thread will block)
Operating System Concepts – 10th Edition 3.100 Silberschatz, Galvin and Gagne
Linux Synchronization
Linux:
• Prior to kernel Version 2.6, disables interrupts to
implement short critical sections
• Version 2.6 and later, fully preemptive
Linux provides:
• semaphores
• spinlocks
• reader-writer versions of both
Operating System Concepts – 10th Edition 3.101 Silberschatz, Galvin and Gagne
Pthreads Synchronization
It provides:
• mutex locks
• condition variables
Operating System Concepts – 10th Edition 3.102 Silberschatz, Galvin and Gagne
Atomic Transactions
System Model
Log-based Recovery
Checkpoints
Concurrent Atomic Transactions
Operating System Concepts – 10th Edition 3.103 Silberschatz, Galvin and Gagne
System Model
Operating System Concepts – 10th Edition 3.104 Silberschatz, Galvin and Gagne
Types of Storage Media
Operating System Concepts – 10th Edition 3.105 Silberschatz, Galvin and Gagne
Log-Based Recovery
Operating System Concepts – 10th Edition 3.106 Silberschatz, Galvin and Gagne
Log-Based Recovery Algorithm
Operating System Concepts – 10th Edition 3.107 Silberschatz, Galvin and Gagne
Checkpoints
Operating System Concepts – 10th Edition 3.108 Silberschatz, Galvin and Gagne
Concurrent Transactions
Operating System Concepts – 10th Edition 3.109 Silberschatz, Galvin and Gagne
Serializability
Operating System Concepts – 10th Edition 3.110 Silberschatz, Galvin and Gagne
Schedule 1: T0 then T1
Operating System Concepts – 10th Edition 3.111 Silberschatz, Galvin and Gagne
Nonserial Schedule
Operating System Concepts – 10th Edition 3.112 Silberschatz, Galvin and Gagne
Schedule 2: Concurrent Serializable Schedule
Operating System Concepts – 10th Edition 3.113 Silberschatz, Galvin and Gagne
Locking Protocol
Operating System Concepts – 10th Edition 3.114 Silberschatz, Galvin and Gagne
Two-phase Locking Protocol
Operating System Concepts – 10th Edition 3.115 Silberschatz, Galvin and Gagne
Timestamp-based Protocols
Operating System Concepts – 10th Edition 3.116 Silberschatz, Galvin and Gagne
Timestamp-based Protocol Implementation
Operating System Concepts – 10th Edition 3.117 Silberschatz, Galvin and Gagne
Timestamp-ordering Protocol
Operating System Concepts – 10th Edition 3.118 Silberschatz, Galvin and Gagne
Schedule Possible Under Timestamp Protocol
Operating System Concepts – 10th Edition 3.119 Silberschatz, Galvin and Gagne
End of Unit 2