Uploaded by Hammad Rajput

Process of Synchronization

advertisement
MUHAMMAD HAMMAD
ROLL NO: 17
SECTION: A
OPERATING SYSTEM
ASSIGNMENT NO: 01
PROCESS OF SYNCHRONIZATION
INTERRUPT DISABLING:
Interrupt disabling is a technique used in computer programming to prevent an interrupt from
occurring during a critical section of code. This is done by temporarily disabling the interrupt,
performing the critical code, and then re-enabling the interrupt.
For example, suppose a program is reading data from a shared memory location, and an
interrupt occurs while the program is reading the data. In this case, the interrupt could
corrupt the data being read, leading to errors in the program's execution. By disabling
interrupts during the critical section of code, the program can ensure that the data is read
correctly.
TEST AND SET INSTRUCTION:
The test and set instruction is a low-level instruction used in computer programming to
implement atomic operations. It is typically used in multi-threaded or multi-process
environments to ensure that only one thread or process can access a shared resource at a
time. The test and set instruction works by testing a memory location and setting a value in
that location if it is currently unset.
For example, if two threads are attempting to access a shared resource, the first thread will
test and set the resource, preventing the second thread from accessing it until the first
thread has finished.
SWAP INSTRUCTION:
The swap instruction is another low-level instruction used in computer programming to
implement atomic operations. It is similar to the test and set instruction, but instead of setting
a value in a memory location, it swaps the values of two memory locations. The swap
instruction is often used in sorting algorithms and other applications that require atomic
operations on multiple variables.
WAIT AND SIGNAL:
Wait and Signal is a synchronization technique used in multi-threaded or multi-process
environments to coordinate the execution of threads or processes. The Wait operation is
used to block a thread or process until a condition is true, while the Signal operation is used
to notify the waiting thread or process that the condition has been met.
For example, in a producer-consumer scenario, the producer thread may wait until the
consumer thread has consumed a value, and the consumer thread may signal the producer
thread once it has consumed the value.
SEMAPHORES:
Semaphores are a synchronization technique used in multi-threaded or multi-process
environments to control access to shared resources. A semaphore is essentially a counter
that is used to keep track of the number of threads or processes that are currently accessing
a shared resource. When a thread or process wants to access the shared resource, it must
first acquire the semaphore. If the semaphore is currently zero, indicating that the shared
resource is currently in use, the thread or process will be blocked until the semaphore is
released.
DEKKER'S ALGORITHM:
Dekker's Algorithm is a synchronization algorithm used to ensure mutual exclusion in
multi-threaded environments. It was one of the first algorithms to solve the critical section
problem and is based on the idea of taking turns. In Dekker's Algorithm, each thread is
assigned a turn, and only the thread that is currently assigned the turn can access the
shared resource.
PETERSON'S ALGORITHM:
Peterson's Algorithm is another synchronization algorithm used to ensure mutual exclusion
in multi-threaded environments. Like Dekker's Algorithm, it is based on the idea of taking
turns, but it uses an additional flag to avoid the possibility of deadlock. In Peterson's
Algorithm, each thread has a flag that indicates whether it wants to enter the critical section.
If both threads want to enter the critical section at the same time, the thread with the lower
turn number is given priority.
BAKERY ALGORITHM:
The Bakery Algorithm is a synchronization algorithm used to ensure mutual exclusion in
multi-threaded environments. It is based on the idea of taking a number when entering a
bakery and only allowing the person with the lowest number to be served. In the Bakery
Algorithm, each thread is assigned a number and only the thread with the lowest number to
be served.
For example, A program that simulates multiple threads trying to access a shared resource
simultaneously. The bakery algorithm is used to prevent race conditions and ensure that
each thread accesses the resource in the order in which it arrived.
MONITORS:
A monitor is a high-level synchronization construct used in concurrent programming to allow
threads to safely access shared resources. A monitor consists of a shared data structure
and a set of procedures or methods that can be called by threads to access the shared data.
For example, A program that simulates multiple threads trying to access a shared buffer. A
monitor is used to synchronize access to the buffer and ensure that threads access the
buffer in a mutually exclusive and orderly fashion.
Download