Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Summary of Chapter 6 Process Synchronization

Introduction
 When two or more process cooperates with each other, their order of execution must be
preserved otherwise there can be conflicts in their execution and inappropriate outputs can
be produced.
 A cooperative process is the one which can affect the execution of other process or can be
affected by the execution of other process. Such processes need to be synchronized so that
their order of execution can be guaranteed.
 Process Synchronization means sharing system resources by processes in such a way that
concurrent access to shared data is handled thereby minimizing the chance of inconsistent
data.
 Concurrent access to shared data may result in data inconsistency.
 Maintaining data consistency requires mechanisms to ensure the orderly execution of
cooperating processes.
 Suppose that we wanted to provide a solution to the consumer-producer problem that fills
all a buffers. We can do so by having an integer count that keeps track of the number of full
buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new
buffer and is decremented by the consumer after it consumes a buffer.
Race Condition
 Is a situation where several processes access and manipulate the same data concurrently
and the outcome of the execution depends on the particular order in which the access takes
place.
 To guard against the race condition, we need to ensure that only one process at a time can
manipulate the data. To make such a guarantee, we require that the processes be
synchronized in some way.
Critical-Section Problem
 A race condition is a situation that may occur inside a critical section.
 Critical section is a code segment that can be accessed by only one process at a time.
Summary of Chapter 6 Process Synchronization

 Consider a system consisting of n processes {P0, P1, ..., Pn−1}. Each process has a segment of
code, called a critical section, in which the process may be changing common variables,
updating a table, writing a file, and so on.
 The important feature of the system is that, when one process is executing in its critical
section, no other process is to be allowed to execute in its critical section. That is, no two
processes are executing in their critical sections at the same time.
 The critical section problem is used to design a set of protocols which can ensure that the
race condition among the processes will never arise.
Solution to Critical-Section Problem
We need to provide a solution in such a way that the following conditions can be satisfied.
1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes
can be executing in their critical sections.
2. Progress - Progress means that if one process doesn't need to execute into critical section
then it should not stop other processes to get into the critical section.
3. Bounded Waiting - We should be able to predict the waiting time for every process to get
into the critical section. The process must not be endlessly waiting for getting into the
critical section.
Peterson’s Solution
 A software mechanism implemented at user mode. It is a solution that can be implemented
for only two processes that alternate execution between their critical sections and
remainder sections.
 Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted.
 The two processes share two variables:
 int turn;
 Boolean flag[2]
 The variable turn indicates whose turn it is to enter the critical section.
 The flag array is used to indicate if a process is ready to enter the critical section. flag[i] =
true implies that process Pi is ready.
Summary of Chapter 6 Process Synchronization

 Peterson solution provides all the necessary requirements such as Mutual Exclusion,
Progress, Bounded Waiting.
 Disadvantages of Peterson’s Solution
 It involves busy waiting
 It is limited to two processes
Synchronization Hardware
 Many systems provide hardware support for critical section code.
 Introduce a state that any solution to the critical-section problem requires a simple tool—a
lock.
 Race conditions are prevented by requiring that critical regions be protected by locks.
 A process must acquire a lock before entering a critical section; it releases the lock when it
exits the critical section.
Semaphore
 Is a variable used to control access to a common resource by multiple processes in a
concurrent system such as a multitasking operating system. It is a synchronization tool that
does not require busy waiting
 Semaphore S – integer variable
 A semaphore uses two atomic operations, wait and signal for process synchronization.
Properties of Semaphores
 It's simple and always have a non-negative integer value.
 Works with many processes.
 Each critical section has unique access semaphores.
 Less complicated.
 Can only be accessed via two indivisible (atomic) operations, wait and signal.
Semaphore Implementation
 Must guarantee that no two processes can execute wait () and signal () on the same
semaphore at the same time.
Summary of Chapter 6 Process Synchronization

 Thus, implementation becomes the critical section problem where the wait and signal code
are placed in the critical section.
 Could now have busy waiting in critical section implementation.
 But implementation code is short
 Little busy waiting if critical section rarely occupied.
Limitations of Semaphores
 Priority Inversion is a big limitation of semaphores and it is a scheduling problem when
lower-priority process holds a lock that cannot be preempted and needed by higher-priority
process.
 With improper use, a process may blocked indefinitely. Such a situation is called Deadlock.
We will be studying deadlocks in detail in Chapter 7.
Semaphore Implementation with no Busy waiting
 With each semaphore there is an associated waiting queue. Each entry in a waiting queue
has two data items:
 value (of type integer)
 pointer to next record in the list
 Two operations:
 block – place the process invoking the operation on the appropriate waiting queue.
 wakeup – remove one of processes in the waiting queue and place it in the ready
queue.

Deadlock and Starvation


 Deadlock – two or more processes are waiting indefinitely for an event that can be caused
by only one of the waiting processes.
 Starvation (indefinite blocking) – A process may never be removed from the semaphore
queue in which it is suspended.
Summary of Chapter 6 Process Synchronization

Classical Problems of Synchronization


Some of the classical problems depicting flaws of process synchronization in systems where
cooperating processes are present.
 Bounded-Buffer (Producer-Consumer) Problem
 Readers and Writers Problem
 Dining-Philosophers Problem
Bounded-Buffer Problem
To make sure that the producer will not try to add data into the buffer if it is full and that the
consumer will not try to remove data from an empty buffer.
A finite buffer pool is used to exchange messages between producer and consumer processes.
Bounded-Buffer Solution
 The producer is to either go to sleep or discard data if the buffer is full. The next time the
consumer removes an item from the buffer, it notifies the producer, who starts to fill the
buffer again.
 In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next
time the producer puts data into the buffer, it wakes up the sleeping consumer.
 N buffers, each can hold one item
 Semaphore mutex initialized to the value 1
 Semaphore full (producer) initialized to the value 0
 Semaphore empty (consumer) initialized to the value N.
 As the producer producing full buffers for the consumer or as the consumer producing
empty buffers for the producer.
Readers-Writers Problem
 Suppose that a database is to be shared among several concurrent processes.
 Some of these processes may want only to read the database, whereas others may want to
update (that is, to read and write) the database. We distinguish between these two types of

processes by referring to the former as readers and to the latter as writers.


Summary of Chapter 6 Process Synchronization

 Obviously, if two readers access the shared data simultaneously, no adverse effects will
result. However, if a writer and some other process (either a reader or a writer) access the
database simultaneously, chaos may ensue.
 A data set is shared among a number of concurrent processes
 Readers – only read the data set; they do not perform any updates.
 Writers – can both read and write.
 Solution – allow multiple readers to read at the same time. Only one single writer can
access the shared data at the same time.
 Shared Data
 Data set
 To ensure that these difficulties do not arise, we require that the writers have exclusive
access to the shared database while writing to the database.
Dining-Philosophers Problem
 Consider five philosophers who spend their lives thinking and eating.
 The philosophers share a circular table surrounded by five chairs, each belonging to one
philosopher.
 In the center of the table is a bowl of rice, and the table is laid with five single chopsticks.
When a philosopher thinks, she does not interact with her colleagues.
 From time to time, a philosopher gets hungry and tries to pick up the two chopsticks that
are closest to her (the chopsticks that are between her and her left and right neighbors).
 A philosopher may pick up only one chopstick at a time.
 Obviously, she cannot pick up a chopstick that is already in the hand of a neighbor. When a
hungry philosopher has both her chopsticks at the same time, she eats without releasing
her chopsticks.
 When she is finished eating, she puts down both of her chopsticks and starts thinking again.
 Dining-Philosophers Problem is a simple representation of the need to allocate several
resources among several processes in a deadlock-free and starvation-free manner.
Summary of Chapter 6 Process Synchronization

Solution to Dining Philosophers


 Allow at most four philosophers to be sitting simultaneously at the table.
 Allow a philosopher to pick up her chopsticks only if both chopsticks are available.
 Use an asymmetric solution; that is, an odd philosopher picks up first her left chopstick and
then her right chopstick, whereas an even philosopher picks up her right chopstick and then
her left chopstick.

You might also like