23CB401 - UNIT 2 - Notes
23CB401 - UNIT 2 - Notes
UNIT 2
PROCESS MANAGEMENT
CPU Scheduler
Whenever the CPU becomes idle, the operating system must select one of the processes
in the ready queue to be executed.
The selection process is carried out by the short-term scheduler (or CPU scheduler).
The ready queue is not necessarily a first-in, first-out (FIFO) queue. It may be a FIFO
queue, a priority queue, a tree, or simply an unordered linked list.
Preemptive Scheduling
CPU scheduling decisions may take place under the following four circumstances:
1. When a process switches from the running state to the waiting state
2. When a process switches from the running state to the ready state
3. When a process switches from the waiting state to the ready state
4. When a process terminates
Under 1 & 4 scheduling scheme is non preemptive.
Otherwise the scheduling scheme is preemptive.
Non-preemptive Scheduling
In non preemptive scheduling, once the CPU has been allocated a process, the process
keeps the CPU until it releases the CPU either by termination or by switching to the
waiting state.
This scheduling method is used by the Microsoft windows environment.
Dispatcher
The dispatcher is the module that gives control of the CPU to the process selected by the
short-term scheduler.
23CB401-OPERATING SYSTEMS
1. CPU utilization: The CPU should be kept as busy as possible. CPU utilization may
range from 0 to 100 percent. In a real system, it should range from 40 percent (for a
lightly loaded system) to 90 percent (for a heavily used system).
2. Throughput: It is the number of processes completed per time unit. For long processes,
this rate may be 1 process per hour; for short transactions, throughput might be 10
processes per second.
3. Turnaround time: The interval from the time of submission of a process to the time of
completion is the turnaround time. Turnaround time is the sum of the periods spent
waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing
I/O.
4. Waiting time: Waiting time is the sum of the periods spent waiting in the ready queue.
5. Response time: It is the amount of time it takes to start responding, but not the time that
it takes to output that response.
Example:
Process Burst Time
P1 24
P2 3
P3 3
23CB401-OPERATING SYSTEMS
If the processes arrive in the order PI, P2, P3, and are served in FCFS order, we get the
result shown in the following Gantt chart:
Gantt Chart
The FCFS algorithm is particularly troublesome for time – sharing systems, where it
is important that each user get a share of the CPU at regular intervals.
P2 1 4
P3 2 9
P4 3 5
Priority Scheduling
A priority is associated with each process, and the CPU is allocated to the process with the
highest priority.( smallest integer highest priority).
Example :
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Waiting time
P1 = 26 – 20 = 6
P2 = 4
P3 = 7 (6+4+7 / 3 = 5.66 ms)
Average waiting time is 17/3 = 5.66 milliseconds.
The performance of the RR algorithm depends heavily on the size of the time–quantum.
If time-quantum is very large (infinite) then RR policy is same as FCFS policy.
If time quantum is very small, RR approach is called processor sharing and appears to the
users as though each of n process has its own processor running at 1/n the speed of real
processor.
while (true)
{
while (counter == 0)
; /* do nothing */
next consumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
counter--;
/* consume the item in next consumed */
}
Let the current value of counter be 5. If producer process and consumer process execute
the statements counter++ and counter—concurrently then the value of counter may be 4,5
or 6 which is incorrect. To explain this further, counter ++ may be implemented in
machine language as follows:
register1 = counter
register1 = register1 + 1
counter = register1
A situation like this, where several processes access and manipulate the same data concurrently
and the outcome of the execution depends on the particular order in which the access takes place,
is called a race condition.
To guard against the race condition above, we need to ensure that only one process at a time can
be manipulating the variable counter.
do {
entry section
critical section
exit section
remainder section
} while (1);
Two general approaches are used to handle critical sections in operating systems:
preemptive kernels and non-preemptive kernels.
A preemptive kernel allows a process to be preempted while it is running in kernel mode.
A non-preemptive kernel does not allow a process running in kernel mode to be
preempted; a kernel-mode process will run until it exits kernel mode, blocks, or
voluntarily yields control of the CPU.
Obviously, a non-preemptive kernel is essentially free from race conditions on kernel
data structures, as only one process is active in the kernel at a time.
We cannot say the same about preemptive kernels, so they must be carefully designed to
ensure that shared kernel data are free from race conditions.
Preemptive kernels are especially difficult to design for SMP architectures, since in these
environments it is possible for two kernel-mode processes to run simultaneously on
different processors.
These primitive operations can be used directly as synchronization tools, or they can be used to
form the foundation of more abstract synchronization mechanisms.
Memory Barriers
How a computer architecture determines what memory guarantees it will provide to an application
program is known as its memory model. In general, a memory model falls into one of two
categories:
1. Strongly ordered, where a memory modification on one processor is immediately visible to all
other processors.
2. Weakly ordered, where modifications to memory on one processor may not be immediately
visible to other processors.
Memory models vary by processor type, so kernel developers can not make any assumptions
regarding the visibility of modifications to memory on a shared-memory multiprocessor. To address
this issue, computer architectures provide instructions that can force any changes in memory to be
propagated to all other processors, thereby ensuring that memory modifications are visible to
threads running on other processors. Such instructions are known as memory barriers or memory
fences. When a memory barrier instruction is performed, the system ensures that all loads and stores
are completed before any subsequent load or store operations are performed. Therefore, even if
instructions were reordered, the memory barrier ensures that the store operations are completed in
memory and visible to other processors before future load or store operations are performed.
Hardware Instructions
Many modern computer systems provide special hardware instructions that allow us either to test
and modify the content of a word or to swap the contents of two words atomically—that is, as one
uninterruptible unit. We can use these special instructions to solve the critical-section problem in a
relatively simple manner. Rather than discussing one specific instruction for one specific machine,
we abstract the main concepts behind these types of instructions by describing the test and set() and
compare and swap() instructions.
Atomic Variables
Typically, the compare and swap() instruction is not used directly to provide mutual exclusion.
Rather, it is used as a basic building block for constructing other tools that solve the critical-section
problem. One such tool is an atomic variable, which provides atomic operations on basic data types
such as integers and booleans. Incrementing or decrementing an integer value may produce a race
condition. Atomic variables can be used in to ensure mutual exclusion in situations where there may
be a data race on a single variable while it is being updated, as when a counter is incremented.
Mutex(Mutual Exclusion) lock is a simple software tool that solves the critical section
problem.
The mutex lock is used to protect critical regions and thus prevent race conditions.
A process must acquire the lock before entering a critical section; it releases the lock
when it exits the critical section.
The acquire() function acquires the lock, and the release() function releases the lock.
A mutex lock has a boolean variable available whose value indicates if the lock is
available or not.
If the lock is available, a call to acquire() succeeds, and the lock is then considered
unavailable.
A process that attempts to acquire an unavailable lock is blocked until the lock is
released.
do {
acquire
lock
critical section
release lock
remainder section
} while (true);
2.2.4 Semaphores
It is a synchronization tool that is used to generalize the solution to the critical section
problem.
A Semaphore S is an integer variable that can only be accessed via two indivisible (atomic)
operations namely
1. wait or P operation ( to test )
2. signal or V operation ( to increment
) wait (s)
{
while(s0)
; s--;
}
signal (s)
{
s++;
}
Mutual Exclusion Implementation using semaphore
do
{
wait(mutex);
critical section
signal(mutex)
remainder section
} while (1);
Semaphore Implementation
The semaphore discussed so far requires a busy waiting. That is if a process is in critical-
section, the other process that tries to enter its critical-section must loop continuously in the
entry code.
To overcome the busy waiting problem, the definition of the semaphore operations wait and
signal should be modified.
When a process executes the wait operation and finds that the semaphore value is not
positive, the process can block itself. The block operation places the process into a waiting
queue associated with the semaphore.
A process that is blocked waiting on a semaphore should be restarted when some other
process executes a signal operation. The blocked process should be restarted by a wakeup
operation which put that process into ready queue.
To implemented the semaphore, we define a semaphore as a record
as: typedef struct {
int value;
struct process *L;
} semaphore;
signal(S)
{
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
}
Types of Semaphores
Counting semaphore – any positive integer value
Binary semaphore – integer value can range only between 0 and 1
2.2.5 Monitors
A monitor is a synchronization construct that supports mutual exclusion and the ability to
wait /block until a certain condition becomes true.
A monitor is an abstract datatype that encapsulates data with a set of functions to operate
on the data.
Characteristics of Monitor
The local variables of a monitor can be accessed only by the local functions.
A function defined within a monitor can only access the local variables of a monitor and its
formal parameter.
Only one process may be active within the monitor at a time.
Syntax of a Monitor
monitor monitor-name
{
// shared variable declarations
function P1 (…) { …. }
…
function Pn (…) {……}
initialization code (….) {
}
}
condition x, y;
x.wait (); // a process that invokes the operation is suspended.
x.signal (); //resumes one of the suspended processes(if any)
Bounded-Buffer Problem:
It is commonly used to illustrate the power of synchronization primitives. Assume that the pool
consists of n buffers, each capable of holding one item. The mutex semaphore provides mutual
exclusion for accesses to the buffer pool and is initialized to the value 1. The empty and full
semaphores count the number of empty and full buffers, respectively. The semaphore empty is
initialized to the value n; the semaphore full is initialized to the value 0.
The code for the producer process is shown, the code for the consumer process is also shown.
Note the symmetry between the producer and the consumer. Interpret this code as the producer
producing full buffers for the consumer, or as the consumer producing empty buffers for the
producer.
In our problem, the producer and consumer processes share the following
data structures:
int n;
semaphore mutex = 1;
semaphore empty = n;
Bounded-Buffer Problem Producer Process:
do {
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
} while (1);
do {
wait(full)
wait(mutex);
…
remove an item from buffer to nextc
…
signal(mutex);
signal(empty);
…
consume the item in nextc
…
} while (1);
Readers-Writers Problem:
A data object (such as a file or record) is to be shared among several concurrent processes. Some of
these processes may want only to read the content of the shared object, whereas others may want to
update (that is, to read and write) the shared object. We distinguish between these two types of
processes by referring to those processes that are interested in only reading as readers, and to the
rest as writers. Obviously, if two readers access the shared data object simultaneously, no adverse
effects will result.
To ensure that these difficulties do not arise, we require that the writers have exclusive access to
the shared object. This synchronization problem is referred to as the readers-writers problem.
Shared data
Initially
wait(wrt);
…
writing is performed
…
signal(wrt);
wait(mutex);
readcount++;
if (readcount == 1)
wait(rt);
signal(mutex);
…
reading is performed
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex):
Dining-Philosophers Problem:
Consider five philosophers who spend their lives thinking and eating. The philosophers share a
common circular table surrounded by five chairs, each belonging to one philosopher. In the center
of the table is a bowl of rice, and the table is laid with five single chopsticks (shown in Figure given
below) When a philosopher thinks, she does not interact with her colleagues. From time to time, a
philosopher gets hungry and tries to pick up the two chopsticks that are closest to her (the
chopsticks that are between her and her left and right neighbors). A philosopher may pick up only
one chopstick at a time. Obviously, she cannot pick up a chopstick that is already in the hand of a
neighbor. When a hungry philosopher has both her chopsticks at the same time, she eats without
releasing her chopsticks
Shared data
semaphore chopstick[5];
Initially all values are 1
2.3 DEADLOCKS
Definition: A process requests resources. If the resources are not available at that time, the
process enters a wait state. Waiting processes may never change state again because the
resources they have requested are held by other waiting processes. This situation is called a
deadlock.
A process must request a resource before using it, and must release resource after using it.
1. Request: If the request cannot be granted immediately then the requesting process must
wait until it can acquire the resource.
2. Use: The process can operate on the resource
3. Release: The process releases the resource.
Process states
Process P1 is holding an instance of resource type R2, and is waiting for an instance of resource
type R1.
Resource Allocation Graph with a deadlock
Process P2 is holding an instance of R1 and R2 and is waiting for an instance of resource type
R3.Process P3 is holding an instance of R3.
P1->R1->P2->R3->P3->R2->P1
P2->R3->P3->R2->P2
1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection and Recovery
Deadlock Prevention:
This ensures that the system never enters the deadlock state.
Deadlock prevention is a set of methods for ensuring that at least one of the necessary
conditions cannot hold.
By ensuring that at least one of these conditions cannot hold, we can prevent the
occurrence of a deadlock.
1. Denying Mutual exclusion
Mutual exclusion condition must hold for non-sharable resources.
Printer cannot be shared simultaneously shared by prevent processes.
Sharable resource - example Read-only files.
If several processes attempt to open a read-only file at the same time, they can be granted
simultaneous access to the file.
A process never needs to wait for a sharable resource.
Deadlock Avoidance:
Deadlock avoidance request that the OS be given in advance additional information
concerning which resources a process will request and use during its life time. With this
information it can be decided for each request whether or not the process should wait.
To decide whether the current request can be satisfied or must be delayed, a system must
consider the resources currently available, the resources currently allocated to each
process and future requests and releases of each process.
Safe State
A state is safe if the system can allocate resources to each process in some order and still
avoid a dead lock.
If no cycle exists, then the allocation of the resource will leave the system in a safe state.
If a cycle is found, then the allocation will put the system in an unsafe state.
Banker's algorithm
Available: indicates the number of available resources of each type.
Max: Max[i, j]=k then process Pi may request at most k instances of resource type Rj
Allocation : Allocation[i. j]=k, then process Pi is currently allocated K instances of
resource type Rj
Need : if Need[i, j]=k then process Pi may need K more instances of resource type Rj
Deadlock detection
(i) Single instance of each resource type
If all resources have only a single instance, then we can define a deadlock detection
algorithm that use a variant of resource-allocation graph called a wait for graph.
Resource Allocation Graph
Deadlock Recovery
1. Process Termination
1. Abort all deadlocked processes.
2. Abort one deadlocked process at a time until the deadlock cycle is eliminated.
After each process is aborted , a deadlock detection algorithm must be invoked to
determine where any process is still dead locked.
2. Resource Preemption
Preemptive some resources from process and give these resources to other processes until the
deadlock cycle is broken.
i. Selecting a victim: which resources and which process are to be preempted.
ii. Rollback: if we preempt a resource from a process it cannot continue with its normal
execution. It is missing some needed resource. we must rollback the process to some safe state,
and restart it from that state.
iii. Starvation : How can we guarantee that resources will not always be preempted from
the same process.