0% found this document useful (0 votes)
23 views

OS Assignment (2) (2)(1)

Uploaded by

rab2.78khan8
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)
23 views

OS Assignment (2) (2)(1)

Uploaded by

rab2.78khan8
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/ 10

OPERATING SYSTEM

Assignment # 2
SUBMITTED TO :MAM SAIRA

Group Members:

INFORMATION TECHNOLOGY
4rth Semester
Section:“A”

1
PROCESS SYNCHRONIZATION
Synchronization:
Coordinating the execution of processes so that no two processes access the same shared
resources and data is known as process synchronization. It is necessary for a multi-process
system where several processes coexist and concurrently attempt to access the same shared
resource or piece of data.

What is Process Synchronization:


An operating system is a piece of software that controls all of the installed programmes on a
computer or other device, essentially making it run more efficiently. Because of this, the
operating system frequently has to handle multiple tasks at once. Unless these processes running
simultaneously share a resource, this usually doesn't pose a synchronization problem in OS.

Take a bank as an example,which maintains a single database for each customer's account
balance. Let's say you had x rupees in your account at the beginning.Now you withdraw some
money from your bank account, and someone tries to look at the balance of your account at the
same time.

The total balance remaining in your account after the transaction will be less than x because you
are taking money out of it. However, since the transaction takes some time, the person interprets
x as your account balance, resulting in inconsistent data. We could guarantee consistent data if
we could somehow make sure that only one process was running at once.

2
In the above image, if Process1 and Process2 occur simultaneously,User 2 will receive the
incorrect account balance as Y as a result of Processl being executed when the balance is X.
When multiple processes share a single resource in a system, inconsistent data can result, which
is why process synchronization is required in the operating system.

How Process Synchronization in OS Works?


Let us take a look at why exactly we need Process Synchronization.For example,If a process1 is
trying to read the data present in a memory location while another process2 is trying to change the
data present at the same location, there is a high chance that the data read by the processl wil1 be
incorrect.

let us look at different elements/sections of a program:

·Entry Section: The entry Section decides the entry of a process.


·Critical Section: Critical section allows and makes sure that only one process is
modifying the shared data.
Exit Section: The entry of other processes in the shared data after the execution of one
process is handled by the Exit section.
Remainder Section: The remaining part of the code which is not categorized as above is
contained in the Remainder section.

Types of Process Synchronization in Operating System:


The following are the types of synchronization:

1.Independent Processes:

3
When a process is executed without affecting or having an impact on another process,that process is
referred to as an independent process. For example the process without any shared
databases,files,variables,etc.

2.Cooperative Processes:
In a computer system, different processes can operate as independent or collaborative processes within
the operating system. When a process won't be impacted by other processes running on the system, it is
said to be independent. Data sharing between processes is not done by independent processes. On the
other hand, any other process running on the system may impact a collaborating process. Data is shared
between processes that are cooperating.

Why Synchronization is Important in Operating System?


1. Mutual exclusion: No other process should be permitted to run in the critical section
while the current process is there.
2. Progress: Any thread must be allowed to enter the critical section if no processes are still
running inside it and other processes are waiting outside it to execute. Only those processes that
aren't running in the other section will decide which process will enter the critical section.
3. No starvation: A process that is starved waits interminably to access the critical section but is
never given the opportunity. Bounded Waiting is another name for no starvation.
· A process shouldn't take an eternity to enter the crucial area.
·There should be a cap or bound that specifies how many other processes are
permitted to access the critical section before a process can request access to its
critical section.
·This process should be permitted access to the critical section once this bound is
reached.

RACE CONDITION

In Operating Systems(OS), a Race Condition is a situation that occurs when two or more
threads or processes access a shared resource, such as a file or a variable,at the same time.

4
What is Race Condition in OS?
A race condition is a problem that occurs in an operating system (OS) where two or more processes or
threads are executing concurrently. The outcome of their execution depends on the order in which
they are executed. In a race condition, the exact timing of events is unpredictable, and the outcome of
the execution may vary based on the timing. This can result in unexpected or incorrect behavior of the
system.
For example:
If two threads are simultaneously accessing and changing the same shared resource, such as a
variable or a file, the final state of that resource depends on the order in which the threads execute.
If the threads are not correctly synchronized, they can overwrite each other's changes,causing
incorrect results or even system crashes.
Effects of Race Condition in OS
In an operating system, a race condition can have several effects, which are discussed below:
·Deadlocks:
A race condition can cause deadlocks, where two or more processes are awaiting the completion
of a job by one another but are unable to move forward, can be brought on by a race condition.
This can happen if two processes try to access the same resource simultaneously, and the
operating system doesn't have a mechanism to ensure that only one can access the resource at a
time.

Data corruption:
A race condition can cause data corruption, where two or more processes simultaneously try to
write/update the exact memory location. This can cause the data to be overwritten or mixed up,
resulting in incorrect results or program crashes.

Security vulnerabilities:
A race condition can also create security vulnerabilities in an operating system. For example, an
attacker may be able to exploit a race condition to gain unauthorized access to a system or to
escalate their privileges.

Performance degradation:
In some cases, a race condition can also cause performance degradation in an operating system.
This can happen if multiple processes are competing for the same resources,such as CPU time
or memory, and the operating system is unable to allocate these resources efficiently.

Types of Race Conditions in OS

5
There are two types of race conditions that occur in the critical section, which are discussed
below.
·Read-Modify-Write:
A Read-Modify-Write race condition occurs when multiple processes or threads attempt to
read,modify,and write the same shared resource concurrently, leading to unexpected behavior and
inconsistencies in the final result.
The race condition arises because each process or thread assumes that the shared resource's
value remains unchanged between the read and write operations.
However,in the presence of other processes or threads modifying the same resource
simultaneously, this assumption may not hold,leading to incorrect results.
To prevent read-modify-write race conditions,synchronization mechanisms such as locks,semaphores,
or atomic operations can ensure that only one process or thread accesses the shared resource at a
time. By enforcing mutual exclusion, the likelihood of inconsistent results is greatly reduced.
·Check-Then-Act:
A Check-Then-Act race condition occurs when multiple processes or threads are checking for the
same condition and then acting upon it without any synchronization mechanism to prevent race
conditions.
Consider you have two threads that check the availability of a resource, such as a file. Both threads
may check the availability of the file and find that it is available. However, between the time that the
first thread checks the file and when it tries to access it, the second thread may also check the file and
find that it is available. This can result in both threads trying to access the file simultaneously, leading
to unexpected behavior and potential errors.
To prevent a "check-then-act" race condition, we can use synchronization mechanisms
(locks,semaphores, or mutexes) to ensure that only one thread can access the resource at a time.

How to Prevent Race Condition in OS


To prevent race conditions,you can use the following ways.

Use locks or semaphores:


Use synchronization mechanisms such as locks or semaphores to ensure that only one
process or thread can access a shared resource at any given time.

Atomic operations:
Use atomic operations, which are operations that cannot be interrupted, to manipulate
shared data.

6
·Avoid global variables:
Avoid using global variables because they can be accessed and modified by multiple processes
or threads simultaneously.

Use message passing:


Use message passing instead of shared memory to communicate between processes or threads. This
ensures that only one process or thread can access a particular message at a time.

Use thread-safe libraries:


Use thread-safe libraries that are designed to prevent race conditions.

·Design for concurrency:


When designing your application,consider concurrency issues from the start. Make sure that your
design can handle multiple processes or threads accessing shared resources.
Examples of Race Condition:
We can understand race conditions by taking real-life examples. Consider a situation where two employees
are working on the same document in a company's shared folder. Employee A opens the document and starts
editing it, but before they can save their changes, Employee B opens the same document and starts making
changes.
When Employee A tries to save their changes, he is unable to change because the document has been
updated by Employee B, and their changes are lost.
This is an example of a race condition because the two employees are "competing" to access and modify the
same resource (in this case, the document). Depending on the timing and order of the actions performed by
employees, the final state of the document can be unpredictable and inconsistent.
To avoid race conditions in this, the company may implement a version control system or a
check-out/check-in mechanism that allows employees to work on different copies of the document and
merge their changes later.

What is the Critical Section in OS?

CRITICAL SECTION: Process

Entry
Section
Critical
Section

Exit
Section Critical Section refers to the code segment or the program that tries
to access or modify the value of the variables in a
shared resource.

The section above the critical section is called the Entry Section. The process that is
entering the critical section must pass the entry section.

The section below the critical section is called the Exit Section.
The section below the exit section is called the Reminder Section and this section has the
remaining code that is left after execution.
When there is more than one process accessing or modifying a shared resource at the same
time, then the value of that resource will be determined by the last process. This is called the
race condition.
Consider an example of two processes, pl and p2. Let value=3 be a variable present in the
shared resource.
Let us consider the following actions are done by the two processes:

The original value of value should be 6,but due to the interruption of the process p2,the value is
changed back to 3. This is the problem of synchronization.

The critical section problem is to make sure that only one process should be in a critical section at
a time. When a process is in the critical section, no other processes are allowed to enter the critical
section. This solves the race condition.

Example of Critical Section Problem:


Let us consider a classic bank example, this example is very similar to the example we have seen
above.

Let us consider a scenario where money is withdrawn from the bank by both the
cashier(through cheque) and the ATM at the same time.
Consider an account having a balance of ₹10,000. Let us considerthat,when a cashier
withdraws the money, it takes 2 seconds for the balance to be updated in the account.
It is possible to withdraw 7000 from the cashier and within the balance update time of
2seconds,also withdraw an amount of ₹6000 from the ATM.

8
·Thus,the total money withdrawn becomes greater than the balance of the bank account.
This happened because of two withdrawals occurring at the same time. In the case of the critical
section, only one withdrawal should be possible and it can solve this problem.

Criteria/Conditions to solve CSP:

The Critical Section Problem is a fundamental concept in operating systems and concurrent
programming. It deals with the challenge of coordinating the access of multiple processes or threads
to a shared resource (or critical section) in a way that avoids race conditions,ensures mutual exclusion,
and prevents deadlocks. To solve the Critical Section Problem, several criteria or conditions must be
satisfied. These conditions are commonly referred to as the requirements for a correct solution to the
problem.

CRITERIA:
There are three main criteria that need to be met:

1.Mutual Exclusion: Only one process (or thread) can be in the critical section at a time.This means
that while one process is executing its critical section code, all other processes are prevented from
entering their respective critical sections.

2. Progress: If no process is currently in its critical section and some processes are trying to enter,then
only those processes that are not in their remainder section (non-critical section) can participate in
deciding which process should enter next. This ensures that processes do not get stuck indefinitely,
and eventually, if a process wants to enter the critical section, it will be able to do so.

3. Bounded Waiting: There should be a limit on the numnber of times other processes can enter the
critical section after a process has made a request to enter its critical section. This prevents a
process from being starved, meaning that a process won't be indefinitely delayed in entering its
critical section.

SOLUTIONS:
Various synchronization mechanisms and algorithms have been developed to achieve these criteria
and solve the Critical Section Problem. Some of the commonly used solutions include:

·Locks and Semaphores: These are low-level synchronization primitives provided by the
operating system or programming libraries. Locks and semaphores can be used to implement
mutual exclusion by allowing only one process to acquire a lock at a time.

9
Mutex (Mutual Exclusion): Mutex is a synchronization primitive that ensures mutual exclusion
by providing a way for processes to request exclusive access to a shared resource. Only one
process can hold the mutex at a time.

Semaphore: A semaphore is a more generalized synchronization primitive that can be used to


control access to a certain number of resources. Semaphores can be used to implement both
mutual exclusion and signaling between processes.

Monitors: Monitors are higher-level synchronization constructs that encapsulate data and
procedures together, ensuring that only one process can execute the methods ofa monitor at
a time.

Readers-Writers Problem: This is a variation of the Critical Section Problem where multiple
processes may need to access a shared resource for reading, while only one process can access
the resource for writing. Solutions for this problem ensure proper synchronization and
prioritization between readers and writers.

·Dining Philosophers Problem: This is another classic synchronization problem involving


multiple philosophers (processes) who alternate between thinking and eating.The challenge is
to avoid deadlocks and ensure that philosophers can safely pick up forks (resources) to eat.

These are just a few examples of synchronization mechanisms and solutions to the Critical Section
Problem. The choice of mechanism depends on the specific requirements of the application and the
programming environment. The key is to ensure that the chosen mechanism satisfies the criteria of
mutual exclusion, progress, and bounded waiting to prevent race conditions and deadlocks.

10

You might also like