









































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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