0% found this document useful (0 votes)
20 views

UNIT 3 OS

os notes

Uploaded by

zunaidkhan1492
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

UNIT 3 OS

os notes

Uploaded by

zunaidkhan1492
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

Processes

• Process Concept
• Process Scheduling
• Operations on Processes
• Cooperating Processes
• Interprocess Communication
• Communication in Client-Server Systems
Process Concept
Process in Memory
Process State
Diagram of Process State
Process Control Block (PCB)
Process Control Block (PCB)
CPU Switch From Process to Process
Process Scheduling Queues

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
Ready Queue And Various I/O Device Queues
Representation of Process Scheduling
Schedulers

Long-term scheduler (or job scheduler) – selects which processes should be brought
into the ready queue
Short-term scheduler (or CPU scheduler) – selects which process should be executed
next and allocates CPU
Addition of Medium Term Scheduling
Schedulers (Cont.)

Short-term scheduler is invoked very frequently (milliseconds) ⇒ (must be fast)


Long-term scheduler is invoked very 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
Context Switch
Process Creation

● Parent process create children processes, which, in turn create


other processes, forming a tree of processes
● Resource sharing
● Parent and children share all resources
● Children share subset of parent’s resources
● Parent and child share no resources
● Execution
● Parent and children execute concurrently
● Parent waits until children terminate
● Address space
● Child duplicate of parent
● Child has a program loaded into it
● UNIX examples
● fork system call creates new process
● exec system call used after a fork to replace the process’ memory
space with a new program
Process Creation
Process Termination
Threads
Single and Multithreaded Processes
Benefits
User Threads
Kernel Threads
Multithreading Models
Many-to-One
One-to-One
Many-to-Many Model
CPU Scheduling

● Basic Concepts
● Scheduling Criteria
● Scheduling Algorithms
● Multiple-Processor Scheduling
● Real-Time Scheduling
● Thread Scheduling
● Operating Systems Examples
● Algorithm Evaluation
Basic Concepts
Alternating Sequence of CPU And I/O Bursts
CPU Scheduler

• Selects from among the processes in memory


that are ready to execute, and allocates the CPU
to one of them
• 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
• All other scheduling is preemptive
Dispatcher
Scheduling Criteria
Optimization Criteria
Scheduling Algorithms

In the uniprogrammming systems example


MS- DOS, when a process waits for any I/O
operation to be done, the CPU remains free it
cause starvation problem.

While In Multiprogramming systems, the


CPU doesn't remain idle during the waiting
time of the Process and it starts executing
other processes.

Multiprogramming systems, the Operating


system schedules the processes on the CPU to
have the maximum utilization of it and this
procedure is called CPU scheduling.
Various Scheduling Algorithms
• First-Come, First-Served (FCFS) Scheduling
• Shortest-Job-Next (SJN) Scheduling
• Priority Scheduling
• Shortest Remaining Time
• Round Robin(RR) Scheduling
• Multiple-Level Queues Scheduling
There are two types of Algorithm: Non-
preemptive or Preemptive.

Non-preemptive algorithms are designed so


that once a process enters the running state,
it cannot be preempted until it completes its
allotted time.

While the preemptive scheduling is based on


priority where a scheduler may preempt a
low priority running process anytime when a
high priority process enters into a ready state.
First Come First Serve (FCFS)
• In FCFS Algorithm jobs(task) are executed
on first come, first serve basis.

• It is a non-preemptive, pre-emptive
scheduling algorithm.

• Easy to understand and implement.

• Its implementation is based on FIFO queue.

• Poor in performance as average wait time is


high.
Process Arrival Time Executive Time Service Time

P0 0 5 0

P1 1 3 5

P2 2 8 8

P3 3 6 16

Wait Time=Service time-Arrival


Time
0-0=0
P0 =>
P1 => 5-1=4
P2 => 8-2=6
16 - 3 = 13
P3 => Avg Wait Time=Sum of wait times
Characteristics of FCFS method
• It offers non-preemptive scheduling algorithm.
• Jobs are always executed on a first-come, first-serve
basis
• It is easy to implement and use.
• However, this method is poor in performance, and
the general wait time is quite high.
Shortest-Job-First (SJR) Scheduling

This algorithm associates with each process


the length of the process's next CPU burst.
When the CPU is available, it is assigned to the
process that has the smallest next CPU burst. If
the next CPU bursts of two processes are the
same, FCFS scheduling is used to break the tie.
Shortest-Job-First (SJR) Scheduling

• Two schemes:

• Non-preemptive – once CPU given to the process

it cannot be preempted until completes its CPU


burst.

• Preemptive – if a new process arrives with CPU

burst length less than remaining time of current


executing process, preempt. This scheme is know
as the Shortest-Remaining-Time-First (SRTF).

• SJF is optimal – gives minimum average waiting


time for a given set of processes.
Example of Non-Preemptive SJF
Priority Scheduling

Priority scheduling is a method of scheduling


processes based on priority. In this method, the
scheduler selects the tasks to work as per the
priority.
Priority scheduling also helps OS to involve priority
assignments. The processes with higher priority
should be carried out first, whereas jobs with equal
priorities are carried out on a round-robin or FCFS
basis.
Priority can be decided based on memory
requirements, time requirements, etc.
Cont….

• Types of Priority Scheduling Algorithm:-


1. Preemptive Priority Scheduling: If the new process
arrived at the ready queue has a higher priority than the
currently running process, the CPU is preempted, which
means the processing of the current process is stopped
and the incoming new process with higher priority gets
the CPU for its execution.
2. Non-Preemptive Priority Scheduling: In case of non-
preemptive priority scheduling algorithm if a new
process arrives with a higher priority than the current
running process, the incoming process is put at the head
of the ready queue, which means after the execution of
Priority Scheduling Example
Priority Scheduling Example
Priority Scheduling

• Advantages of priority scheduling (non-


preemptive):
Higher priority processes like system processes are
executed first.
• Disadvantages of priority scheduling (non-
preemptive):
It can lead to starvation if only higher priority
process comes into the ready state.
If the priorities of more two processes are the same,
Priority Scheduling
• Problem with Priority Scheduling Algorithm
In priority scheduling algorithm, the chances
of indefinite blocking or starvation.
A process is considered blocked when it is ready to
run but has to wait for the CPU as some other
process is running currently.
But in case of priority scheduling if new higher
priority processes keeps coming in the ready queue
then the processes waiting in the ready queue with
lower priority may have to wait for long durations
Priority Scheduling
• Using Aging Technique with Priority Scheduling
To prevent starvation of any process, we can use the
concept of aging where we keep on increasing the
priority of low-priority process based on the its
waiting time.
For example, if we decide the aging factor to
be 0.5 for each day of waiting, then if a process with
priority 20(which is comparatively low priority)
comes in the ready queue. After one day of waiting,
its priority is increased to 19.5 and so on. Doing so,
Round Robin Scheduling
• Round Robin(RR) scheduling algorithm is mainly designed for
time-sharing systems. This algorithm is similar to FCFS
scheduling, but in RR scheduling, preemption is added which
enables the system to switch between processes.
A fixed time is allotted to each process, called a quantum, for
execution.
Once a process is executed for the given time period that
process is preempted and another process executes for the
given time period.
Context switching is used to save states of preempted processes.
This algorithm is simple and easy to implement
Round Robin Example

• Round Robin(RR) scheduling algorithm is mainly designed for time-sharing


systems. This algorithm is similar to FCFS scheduling, but in Round
Robin(RR) scheduling, preemption is added which enables the system to
switch between processes.
A fixed time is allotted to each process, called a quantum, for execution.
Once a process is executed for the given time period that process is
preempted and another process executes for the given time period.
Context switching is used to save states of preempted processes.
This algorithm is simple and easy to implement
It is important to note here that the length of time quantum is generally
from 10 to 100 milliseconds in length.
Round Robin Example

• Round Robin(RR) scheduling algorithm is mainly designed for time-sharing


systems. This algorithm is similar to FCFS scheduling, but in Round
Robin(RR) scheduling, preemption is added which enables the system to
switch between processes.
A fixed time is allotted to each process, called a quantum, for execution.
Once a process is executed for the given time period that process is
preempted and another process executes for the given time period.
Context switching is used to save states of preempted processes.
This algorithm is simple and easy to implement
It is important to note here that the length of time quantum is generally
from 10 to 100 milliseconds in length.
Advantage and Disadvantage RR

• Advantages of round-robin:
No starvation will be there in round-robin because
every process will get chance for its execution.
Used in time-sharing systems.
• Disadvantages of round-robin:
We have to perform a lot of context switching here,
which will keep the CPU idle.
Multilevel Queue

• Ready queue is partitioned into separate queues:


foreground (interactive)
background (batch)
• Each queue has its own scheduling algorithm,
foreground – RR
background – FCFS
• Scheduling must be done between the queues.
• Fixed priority scheduling; (i.e., serve all from
foreground then from background). Possibility of
starvation.
• Time slice – each queue gets a certain amount of CPU
time which it can schedule amongst its processes; i.e.,
80% to foreground in RR
• 20% to background in FCFS
Multilevel Queue Scheduling
Multilevel Queue Scheduling

System Process The Operating system itself has its own


process to run and is termed as System Process.
Interactive Process The Interactive Process is a process
in which there should be the same kind of
interaction (basically an online game ).
Batch Processes Batch processing is basically a
technique in the Operating system that collects the
programs and data together in the form of
the batch before the processing starts.
Student Process The system process always gets the
highest priority while the student processes always get
the lowest priority.
Multilevel Feedback Queue

• In a multilevel queue-scheduling algorithm, processes are


permanently assigned to a queue on entry to the system.
Processes do not move between queues. In general, a
multilevel feedback queue scheduler is defined by the
following parameters:
The number of queues.
The scheduling algorithm for each queue.
The method used to determine when to upgrade a
process to a higher-priority queue.
The method used to determine when to demote a
process to a lower-priority queue.
Multilevel Feedback Queue
Multiple-Processor Scheduling

• CPU scheduling more complex when multiple CPUs are

available.

• Homogeneous processors within a multiprocessor.

• Load sharing or load balancing


Real-Time Scheduling

• Hard real-time systems – required to complete a critical

task within a guaranteed amount of time.

• Soft real-time computing – requires that critical

processes receive priority over less fortunate ones.


Deadlock
• In a multiprogramming system, processes request
resources. If those resources are being used by other
processes then the process enters a waiting state. However,
if other processes are also in a waiting state, we have
deadlock.

• A set of processes is in a deadlock state if every process in


the set is waiting for an event (release) that can only be
caused by some other process in the same set.

Example
-System has 2 disk drives
-P1 and P2 process each hold one disk drive and each
1 Aprilneeds
2024 another one 62
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


1 Aprilwaiting
2024 processes such that P0 is waiting for63 a
Resource-Allocation Graph

A resource allocation graph is a set of vertices V and a


set of edges E such that:
• 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

1 April 2024 64
Resource-Allocation Graph
• Process

• Resource Type with 4 instances

• Pi requests instance of Rj P
i

• Pi is holding an instance of Rj

1 April 2024 65
Example of a Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Resource Allocation Graph With A Cycle But No Deadlock
Safe, unsafe , deadlock state spaces
Resource Allocation Graph With A Deadlock
Before P3 requested an After P 3 requested an
instance of R2 instance of R2

A Cycle In the Graph May cause Deadlock

1 April 2024 70
Methods for Handling Deadlock
⚫ Prevention
◦ Ensure that the system will never enter a
deadlock state
⚫ Avoidance
◦ Ensure that the system will never enter an
unsafe state
⚫ Detection
◦ Allow the system to enter a deadlock state and
then recover
⚫ Do Nothing
◦ Ignore the problem and let the user or system
administrator respond to the problem; used by
most operating systems, including Windows and
1 April 2024 UNIX 71
Deadlock Prevention
• To prevent deadlock, we can restrain the ways that
a request can be made
• Do not allow one of the four conditions to occur
• Mutual Exclusion :

• if no resource were ever assigned to a single


process exclusively ,we would never have
deadlock.

• Shared entities (read only files) don't need mutual


exclusion (and aren’t susceptible to deadlock.)

• Prevention not possible, since some devices are


naturally non-sharable
1 April 2024 72
Deadlock Prevention
Hold And Wait :
we must guarantee that whenever a process requests
a resource, it does not hold any other resources

• Require a process to request and be allocated


all its resources before it begins execution

• allow a process to request resources only when


the process has none

Result: Low resource utilization; starvation


possible

1 April 2024 73
Deadlock Prevention
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
• Pi Rj Pj Rk
• A process will be restarted only when it can
regain its old resources, as well as the new ones
that it is requesting.
• Allow preemption - if a needed resource is held
by another process, which is also waiting on
some resource, steal it. Otherwise wait.
Circular Wait :
• Impose a total ordering of all resource types,
and require that each process requests resources
1 April 2024 74
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
1 April 2024 75
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 Pi, the resources that P i 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


1 April 2024 76
resources, and so on
Safe State
• 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.

1 April 2024 77
Safe State

1 April 2024 78
Avoidance algorithms
• For a single instance of a resource type, use a
resource-allocation graph
• For multiple instances of a resource type, use the
banker’s algorithm

1 April 2024 79
Resource-Allocation Graph with Claim Edges

Assignme
nt Reque
edge st
edge

Clai Claim
m edge
edge

1 April 2024 80
Unsafe State In Resource-Allocation Graph

Assignme
nt Reque
edge st
edge

Assignme
Clai nt
m edge
edge

1 April 2024 81
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

1 April 2024 82
Banker’s Algorithm

• Multiple instances
• Each process must a priori claim maximum use
• When a process requests a resource it may
have to wait
• When a process gets all its resources it must
return them in a finite amount of time

1 April 2024 83
Data Structures for the Banker’s Algorithm

Let n = number of processes, and m = number of


resources types.
• Available: Vector of length m. If available [j] = k,
there are k instances of resource type Rj available.

• Max: n x m matrix. If Max [i,j] = k, then process Pi


may request at most k instances of resource type Rj.

• Allocation: n x m matrix. If Allocation[i,j] = k then


Pi is currently allocated k instances of Rj.

• Need: n x m matrix. If Need[i,j] = k, then Pi may


need k more instances of Rj to complete its task.
1 April 2024 84
Safety Algorithm
1.Let Work and Finish be vectors of length m and n,
respectively.
Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1

2.Find an i such that both:


(a) Finish [i] = false
(b) Needi ≤ Work
If no such i exists, go to step 4

3. Work = Work + Allocationi


Finish[i] = true
go to step 2

4.If Finish [i] == true for all i, then the system is in a safe
state
1 April 2024 85
Resource-Request Algorithm for Process Pi
Requesti = request vector for process Pi. If Requesti [j] = k then
process Pi wants k instances of resource type Rj

1. If Requesti ≤ Needi go to step 2. Otherwise, raise error


condition, since process has exceeded its maximum claim
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must
wait, since resources are not available
3. Pretend to allocate requested resources to Pi by
modifying the state as follows:
Available = Available – Requesti;
Allocationi = Allocationi + Requesti;
Need i = Needi – Requesti;
• If safe ⇒ the resources are allocated to Pi
• If unsafe ⇒ Pi must wait, and the old resource-
allocation state is restored

1 April 2024 86
Example of Banker’s Algorithm
• 5 processes P0 through P4;
3 resource types:
A (10 instances), B (5 instances), and C (7 instances)
• Snapshot at time T0:
Allocation Max Available
ABC ABC ABC
P0 0 1 0 753 332
P1 2 0 0 322
P2 3 0 2 902
P3 2 1 1 222
P4 0 0 2 433

1 April 2024 87
Example (Cont.)
• The content of the matrix Need is defined to be Max –
Allocation

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

1 April 2024 88
Example
P1 Request (1,0,2)
• Check that Request ≤ Available (that is, (1,0,2) ≤ (3,3,2) ⇒ true
Allocation Need Available
ABC ABC ABC
P0 0 1 0 7 4 3 2 3 0
P1 302 020
P2 3 0 2 600
P3 2 1 1 0 1 1
P4 0 0 2 431

• Executing safety algorithm shows that sequence < P1, P3, P4, P0,
P2> satisfies safety requirement

1 April 2024 89
Deadlock Detection and Recovery
• Allow system to enter deadlock state
• Detection algorithm
• Recovery scheme

1 April 2024 90
Single Instance of Each Resource Type
• Maintain wait-for graph
• Nodes are processes
• Pi → Pj if Pi is waiting for Pj

• Periodically invoke an algorithm that searches for a cycle in the


graph. If there is a cycle, there exists a deadlock

• An algorithm to detect a cycle in a graph requires an order of n2


operations, where n is the number of vertices in the graph

1 April 2024 91
Single Instance of Each Resource Type

Corresponding wait-for
Resource-Allocation Graph
graph

1 April 2024 92
Several Instances of a Resource Type
• Available: A vector of length m indicates the number of
available resources of each type
• Allocation: An n x m matrix defines the number of
resources of each type currently allocated to each process
• Request: An n x m matrix indicates the current request of
each process. If Request [i][j] = k, then process Pi is
requesting k more instances of resource type Rj.

1 April 2024 93
Detection Algorithm
1. Let Work and Finish be vectors of length m and n,
respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocationi ≠ 0, then
Finish[i] = false; otherwise, Finish[i] = true
2. Find an index i such that both:
(a) Finish[i] == false
(b) Requesti ≤ Work
If no such i exists, go to step 4
3. Work = Work + Allocationi
Finish[i] = true
go to step 2
4. If Finish[i] == false, for some i, 1 ≤ i ≤ n, then the system is in
deadlock state. Moreover, if Finish[i] == false, then Pi is
deadlocked

1 April 2024 94
Recovery from Deadlock
Process Termination :

• Abort all deadlocked processes


• Abort one process at a time until the deadlock cycle is eliminated

In which order should we choose to abort?


1. Priority of the process
2. How long process has computed, and how much longer to
completion
3. Resources the process has used
4. Resources process needs to complete
5. How many processes will need to be terminated
6. Is process interactive or batch?

1 April 2024 95
Recovery from Deadlock
Resource Preemption :

• Selecting a victim – minimize cost

• Rollback – return to some safe state, restart process for


that state

• Starvation – same process may always be picked as victim,


include number of rollback in cost factor

1 April 2024 96
THANK YOU

You might also like