You are on page 1of 7

Operating System Module 2

1. Process scheduling
When more than one process is runnable, the OS must decide which one is to run first.
That part of the OS concerned with this decision is called the Scheduler, and the algorithm it uses
is called the Scheduling algorithm.

A good scheduling algorithm constitutes the following criteria:


1. Fairness : Make sure each process gets its fair share of the CPU.
2. Efficiency : Keep the CPU busy 100% of the Time.
3. Response Time : Minimize response time for the interactive users.
4. Turnaround Time: Minimize the time, batch users must wait for the output.
5. Throughput: Maximize the number of jobs processed per hour.
The preemptive scheduling strategy allows the process to temporarily stop it execution
and assign another process to the CPU. On the other hand, non-preemptive scheduling
strategy allows the process to complete its execution.

1.1 Round Robin scheduling


Round Robin is an oldest scheduling algorithm. Each process was assigned a time
interval (quantum). If the quantum time expired or the process was finished the job
within this quantum time, then process switching will occur. If the quantum time runs
out without finishing the entire job, then the process will be placed at the end of the
list. It gives equal chance to all process.
Assigning too small quantum time will causes more process switching – reduce
the efficiency of the CPU. It is good for I/O bound and CPU bound process.

1.2 Priority Scheduling


Each process is assigned a priority and the runnable process with the highest priority is
allowed to run. The scheduler may decrease the priority of the currently running process at each
clock tick to prevent high priority processes from running indefinitely. If the priority comes down
under the next highest priority then process switching will occur.
1.3 Shortest Job First (SJF)
The run time of each process must be known in advance. It selects the process from the
READY queue, which has the shortest estimated run time and assigns the CPU to that job. It is a
priority scheme.
What will happen for long job?

1.4 First Come First Served (FCFS)


Processes are assigned to the CPU according to their arrival order and it allows to complete it
job. Therefore it is a Non-Preemptive scheduling.

Question:- Five batch jobs arrived at almost same time. The estimated running times 10, 66, 2, 4
and 8 min. Their priorities are 3, 5, 2, 1 and 4 respectively. For each of the following scheduling
algorithms determine the mean process turnaround time. ( ignore process switching overhead )
Round robin
Priority scheduling
First come First serve
Shortest job first

Draw the Gantt chart to show the process scheduling when applying the FCFS, SJF, and Round
Robin(quantum = 4ms) preemptive methods to the processes given below. Draw separate Gantt
chart for each of the methods.

Process Arrival Time Burst Time(ms) Priority


P1 8 6 1(high)
P2 12 16 2
P3 0 8 3
P4 4 10 4
P5 10 20 5(low)

Find the following for each scheduling method given above.


 average turnaround time.
 average wait time.
 average response time.
2. Deadlock
Computer systems are full of resources that can be used by only one process at a time. A
process need exclusive access to all resources it was acquired.

Question:- Explain what is preemptable resource and Non-preemptable resource.

The sequence of events required to use a resource is:


i. Request the resource
ii. Use the resource
iii. Release the resource.

If the resource is not available when it is requested, the requesting process is forced to wait. In
some O/S the process is automatically blocked when a resource request fails, and awakened
when it becomes available.
The Deadlock is a situation where more than one processes are in waiting state for an
event occur, and that event will never occur, therefore processes are wait forever. Each
process will wait for the release of the resources from other process and that will never
occur. When resources are shared among a large no of users each of whom maintains
exclusive control over particular resources allocated to the user, it is possible for dead
locks to develop in which processes of some users will never be able to finish.

Four conditions for the deadlock


 Mutual exclusion condition: Once a process has been allocated to a particular resource,
it has exclusive use of the resource. No other processes can use the resource while it is
allocated to a process.
 Hold and wait condition: A process may hold a resource at the same time it request
another resource.
 No preemption condition: Resources previously granted cannot be forcibly taken away
from a process. They must be explicitly released by the process holding them
 Circular wait condition: There must be a circular chain of two or more processes, each
of which is waiting for a resource held by the next member of the chain
2.1 Deadlock Modelling
Conditions are modeled with a directed graph. The nodes of the graph are process
and resource. The circle denotes the process and the square denote the resource. An arc
from resource to process indicates granted, and an arc from process to resource indicate
that the process is currently blocked waiting for that resource.

Resource Allocation Graph

Pr Re

Pr Re

Question:- Draw the RAG to the following request/release sequence.


A request R
B request S
C request T
A request S
B request T
C request R

 Deadlock detection
 Deadlock recovery
 Deadlock avoidance
 Deadlock prevention

Deadlock detection
Use the resource allocation graph to find out the circular wait.
Deadlock Avoidance
The system must be able to decide whether granting a resource is safe or not, only
make allocation when it is safe. Certain information must be available in advance to
apply the avoidance.

Safe and Unsafe: - it is safe state, if it is possible for the OS to allow all current process to
complete their jobs within a finite time. If not, then the current system state is called
unsafe, only when the allocation results in safe states rather than unsafe states.

Bankers Algorithm
The OS will accept a process’s request if that process's maximum need for a
resource does not exceed. A process may obtain or release resource one by one. Some
times a process may have to wait to obtain an additional resource, but the OS guarantees
a finite wait. If the OS is able to satisfy a process’s maximum need for tape drives, then
the user guarantees that the tape drive will be used and released to the OS within a finite
time.

E.g.:- Safe state


Suppose a system has 12 equivalent tape drives and 3 users sharing the drives.

Current loan Max. needed


Process-1 1 4
Process-2 4 6
Process-3 5 8

Available - 2

If two available drives are given to Process-2, fulfilling process-2’s maximum need, the
process-2 may run to completion. Process-2 will release all 6 resources. These can be
assigned to process-1 and process-3.

E.g.:- Safe state


Current Max. needed
loan
Process-1 8 10 Available - 1

Process-2 2 5
Process-3 1 3

Suppose Process-1 requests a resource and it was granted the available resource.
Therefore, a three way deadlock could occur if each process requests at least one more
resource before releasing any drives. An unsafe state leads to a deadlock.

Explain the banker’s algorithm for multiple resources

Deadlock prevention
Ensure that at least one of the four conditions is not satisfied, and then deadlock is
impossible.

 Denying Mutual exclusion condition


It is not possible.

 Denying hold and wait condition


Require all processes to request all their resources before starting execution. If every
thing is available the process will be allocated. If resources are busy nothing will be
allocated.
(Difficult to know all the resources in advance)
 Denying No preemption condition
When a process holding resources is denied a request for additional resources that
process must release resources it is holding and if necessary, request them again, together
with the additional resources.
 Denying Circular wait condition
Assign a global numbering to all resources, and a process must request resources in linear
ascending order.
Process A currently holds a resource with number k, request another resource with number m. it
will be granted if k < otherwise A must release all the resources and restart the resource request.

Deadlock recovery
Usually several process will lose some or all of the work they have done. In current
systems recovery is ordinarily performed by forcibly removing a process from the system and
reclaiming its resource. The removed process is lost but the remaining process may now be able
to complete. Some times it is necessary to remove several processes until sufficient resources
have been reclaimed to allow remaining process to finish.
Recovery somehow seems like an inappropriate term here because some processes are killed for
the benefit of others .Process may be removed according to some priority order.

You might also like