CSCE 313: Introduction to Computer Systems

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

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

Multithreading Programming II

Synchronization Mechanisms

POSIX Threads. Paolo Burgio

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

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

real time operating systems course

Synchronization Primitives

Locks and semaphores. Johan Montelius KTH

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

Locks and semaphores. Johan Montelius KTH

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a

Process Synchronization

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

CS 153 Lab6. Kishore Kumar Pusukuri

Resource Access Control (2) Real-Time and Embedded Systems (M) Lecture 14

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Real Time Operating Systems and Middleware

Shared Memory: Virtual Shared Memory, Threads & OpenMP

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

W4118 Operating Systems. Instructor: Junfeng Yang

ANSI/IEEE POSIX Standard Thread management

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Operating systems and concurrency (B10)

Posix Threads (Pthreads)

CS 33. Multithreaded Programming III. CS33 Intro to Computer Systems XXXIV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Multithread Programming. Alexandre David

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

Paralleland Distributed Programming. Concurrency

Synchronization. Semaphores implementation

Week 3. Locks & Semaphores

A Note About Makefiles. In OneFS/BSD be sure to build using gmake You may have to remove -pedantic-errors from CFLAGS

POSIX PTHREADS PROGRAMMING

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

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

Introduction to parallel computing

Process Synchronization. studykorner.org

POSIX Semaphores. Operations on semaphores (taken from the Linux man page)

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

More Types of Synchronization 11/29/16

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

CS345 Opera,ng Systems. Φροντιστήριο Άσκησης 2

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

Synchroniza+on II COMS W4118

Real Time Operating System Support for Concurrency

Concurrency, Parallel and Distributed

Topics: Process (Thread) Synchronization (SGG, Ch 5 in 9th Ed Ch 6 in Old Ed) (USP, Chapter 13-14) CS 3733 Operating Systems

Multi-threaded Programming

Network Programming TDC 561

Programming RT systems with pthreads

Lecture 6. Process Synchronization

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

Shared Memory Parallel Programming with Pthreads An overview

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

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

Operating Systems (2INC0) 2017/18

Programming RT systems with pthreads

Thread and Synchronization

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

Operating systems and concurrency (B08)

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

Concurrent Programming Lecture 10

POSIX / System Programming

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

Chapter 4 Concurrent Programming

Synchronization. Dr. Yingwu Zhu

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

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

Process Synchronization(2)

Computer Systems Laboratory Sungkyunkwan University

THREADS. Jo, Heeseung

Synchronization I q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors

LSN 13 Linux Concurrency Mechanisms

Parallel Programming with Threads

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

Programming Shared Address Space Platforms (cont.) Alexandre David B2-206

Introduction to Embedded Systems

Midterm I SOLUTIONS October 14 th, 2015 CS162: Operating Systems and Systems Programming

11/04/2018. Outline. Process. Process. Pthread Library. Process and threads

Process Synchronization(2)

Advanced Multiprocessor Programming: Locks

Resource sharing and blocking. The Mars Pathfinder Unbounded priority inversion Priority inheritance

UMTX OP (2) FreeBSD System Calls Manual UMTX OP (2)

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

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

Programming with Shared Memory PART I. HPC Fall 2010 Prof. Robert van Engelen

Operating System Labs. Yuanbin Wu

Engineering Robust Server Software

Resource Access Control in Real-Time Systems. Resource Access Control in Real-Time Systems

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

Threads. Jo, Heeseung

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Threads. lightweight processes

Lecture 9. Remote Procedure Calls Introduction to Multithreaded Programming

Lecture 6 Coordinating Resources

Process Synchronization(2)

More Shared Memory Programming

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Transcription:

CSCE 313 Introduction to Computer Systems Instructor: Dr. Guofei Gu http://courses.cse.tamu.edu/guofei/csce313 POSIX Thread Synchronization Mutex Locks Condition Variables Read-Write Locks Semaphores Reading: R&R, Ch 13/14 1

POSIX Thread Synchronization Mutex Locks Condition Variables Read-Write Locks Semaphores Reading: R&R, Ch 13 Mutex Locks #include <pthread.h> int pthread_mutex_init( pthread_mutex_t * restrict mutex, const pthread_mutexattr_t * restrict attr); pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; EAGAIN: System lacks non-memory resources to initialize *mutex ENOMEM: System lacks memory resources to initialize *mutex EPERM: Caller does not have appropriate privileges int pthread_mutex_destroy(pthread_mutex_t *mutex); int pthread_mutex_lock (pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock (pthread_mutex_t *mutex); EINVAL: mutex configured with priority-ceiling on, and caller s priority is higher than mutex s current priority ceiling. EBUSY: another thread holds the lock (returned to mutex_trylock) 2

Mutex Locks: Operations pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mylock); /* critical section */ pthread_mutex_unlock(&mylock); Use mutex locks to preserve critical sections or obtain exclusive access to resources. Hold mutexes for short periods of time only! Short periods?! For example, changes to shared data structures. Use Condition Variables when waiting for events! Uses for Mutex Locks: Unsafe Library Functions Def: Thread-safe function: Exhibits no race conditions in multithreaded environment. Many library functions are not thread-safe! Can be made thread-safe with mutexes. #include <pthread.h> #include <stdlib.h> int randsafe(int * result) { static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; int error; if (error = pthread_mutex_lock(&lock)) *result = rand(); return pthread_mutex_unlock(&lock); 3

Threads and Priorities: Interlude on Mars! Landing on July 4, 1997 experiences software glitches Pathfinder experiences repeated RESETs after starting gathering of meteorological data. RESETs generated by watchdog process. Timing overruns caused by priority inversion. Resources: http://research.microsoft.com/~mbj /Mars_Pathfinder/ Priority Inversion on Mars Pathfinder Task bc_dist becomes active blocks on mutex Task bc_sched detects overrun high priority other tasks Task ASI/MET starts locks mutex gets preempted low priority 4

POSIX Thread Synchronization Mutex Locks Condition Variables Read-Write Locks Semaphores Reading: R&R, Ch 13 POSIX Condition Variables waiting for a particular condition (e.g. x==y) while (x!= y); pthread_mutex_lock(&m); while (x!= y) pthread_cond_wait(&v, &m); /* now we are in the critical section */ pthread_mutex_unlock(&m); correct strategy to wait for condition while (x!= y): 1. lock a mutex 2. test the condition (x==y) 3. if TRUE, unlock mutex and exit loop 4. if FALSE, suspend thread and unlock mutex (?!!!) pthread_mutex_lock(&m); x++; pthread_cond_signal(&v); pthread_mutex_unlock(&m); When cond_wait returns, thread owns mutex and can test condition again. Call cond_wait only if you own mutex! 5

Example: Thread-Safe Barrier Locks /* shared variables */ static pthread_cond_t bcond = PTHREAD_CONT_INITIALIZER; static pthread_mutex_t bmutex = PTHREAD_MUTEX_INITIALIZER; static int count = 0; /* how many threads are waiting? */ static int limit = 0; /* initialize the barrier */ int initbarrier(int n) { int error; if (error = pthread_mutex_lock(&bmutex)) if (limit!= 0) { /* don t initialize barrier twice! */ pthread_mutex_unlock(&bmutex); return EINVAL; limit = n; return pthread_mutex_unlock(&bmutex); Example: Thread-Safe Barrier Locks /* shared variables */ static pthread_cond_t /* wait bcond at barrier = PTHREAD_CONT_INITIALIZER; until all n threads arrive */ static pthread_mutex_t int waitbarrier(void) bmutex = PTHREAD_MUTEX_INITIALIZER; { static int count = 0; int /* how berror many = threads 0; are waiting? */ static int limit = 0; int error; if (error = pthread_mutex_lock(&bmutex)) if (limit <= 0) { /* barrier not initialized?! */ pthread_mutex_unlock(&bmutex); return EINVAL; count++; while ((count < limit) &&!berror) berror = pthread_cont_wait(&bcond, &bmutex); if (!berror) /* wake up everybody */ berror = pthread_cont_broadcast(&bcond); error = pthread_mutex_unlock(&bmutex); if (berror) return berror; 6

Timed Wait on Condition Variables #include <pthread.h> int pthread_cond_timedwait( pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec * abstime); Reader/Writer Locks R/W locks differentiate between exclusive (write) and shared (read) access. Reader vs. writer priority not specified in POSIX. #include <pthread.h> int pthread_rwlock_init( pthread_rwlock_t * rwlock, const pthread_rwlockattr_t * attr); EAGAIN: System lacks non-memory resources to initialize *rwlock ENOMEM: Yada Yada int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock); int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock); int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock); int pthread_rwlock_unlock (pthread_rwlock_t *rwlock); 7

R/W Lock Example: Vanilla Shared Container /* shared variable */ static pthread_rwlock_t listlock; static int lockiniterror = 0; int init_container(void){ return pthread_rwlock_init(&listlock, NULL) /* add an item */ int add_data_r(data_t data, key_t key) { int error; if (error = pthread_rwlock_wrlock(&listlock)) { errno = error; return -1; add_data(data, key); if (error = pthread_rwlock_unlock(&listlock)) { errno = error; error = -1; R/W Lock Example: Vanilla Shared Container /* shared variable */ static pthread_rwlock_t listlock; static int lockiniterror = 0; /* add an item */ int get_data_r(key_t key, data_t * datap) { int error; if (error = pthread_rwlock_rdlock(&listlock)) { errno = error; return -1; get_data(key, datap); if (error = pthread_rwlock_unlock(&listlock)) { errno = error; error = -1; 8

POSIX Semaphores Similar to what we have described in the previous classes #include <semaphore.h> Sem_t sem; int sem_init(sem_t *sem, int pshared, unsigned value); int sem_wait(sem_t *sem); int sem_trywait(sem_t *sem); int sem_post(sem_t *sem); int sem_destroy(sem_t *sem); 9