0% found this document useful (0 votes)
30 views9 pages

unit 4 OS

Uploaded by

rathodnikil07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views9 pages

unit 4 OS

Uploaded by

rathodnikil07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Subject: Operating System

Unit IV:

Que) what are the various solution to critical section problem.


The critical section is a code segment where the shared variables can be accessed. An atomic
action is required in a critical section i.e. only one process can execute in its critical section at a
time. All the other processes have to wait to execute in their critical sections.

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.

Solution to the Critical Section Problem

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.

Shared var mutex: semaphore = 1;


Process i
begin
.
.
P(mutex);
execute CS;
V(mutex);
.
.
End;

The definitions of wait and signal are as follows −

 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

 It is a mechanism that can be used to provide synchronization of tasks.


 It is a low-level synchronization mechanism.
 Semaphore will always hold a non-negative integer value.
 Semaphore can be implemented using test operations and interrupts, which should be
executed using file descriptors.

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.

Que) Types of Semaphores


The two common kinds of semaphores are

 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:

Counting Semaphore Binary Semaphore


No mutual exclusion Mutual exclusion
Any integer value Value only 0 and 1
More than one slot Only one slot
Provide a set of Processes It has a mutual exclusion mechanism.

Que) Difference between Counting and Binary Semaphores.

Criteria Binary Semaphore Counting Semaphore

A counting semaphore is a semaphore


A Binary Semaphore is a that has multiple values of the counter.
semaphore whose integer value The value can range over an unrestricted
Definition range over 0 and 1. domain.

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;

0 means that a process or a


thread is accessing the critical
section, other process should
wait for it to exit the critical The value can range from 0 to N, where N
section. 1 represents the critical is the number of process or thread that
Representation section is free. has to enter the critical section.

No waiting queue is present then


FCFS (first come first serve) is Waiting queue is present then FCFS (first
not followed so,starvation is come first serve) is followed so,no
Starvation possible and busy wait present starvation hence no busy wait.
Used only for a single instance of Used for any number of instance of
Number of resource type R.it can be resource of type R.it can be used for any
instance usedonly for 2 processes. number of processes.

Parameters Semaphore Mutex

Mechanism It is a type of signaling mechanism. It is a locking mechanism.

Data Type Semaphore is an integer variable. Mutex is just an object.

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.

You can have multiple program threads in


Thread You can have multiple program threads.
mutex but not simultaneously.

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.

Types of Semaphore are counting semaphore and


Types Mutex has no subtypes.
binary semaphore and

Semaphore value is modified using wait () and


Operation Mutex object is locked or unlocked.
signal () operation.

Que) Difference between Semaphore vs. Mutex

Que) explain classical problem of synchronization.

The classical problems of synchronization are as follows:


1. Bound-Buffer problem
2. Sleeping barber problem
3. Dining Philosophers problem
4. Readers and writers problem
1) Bound-Buffer problem
Also known as the Producer-Consumer problem. In this problem, there is a buffer of n
slots, and each buffer is capable of storing one unit of data. There are two processes that are
operating on the buffer – Producer and Consumer. The producer tries to insert data and the
consumer tries to remove data.
If the processes are run simultaneously they will not yield the expected output.
The solution to this problem is creating two semaphores, one full and the other empty to
keep a track of the concurrent processes.
2) Sleeping Barber Problem
This problem is based on a hypothetical barbershop with one barber.
When there are no customers the barber sleeps in his chair. If any customer enters he will
wake up the barber and sit in the customer chair. If there are no chairs empty they wait in
the waiting queue.
3) Dining Philosopher’s problem
This problem states that there are K number of philosophers sitting around a circular table
with one chopstick placed between each pair of philosophers. The philosopher will be able
to eat if he can pick up two chopsticks that are adjacent to the philosopher.
This problem deals with the allocation of limited resources.
4) Readers and Writers Problem
This problem occurs when many threads of execution try to access the same shared
resources at a time. Some threads may read, and some may write. In this scenario, we may
get faulty outputs.

Que) What is Mutual Exclusion in OS?

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.

Que) Why is Mutual Exclusion Required?


An easy example of the importance of Mutual Exclusion can be envisioned by implementing a linked
list of multiple items, considering the fourth and fifth need removal. The deletion of the node which
sits between the other two nodes is done by modifying the previous node’s next reference directing
the succeeding node.

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.

Que) Necessary Conditions for Mutual Exclusion

 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.

Example of Mutual Exclusion

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 in os has a simple syntax similar to how we define a class, it is as follows:

Monitor monitorName{
variables_declaration;
condition_variables;

procedure p1{ ... };


procedure p2{ ... };
...
procedure pn{ ... };

{
initializing_code;
}

Monitor in an operating system is simply a class containing variable_declarations,


condition_variables, various procedures (functions), and an initializing_code block that is used for
process synchronization.

Characteristics of Monitors in OS

A monitor in os has the following characteristics:

 We can only run one program at a time inside the monitor.


 Monitors in an operating system are defined as a group of methods and fields that are
combined with a special type of package in the os.
 A program cannot access the monitor's internal variable if it is running outside the monitor.
Although, a program can call the monitor's functions.
 Monitors were created to make synchronization problems less complicated.
 Monitors provide a high level of synchronization between processes.

Components of Monitor in an operating system


The monitor is made up of four primary parts:

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

 Monitors must be implemented with the programming language.


 Monitor increases the compiler's workload.
 The monitor requires to understand what operating system features are available
for controlling crucial sections in the parallel procedures.

You might also like