1 / 13

Project #3 Threads and Synchronization

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.

weberjane
Download Presentation

Project #3 Threads and Synchronization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Project #3Threads and Synchronization CS-3013, Operating SystemsA-term 2009 Due Tuesday, September 22, 2009, 11:59 PM Project #3, Threads and Synchronization

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. As Simple as That! With a few minor challenges! Project #3, Threads and Synchronization

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. Questions? Project #3, Threads and Synchronization

More Related