0% found this document useful (0 votes)
40 views50 pages

Process Synchronization

The document discusses interprocess synchronization and mechanisms used to preserve consistency when processes share resources. It describes the producer-consumer problem paradigm with a producer process that produces information for a consumer process. To allow concurrent execution, a shared buffer is used that is implemented as a circular array with pointers to track empty and full slots. Semaphores are introduced as a synchronization tool that uses wait() and signal() operations to control access to shared resources without busy waiting. Classical synchronization problems like bounded buffer, readers-writers, and dining philosophers are also mentioned.

Uploaded by

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

Process Synchronization

The document discusses interprocess synchronization and mechanisms used to preserve consistency when processes share resources. It describes the producer-consumer problem paradigm with a producer process that produces information for a consumer process. To allow concurrent execution, a shared buffer is used that is implemented as a circular array with pointers to track empty and full slots. Semaphores are introduced as a synchronization tool that uses wait() and signal() operations to control access to shared resources without busy waiting. Classical synchronization problems like bounded buffer, readers-writers, and dining philosophers are also mentioned.

Uploaded by

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

InterProcess Synchronization

Cooperating Processes
• Independent process cannot affect or be affected by
the execution of another process
• Cooperating process can affect or be affected by the
execution of another process
• Advantages of process cooperation
– Information sharing
– Computation speed-up
– Modularity
– Convenience
Example
• Two Processes P1 & P2
- Sharing Common Variable – Shared
- P1 updates shared as Initial Value
shared = shared + 3 Shared = 4
- P2 updates shared as
shared = shared – 3
Process P1:
1. Load the value of shared in a register of CPU A1.reg1 = shared
A2.reg1 = reg1 + 3
2. Add 3 to the value of the register A3.shared = reg1
3. Load the new value of the register in shared
Process P2:
1. Load the value of shared in a register of CPU B1.reg2 = shared
2. Subtract 3 to the value of the register B2.reg2 = reg2 - 3
B3.shared = reg2
3. Load the new value of the register in shared
Interprocess Synchronization
 Cooperating process must synchronize with each other
when they are to use shared resources, such as
- common data structures
- physical devices
Interprocess Synchronization: A set of protocols and
mechanisms used to preserve system integrity and
consistency when concurrent processes share resources
that are serially reusable.
Process states and its operations can be corrupted if
manipulated concurrently and without synchronization.
Need of Interprocess Synchronization

• Concurrent access to shared data may result in data


inconsistency
• Maintaining data consistency requires mechanisms
to ensure the orderly execution of cooperating
processes

Concurrent cooperating processes must communicate for such


purposes as
- Exchanging data
- Reporting Progress
- Shared Memory etc.
Producer-Consumer Problem
 Concurrent execution of cooperating processes requires
mechanism that allow processes to communicate with
one another and to synchronize their action.
Paradigm for cooperating processes,
“ Producer process produces information that is
consumed by a consumer process.”
Example : A print program produces characters that
are consumed by the printer driver
A Compiler may produce assembly code,
which is consumed by an assembler
Producer-Consumer Problem
 To allow producer and consumer process to run concurrently, we
must have available a buffer of items that can be filled by the
producer and empted by the consumer.
– unbounded-buffer places no practical limit on the size of the
buffer
– bounded-buffer assumes that there is a fixed buffer size

A producer can produce one item while the consumer is consuming


another item.

 The producer and consumer must be synchronized,


so that the Consumer does not try to consume an item that has not
yet been produced.
In this situation, the consumer must wait until and item is produced.
Bounded-Buffer – Shared-Memory Solution
• Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;

 The shared buffer is implemented as a circular array with two


logical pointers : in and out
in :- points to the next free position
out :- points to the first full position
 Buffer is empty :- in==out
 Buffer is full :- (in+1) % buffer_size == out
Bounded-Buffer – Producer Insert() Method

while (true) {
/* Produce an item */
while ((( in + 1) % BUFFER SIZE) == out)
; /* do nothing -- no free buffers */
buffer[in] = nextProduceditem;
in = (in + 1) % BUFFER SIZE;
}
Bounded Buffer – Consumer Remove() Method

while (true) {
while (in == out)
; // do nothing -- nothing to consume

// remove an item from the buffer


nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
return item;
}

• Solution is correct, but can only use BUFFER_SIZE-1


elements
Solution
• Suppose that we wanted to provide a solution to
the consumer-producer problem that fills all the
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
- Is decremented by the consumer after it
consumes a buffer.
Producer
while (true) {

/* produce an item and put in nextProduced */


while (count == BUFFER_SIZE)
; // do nothing
buffer [in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
count++;
}
Consumer
while (true) {
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;

/* consume the item in


nextConsumed
}
Bounded Buffer
Bounded Buffer
Bounded Buffer
Bounded Buffer
Race Condition
Critical – Section Problem
Solution-CS Problem
Solution-CS Problem - Requirement
Algorithm - 1
Algorithm - 2
Algorithm - 3
Bakery Algorithm
Bakery Algorithm
Bakery Algorithm
Semaphore
• Synchronization tool that does not require busy waiting
• Semaphore S – integer variable
• Two standard operations modify
S: wait() and signal()
– Originally called P() and V()
• Less complicated
• Can only be accessed via two indivisible (atomic) operations
– wait (S) {
while S <= 0
; // no-op
S--;
}
– signal (S) {
S++;
}
Semaphore as General Synchronization Tool
• Counting semaphore
– Integer value can range over an unrestricted domain
• Binary semaphore
– Integer value ranges only between 0 and 1
– Can be simpler to implement
• Provides mutual exclusion
– Semaphore S; // initialized to 1
– wait (S);
Critical Section
signal (S);

Must guarantee that no two processes can execute wait () and signal () on the same
semaphore at the same time
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.
Classical Problems of Synchronization
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem
Bounded-Buffer Problem
Solution of the problem using Semaphores

While (true) {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );

// eat

signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );

// think

You might also like