CS 149 Operating Systems February 17 Class Meeting
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Semaphores o A semaphore is a shared variable that holds an integer value and has atomic actions: wait(s) { while (s <= 0) block; s--; } n signal(s) { s++; } Testing and modifying a semaphore’s value must all be done without interruption. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 2
Semaphores, cont’d o A semaphore that can have any integer value is a counting semaphore. n Used to ensure processes that depend on each other will execute in the proper sequence: S 1; signal(s); n o wait(s); S 2; Just “semaphore” means “counting semaphore”. Statement S 2 can only execute after statement S 1. A binary semaphore only has the values 0 or 1. n Also called a mutex, to implement mutual exclusion: wait(m); // lock // execute critical region signal(m); // unlock Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak Just say “mutex” rather than “binary semaphore”. 3
Example Multithreading Program o Professor Zemma Fore is extremely popular with her students! n o During each of her office hours, students line up to visit her in order to get help and advice. Prof. Fore has a small office. n n Inside, there is room for only one student to visit. Outside her office, there are three chairs for students to sit and wait their turns. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 4
Example Multithreading Program, cont’d o During an office hour: n At the start of the hour, Prof. Fore opens her door for office visits. n Students arrive at random times. n Prof. Fore sees students in the order that they arrive. n Each visit with the professor lasts a random time: 1, 2, 3, 4, or 5 minutes. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 5
Example Multithreading Program, cont’d o During an office hour, cont’d: n At the end of a visit, the next waiting student (if any) immediately enters Prof. Fore’s office for a visit. n Whenever there are no students visiting or waiting, Prof. Fore works on the design of her programming language “Par. Fore” for her parallel programming class. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 6
Example Multithreading Program, cont’d o Whenever a student arrives: n If Prof. Fore is not already meeting with another student, the arriving student can immediately start an office visit. n If Prof. Fore is already meeting with another student, the arriving student sits down on an empty chair outside her office to wait. n If there are no empty chairs, the student leaves. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 7
Example Multithreading Program, cont’d o At the end of the office hour: n If Prof. Fore is not meeting a student, she closes her door and no more students can visit. n If she is meeting a student, she allows that visit to complete (which may take her past her closing time) before closing her door. n Any students still waiting then leave. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 8
Example Multithreading Program, cont’d o Write a program to simulate students visiting Prof. Fore during one of her office hours. n Design your program such that 1 second real time = 1 minute simulated time. n Write your program in C using the Pthreads library. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 9
Example Multithreading Program, cont’d o The program should print a message with a timestamp as each event occurs. n Events: student arrives, student starts office visit, etc. n The output should be in a tabular format that shows the state changes of the components of your simulation. n Will Prof. Fore ever find time to work on her language Par. Fore the course? Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak Demo 10
Example Multithreading Program, cont’d o The program contains a subtle threading bug! n n Causes a deadlock under certain circumstances. Can you find the error and correct it? Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 11
Assignment #3 o Create a multithreaded simulation of students enrolling into three sections of CS 149. n o Simulate each student with a thread. Three types of students: n n n Graduating seniors (GS) have highest priority. “Regular” seniors (RS) Everyone else (EE) have lowest priority. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 12
Assignment #3, cont’d o 75 students arrive for enrollment at random times spread over 2 minutes of real time. n o Arrival times are whole seconds. For each student: n n n Assign a unique id. Randomly be GS, RS, or EE with equal probability. Can enroll only into Section 1, Section 2, Section 3, or any section with equal probability. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 13
Assignment #3, cont’d o Three queues: GS queue, RS queue, EE queue n n o Each arriving student enters the appropriate queue. Students leave at the heads of the queues to be enrolled into a section. The queues work simultaneously. n n n Each queue can process and enroll only one student at a time. GS queue: randomly 1 or 2 seconds to process RS queue: randomly 2, 3, or 4 seconds to process EE queue: randomly 3, 4, 5, or 6 seconds to process Compute a new process time for each student. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 14
Assignment #3, cont’d o Each CS 149 section has a max capacity of 20. n n o Drop any student who cannot enroll after processing. Process the next student in the queue. The simulation finishes when n n All 75 students have been processed, or All three sections are full. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 15
Assignment #3, cont’d o Print a line for each event as it occurs: n n n o Timestamp each event print line. n n o A student arrives and enters a queue. A queue starts to process a student. A queue finishes processing a student and then either drops or enrolls the student into a section. Minutes and seconds, such as 0: 05 and 1: 12 The simulation starts at time 0: 00. Identify a student by id and type, such as #12. GS Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 16
Assignment #3, cont’d o At the end of the simulation, print: n n n o Who is enrolled in each section of CS 149. What was the turnaround time for each student (from arrival time to being enrolled or dropped). The average turnaround time of each queue. Who could not enroll and was dropped. Any other statistics you find interesting. Tips n n n What threads besides student threads? What are the critical regions? What process synchronization is necessary? Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 17
Assignment #3, cont’d o Extra credit (up to 10 points): n n o Impatient students give up and leave after waiting 10 seconds in a queue before being processed. At the end of the simulation, print the number of students who gave up and left. Turn in a zip file named after your team: n n n C source files Text file containing output from a simulation run. 1 or 2 page report on your software design. What threads? What shared data and critical regions? Note clearly if you What synchronization? Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak did the extra credit. 18
Assignment #3, cont’d o Subject line: CS 149 -section Assignment #3 team name n o CC all team members. Due Friday, February 27. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 19
Readers-Writers Problem o o Suppose that a file is a shared resource among multiple processes. n It is permissible for multiple processes to be reading the file at the same time. n But if one process is writing to the file, no other process (reader or writer) can access the file. Can we use semaphores and mutexes to solve this problem? Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 20
Readers-Writers Problem, cont’d o If no processes are currently accessing the file, then either a reader process or a writer process can start to access the file. o If a writer process starts to access the file: n o It must prevent any other reader or writer process from accessing the file until it’s done. If a reader process starts to access the file: n n It should allow other reader processes to access the file. It should prevent a writer process from accessing the file. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 21
Readers-Writers Problem, cont’d o Use semaphore rw_sem to keep other processes from accessing the file. o The writer process must always wait on rw_sem. n n A writer process blocks another writer process. A reader process blocks any writer process. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 22
Readers-Writers Problem, cont’d o The first reader process to access the file must wait on rw_sem n o However, subsequent reader processes don’t have to wait. n o Force any writer processes to wait. Multiple readers can read the file at the same time. The last reader process to complete its access of the file must signal the semaphore. n Let in any waiting writer process. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 23
Readers-Writers Problem, cont’d o Therefore, we must keep a count of how many reader processes are currently accessing the file. n If the count > 0, then a writer process can’t access the file. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 24
Readers-Writers Problem, cont’d o Writer process: Pseudocode Semaphore rw_sem = 1; // one shared resource (the file) void writer(void) { for (; ; ) { wait(&rw_sem); // keep out all other processes. . . // write to the file signal(&rw_sem); // let in a waiting process } } Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 25
Readers-Writers Problem, cont’d o Reader process: Pseudocode int count = 0; // count of reader processes void reader(void) { for (; ; ) { count++; if (count == 1) { wait(&rw_sem); }. . . count--; if (count == 0) { signal(&rw_sem); } } } Computer Science Dept. Spring 2015: February 17 What is wrong with this code? // the first reader only // keeps out writers // read the file // the last reader only // lets in a waiting writer We have a race condition with shared variable count. CS 149: Operating Systems © R. Mak 26
Readers-Writers Problem, cont’d o We have a race condition with variable count. o Therefore, any reader code that accesses count should be a critical region. n Use a mutex to enforce the mutual exclusion. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 27
Readers-Writers Problem, cont’d o Reader processes (corrected): Pseudocode int count = 0; // count of reader processes Mutex mutex = 1; void reader(void) { for (; ; ) { lock(&mutex); // start critical region count++; if (count == 1) { // first reader only wait(&rw_sem); // keep out writers } unlock(&mutex); // end critical region. . . // read the file lock(&mutex); // start critical region count--; if (count == 0) { // last reader only signal(&rw_sem); // let in a waiting writer } unlock(&mutex); // end critical region } Computer Science Dept. CS 149: Operating Systems } Spring 2015: February 17 © R. Mak 28
Dining Philosophers Problem o Five philosophers are seated at a round table. n n o Each philosopher has a plate of spaghetti. There is a single fork between each plate. A philosopher alternates between thinking and eating. n In order to eat spaghetti, a philosopher must obtain both the left and the right fork. Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 29
Dining Philosophers Problem, cont’d o Lunchtime in SJSU’s Department of Philosophy. Computer Science Dept. Spring 2015: February 17 Operating Systems: Design and Implementation CS 149: Operating Systems Tanenbaum & Woodhull © R. Mak (c) 2006 Prentice-Hall, Inc. All rights reserved. 0 -13 -142938 -8 30
Dining Philosophers Problem o How can we enable all the philosophers to eat with the shared forks? o Concurrency-control problem: How to allocate limited resources among competing processes. n n No deadlocks No starvation (in the process sense) Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 31
Dining Philosophers Problem, cont’d #define N 5 o void dine(int id) { int left = id; int right = (id+1)%N; } for (; ; ) { o think(); take_fork(left); take_fork(right); eat(); release_fork(left); release_fork(right); } o Computer Science Dept. Spring 2015: February 17 Why won’t this work? n What if each philosopher simultaneously picks up the left fork? n Starvation! A possible fix: n After failing to get a fork, wait a random amount of time and try again. Is there a solution that doesn’t involve retries? CS 149: Operating Systems © R. Mak 32
Dining Philosophers Problem: A Solution o o Since the forks are a shared resource, the dining code that accesses forks are a critical region. Therefore, use a mutex to enforce mutual exclusion. n n Lock the mutex before taking the forks. Unlock the mutex after releasing the forks. Computer Science Dept. Spring 2015: February 17 for (; ; ) { think(); lock(&mutex); take_fork(left); take_fork(right); eat(); release_fork(left); release_fork(right); unlock(&mutex); } CS 149: Operating Systems © R. Mak 33
Dining Philosophers: A Solution, cont’d for (; ; ) { think(); lock(&mutex); take_fork(left); take_fork(right); eat(); release_fork(left); release_fork(right); unlock(&mutex); } o Problems? n Only one philosopher can eat at a time! Computer Science Dept. Spring 2015: February 17 CS 149: Operating Systems © R. Mak 34
- Slides: 34