Project 4: Synchronization

Similar documents
Operating systems. Lecture 12

CS 105, Spring 2007 Ring Buffer

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

CS342 - Spring 2019 Project #3 Synchronization and Deadlocks

CSE 380: Homework 2: Synchronization

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

CS 105, Spring 2015 Ring Buffer

Process Management And Synchronization

Puzzle: Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.

COMP 321: Introduction to Computer Systems

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer Problem.

cs 140 project 1: threads 9 January 2015

Operating Systems, Assignment 2 Threads and Synchronization

CPSC 545 Computing Systems Project 2: Hungary Eagles Need Feeding Due: April 27 th, 23:59PM

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Threads. Threads (continued)

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Compile the Hello World program

Intermediate Programming, Spring Misha Kazhdan

CS 261 Fall Mike Lam, Professor. Threads

Getting the files for the first time...2. Making Changes, Commiting them and Pull Requests:...5. Update your repository from the upstream master...

Software Development I

Gabrielle Evaristo CSE 460. Lab Dining Philosophers and Deadlock

Interprocess Communication and Synchronization

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

Process Synchronization

CS450 OPERATING SYSTEMS FINAL EXAM ANSWER KEY

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Process Synchronization(2)

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CS Mid term Exam Maximum points 100; Maximum time allowed: 75 minutes

Operating systems fundamentals - B06

Process Synchronization(2)

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

CS533 Concepts of Operating Systems. Jonathan Walpole

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

KING FAHD UNIVERSITY OF PETROLEUM & MINERALS. Information and Computer Science Department. ICS 431 Operating Systems. Lab # 9.

ECE 598 Advanced Operating Systems Lecture 23

Operating Systems. Week 7 5 th Semester, 2012 IN Fei Peng PAI Group University of Fribourg

Introduction to Operating Systems

5 Classical IPC Problems

CSEN 602-Operating Systems, Spring 2018 Practice Assignment 2 Solutions Discussion:

Shared Memory Programming. Parallel Programming Overview

Process Synchronization(2)

PROCESS SYNCHRONIZATION

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

CSci 4061 Introduction to Operating Systems. (Threads-POSIX)

Introduction to pthreads

EPL372 Lab Exercise 2: Threads and pthreads. Εργαστήριο 2. Πέτρος Παναγή

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

POSIX Threads and OpenMP tasks

Concurrent Server Design Multiple- vs. Single-Thread

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

HW2d Project Addenda

POSIX Threads. Paolo Burgio

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Programming Languages

Chapter 2 Processes and Threads

Agenda. - Final Project Info. - All things Git. - Make sure to come to lab for Python next week

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 1

Question 1 Monitor pseudo-code

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Synchronization Classic Problems

1. Introduction. 2. Project Submission and Deliverables

Synchronization Principles II

b. Developing multiple versions of a software project in parallel

real time operating systems course

Process Synchronization. studykorner.org

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Lecture 5 Threads and Pthreads II

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

1. Which of these Git client commands creates a copy of the repository and a working directory in the client s workspace. (Choose one.

LSN 13 Linux Concurrency Mechanisms

Recitation 14: Proxy Lab Part 2

Multicore and Multiprocessor Systems: Part I

Chapter 4 Concurrent Programming

Assignment #2. Problem 2.1: airplane synchronization

Introduction to Git. Database Systems DataLab, CS, NTHU Spring, 2018

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

PThreads in a Nutshell

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

pthreads CS449 Fall 2017

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Midterm Exam. October 20th, Thursday NSC

dbug User Manual Computer Science Department Carnegie Mellon University

Lab 01 How to Survive & Introduction to Git. Web Programming DataLab, CS, NTHU

Git. Charles J. Geyer School of Statistics University of Minnesota. Stat 8054 Lecture Notes

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CS314 Software Engineering Configuration Management

Transcription:

Project 4: Synchronization CS 311 For this project, you ll implement variations of two of the classic synchronization problems: Dining Philosophers and The Sleeping Barber. You ll use Pthreads mutex and condition variables in your implementation of the Dining Philosophers problem and POSIX unnamed semaphores in your implementation of the Sleeping Barber problem. Your solutions will be thread-based. You don t need your virtual machines for this project you can use phoenix. You ll be using a new repository on GitHub to submit this project and to collaborate with your partner. See GoucherLearn for the link to use to create your team and to create your repository. So, I had problems decoding your initials with the Project 2 repos, so let s try first name, last initial this time for the team names. For example: JillZ_TomK Remember to create a new directory for your working files and the local copy of your repo (El Presidente, I m looking at YOU!!!). As a refresher, here s the basic sequence of git commands: mkdir Project4 cd Project4 git init # DON T DO THIS IN YOUR HOME DIRECTORY!!!!!! git remote add origin YOUR_GITHUB_REPO_URL # add files git add. git commit -m "Initial commit." git push -u origin master # Only for the very first push to the remote git push # All pushes after the very first push git clone YOUR_GITHUB_REPO_URL git pull References The following are all available on, or linked to from, the course web site, or in the textbook: 1. Dining Philosophers Solution Using Monitors, Section 5.8.2 of the textbook. Note that this is a monitor-based solution, so it will require some modification the addition of mutual exclusion for critical sections and the use of while loops with condition variables, rather than if statements. 2. The Sleeping Barber Problem. Includes a semaphore-based solution. 3. Pthreads Synchronization, Section 5.9.4 of the textbook for introductory material on Pthreads mutex and condition variables, and POSIX unnamed semaphores. 4. Project 1 and Project 2 in Chapter 5, on pp. 251 253, for useful background information. 1

5. semaphore.c A concise illustration of POSIX semaphore functions in Linux. 6. producerconsumer.c An illustration of Pthreads operations in Linux, including thread creation, parameter passing, join, and mutex and condition variable operations. 7. hellothreaded.c An illustration of how to manage a fair number of threads using a pthread_t array. Code Structure for an Actor This illustrates the basic structure of a philosopher, barber, customer, etc. thread that requests or performs some service at various intervals. Because this is an infinite loop, you ll need to terminate your programs by typing Ctrl-c. /* Remember to inlude the Pthreads library when compiling: * * gcc -o foo foo.c -lpthread */ /* Include files */ #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> #include <assert.h> /* Function prototypes */ void delay(unsigned int *seed, unsigned int min, unsigned int max); void *actor1(void * ptr); /* Sleep a random amount of time between min and max seconds. */ void delay(unsigned int *seed, unsigned int min, unsigned int max) /* assert() terminates the program if its condition is false. */ assert(min <= max); sleep((unsigned int) (min + (rand_r(seed) % (max - min + 1)))); void *actor1(void * ptr) long tid = (long) ptr; /* Each thread needs its own private seed for the pseudo-random * number generator. */ unsigned int seed = tid; while (1) /* Simulate action 1 by sleeping. */ 2

printf("action 1\n"); delay(&seed, 3, 8); /* Some synchronization here. */ /* Simulate action 2 by sleeping. */ printf("action 2\n"); delay(&seed, 0, 10); /* More synchronization here. */ /* etc. */ int main() pthread_t actor; pthread_create(&actor, NULL, actor1, (void *) 1l); pthread_join(actor, NULL); return 0; I/O Hints In order to un-buffer I/O, put the following at the beginning of main(): /* Use unbuffered output for output logging purposes. */ setvbuf(stdout, (char *)NULL, _IONBF, 0); setvbuf(stderr, (char *)NULL, _IONBF, 0); You may find the following useful for timestamping your output: /* Necessary include files */ #include <time.h> #include <sys/time.h> #include <string.h> /* Function prototype */ void timestamp(char *msg); /* Function */ void timestamp(char *msg) char buf[128]; struct timeval tv; struct tm ltv; 3

gettimeofday(&tv, NULL); localtime_r(&tv.tv_sec, &ltv); sprintf(buf, "%02d:%02d:%05.2f ", ltv.tm_hour, ltv.tm_min, (double) (ltv.tm_sec + tv.tv_usec / 1.0E6)); strncat(buf, msg, 128 - strlen(buf) - 1); printf("%s", buf); /* Usage example */ foo(int tid) char msg[128]; sprintf(msg, "Cust %d wants haircut\n", tid); timestamp(msg); Dining Philosophers Implement a mutex and condition variable-based solution to Dining Philosophers, based upon the monitor-based pseudo-code given in Section 5.8.2 of the textbook. Using a turn variable, ensure that starvation is avoided in your implementation, without unduly causing the other philosophers to go hungry. (I.e., if it s philosopher 3 s turn and philosopher 3 is thinking, philosophers 2 and 4 shouldn t be prevented from eating.) Once the philosopher whose turn it is finally eats, it s the next philosopher s turn, where next is used in a circular way. Because of the real possibility of spurious signal operations, ensure that your condition variable wait operations are guarded by while statements. The Sleeping Barber Implement a POSIX unnamed semaphores-based solution to The Sleeping Barber, using the pseudocode in the The Sleeping Barber Problem reference document as a starting point. The town in which the barber cuts hair has one Very Important Person (VIP). When the VIP visits the barber, he gets preferential service he doesn t preempt a customer receiving a haircut at the time he arrives, nor does he cause a fifth waiting customer to relinquish his seat, but he will always be the next customer to receive a haircut. The barber thread should contain logic and printf() statements to indicate when the barber has no customers and will therefore go to sleep. Finally, use a semaphore, controlled by the barber, to explicitly force a rendezvous between the barber and the customer/vip receiving a haircut. Assume that a customer s haircut takes one second, but that the VIP s haircut takes two seconds. The VIP needs a new haircut five seconds after the previous haircut. Ordinary customer i needs a new haircut i + 2 seconds after the previous visit to the barbershop. To demonstrate your logic, run your program with two ordinary customers, demonstrating that the barber sleeps occasionally. Then, run your program with 10 ordinary customers, demonstrating that sometimes customers leave without getting a haircut, that the VIP receives priority treatment, and that the barber doesn t sleep when customers are waiting. 4

Project 4 Turn-in By the project deadline, email to me the GitHub https URL of your project repository. Remember to document assistance you received from others. This can be done in the email you send me your repo URL, in your README.md file, or in comments at the very top of your source code files. This project will be assessed as follows: Dining Philosophers: 40% The Sleeping Barber: 40% Readability: 10% Verifiability: 10% (Your source code should contain a sufficient number of informative printf() calls so that I can understand what s happening as your program runs.) 5