



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
Material Type: Notes; Professor: Berger; Class: Operating Systems; Subject: Computer Science; University: University of Massachusetts - Amherst; Term: Fall 2007;
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




CMPSCI 377 Operating Systems Fall 2007
Lecturer: Emery Berger Scribe: Andrew Huang
Threads must ensure consistency. Otherwise, threads may result in a race condition leading to a non- deterministic result. A race is when the result is dependent the order in which the threads finish.
For example: Thread A says x = 1, and if (x == 2), then print ‘What?’ Thread B says x = 2. What is x?
Threads require synchronization operations for this to be answered.
The too much milk problem
time You Your roommate 3:00 Arrive Home 3:05 Look in fridge, no milk 3:10 Leave for grocery 3:15 Arrive home 3:20 Arrive at grocery Look in fridge, no milk 3:25 Buy milk Leave for grocery 3:30 Arrive home, put milk in fridge 3:35 Arrive at grocery 3:40 Buy milk 3:45 Arrive home, put milk in fridge 3:50 Uh oh!
This is why we need to synchronize activities!
Terminology
Definition 9.1 Mutual exclusion (mutex) prevents multiple threads from entering. The critical sec- tion is code that only one thread can execute at a time. A lock is a mechanism for mutual exclusion.
9-2 Lecture 9: October 18
Correctness properties
First: use atomic loads stores as building blocks
thread A thread B
if(no milk && no note) if (no milk && no note) leave note leave note buy milk buy note remove note remove note
! If both threads switch after reading the if statement, then both will buy milk.
Idea: use labeled notes
thread A thread B
leave note A leave note B if (no note B) if (no note A) if (no milk) if (no milk) buy milk buy milk remove note A remove note B
9-4 Lecture 9: October 18
Asymmetrical
Poor utilization
Possibly non-portable
Synchronization (making an area of code atomic) is complicated Better way - provide language-level support
Increasingly high-level approaches (pthread supplies all these):
Provide mutual exclusion to shared data via two atomic routines:
Rules:
Lecture 9: October 18 9-
POSIX standard for C/C++ Mutual exclusion locks
pthread mutex init(l); .. pthread mutex lock(l); update data; /* critical section */ pthread mutex unlock(l);
Routine Prefix Functional Group pthread Threads themselves and miscellaneous subroutines pthread attr Threads attributes objects pthread mutex Mutexes
thread A thread B
p m lock(l) p m lock(l) if (no milk) if (no milk) buy milk buy milk p m lock(l) p m lock(l)
The code in both threads is exactly the same. Clean, symmetric - but how do we implement it? Hardware support prevents both locks from happening at the same time. Dekker’s algorithm allows mutual exlusion.
Requires hardware support (in general) Can build on atomic operations:
Lecture 9: October 18 9-
Could run Z=x, and then X=1, because reads can come first but..
Barrier prevents misordering X = 1 BARRIER //don’t execute until above is done Y = 2 Z = X
Communication between threads:via shared variables Critical sections = regions of code that modify or access shared variables Must be protected by synchronization primitives that ensure mutual exclusion