unit 4 OS
unit 4 OS
Unit IV:
In the above diagram, the entry section 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 the critical section is
free.
The critical section problem needs a solution to synchronize the different processes.
Mutual Exclusion
Mutual exclusion implies that only one process can be inside the critical section at any time.
If any other processes require the critical section, they must wait until it is free.
Progress
Progress means that if a process is not using the critical section, then it should not stop any
other process from accessing it. In other words, any process can enter a critical section if it is
free.
Bounded Waiting
Bounded waiting means that each process must have a limited waiting time. Itt should not
wait endlessly to access the critical section.
Que) what is Semaphore?
Semaphore is simply a variable that is non-negative and shared between threads. A semaphore is
a signaling mechanism, and a thread that is waiting on a semaphore can be signaled by another
thread. It uses two atomic operations, 1) Wait, and 2) Signal for the process synchronization. A
semaphore either allows or disallows access to the resource, which depends on how it is set up.
Semaphores are integer variables that are used to solve the critical section problem by using two
atomic operations wait and signal that are used for process synchronization.
Example of Semaphore
The below-given program is a step by step implementation, which involves usage and declaration of
semaphore.
Wait
The wait operation decrements the value of its argument S, if it is positive. If S is negative or
zero, then no operation is performed.
wait(S)
{
while (S<=0);
S--;
}
Signal
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
Characteristic of Semaphore
Advantages of Semaphores
Semaphores allow only one process into the critical section. They follow the mutual
exclusion principle strictly and are much more efficient than some other methods of
synchronization.
There is no resource wastage because of busy waiting in semaphores as processor time is
not wasted unnecessarily to check if a condition is fulfilled to allow a process to access the
critical section.
Semaphores are implemented in the machine independent code of the microkernel. So they
are machine independent.
Disadvantages of Semaphores
Semaphores are complicated so the wait and signal operations must be implemented in the
correct order to prevent deadlocks.
Semaphores are impractical for last scale use as their use leads to loss of modularity. This
happens because the wait and signal operations prevent the creation of a structured layout
for the system.
Semaphores may lead to a priority inversion where low priority processes may access the
critical section first and high priority processes later.
Counting semaphores
Binary semaphores.
Counting Semaphores
This type of Semaphore uses a count that helps task to be acquired or released numerous times. If
the initial count = 0, the counting semaphore should be created in the unavailable state.
However, If the count is > 0, the semaphore is created in the available state, and the number of
tokens it has equals to its count.
Binary Semaphores
The binary semaphores are quite similar to counting semaphores, but their value is restricted to 0
and 1. In this type of semaphore, the wait operation works only if semaphore = 1, and the signal
operation succeeds when semaphore= 0. It is easy to implement than counting semaphores.
Here, are some major differences between counting and binary semaphore:
typedef struct {
int semaphore_variable;
typedef struct { Queue list;
Structure int semaphore_variable; //A queue to store the list of task
Implementation }binary_semaphore; }counting_semaphore;
The wait and signal operations can modify a It is modified only by the process that may
Modification
semaphore. request or release a resource.
If no resource is free, then the process requires a If it is locked, the process has to wait. The
Resource resource that should execute wait operation. It process should be kept in a queue. This needs
management should wait until the count of the semaphore is to be accessed only when the mutex is
greater than 0. unlocked.
Value can be changed by any process releasing or Object lock is released only by the process,
Ownership
obtaining the resource. which has obtained the lock on it.
Mutual exclusion also known as Mutex is a unit of code that avert contemporaneous access to
shared resources. Mutual exclusion is concurrency control’s property that is installed for the
objective of averting race conditions.
In simple words, it's a condition in which a thread of execution does not ever get involved in a
critical section at the same time as a concurrent thread of execution so far using the critical section.
This critical section can be a period for which the thread of execution uses the shared resource
which can be defined as a data object, that different concurrent threads maybe attempt to alter
(where the number of concurrent read operations allowed is two but on the other hand two write
or one read and write is not allowed, as it may guide it to data instability).
Mutual exclusion in OS is designed so that when a write operation is in the process then another
thread is not granted to use the very object before the first one has done writing on the critical
section after that releases the object because the rest of the processes have to read and write it.
In a simple explanation, whenever node “i” want to be removed, at that moment node “ith - 1” 's
next reference is modified, directing towards the node “ith + 1”. Whenever a shared linked list is in
the middle of many threads, two separate nodes can be removed by two threads at the same time
meaning the first thread modifies node “ith - 1” next reference, directing towards the node “ith + 1”,
at the same time second thread modifies node “ith” next reference, directing towards the node “ith
+ 2”. Despite the removal of both achieved, linked lists required state is not yet attained because
node “i + 1” still exists in the list, due to node “ith - 1” next reference still directing towards the
node “i + 1”.
Now, this situation is called a race condition. Race conditions can be prevented by mutual
exclusion so that updates at the same time cannot happen to the very bit about the list.
Mutual exclusion should be ensured in the middle of different processes when accessing
shared resources. There must not be two processes within their critical sections at any time.
Assumptions should not be made as to the respective speed of the unstable processes.
The process that is outside the critical section must not interfere with another for access to
the critical section.
When multiple processes access its critical section, they must be allowed access in a finite
time, i.e. they should never be kept waiting in a loop that has no limits.
There are many types of mutual exclusion, some of them are mentioned below :
Locks :
It is a mechanism that applies restrictions on access to a resource when multiple threads of
execution exist.
Recursive lock :
It is a certain type of mutual exclusion (mutex) device that is locked several times by the
very same process/thread, without making a deadlock. While trying to perform
the "lock" operation on any mutex may fail or block when the mutex is already locked, while
on a recursive mutex the operation will be a success only if the locking thread is the one that
already holds the lock.
Semaphore :
It is an abstract data type designed to control the way into a shared resource by multiple
threads and prevents critical section problems in a concurrent system such as a
multitasking operating system. They are a kind of synchronization primitive.
Readers writer (RW) lock :
It is a synchronization primitive that works out reader-writer problems. It grants
concurrent access to the read-only processes, and writing processes require exclusive
access. This conveys that multiple threads can read the data in parallel however exclusive
lock is required for writing or making changes in data. It can be used to manipulate access
to a data structure inside the memory.
Que) What is a monitor in OS?
Monitors are a programming language component that aids in the regulation of shared data access.
The Monitor is a package that contains shared data structures, operations, and synchronization
between concurrent procedure calls. Therefore, a monitor is also known as a synchronization
tool. Java, C#, Visual Basic, Ada, and concurrent Euclid are among some of the languages that
allow the use of monitors. Processes operating outside the monitor can't access the monitor's
internal variables, but they can call the monitor's procedures.
For example, synchronization methods like the wait() and notify() constructs are available in the
Java programming language.
Syntax of monitor in OS
Monitor monitorName{
variables_declaration;
condition_variables;
{
initializing_code;
}
Characteristics of Monitors in OS
1. Initialization: The code for initialization is included in the package, and we just need it
once when creating the monitors.
2. Private Data: It is a feature of the monitor in an operating system to make the data private.
It holds all of the monitor's secret data, which includes private functions that may only be
utilized within the monitor. As a result, private fields and functions are not visible outside of
the monitor.
3. Monitor Procedure: Procedures or functions that can be invoked from outside of the
monitor are known as monitor procedures.
4. Monitor Entry Queue: Another important component of the monitor is the Monitor Entry
Queue. It contains all of the threads, which are commonly referred to as procedures only.
Advantages of Monitor in OS
Monitors offer the benefit of making concurrent or parallel programming easier and less
error-prone than semaphore-based solutions.
It helps in process synchronization in the operating system.
Monitors have built-in mutual exclusion.
Monitors are easier to set up than semaphores.
Monitors may be able to correct for the timing faults that semaphores cause.
Disadvantages of Monitor in OS