Os Merged
Os Merged
Os Merged
Scheduling
Content:
- Processes Scheduling
- CPU Scheduling
- Pre-emptive Scheduling
- Non-Pre-emptive Scheduling
- Resource allocation and Management
- Deadlocks
- Deadlock Handling Mechanisms
Scheduling:
• While most programmes have some alternative CPU cycle shuffling and waiting for some
form of I/O.
• The time spent waiting for I/O is wasted in a simple machine running a single operation.
• A scheduling mechanism enables the CPU to be used by one process while another is
waiting for I/O.
• The idea is to make the operating system construction as "effective" and "reasonable" as
possible. It is subject to different in often complex situations, and often subject to
changing policy objectives.
CPU Cycle:
CPU cycle generally, the time needed for one basic processor operation, such as an
addition, to be executed; this time is usually the reciprocal clock rate. The word was
traditionally used for the time needed for one basic (e.g. add or subtract) computer
instruction to be fetched and executed.
CPU Scheduler:
It is the task of the CPU Scheduler to pick another process from the ready
queue to run next once the CPU becomes idle.
A FIFO queue is not necessarily the storage structure for the ready queue and
the algorithm used to pick the next process.
As well as various customizable parameters for each algorithm, there are many
alternatives to choose from, which is the fundamental subject of this entire
module.
Pre-emptive and non pre- emptive Scheduling:
One of the following situation, CPU scheduling decisions take place:
ii) Whenever a process transitions from the running state to the waiting state, such
as a wait() system call for an I/O request or invocation.
iii) Whenever a process switches from the waiting state to the ready state, say at
completion of I/O or a return from wait( ).
iv) Whenever a process switches from the running state to the ready state, for
example in response to an interrupt.
Dispatcher:
- The dispatcher is the module that gives control of the CPU to the process selected
by the scheduler. This function involves:
• Switching context.
• Switching to user mode.
• Jumping to the proper location in the newly loaded program.
- There are several different criteria to consider when trying to select the
"best" scheduling algorithm for a particular situation and environment,
including:
P1 P2 P3
0 24 27 30
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
FCFS
First-Come First-Serve Scheduling, FCFS:
First-Come First-Serve Scheduling, FCFS:
Shortest-Job-First Scheduling, SJF:
Shortest-Job-First Scheduling, SJF:
Priority Scheduling, PS:
Round Robin (RR)
ROUND ROBIN(RR)
• Mutual Exclusion: One or more than one resource are non-shareable (Only one process can
use at a time)
• Hold and Wait: A process is holding at least one resource and waiting for resources.
• No Preemption: A resource cannot be taken from a process unless the process releases the
resource.
• Circular Wait: A set of processes are waiting for each other in circular form.
Suppose n processes, P1, …. Pn share m identical resource units, which can be
reserved and released one at a time. The maximum resource requirement of process Pi
is Si, where Si > 0.
Deadlock can be solved by preventing any one of the conditions from
holding
Detection and Recovery
Resource Allocation graph describes the deadlock more precisely:
DEADLOCK EXAMPLES
SYSTEM MODEL
• R = {R1, R2, …, Rm}, the set consisting of all resource types in the system
• Pi requests instance of Rj Pi
Rj
• Pi is holding an instance of Rj
Pi
Rj
EXAMPLE OF A RESOURCE ALLOCATION GRAPH
RESOURCE ALLOCATION GRAPH WITH A DEADLOCK
GRAPH WITH A CYCLE BUT NO DEADLOCK
BASIC FACTS
In deadlock avoidance, the request for any resource will be granted if the resulting
state of the system doesn't cause deadlock in the system. The state of the system will
continuously be checked for safe and unsafe states.
In order to avoid deadlocks, the process must tell OS, the maximum number of
resources a process can request to complete its execution.
The simplest and most useful approach states that the process should declare the
maximum number of resources of each type it may ever need. The Deadlock
avoidance algorithm examines the resource allocations so that there can never be a
circular wait condition.
Example 1:
Example 2 :
DEADLOCK HANDLING MECHANISMS
• Deadlock prevention
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption and
circular wait holds simultaneously. If it is possible to violate one of the four conditions at
any time then the deadlock can never occur in the system.
Deadlock avoidance
In deadlock avoidance, the operating system checks whether the system is in safe state
or in unsafe state at every step which the operating system performs. The process
continues until the system is in safe state. Once the system moves to unsafe state, the
OS has to backtrack one step.
• Deadlock detection and recovery
This approach let the processes fall in deadlock and then periodically check whether
deadlock occur in the system or not. If it occurs then it applies some of the recovery
methods to the system to get rid of deadlock.
RECOVERY FROM DEADLOCK: PROCESS TERMINATION
• Rollback – return to some safe state, restart process for that state
Module 4
Concurrency
2
Shared memory region resides in the address space of the process creating
the shared memory
Other processes can attach with this shared memory segment for
communication
This concept allow one process to access the memory space of others
Reading and writing data in the shared memory creates the exchange of
information between the processes
Simultaneous writing on the same location should be avoided
Ex of Cooperating Processes: Producer – Consumer Problem
Producer process produces the product that consumed by the Consumer
process
Shared Memory – Producer Consumer Problem 7
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
test_and_set Instruction
Definition:
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
1.Executed atomically
2.Returns the original value of passed parameter
3.Set the new value of passed parameter to “TRUE”.
Solution using test_and_set()
Shared Boolean variable lock, initialized to FALSE
Solution:
do {
while (test_and_set(&lock))
; /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);
compare_and_swap Instruction
Definition:
int compare _and_swap(int *value, int expected, int new_value) {
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
1.Executed atomically
2.Returns the original value of passed parameter “value”
3.Set the variable “value” the value of the passed parameter “new_value”
but only if “value” ==“expected”. That is, the swap takes place only under
this condition.
Solution using compare_and_swap
Shared integer “lock” initialized to 0;
Solution:
do {
while (compare_and_swap(&lock, 0, 1) != 0)
; /* do nothing */
/* critical section */
lock = 0;
/* remainder section */
} while (true);
Mutex Locks
OS designers build software tools to solve critical section
problem
Simplest is mutex lock
Protect a critical section by first acquire() a lock then
release() the lock
Boolean variable indicating if lock is available or not
Calls to acquire() and release() must be atomic
Usually implemented via hardware atomic instructions
But this solution requires busy waiting
This lock therefore called a spinlock
acquire() and release()
acquire() {
while (!available)
; /* busy wait */
available = false;
}
release() {
available = true;
}
do {
acquire lock
critical section
release lock
remainder section
} while (true);
31
32
33
34
Bakery Algorithm
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Bounded Buffer Problem (Cont.)
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
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 considered – all involve
some form of priorities
Shared Data
Data set
Semaphore rw_mutex initialized to 1
Semaphore mutex initialized to 1
Integer read_count initialized to 0
Readers-Writers Problem (Cont.)
The structure of a writer process
do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
Readers-Writers Problem (Cont.)
The structure of a reader process } while (true);
do {
wait(mutex);
read_count++; The structure of a writer process
if (read_count == 1)
wait(wrt); do {
signal(mutex); wait(wrt);
... ...
/* writing is performed */
/* reading is
performed */ ...
signal(wrt);
...
} while (true);
wait(mutex);
read count--;
if (read_count == 0)
signal(wrt);
signal(mutex);
51
Dining-Philosophers Problem
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
What is the problem with this algorithm?
55
Dining-Philosophers Problem Algorithm (Cont.)
Deadlock handling
Allow at most 4 philosophers to be sitting simultaneously at
the table.
Allow a philosopher to pick up the forks only if both are
available (picking must be done in a critical section.
Use an asymmetric solution -- an odd-numbered
philosopher picks up first the left chopstick and then the
right chopstick. Even-numbered philosopher picks up first
the right chopstick and then the left chopstick.
Problems with Semaphores
Incorrect use of semaphore operations:
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
Synchronization
IPC in Unix
Inter process communication (IPC) refers to the coordination of
activities among cooperating processes. A common example of
this need is managing access to a given system resource.
References