









































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Process synchronization, focusing on race conditions and critical regions. It covers different levels of concurrency, including multiprogramming, multithreading, multiprocessors, and multicomputers. The importance of process interaction and concurrency in software and hardware views. It also delves into race conditions in i/o and variable sharing, providing examples of single-threaded echo and producer/consumer scenarios. The concept of indivisible execution blocks, or critical regions, and proposes solutions using atomic locks, testandset instruction, swap instruction, and semaphores.
Typology: Slides
1 / 49
This page cannot be seen from the preview
Don't miss anything!










































o processes unaware of each other
ļ¼ they must use shared resources independently, without interfering, and leave them intact for the others
resource
o processes indirectly aware of each other
ļ¼ they work on common data and build some result together via the data
data
o processes directly aware of each other
ļ¼ they cooperate by communicating, e.g., exchanging messages
messages
Process Interaction & Concurrency
CPU
CPU
ļ¼ the outcome depends on the CPU scheduling or āinterleavingā of the threads (separately, each thread always does the same thing)
./multi_shopping grabbing the salad... grabbing the milk... grabbing the apples... grabbing the butter... grabbing the cheese...
./multi_shopping grabbing the milk... grabbing the butter... grabbing the salad... grabbing the cheese... grabbing the apples...
Race Condition
char chin, chout;
void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); }
./echo Hello world! Hello world!
Single-threaded echo
char chin, chout;
void echo() { do { chin = getchar(); chout = chin; putchar(chout); } while (...); }
1 5 6
2 3 4 unlucky CPU scheduling
ļ
Multithreaded echo (unlucky)
./echo Hello world! ee....
Race Condition
void echo() { char chin, chout;
do { chin = getchar(); chout = chin; putchar(chout); } while (...); }
void echo() { char chin, chout;
do { chin = getchar(); chout = chin; putchar(chout); } while (...); }
./echo Hello world! Hello world!
Single-threaded echo
1 5 6
2 3 4 unlucky CPU scheduling
ļ
Multithreaded echo (unlucky)
./echo Hello world! eH....
changed to local variables
Race Condition
while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }
while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed }
register1 = count register1 = register1 + 1 count = register
register2 = count register2 = register2 - 1 count = register
enter critical region?
exit critical region
enter critical region?
exit critical region
ļ¼ critical regions can be protected from concurrent access by padding them with entrance and exit gates o a thread must try to check in, then it must check out
void echo() { char chin, chout; do {
chin = getchar(); chout = chin; putchar(chout);
} while (...); }
void echo() { char chin, chout; do {
chin = getchar(); chout = chin; putchar(chout);
} while (...); }
Critical Regions
B^ critical region
Atomic lock