CS 25200: Systems Programming. Lecture 26: Classic Synchronization Problems

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

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Semaphores Semaphores: A Definition

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

POSIX / System Programming

Chapter 6: Process Synchronization

Process Synchronization(2)

Problem 1: Concepts (Please be concise)

Announcements. Class feedback for mid-course evaluations Receive about survey to fill out until this Friday

Lecture 6. Process Synchronization

Achieving Synchronization or How to Build a Semaphore

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks

Process Synchronization(2)

CMSC421: Principles of Operating Systems

Process Synchronization. studykorner.org

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

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

Systems Programming/ C and UNIX

More Shared Memory Programming

Synchronization II. q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks

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

Synchronization. Semaphores implementation

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010

Process Synchronization Mechanisms

Synchronization. Dr. Yingwu Zhu

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

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

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

Process Synchronization(2)

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

W4118 Operating Systems. Instructor: Junfeng Yang

Synchronising Threads

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C

Synchronization Primitives

Synchroniza+on II COMS W4118

2 Threads vs. Processes

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

More Types of Synchronization 11/29/16

Concurrency. Chapter 5

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

CMSC421: Principles of Operating Systems

Sistemi in tempo reale

Process Synchronization

Using Semaphores CS 241. March 14, University of Illinois

Interprocess Communication and Synchronization

Problem Set 2. CS347: Operating Systems

CS 153 Lab6. Kishore Kumar Pusukuri

Introduction to PThreads and Basic Synchronization

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

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

[537] Semaphores. Tyler Harter

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Concurrency - II. Recitation 3/24 Nisarg Raval Slides by Prof. Landon Cox and Vamsi Thummala

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

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

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

Operating systems and concurrency (B08)

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

Concept of a process

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

Paralleland Distributed Programming. Concurrency

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

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

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

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

Lecture 8: September 30

Chapter 6 Process Synchronization

Thread and Synchronization

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

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

Reminder from last time

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Chapter 6 Synchronization

Locks. Dongkun Shin, SKKU

Shared Memory: Virtual Shared Memory, Threads & OpenMP

Sistemi in tempo reale Anno accademico

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

ANKARA UNIVERSITY COMPUTER ENGINEERING DEPARTMENT BLM334-COM334 PROJECT

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

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Solving the Producer Consumer Problem with PThreads

CS 550 Operating Systems Spring Concurrency Semaphores, Condition Variables, Producer Consumer Problem

Week 3. Locks & Semaphores

Concurrent Programming I. Anna Lina Ruscelli Scuola Superiore Sant Anna

Process Synchronization

Last Class: Synchronization

Concurrency: Signaling and Condition Variables

Locks and semaphores. Johan Montelius KTH

Threads need to synchronize their activities to effectively interact. This includes:

CS3733: Operating Systems

CSC501 Operating Systems Principles. Process Synchronization

Transcription:

CS 25200: Systems Programming Lecture 26: Classic Synchronization Problems Dr. Jef Turkstra 2018 Dr. Jeffrey A. Turkstra 1

Announcements Lab 5 posted this evening Web server Password protection SSL cgi-bin File listings etc 2018 Dr. Jeffrey A. Turkstra 2

Lecture 26 Producer/consumer problem Readers and writers problem 2018 Dr. Jeffrey A. Turkstra 3

sem_wait (atomic) void sem_wait(semaphore S) { if (s->count > 0) { s->count--; return; add(s->q, current_thread); sleep(); // re-dispatch return; 2018 Dr. Jeffrey A. Turkstra 4

sem_post (atomic) void sem_post(semaphore S) { if (isempty(s->q)) { s->count++; else { thread = removefirst(s->q); wakeup(thread); // put thread on ready q return; 2018 Dr. Jeffrey A. Turkstra 5

Producer/consumer Suppose we have a circular bufer that is of a fixed sizee Want multiple threads to communicate using this bufer Common paradigm in device drivers and pipes Although usually have a pool of bufers instead of a single bufer 2018 Dr. Jeffrey A. Turkstra 6

Circular bufer 20 byte bufer with 14 elements G H I J K L M N A B C D E F Tail Head 2018 Dr. Jeffrey A. Turkstra 7

Interface enqueue() - add an item to the queue Block if queue is full dequeue() - remove an item from the queue Block if empty Need condition synchronizeation empty and full Do we need mutual exclusion? 2018 Dr. Jeffrey A. Turkstra 8

Implementation #include <pthread.h> #defne MAXSIZEE 32 typedef struct { char queue[maxsizee]; int head; int tail; pthread_mutex_t mutex; sem_t emptysem; sem_t fullsem; BoundedBufer; 2018 Dr. Jeffrey A. Turkstra 9

Initialization BoundedBufer ; pthread_mutex_init(& ->mutex, NULL); sem_init(& ->emptysem, 0, 0); sem_init(& ->fullsem, 0, MAXSIZEE); ->head = 0; ->tail = 0; 2018 Dr. Jeffrey A. Turkstra 10

enqueue() and dequeue() void enqueue(int val) { sem_wait(& ->fullsem); pthread_mutex_lock(& ->mutex); ->queue[tail] = val; ->tail = (tail + 1) % MAXSIZEE; pthread_mutex_unlock(& ->mutex); sem_post(& ->emptysem); int dequeue() { sem_wait(& ->emptysem); pthread_mutex_lock(& ->mutex); int val = queue[head]; head = (head + 1) % MAXSIZEE; pthread_mutex_unlock(& ->mutex); sem_post(& ->fullsem); return val; 2018 Dr. Jeffrey A. Turkstra 11

Bounded bufer notes emptysem represents the number of items in the queue fullsem represents the number of (empty) spaces in the queue If this seems backwards, you can switch them So, do we still need mutexes? 2018 Dr. Jeffrey A. Turkstra 12

Readers and writers problem Allow concurrent reads, but exclusive writes Constraints: Writers can proceed only if no active readers or writers Readers can proceed only if no active writers Need shared variables (state variables) to keep track of who is reading and writing Used in: databases, lookup tables, dictionaries, etc 2018 Dr. Jeffrey A. Turkstra 13

Interface read_lock() - obtain access to read Wait if writer holds lock read_unlock() - signal completion of reading write_lock() - obtain access to write Wait if any readers or writer holding the lock write_unlock() - signal completion of writing 2018 Dr. Jeffrey A. Turkstra 14

Implementation typedef struct { int readers; sem_t semaccess; pthread_mutex_t mutex; rwlock_t; rwlock_t rwlock; sem_init(&rwlock->semaccess, 0, 1); pthread_mutex_init(&rwlock->mutex); 2018 Dr. Jeffrey A. Turkstra 15

Readers void read_lock() { pthread_mutex_lock(&rwlock->mutex); rwlock->readers++; if (rwlock->readers == 1) { // First reader, o tain access sem_wait(&rwlock->semaccess); pthread_mutex_unlock(&rwlock->mutex); void read_unlock() { pthread_mutex_lock(&rwlock->mutex); rwlock->readers--; if (rwlock->readers == 0) { // Last reader, allow one writer to proceed sem_post(&rwlock->semaccess); pthread_mutex_unlock(&rwlock->mutex); 2018 Dr. Jeffrey A. Turkstra 16

Writers void write_lock() { sem_wait(&rwlock->semaccess); void write_unlock() { sem_post(&rwlock->semaccess); 2018 Dr. Jeffrey A. Turkstra 17

Notes Mutexes and semaphores are fair Thread that is queued first wakes up first Spin locks are not fair This implementation sufers from potential starvation of writers May take a long time for a writer to be able to write if readers is always > 0 2018 Dr. Jeffrey A. Turkstra 18

Questions? 2018 Dr. Jeffrey A. Turkstra 19