Os Notes
Os Notes
2
CPU SCHEDULING
4 Basic Concepts
In a single-processor system, only one process can run at a time. Others
must wait until the CPU is free and can be rescheduled.
The objective of multiprogramming is to have some process running at all
times, to maximize CPU utilization. The idea is relatively simple.
Several processes are kept in memory at one time. When one process has
to wait, the operating system takes the CPU away from that process and
gives the CPU to another process. This pattern continues.
Every time one process has to wait, another process can take over use of
the CPU.
Scheduling of this kind is a fundamental operating-system function.
The CPU is, of course, one of the primary computer resources. Thus, its
scheduling is central to operating-system design.
5
CPU Scheduler
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
P4 P1 P3 P2
0 3 9 16 24
Process P1 is started at time 0, since it is the only process in the queue. Process P2
arrives at time 1. The remaining time for process P1 (7 milliseconds) is larger than
the time required by process P2 (4 milliseconds), so process P1 is preempted, and
process P2 is scheduled.
Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec
Priority Scheduling
20
A priority is associated with each process, and the CPU is allocated to the
process with the highest priority. Equal-priority processes are scheduled in FCFS
order. Priorities are generally indicated by some fixed range of numbers
As an example, consider the following set of processes, assumed to have arrived
at time 0 in the order P1, P2, ···, P5, with the length of the CPU burst given in
milliseconds:
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Starvation
A major problem with priority scheduling algorithms is indefinite blocking, or
starvation.
A priority scheduling algorithm can leave some low priority processes waiting
indefinitely. In a heavily loaded computer system, a steady stream of higher-priority
processes can prevent a low-priority process from ever getting the CPU. This is
known as starvation.
A solution to the problem of indefinite blockage of low-priority processes is aging.
Aging involves gradually increasing the priority of processes that wait in the system
for a long time.
22 Round Robin(RR) Scheduling
The round-robin (RR) scheduling algorithm is designed especially for
timesharing systems. It is similar to FCFS scheduling, but preemption is added
to enable the system to switch between processes.
A small unit of time, called a time quantum or time slice, is defined. A time
quantum is generally from 10 to 100 milliseconds in length. The CPU
scheduler goes around the ready queue, allocating the CPU to each
process for a time interval of up to 1 time quantum.
To implement RR scheduling, new processes are added to the tail of the
ready queue. The CPU scheduler picks the first process from the ready
queue, sets a timer to interrupt after 1 time quantum, and dispatches the
process.
After 1 time quantum, the timer will go off and will cause an interrupt to the
operating system. The process will be put at the tail of the ready queue. The
CPU scheduler will then select the next process in the ready queue.
23 Example of RR with Time Quantum = 4
The average waiting time under the RR policy is often long. Consider the
following set of processes that arrive at time 0, with the length of the CPU
burst given in milliseconds:
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 time.
• The average waiting time for this schedule - P1 waits for 6 milliseconds (10 - 4),
P2 waits for 4 milliseconds, and P3 waits for 7 milliseconds. Thus, the average
waiting time is 17/3 = 5.66 milliseconds.
Multilevel Queue Scheduling
24
Another class of scheduling algorithms that has been created for
situations in which processes are easily classified into different groups.
For example: A common division is made between foreground(or
interactive) processes and background (or batch) processes. These two
types of processes have different response-time requirements, and so
might have different scheduling needs. In addition, foreground
processes may have priority over background processes.
A multi-level queue scheduling algorithm partitions the ready queue
into several separate queues. The processes are permanently assigned
to one queue, generally based on some property of the process, such
as memory size, process priority, or process type. Each queue has its
own scheduling algorithm.
For example: separate queues might be used for foreground and
background processes. The foreground queue might be scheduled by
Round Robin algorithm, while the background queue is scheduled by
an FCFS algorithm. Scheduling must be done between the queues also.
Consider an example of a multilevel queue-scheduling algorithm with
25 five queues:
System Processes
Interactive Processes
Interactive Editing Processes
Batch Processes
Student Processes
Each queue has absolute priority over lower-priority queues. No
process in the batch queue, for example, could run unless the
queues for system processes, interactive processes, and interactive
editing processes were all empty. If an interactive editing process
entered the ready queue while a batch process was running, the
batch process will be preempted.
26 Multilevel Feedback Queue Scheduling
When the multilevel queue scheduling algorithm is used,
processes are permanently assigned to a queue when they
enter the system. processes do not move from one queue to
the other, since processes do not change their foreground or
background nature.
The multilevel feedback queue scheduling algorithm, in
contrast, allows a process to move between queues. The
idea is to separate processes according to the characteristics
of their CPU bursts.
If a process uses too much CPU time, it will be moved to a
lower-priority queue. Similarly, a process that waits too long in
a lower-priority queue may be moved to a higher-priority
queue. This form of aging prevents starvation.
Example of Multilevel Feedback
27 Queue
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new job enters queue Q0 which is served
FCFS. 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 FCFS and
receives 16 additional milliseconds. If it still
does not complete, it is preempted and
moved to queue Q2.
PROCESS SYNCHRONIZATION
29
Processes are categorized as one of the following two types:
Independent Process: The execution of one process does not affect
the execution of other processes.
Cooperating Process: A process that can affect or be affected by
other processes executing in the system.
Cooperating processes can directly share a logical address space.
Concurrent access to shared data may result in data
inconsistency.
Processes Synchronization is the way by which processes that share
the same memory space are managed in an operating system.
It helps maintain the consistency of data by using variables or
hardware so that only one process can make changes to the
shared memory at a time.
30 The Critical-Section Problem
Consider a system consisting of n processes {P0, P1, ..., Pn−1}. Each process
has a segment of code, called a critical section, in which the process
may be changing common variables, updating a table, writing a file,
and so on.
When one process is executing in its critical section, no other process is
allowed to execute in its critical section. That is, no two processes are
executing in their critical sections at the same time.
The critical-section problem is to design a protocol that the processes
can use to cooperate.
Each process must request permission to enter its critical section. The
section of code implementing this request is the entry section.
The critical section may be followed by an exit section. The remaining
code is the remainder section.
31
Advantages:
Accessing a file that has been allocated contiguously is easy.
Both the Sequential and Direct Accesses are supported by
this. For sequential access, the file system remembers the disk
address of the last block referenced and, when necessary,
reads the next block. For direct access, the address of the ith
block of the file which starts at block b can easily be
obtained as (b+i).
This is extremely fast since the number of seeks are minimal
because of contiguous allocation of file blocks.
72
Disadvantages:
This method suffers from external fragmentation. This makes it
inefficient in terms of memory utilization.
Another problem with contiguous allocation is determining how
much space is needed for a file. When the file is created, the
total amount of space it will need must be found and
allocated. Increasing file size is difficult because it depends on
the availability of contiguous memory at a particular instance.
73
Linked Allocation
With linked allocation, each file is a
linked list of disk blocks; the disk blocks
may be scattered anywhere on the
disk.
The directory contains a pointer to the
first and last blocks of the file.
For example, a file of five blocks might
start at block 9 and continue at block
16, then block 1, then block 10, and
finally block 25. Each block contains a
pointer to the next block. These pointers
are not made available to the user.
74
Advantages:
To create a new file, we simply create a new entry in the
directory. With linked allocation, each directory entry has a
pointer to the first disk block of the file. This pointer is initialized to
null to signify an empty file.
A write to the file causes the free-space management system to
find a free block, and this new block is written to and is linked to
the end of the file. To read a file, we simply read blocks by
following the pointers from block to block.
There is no external fragmentation with linked allocation, and any
free block on the free-space list can be used to satisfy a request.
The size of a file need not be declared when the file is created. A
file can continue to grow as long as free blocks are available.
75
Disadvantages:
Because the file blocks are distributed randomly on the disk, a
large number of seeks are needed to access every block
individually. This makes linked allocation slower.
It can be used effectively only for sequential-access files. To
find the ith block of a file, we must start at the beginning of that
file and follow the pointers until we get to the ith block.
Pointers required in the linked allocation incur some extra
overhead.
The files are linked together by pointers scattered all over the
disk, and problem arises if a pointer were lost or damaged.
76
An important variation on linked allocation is the use of a file-
allocation table (FAT).
The table has one entry for each disk block and is indexed by block
number.
The directory entry contains the block number of the first block of the
file.
The table entry indexed by that block number contains the block
number of the next block in the file. This chain continues until it
reaches the last block, which has a special end-of-file value as the
table entry.
An unused block is indicated by a table value of 0. Allocating a new
block to a file is a simple matter of finding the first 0-valued table
entry and replacing the previous end-of-file value with the address of
the new block. The 0 is then replaced with the end-of-file value.
77
An illustrative example is the FAT structure for a file consisting of disk blocks 217,
618, and 339.
78
Indexed Allocation
Linked allocation solves the external-fragmentation and size-
declaration problems of contiguous allocation.
However, in the absence of a FAT, linked allocation cannot
support efficient direct access, since the pointers to the blocks
are scattered with the blocks themselves all over the disk and
must be retrieved in order.
Indexed allocation solves this problem by bringing all the pointers
together into one location: the index block.
In this scheme, a special block known as the Index
block contains the pointers to all the blocks occupied by a file.
79
Each file has its own index block.
The ith entry in the index block contains the disk address of the ith file
block. The directory entry contains the address of the index block. To find
and read the ith block, we use the pointer in the ith index-block entry.
80 Free Space Management
Since disk space is limited, we need to reuse the space from
deleted files for new files,
To keep track of free disk space, the system maintains a free-
space list. The free-space list records all free disk blocks.
To create a file, we search the free-space list for the required
amount of space and allocate that space to the new file. This
space is then removed from the free-space list. When a file is
deleted, its disk space is added to the free-space list.
The process of looking after and managing the free
blocks of the disk is called free space management.
81
Advantages –
Simple to understand.
Finding the first free block is efficient. It requires scanning the words (a group
of 8 bits) in a bitmap for a non-zero word. (A 0-valued word has all bits 0). The
first free block is then found by scanning for the first 1 bit in the non-zero word.
The block number can be calculated as:
(number of bits per word) *(number of 0-values words) + offset of bit first bit 1
in the non-zero word .
84 Linked List
Another approach to free-space management is to link together
all the free disk blocks, keeping a pointer to the first free block in a
special location on the disk and caching it in memory.
This first block contains a pointer to the next free disk block, and so
on.
Consider the example in which blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13,
17, 18, 25, 26, and 27 were free and the rest of the blocks were
allocated.
In this situation, we would keep a pointer to block 2 as the first free
block. Block 2 would contain a pointer to block 3, which would
point to block 4, which would point to block 5, which would point
to block 8, and so on
This scheme is not efficient; to traverse the list, we must read each
block, which requires substantial I/O time.
85
86
Grouping
This approach stores the address of the free blocks in the first free
block.
The first free block stores the address of some, say n free blocks.
Out of these n blocks, the first n-1 blocks are actually free and the
last block contains the address of next free n blocks.
An advantage of this approach is that the addresses of a group of
free disk blocks can be found easily.
87
Counting