130 likes | 151 Views
Project #3 Threads and Synchronization. CS-3013, Operating Systems A-term 2009 Due Tuesday, September 22, 2009, 11:59 PM. Objective. To learn to use threads and synchronization mechanisms in user space To implement a multi-threaded program that aggressively pounds on a shared resource.
E N D
Project #3Threads and Synchronization CS-3013, Operating SystemsA-term 2009 Due Tuesday, September 22, 2009, 11:59 PM Project #3, Threads and Synchronization
Objective • To learn to use threads and synchronization mechanisms in user space • To implement a multi-threaded program that aggressively pounds on a shared resource Project #3, Threads and Synchronization
The Problem • Model a communal bathroom used by both sexes • Possible states of the bathroom:– • Vacant • Occupied by women • Occupied by men • Rules:– • Anyone may enter if vacant • A user may enter if occupied by same sex • A user must wait until vacant if occupied by opposite sex • Any number of users may by waiting at same time. Project #3, Threads and Synchronization
Multiple threads trying to access or change at same time! Waits until vacant if occupied by opposite gender Sets state to vacant if this is the last user occupying Implementation • Control module:– • A .h file and a .c file • Interface:– Enum gender = {male, female};void Enter(gender g);void Leave(void);void Initialize(...); • Must correctly maintain:– • State • # of users in bathroom • Gender of users in bathroom Project #3, Threads and Synchronization
With no outstanding user threads! Multi-threaded Test Program • main() function is master thread • Interprets argc and argv • Spawns n user threads • Randomly sets gender, loop count of each user thread • Specifies arrival time and stay time • Mean and standard deviation • Waits for user threads to finish • Prints summary • Exits cleanly Project #3, Threads and Synchronization
So that master thread knows that user thread is done User Thread • Loops loop count times • For each iteration:– • Generate random arrival and stay times • Sleep arrival units of time • Invoke Enter(gender) • May wait a long time if occupied by opposite gender • Sleep stay units of time • Invoke Leave() • Collect statistics • Print statistics (min, max, average wait time to enter) • Exit cleanly Project #3, Threads and Synchronization
As Simple as That! With a few minor challenges! Project #3, Threads and Synchronization
Synchronization Challenges • Shared database representing state of bathroom, count, etc. • Mutex for protecting shared access • Semaphore or condition variable for waiting • Clean exit from user thread • pthread_join or barrier synchronization & detach • Printing to stdout from multiple threads • Without getting mixed up. Project #3, Threads and Synchronization
Other Challenges • Random number generation • Around a mean; use “standard deviation” for width • Optional (extra credit) • Poisson distribution for arrivals • Gaussian distribution for stay times • Simulating units of time • #include <usleep.h> • Suggest 10 or 100 milliseconds for one time unit. Project #3, Threads and Synchronization
Write-up • Explain invariant of shared data structure • Show Enter() and Leave() preserve invariant • Or show states & state transition diagram • Explain master thread and user thread relationship • Creation and termination Project #3, Threads and Synchronization
Individual and Class Project • Suggest you define • Common command line format • Common .h file for control module • May discuss algorithms, strategies, etc. • May help each other test programs • Write individual code • Own style, own words, own documentation, etc. Project #3, Threads and Synchronization
Due Date • Project due on Tuesday, September 22, at 11:59 PM • Submit via web-based Turnin • Project 3 • Report to instructor or TAs any difficulties Project #3, Threads and Synchronization
Questions? Project #3, Threads and Synchronization