Beruflich Dokumente
Kultur Dokumente
Operating System
Unit - 2
M.S.Maheshan
Asst. Professor
Dept. of IS&E
SJCE
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
PROCESS CONCEPT
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
•Note: in addition, for detailed explanation of swapping and suspended state refer PC
book page-167
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
THREADS
• Source: Wikipedia
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Types of Threads
• Can be classified into 2 distinct categories
kernel threads
user threads
• The former are managed and scheduled by kernel, whereas
the latter are managed and scheduled in userspace.
• Here when we use the term “thread”, it is referred as kernel
threads, whereas the term “fiber” is sometimes used to refer
user threads.
• The OS does not provide fibers, an application may
implement its own fibers using repeated calls to worker
functions.
• A fiber can be scheduled to run in any thread in the same
process.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
1. Kernel-Level Threads
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Many-to-One
One-to-One
Many-to-Many
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Many-to-One
•Many user-level threads mapped to single kernel thread
•Examples:
Solaris Green Threads
GNU Portable Threads
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
One-to-One
•Each user-level thread maps to kernel thread
•Examples
Windows NT/XP/2000
Linux
Solaris 9 and later
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Many-to-Many
•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
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Threads: Priority
•Thread priorities are used by thread scheduler while
deciding to release a thread that should be allowed to run.
•Usually higher-priority threads get more CPU time than
lower-priority threads.
•Priorities are integers that specify the relative priority on one
thread to another.
•This is used to decide when to switch from one running
thread to the next. This is called context switch.
•Rules to determine when a context switch takes place is
simple:
A thread can voluntarily release control: This is done by
explicitly sleeping
A higher-priority thread can preempt a thread.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
MULTITHREADING
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
SCHEDULING
•The term scheduling, refer to set of defined policies and
suitable mechanisms built into the OS that determines the order
in which the work has to be carried out by the computer.
•In Unit-1 we have already discussed different forms of
scheduling
• long-term scheduling
• medium-term scheduling
• short-term scheduling
•The short-term scheduler, also known as dispatcher, governs
the order of execution of runnable processes.
•This module decides which processes in the pool of ready
processes in the memory gets processor.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Scheduling Criteria
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
1. Preemptive
2. Nonpreemptive
•Cooperative
•Run-to-completion
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Non-Preemptive Strategies
•FCFS Scheduling (First Come First Serve)
•SJF/SJN/SPN (Shortest Process Next)
•SRTN (Shortest Remaining Time Next)
•Priority scheduling
•Deadline scheduling
Preemptive Strategies
•Round Robin scheduling
•Multilevel Queue Scheduling
•Multilevel Feedback Queue Scheduling
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
FCFS:
Process Burst Time
P1 24
P2 3
P3 3
0 24 27 30
•Waiting time for P1 = 0; P2 = 24; P3 = 27
•Average waiting time: (0 + 24 + 27)/3 = 17
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
P2 P3 P1
0 3 6 30
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
SJF:
•Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the
shortest time
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
P4 P1 P3 P2
0 3 9 16 24
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
P1 P2 P4 P1 P3
0 1 5 10 17 26
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Priority Scheduling:
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
P2 P5 P1 P3 P4
0 1 6 16 18 19
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Deadline Scheduling:
•Deadline scheduling can be considered as one form of
priority scheduling, where priorities assigned in this case is
the deadlines associated with each process.
•Deadline scheduling may be,
Preemptive
Nonpreemptive
•The system workload here consists of a combination of
available processes and these scheduling must have complete
knowledge regarding how much time is required to execute
each process.
•The scheduler will admit a process in the ready list if the
scheduler can guarantee that it will be able to meet each
deadline imposed by the process
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
•If we use a time quantum of 4 milliseconds, then P1 gets the
first 4 msec. since it requires another 20msec, it is preempted
after first time quantum and CPU is given to next process.
•P2 does not need 4 msec, so it quits before its time quantum
expires, CPU is given to next process P3.
•Once each process has received 1 time quantum, the CPU is
returned to process P1 for additional time quantum.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
•Scheduling
A new job enters queue Q0 which is to be served
When it gains CPU, job receives 8 milliseconds
If it does not finish in 8 milliseconds, job is moved to
queue Q1
At Q1 job is again served and receives 16 additional
milliseconds
If it still does not complete, it is preempted and moved
to queue Q2
Process in the queue Q2 are run on an FCFS basis.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
INTERPROCESS SYNCHRONIZATION
Concept:
•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
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Producer:
while (true) {
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Consumer:
while (true) {
while (counter == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
register1 = counter
register1 = register1 + 1
counter = register1
register2 = counter
register2 = register2 - 1
count = register2
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Critical-Section Problem:
•Consider system of n processes {p0, p1, … pn-1}
•Each process has critical section segment of code
Process may be 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, i.e.,
Each process must ask permission to enter critical
section in entry section, may follow critical section with
exit section, then remainder section
•This is especially challenging with preemptive kernels
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Peterson’s Solution:
•Is a classic software-based solution to the critical-section
problem.
•Because of the way modern computer architectures perform
basic machine-language instructions, such as load and store,
there are no guarantees that Peterson's solution will work
correctly on such architectures.
• However we present the solution because it provides a good
algorithmic description of solving the critical-section problem
and illustrates some of the complexities involved in designing
software that addresses the requirements of mutual exclusion,
progress, and bounded waiting.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
do {
flag[i] = TRUE;
turn = j;
while (flag[j] && turn == j);
Algorithm for
critical section Process Pi
flag[i] = FALSE;
remainder section
} while (TRUE);
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Synchronization Hardware:
•software-based solutions such as Peterson‘s are not guaranteed
to work on modern computer architectures.
•Instead, we can generally state that any solution to the critical-
section problem requires a simple tool-a lock.
•Race conditions are prevented by requiring that critical regions
be protected by locks.
•That is, a process must acquire a lock before entering a critical
section; it releases the lock when it exits the critical section
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
• Definition: • Solution:
do {
boolean TestAndSet (boolean while ( TestAndSet (&lock ))
*target) ; // critical section
{ lock = FALSE;
boolean rv = *target; // remainder section
*target = TRUE; } while (TRUE);
return rv:
}
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
• Definition: • Solution:
do {
void Swap (boolean *a, key = TRUE;
boolean *b) while ( key == TRUE)
{ Swap (&lock, &key );
boolean temp = *a; // critical section
*a = *b; lock = FALSE;
*b = temp: // remainder section
} } while (TRUE);
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Semaphore:
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Semaphore Implementation:
•Must guarantee that no two processes can execute wait () and
signal () on the same semaphore at the same time
•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
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
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);
}
}
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Bounded-Buffer Problem
Dining-Philosophers Problem
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Bounded-Buffer Problem:
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Dining-Philosophers Problem:
Readers-Writers Problem:
•A data set is shared among a number of concurrent processes
Readers – only read the data set; they do not perform any
updates
Writers – can both read and write
•Problem – allow multiple readers to read at the same time
Only one single writer can access the shared data at the same
time
•Several variations of how readers and writers are treated – all
involve priorities
•Shared Data
Data set
Semaphore mutex initialized to 1
Semaphore write initialized to 1
Integer readcount initialized to 0
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
•Example
System has 2 disk drives
P1 and P2 each hold one disk drive and each needs
another one
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
System Model:
•A system consists of a finite number of resources to be
distributed among a number of competing processes.
•The resources are partitioned into several types, each
consisting of some number of identical instances.
•Memory space, CPU cycles, files, and I/0 devices (such as
printers and DVD drives) are examples of resource types.
•If a system has two CPUs, then the resource type CPU has
two instances.
•Similarly, the resource type printer may have five instances
•If a process requests an instance of a resource type, the
allocation of any instance of the type will satisfy the request.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Resource-Allocation Graph:
•Deadlocks can be described more precisely in terms of a
directed graph called system resource-allocation Graph.
•Graph consists of 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
•Pictorially we represent each process Pi as a circle and each
resource type Rj as a rectangle
•Since resource type Rj may have more than one instance, we
represent each such instance as a dot within the rectangle
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
•Process
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Resource instances:
•One instance of resource type R1
•Two instances of resource type R2
•One instance of resource type R3
•Three instances of resource type R4
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Process states:
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
•To ensure that deadlocks never occur, the system can use either
the deadlock-prevention or a deadlock-avoidance scheme.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
DEADLOCK PREVENTION:
•For a deadlock to occur, each of the four necessary conditions
must hold.
•By ensuring that at least one of these conditions cannot hold, we
can prevent the occurrence of a deadlock.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
•No Preemption –
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
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
DEADLOCK AVOIDANCE:
•An alternative method for avoiding deadlocks is to require
additional information about how resources are to be
requested
•For example, in a system with one tape drive and one printer,
the system might need to know that process P will request
first the tape drive and then the printer before releasing both
resources, whereas process Q will request first the printer and
then the tape drive.
•With this knowledge of the complete sequence of requests
and releases for each process, the system can decide for each
request whether or not the process should wait in order to
avoid a possible future deadlock.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Safe State:
•A state is safe if the system can allocate resources to each process
in some order and still avoid a deadlock
•System is in safe state if there exists a sequence <P1, P2, …, Pn>
of ALL the processes in the systems such that for each Pi, 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
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
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.
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Avoidance algorithms,
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Banker’s Algorithm:
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Safety Algorithm:
Algorithm for finding out whether or not a system is in a safe state.
4. If Finish [i] == true for all i, then the system is in a safe state
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Resource-Request Algorithm:
Algorithm for determining whether requests can be safely granted.
Let Request = request vector for process Pi. If Requesti [j] = k then process Pi
wants k instances of resource type Rj
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
Need
ABC
P0 743
P1 122
P2 600
P3 011
P4 431
The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies
safety criteria
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®
JSS MAHAVIDYAPEETHA
SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
(AUTONOMOUS), MYSURU – 570 006
Under Visvesvaraya Technological University, Belagavi
©MSM ®