OS Unit - 4
OS Unit - 4
Race Condition:
A race condition is a situation that may occur inside a critical
section. This happens when the result of multiple thread
execution in critical section differs according to the order in
which the threads execute.
Race conditions in critical sections can be avoided if the
critical section is treated as an atomic instruction. Also,
proper thread synchronization using locks or atomic
variables can prevent race conditions.
Critical Section:
do{
Entry Section
Critical Section
Exit Section
Remainder Section
} while (TRUE);
In the above diagram, the entry sections handles the entry into the critical
section. It acquires the resources needed for execution by the process. The exit
section handles the exit from the critical section. It releases the resources and
also informs the other processes that critical section is free.
Race condition would not have happened if P1 had completed all steps without
preemption ; or P2 had been prevented from executing the function until P1
had completed all steps.
Mutual Exclusion:
Trying:
Critical Section:
Exit:
The process leaves the critical section and makes the shared
resource available to other processes.
Hardware Solution:
Both these instructions are atomic instructions which means that when a
process is executing any of these instructions it cannot be preempted until the
instruction is complete.
1. TestAndSet() Instructions:
*lock = TRUE;
return initial;
}
Implementation of TestAndSet() Instruction:
do {
while (TestAndSet(&lock));
critical section
lock = FALSE;
remainder section
} while(TRUE);
2. Swap() Instruction:
The swap() instruction uses two boolean variables lock and key.
Boolean t = *a;
*a = *b;
*b = t;
}
Implementation of Swap() Instruction:
do {
key = TRUE;
Swap(&lock, &key);
//critical section
lock = FALSE;
//remainder section
} while (TRUE);
Strict Alternation:
This approach can only be used for only two processes. In general, let the two
processes be Pi and Pj. They share a variable called turn variable. The pseudo
code of the program can be given as following.
For Process Pi:
1. Non - CS
2. while (turn ! = I);
3. Critical Section
4. turn = j;
5. Non - CS
The actual problem of the lock variable approach was the fact that the process
was entering in the critical section only when the lock variable is 1. More than
one process could see the lock variable as 1 at the same time hence the mutual
exclusion was not guaranteed there.
This problem is addressed in the turn variable approach. Now, A process can
enter in the critical section only in the case when the value of the turn variable
equal to the PID of the process.
There are only two values possible for turn variable, i or j. if its value is not i then
it will definitely be j or vice versa.
In the entry section, in general, the process Pi will not enter in the critical section
until its value is j or the process Pj will not enter in the critical section until its
value is I .
Initially, two processes Pi and Pj are available and want to execute into critical
section.
The turn variable is equal to i hence Pi will get the chance to enter into the
critical section. The value of Pi remains I until Pi finishes critical section.
Pi finishes its critical section and assigns j to turn variable. Pj will get the
chance to enter into the critical section. The value of turn remains j until Pj
finishes its critical section.
Peterson’s Solution:
The producer consumer problem (or bounded buffer problem) describes two
processes, the producer and the consumer, which share a common, fixed-size
buffer used as a queue. Producer produce an item and put it into buffer. If
buffer is already full then producer will have to wait for an empty block in
buffer. Consumer consume an item from buffer. If buffer is already empty
then consumer will have to wait for an item in buffer. Implement Peterson’s
Algorithm for the two processes using shared memory such that there is
mutual exclusion between them. The solution should have free from
synchronization problems.
The Producer Consumer Problem:
The Producer process creates an item and adds it to the shared buffer.
The Consumer process takes items out of the shared buffer and “consumes”
them.
Certain conditions must be met by the Producer and the Consumer processes
to have consistent data synchronization:
1. The Producer process must not produce an item if the shared buffer is
full.
2. The Consumer process must not consume an item if the shared buffer is
empty.
3. Access to the shared buffer must be mutually exclusive; this means that
at any given instance, only one process should be able to access the
shared buffer and make changes to it.
Semaphores:
1. Binary Semaphore –
This is also known as mutex lock. It can have only two values – 0
and 1. Its value is initialized to 1. It is used to implement the
solution of critical section problems with multiple processes.
2. Counting Semaphore –
Its value can range over an unrestricted domain. It is used to
control access to a resource that has multiple instances.
Some point regarding P and V operation:
1. P operation is also called wait, sleep, or down operation, and
V operation is also called signal, wake-up, or up operation.
2. Both operations are atomic and semaphore(s) is always
initialized to one. Here atomic means that variable on which
read, modify and update happens at the same time/moment
with no pre-emption i.e. in-between read, modify and update
no other operation is performed that may change the
variable.
3. A critical section is surrounded by both operations to
implement process synchronization. See the below image. The
critical section of Process P is in between P and V operation.
Event Counters:
Performance counters are bits of code that monitor, count, or measure events
in software, which allow us to see patterns from a high-level view. They are
registered with the operating system during installation of the software,
allowing anyone with the proper permissions to view them.
Monitors:
Message Passing:
The message passing model has slower communication than the shared
memory model because the connection setup takes time.
These problems are used for testing nearly every newly proposed
synchronization scheme.
Reader’s & Writer Problem:
Once a writer is ready, it performs its write. Only one writer may
write at a time.
Scheduling Algorithms: