Project #3 Threads and Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
CS 149: Operating Systems February 3 Class Meeting
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Chapter 2 Processes and Threads
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Using synchronization primitives CS-3013 A-term Using Synchronization Primitives in C, C++, and Linux Kernel CS-3013 Operating Systems A-term 2008.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Project #3, Threads and Synchronization CS-3013 A-term Project #3 Threads and Synchronization CS-3013, Operating Systems A-term 2009 Due Tuesday,
Understanding BubbleSort CS-502 (EMC) Fall Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Introduction to Synchronization CS-3013 A-term Introduction to Synchronization CS-3013 Operating Systems (Slides include materials from Modern Operating.
Introduction to Synchronization CS-3013 A-term Introduction to Synchronization CS-3013, Operating Systems A-term 2009 (Slides include materials from.
02/02/2004CSCI 315 Operating Systems Design1 Threads Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Project 1, Command Shell CS-502 (EMC) Fall Programming Project #1 Command Shell CS-502, Operating Systems EMC, Fall 2009 (Slides include materials.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
THREAD Subject:T0934 / Multimedia Programming Foundation Session:3 Tahun:2009 Versi:1/0.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Chapter 4 – Thread Concepts
Threads in Java Two ways to start a thread
Processes and threads.
PROCESS MANAGEMENT IN MACH
PARALLEL PROGRAM CHALLENGES
CSE 501N Fall ‘09 21: Introduction to Multithreading
Day 12 Threads.
Project 3 Threads and Synchronization
Chapter 4 – Thread Concepts
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Multithreading Tutorial
Processes in Unix, Linux, and Windows
Nachos Threads and Concurrency
Processes in Unix, Linux, and Windows
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Multithreading Chapter 23.
Recitation 14: Proxy Lab Part 2
Processes in Unix, Linux, and Windows
Introduction to Memory Management
Multithreading.
Multithreading Tutorial
Multithreaded Programming
CS-3013 Operating Systems Hugh C. Lauer
Critical section problem
Threads and Concurrency
Programming Project #1 Command Shell
Programming Project #1 Fork and Command Shell
Concurrency: Mutual Exclusion and Process Synchronization
Multithreading Tutorial
Introduction to Synchronization
CSC227: Operating Systems
Multithreading Tutorial
First Semester 1439/1440 Welcome 
Alternate Project #4 Process and Thread Performance
Thread Synchronization including Mutual Exclusion
Threads and Concurrency
CS-3013 Operating Systems Hugh C. Lauer
Processes in Unix, Linux, and Windows
9. Threads SE2811 Software Component Design
CMPE 135: Object-Oriented Analysis and Design April 30 Class Meeting
CS-3013 Operating Systems Hugh C. Lauer
CS 144 Advanced C++ Programming May 7 Class Meeting
9. Threads SE2811 Software Component Design
Presentation transcript:

Project #3 Threads and Synchronization CS-3013 Operating Systems Hugh C. Lauer (Slides include materials from Slides include materials from Modern Operating Systems, 3rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7th ed., by Silbershatz, Galvin, & Gagne) CS-3013, C-Term 2012 Project #3 Threads and Synchronization

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 CS-3013, C-Term 2012 Project #3 Threads and Synchronization 2

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 Any number of same sex may be in bathroom at same time. A user must wait until vacant if occupied by opposite sex Any number of users may by waiting at same time. Problem #51 of Tanenbaum, p. 174 CS-3013, C-Term 2012 Project #3 Threads and Synchronization 3

Project #3 Threads and Synchronization Implementation No Java. Too easy with SYNCHRONIZED classes Control module in C Using pthread synchronization functions Multi-threaded Test Program One thread per simulated person Operate for simulated time Random attempts to access and stay in bathroom CS-3013, C-Term 2012 Project #3 Threads and Synchronization 4

Project #3 Threads and Synchronization Implementation Control module:– A .h file and a .c or .cpp file Interface:– enum gender = {male, female}; void Enter(gender g); void Leave(void); void Initialize(...); void Finalize(...); Must correctly maintain:– State # of users in bathroom Gender of users in bathroom CS-3013, C-Term 2012 Project #3 Threads and Synchronization 5

Project #3 Threads and Synchronization Implementation Control module:– A .h file and a .c or .cpp file Interface:– enum gender = {male, female}; void Enter(gender g); void Leave(void); void Initialize(...); void Finalize(...); Must correctly maintain:– State # of users in bathroom Gender of users in bathroom Waits until vacant if occupied by opposite gender Sets state to vacant if this is the last user to leave Notifies waiting threads that they can proceed Multiple threads trying to access or change at same time! CS-3013, C-Term 2012 Project #3 Threads and Synchronization 6

Multi-threaded Test Program main() function is master thread Interprets argc and argv Spawns n user threads representing individuals Randomly sets gender, loop count of each user thread Specifies mean arrival time and mean stay time Waits for user threads to finish Prints summary Exits cleanly With no outstanding user threads! CS-3013, C-Term 2012 Project #3 Threads and Synchronization 7

Project #3 Threads and Synchronization User Thread Loops loop count times A random number For each iteration:– Generate random arrival and stay times based on means 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 So that master thread knows that user thread is done CS-3013, C-Term 2012 Project #3 Threads and Synchronization 8

With a few minor challenges! As Simple as That! With a few minor challenges! CS-3013, C-Term 2012 Project #3 Threads and Synchronization 9

Synchronization Challenges Note that mutex is for managing data structure, not bathroom Shared data structure 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 race conditions or getting mixed up. Semaphore or condition variable is for waiting for access to bathroom CS-3013, C-Term 2012 Project #3 Threads and Synchronization 10

Project #3 Threads and Synchronization Other Challenges Random number generation Loop count (per thread) Arrival interval, stay time per iteration of thread Normal distribution Box-Muller transform — uses two random #’s Use ½ “standard deviation” for width Simulating units of time #include <usleep.h> Suggest 1or 10 milliseconds for one time unit. CS-3013, C-Term 2012 Project #3 Threads and Synchronization 11

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 Analysis of test cases Three with different parameters CS-3013, C-Term 2012 Project #3 Threads and Synchronization 12

Individual Project, but … You may discuss algorithms, strategies, etc. You may share .h file for bathroom interface May help each other & swap test programs Must write individual code Own style, own words, own documentation, etc. CS-3013, C-Term 2012 Project #3 Threads and Synchronization 13

Project #3 Threads and Synchronization Due Date Project due on Wednesday, February 8, 2012, 11:59 PM Submit via Turnin Project 3 Zip all files together! Report to instructor or TAs any difficulties CS-3013, C-Term 2012 Project #3 Threads and Synchronization 14

Project #3 Threads and Synchronization Questions? CS-3013, C-Term 2012 Project #3 Threads and Synchronization 15