Docsity

InterProcess Communication: Understanding Race Conditions and Synchronization Techniques -, Study notes of Computer Science

Toggle sidebar
bg1
InterProcess Communication
Race Condition
1856
A[tail]=20;
tail++;
A[tail]=9;
tail++;
ProcessAProcessB
tail A[]
Enqueue():
SharedData:
process
switch
209465 ?
Critical Regions
ProcessA
ProcessBBblocked
Bblocked
Aenters
criticalregion
Btriestoenter
criticalregion
Benters
criticalregion
Aleaves
criticalregion
Bleaves
criticalregion
Time
Goals
•Notwoprocesses(threads)canbeintheir
criticalregionatthesametime
•Noassumptionsabout#ofCPUsortheir
speed
•Noprocessoutsideofitscriticalregionmay
blockanotherprocess
•Noprocessshouldhavetowaitforeverto
enteritscriticalregion
Strict Alternation
while (TRUE){
while (turn!=0)
;/*loop*/
critical_region();
turn=1;
noncritical_region();
}
while (TRUE){
while (turn!=1)
;/*loop*/
critical_region();
turn=0;
noncritical_region();
}
ProcessAProcessB
Busy Waiting
#define FALSE 0
#define TRUE 1
#define N2//#ofprocesses
int interested[N]; //Setto1ifprocessjisinterested
int last_request; //Whorequestedentrylast?
void enter_region(intprocess)
{
int other=1‐process;//#oftheotherprocess
interested[process]=TRUE; //showinterest
last_request=process; //Setittomyturn
while (interested[other]==TRUE&&last_request==process)
;//Waitwhiletheotherprocessruns
}
void leave_region(int process)
{
interested[process]=FALSE;//I’mnolongerinterested
}
pf3
pf4

Partial preview of the text

Download InterProcess Communication: Understanding Race Conditions and Synchronization Techniques - and more Study notes Computer Science in PDF only on Docsity!

InterProcess Communication

Race Condition

A[tail] = 20;

tail++;

A[tail] = 9;

tail++;

Process A Process B

tail A[]

Enqueue():

Shared Data:

process switch

Critical Regions

Process A

Process B BB blockedblocked

A enters critical region

B tries to enter critical region

B enters critical region

A leaves critical region

B leaves critical region

Time

Goals

• No two processes (threads) can be in their

critical region at the same time

• No assumptions about # of CPUs or their

speed

• No process outside of its critical region may

block another process

• No process should have to wait forever to

enter its critical region

Strict Alternation

while (TRUE) { while (turn != 0) ; /* loop */ critical_region (); turn = 1; noncritical_region (); }

while (TRUE) { while (turn != 1) ; /* loop */ critical_region (); turn = 0; noncritical_region (); }

Process A Process B

Busy Waiting

#define FALSE 0 #define TRUE 1 #define N 2 // # of processes int interested[N]; // Set to 1 if process j is interested int last_request; // Who requested entry last?

void enter_region(int process) { int other = 1 ‐process; // # of the other process interested[process] = TRUE; // show interest last_request = process; // Set it to my turn while (interested[other]==TRUE && last_request == process ) ; // Wait while the other process runs }

void leave_region (int process) { interested[process] = FALSE; // I’m no longer interested }

Hardware Support

Code for process Pi

while (1) { while (TestAndSet(lock)) ; // critical section lock = 0; // remainder of code }

Code for process Pi

while (1) { while (Swap(lock,1) == 1) ; // critical section lock = 0; // remainder of code }

int lock = 0;

Consumer Item citm; while (1) { if (counter == 0) sleep(); citm = buffer[out]; out = (out+1) % n; counter ‐= 1; if (count == n‐1) wakeup(producer); consume the item in citm … }

Producer/Consumer Problem

Shared variables const int n; typedef … Item; Item buffer[n]; int in = 0, out = 0, counter = 0;

Atomic statements:

countercounter +=+= 1;1; countercounter ‐‐== 1;1;

Producer Item pitm; while (1) { … produce an item into pitm … if (counter == n) sleep(); buffer[in] = pitm; in = (in+1) % n; counter += 1; if (counter==1) wakeup(consumer); }

Semaphore with Blocking

class Semaphore { int value; ProcessList pl;

void down () { value ‐= 1; if (value < 0) { // add this process to pl pl.enqueue(currentProcess); Sleep(); } }

void up () { Process P; value += 1; if (value <= 0) { // remove a process P from pl P = pl.dequeue(); Wakeup(P); } } }

Producer/Consumer with Semaphores

Producer int in = 0; Item pitem; while (1) { // produce an item // into pitem empty.down(); mutex.down(); buffer[in] = pitem; in = (in+1) % n; mutex.up(); full.up(); }

const int n; Semaphore empty(n),full(0),mutex(1); Item buffer[n];

Consumer int out = 0; Item citem; while (1) { full.down(); mutex.down(); citem = buffer[out]; out = (out+1) % n; mutex.up(); empty.up(); // consume item from // citem }

Binary Semaphore

Semaphore that only takes on the values 0 or 1

Counting Semaphore

Dining Philosophers