OS Unit-3
OS Unit-3
140702
UNIT-3
Content of Unit-3
Inter process Communication:
Race Conditions
Critical Section
Mutual Exclusion
Hardware Solution
Strict Alternation
Peterson’s Solution
The Producer Consumer Problem
Semaphores
Event Counters
Monitors, Message Passing.
Classical IPC Problems: Reader’s & Writer Problem,
Dinning Philosopher Problem, Scheduling, Scheduling
Algorithms.
RACE CONDITION
In Some Operating System ,process that working together may share some
common storage that each one can read and write.the storage may be in main
memory or it may be in shared memory .the location of shared memory do not
change the nature of communication works in practice,let us consider simple but
common examples:a print spooler .when a process wants to print a file ,it enters
the file in special spooler directories.
Imagine that spooler directory has a large number of slots ,number 0 to n each one
capable of holding the file name. Also imagine in and out variable
This is represent in the figure.if process A and process B are used same memory location
than there will be race between this two process to execute the output. Suppose Process A
first used share memory and execute the output but interrupt will occurs than process A
have to wait for a While and same time process B Can be use to the share Memory but
Process A awake and return to the memory and execute again and process B in terms of
that process execute but never get the output. so this situation is treat as race condition.
Race between two process condition is that process share a memory area
Race Condition:-When Two or processes are reading or writing the some shared
data and final result depends on who runs precisely when are called that is call
race condition. means which process execute first that depends the inter process
scheduling of that process.
Grow More Faculty of Degree Engineering Page 2
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: -
140702
CRITICAL SECTION
A Critical Section is a Sequence or block of code run by a thread/process. When one
process executes the critical region than no other process will allow
executing the critical region if these occurs than there will be chance of problem on critical
region. a solution of critical section must be follow the three requirements:
Grow More Faculty of Degree Engineering Page 3
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: -
140702
MUTUAL EXCLUSION
A way of making sure that if one process is using a shared modifiable
data, the other processes will be excluded from doing the same thing.
Formally, while one process executes the shared variable, all other processes
desiring to do so at the same time moment should be kept waiting; when that process
has finished executing the shared variable, one of the processes waiting; while that
process has finished executing the shared variable, one of the processes waiting to do
so should be allowed to proceed. In this fashion, each process executing the shared
data (variables) excludes all others from doing so simultaneously. This is called
Mutual Exclusion.
Note that mutual exclusion needs to be enforced only when processes access
shared modifiable data - when processes are performing operations that do not conflict
with one another they should be allowed to proceed concurrently.
(3)OS SOULTION:-Semaphores,Mutex
Grow More Faculty of Degree Engineering Page 4
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: - 140702
(1)SOFTWARE SOLUTIONS
Software Approches can be implemented for current process that executes on a single
processor or multiprocessor machine with the shared main memory.
Flag[0]=false
Flag[1]=false
Turn = 0/1
Po P1
Flag[0]:-true Flag[1]:-true
While(flag[1]=true) While(flag[0]=true)
{ {
If turn!= 0 If turn!= 1
{ {
Flag[0]=false Flag[1]=false
While(turn!=0) While(turn!=1)
{ {
} }
Flag[0]=true Flag[1]=true
} }
Process indicates an intension to enter the critical region which is tested by outer
while loop if other process has not flagged intent, the critical region can be
entered safely irrespective of the current turn.
Flag[0]=0
Flag[1]=1
Turn;
P0 P1
Flag[0]=1 Flag[1]=1
turn=1 turn=0
While(flag[1]==1 && turn==1) While(flag[0]==1 && turn==0)
{ {
//busy wait //busy wait
} }
// critical section // critical section
….. ……
// end of critical section // end of critical section
The algorithm used two variables flag and turn.if the value of flag is 1 that
indicates that process want to enter in critical region .the variable turn holds the ID
of process whose turn it is.
If one process want to enter critical section and same time another process is
inside the critical region so the process becomes busy wait.
So at a time one process can use the critical region so there is no chance
for problem on scenario of mutual exclusion
This algo is based on analogy of bakery. a numbering machine its entrance so that
each customer is given a unique number .number is increase by one customer
enter into the store.
In Computer world the customer’s will be thread ,identify the letter I obtaind form
a global variable .it’s possible that more than one thread will get the same number
when they request it this cannot be avoid.
The critical section is that part of code that requires exclusive access to resources and
may only be executed by one thread at a time. In the bakery analogy, it is when the
customer trades with the baker and others must wait.
When a thread wants to enter the critical section, it has to check whether it is its turn
to do so. It should check the numbers of every other thread to make sure that it has the
smallest one. In case another thread has the same number, the thread with the smallest
i will enter the critical section first.
Grow More Faculty of Degree Engineering Page 7
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: -
140702
(2)HARDWARE SOLUTIONS
Special machine Instructions are the hardware solution for the mutual Exclusion
The hardware solution is the use of machine instruction named test and set the
variables and to test the value and set it to a stipulated value
Function testset
begin
if i=0 then
begin
i=1
testset:=true
end
else testset:-false
end
(3)OS SOLUTIONS
There are two os solution for problem of mutual exclusion is mutex
and semaphores
Semaphore:- this are a programming construct design by the dijkstra in the
late 1960 model was the operation of railroads
Consider the stretch of rail road in which single track over which only one train
at a time is allowed. Guarding the track is semaphore.
A Train must wait before entering the single track until the semaphore is in
state that permits travel.
When train enter the track than semaphore tries to prevent other train to enter into
the track.
In computer version semaphores apperes as a integer variable.the process or thread have to wait
to enter into the critical region until the integer variable becomes 0
Grow More Faculty of Degree Engineering Page 9
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: -
140702
Grow More Faculty of Degree Engineering Page 10
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: -
140702
MUTEX:-Mutexis like small token passing program. token is passed to the particular
process.process which have the token is access the critical region and after using the
critiacal section process must have to relase that token.
We demand of our solution that no reader be kept waiting unless a writer has
already obtained permission to use the resources; i.e. no reader should wait simply
because a writer is waiting for other readers to finish. It is possible that a writer
could wait indefinitly while a stream of readers arrived.
When a reader arrives and a writer is waiting, the reader is suspended behind the
writer instead of being admitted immediately. In this way, a writer has to wait for
readers that were active when it arrived to finish but does not to wait for readers
that came along after it.
The readers-writers problem is commonly seen in database applications, where users are separated
into readers who never modify the database, and writers who read and modify the database. Although
we want only one writer at the same, using a single lock on the database would be overly restrictive,
since many readers can read at the same time.
Constraints
1. A reader should wait when a writer is accessing or waiting for the database (Condition
okToRead). (The writer has a higher priority over reader, since it is more difficult for a
writer to achieve exclusive access of the database.)
2. A writer should wait when there is a reader or a writer accessing the database (Condition
okToWrite).
3. A reader or a writer should wait when someone is modifying global states (Lock lock).
Reader
/ wait until no writers
/ access database
/ wake up waiting writers
Writer
/ wait until no readers or writers
/ access database
/ wake up waiting readers or writers
There are N philosphers sitting around a circular table eating spaghetti and
discussing philosphy. The problem is that each philosopher needs 2 forks to
eat, and there are only N forks, one
between each 2 philosophers. Design an algorithm that the philosophers can follow that
insures that none starves as long as each philosopher eventually stops eating, and such that
the maximum number of philosophers can eat at once.
Grow More Faculty of Degree Engineering Page 15
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: -
140702
Why describe problems this way? Well, the analogous situations in computers are
sometimes so technical that they obscure creative thought. Thinking about philosophers
makes it easier to think abstractly. And many of the early students of this field were
theoreticians who like abstract problems.There are a bunch of named problems - Dining
Philosophers, Drinking PhiliosophersHere’s an approach to the Dining Phils 1 that’s
simple and wrong:
void philosopher() {
while(1) {
sleep();
get_left_fork();
get_right_fork();
eat();
put_left_fork();
put_right_fork();
If every philosopher picks up the left fork at the same time, noone gets to eat
- ever.
Some other suboptimal alternatives:
• Pick up the left fork, if the right fork isn’t available for a given time, put the
left fork down,
wait and try again. (Big problem if all philosophers wait the same time - we get
the same failure mode as before, but repeated.) Even if each philosopher waits a
different random time, an
MONITORS
A primary aim of an operating system is to share a computer installation among many
programs making unpredictable demands upon its resources. A primary tusk of its designer
is therefore to construct resource allocation (or scheduling) algorithms for resources of
various kinds (main store, drum store, magnetic tape handlers, consoles, etc.). In order to
simplify his task, he should try to construct separate schedulers for each class of resource.
Each scheduler will consist of a certain amount of local administrative data, together with
some procedures and functions which are called by programs wishing to acquire and release
resources. Such a collection of associated data and procedures is known as a monitor
Monitor introduce the new name scope that shared data and the procedural that
are allow to access the data .we can say that monitors are 70% used to solve the
classical problem of an operating system.
The execution of a process consists of a cycle of CPU execution and I/O wait.
A process begins with a CPU burst, followed by an I/O burst, followed by
another CPU burst and so on. The last CPU burst will end will a system request to
terminate the execution.
The CPU burst durations vary from process to process and computer to computer.
An I/O bound program has many very short CPU bursts.
A CPU bound program might have a few very long CPU bursts.
Grow More Faculty of Degree Engineering Page 19
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: -
140702
Types of Scheduling
The key to the multiprogramming is scheduling. There are four types of scheduling that
an OS has to perform. These are:
o Long Term scheduling
The long term scheduling determines which programs are admitted to the system
for processing. Thus, it controls the level of multiprogramming.
Once admitted, a job or a user program becomes a process and is added to
the queue for the short term scheduling (in some cases added to a queue
for medium term scheduling).
Long term scheduling is performed when a new process is created.
The criteria used for long-term scheduling may include first-come-first
serve, priority, expected execution time, and I/O requirements.
o Medium-Term Scheduling
The medium-term scheduling is a part of swapping function. This is a
decision to add a process to those that are at least partially in main
memory and therefore available for execution.
The swapping-in decision is made on the need to manage the degree of
multiprogramming and the memory requirements of the swapped-out
process.
o Short-Term Scheduling
A decision of which ready process to execute next is made in short-term
scheduling.
o I/O Scheduling
The decision as to which process’s pending I/O requests shall be handled
by the available I/O device is made in I/O scheduling.
CPU Scheduler
Whenever, the CPU becomes idle, the OS must select one of the processes in the ready-
queue to be executed.
The selection process is carried out the short-term scheduler or CPU scheduler. The CPU scheduler
selects a process from the ready queue and allocates the CPU to that process.
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the short-term
scheduler; this involves:
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program
Dispatch latency – time it takes for the dispatcher to stop one process and start another
running.
User oriented
Turnaround time: time interval from submission of job until its completion. It includes
actual processing time plus time spent waiting for resources, including the processor. OR
the amount of time between moment a process first enters Ready State and the moment
the process exits Running State for the
last time (Completed).
Waiting time: sum of periods spent waiting in ready queue.
Response time: time interval from submission of job to first response.
Often a process can begin producing some output to the
Grow More Faculty of Degree Engineering
SUBJECT NAME:-OPERATING SYSTEM SUBJECT CODE: -
140702
user while continuing to process the request. Thus this is a better measure than
turnaround time from the user’s point of view.
Normalized turnaround time: ratio of turnaround time to service time.
Service Time: The amount of time process needs to be in running state (Acquired
CPU) before it is completed.
System oriented
CPU utilization: percentage of time CPU is busy. CPU utilization may range
from 0 to 100%. In a real system, it should range from 40% to 90%.
Throughput: number of jobs completed per time unit. This depends on the
average length of the processes.
Service time, wait time, turn around time, and throughput are some of the metrics used to
compare scheduling algorithms.
Any good scheduler should:
Maximize CPU utilization and throughput
Minimize turnaround time, waiting time, response time
AVERAGE T.A.T:-24+27+30/3=27 ms
AVERAGE W.T.:-0+24+27/3=17 ms
(2)SJF (SHORTEST JOB FIRST) SCHEDULING(non-
preemptive)
(3)SJF (Preemptive)
(4)Priority Scheduling(Non-Preemptive)