Unit-3 Process Synchronization DSP
Unit-3 Process Synchronization DSP
Synchronization
• To introduce the critical-section
problem, whose solutions can be used
to ensure the consistency of shared
data.
• To present both software and
hardware solutions of the critical-
Objectives section problem.
• To examine several classical process-
synchronization problems.
• To explore several tools that are
used to solve process synchronization
problems
• Process Synchronization is the coordination of
execution of multiple processes in a multi-process
system to ensure that they access shared
resources in a controlled and predictable manner.
• The main objective of process synchronization is
to ensure that multiple processes access shared
resources without interfering with each other
Process and to prevent the possibility of inconsistent data
due to concurrent access.
Synchronization
• To achieve this, various synchronization
techniques such as semaphores, monitors, and
critical sections are used.
• In a multi-process system, synchronization is
necessary to ensure data consistency and
integrity, and to avoid the risk of deadlocks and
other synchronization problems.
• 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
Process or can be affected by the execution of
Synchronization other process. Such processes need to be
synchronized so that their order of
execution can be guaranteed.
• The procedure involved in preserving the
appropriate order of execution of
cooperative processes is known as
Process Synchronization.
• Processes can execute concurrently
• May be interrupted at any time, partially completing
execution
• Concurrent access to shared data may result in data
inconsistency
• Maintaining data consistency requires mechanisms to
ensure the orderly execution of cooperating processes
• Illustration of the problem using producer-consumer
Background problem:
/* do nothing */
Producer
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
while (true) {
while (counter == 0)
/* do nothing */
next_consumed = buffer[out];
counter--;
}
• The Producer-Consumer problem is a classical
multi-process synchronization problem, that is
we are trying to achieve synchronization
between more than one process.
critical section
Critical section
problem
exit section
remainder section
} while (true);
• A solution to the critical-section problem must
satisfy the following three requirements:
Mutual Exclusion : “Only one process should
execute in its critical section”. If process Pi is
executing in its critical section, then no other
Solution to Critical processes can be executing in their critical
sections.
Section Problem
Progress : If no process is executing in its critical
section and there exist some processes that wish
to enter their critical section, then the selection
of the processes that will enter the critical
section next cannot be postponed indefinitely
Bounded Waiting - A bound must exist on the
number of times that other processes are
allowed to enter their critical sections after a
process has made a request to enter its critical
Solution to Critical section and before that request is granted
Progress : No
Bounded Wait : -
Case:2
P0 P1
while (1) while (1)
{ {
flag[0]=true; flag[1]=true;
while (flag[1] == true); while (flag[0] == true);
critical_ region();
critical_ region();
flag[0] = false ;
flag[0] = false ;
noncritical_ region();
noncritical_ region();
}
}
Initially flag[0]=flag[1]=false
Case:2 Progress : No
Bounded Wait : -
• Process Pi
Disable interrupts
<critical
section>
Hardware
• Its very time consuming and decrease the efficiency
of system
• Also not practical solution
• Not feasible for multi-processor
environment because disable the
interrupt is time consuming task and
not desirable
• Modern machines provide special atomic
Synchronization hardware instructions
{ //Entry Section
}
<critical section>
lock = false;
Semaphore S2
Create a semaphore “semaphore” initialized to 0
Usage P1:
S1;
signal(semaphore);
P2:
wait(semaphore);
S2;
• Can implement a counting semaphore S as a binary
semaphore
Producer consumer using semaphores
• Buffer = 3
• Full & Empty variable define buffer
status
• Mutex Lock ( 1 for free, 0 for occupied)
• Semaphore S
int signal(int s)
int
mutex=1,full=0,empty=3,x {
=0; return(++s);
}
int wait(int s)
{ void consumer()
return(--s); {
Producer } mutex=wait(mutex);
consumer
full=wait(full);
void producer() empty=signal(empty);
using {
mutex=wait(mutex);
printf("\n consumer
consumes
semaphores full=signal(full);
empty=wait(empty); x--;
item%d",x);
x++; mutex=signal(mutex);
printf("\nproducer }
produces the
item%d",x);
mutex=signal(mutex);
}
• Must guarantee that no two processes can execute
the wait() and signal() on the same
semaphore at the same time
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Bounded Buffer Problem (Cont.)
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
• 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
• Problem – allow multiple readers to read at the
do {
wait(write_mutex);
...
/* writing is performed */
...
signal(write_mutex);
} while (true);
Readers-Writers Problem (Cont.)
• The structure of a reader process
do {
wait(read_mutex);
read_count++;
if (read_count == 1)
wait(write_mutex);
signal(read_mutex);
...
/* reading is performed */
...
wait(read_mutex);
read count--;
if (read_count == 0)
signal(write_mutex);
signal(read_mutex);
} while (true);
• First variation – no reader kept waiting unless
writer has permission to use shared object
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
• What is the problem with this algorithm?
• Deadlock handling
• Allow at most 4 philosophers to be sitting
simultaneously at the table.
• Allow a philosopher to pick up the forks
Dining- only if both are available (picking must be
done in a critical section.
Philosophers • Use an asymmetric solution -- an odd-
(Cont.)
End of Unit 3
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013