






















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
OPERATING SYSTEMS: THREE EASY PIECES. By Arpaci-Dusseau and Arpaci-Dusseau. Chapter 2.3. MODERN OPERATING SYSTEMS (MOS). By Andrew Tanenbaum ...
Typology: Schemes and Mind Maps
1 / 30
This page cannot be seen from the preview
Don't miss anything!























Chapter 26, 28, 31 OPERATING SYSTEMS: THREE EASY PIECES By Arpaci-Dusseau and Arpaci-Dusseau Chapter 2. MODERN OPERATING SYSTEMS (MOS) By Andrew Tanenbaum
objdump –-source thread-v [line 415] g_counter++; 400c38: 8b 05 6e 14 20 00 mov 0x201465,%eax # 6020ac <g_counter> 400c3e: 83 c0 01 add $0x1,%eax 400c41: 89 05 65 14 20 00 mov %eax,0x201465 # 6020ac <g_counter> Source code from “-g” flag Address Object code Assembly code Reference location
mov g_counter %eax add 1 %eax mov %eax g_counter mov 0x20146e(%rip),%eax add $0x1,%eax mov %eax,0x201465(%rip) Counter is 50. Thread T1 & T2, one processor. WCGW?
“critical section” “race condition”
What synchronization primitives should be provided? What support needed from hardware to build? How to make correct and efficient? How do programmers use them?
What basic mechanism can stop B from entering critical region when A in? Hint: just need to block access
lock_t mutex; // globally-allocated ’mutex’ … lock(&mutex); x = x + 1; // critical region unlock(&mutex);
How to build efficient lock? What hardware support is needed? What OS support? pthread_mutex_t lock; pthread_mutex_lock(&lock); x = x + 1; // or general CR pthread_mutex_unlock(&lock); See: “thread-v1.c”
This almost works … but not quite. Why not? Hint, has race condition - Can you spot it?
Done with hardware support. All modern computers since 1960’s e.g., x86 has compare-and-exchange Others: compare-and-swap, fetch- and-add, … all atomic
int mutex; // 0 -> lock available, 1 -> held void lock(int *mutex) { while (TestAndSet(mutex)) // 1 if held ; // spin-wait (do nothing) // once here, have lock! } void unlock(int *mutex) { *mutex = 0; } Note, no need to protect unlock() (Exercise: why not?) Now, what is major remaining shortcoming? Hint: code works, but could be more efficient