

















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!


















Stack (T1) Code Static Data Heap Stack (T2) Stack (T3) Thread 3 Thread 2 PC (T1) PC PC^ (T3) (T2) Thred 1
balance = get_balance(account); balance = balance – amount; balance = get_balance(account); balance = balance – amount; put_balance(account, balance); put_balance(account, balance); Execution sequence seen by CPU Context switch
withdraw (account, amount) { balance = get_balance(account); balance = balance – amount; put_balance(account, balance); return balance; } Critical Section
withdraw (account, amount) { acquire(lock); balance = get_balance(account); balance = balance – amount; put_balance(account, balance); release(lock); return balance; } acquire(lock); balance = get_balance(account); balance = balance – amount; balance = get_balance(account); balance = balance – amount; put_balance(account, balance); release(lock); acquire(lock); put_balance(account, balance); release(lock); Critical Section
struct lock { int held = 0; } void acquire (lock) { while (lock->held); lock->held = 1; } void release (lock) { lock->held = 0; } A context switch can occur here, causing a race condition
struct lock { int turn = 0; } void acquire (lock) { while (lock->turn != this_thread ); } void release (lock) { lock->turn = other_thread ; }
struct lock { int turn = 0; int interested[2] = [FALSE, FALSE]; } void acquire (lock) { lock->interested[ this_thread ] = TRUE; turn = other_thread ; while (lock->interested[ other_thread ] && turn== other_thread ); } void release (lock) { lock->interested[this_thread] = FALSE; }
struct lock { int held = 0; } void acquire (lock) { while ( test-and-set(&lock->held) ); } void release (lock) { lock->held = 0; }