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

Similar documents
Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Roadmap. Concurrent Programming. Concurrent Programming. Communication Between Tasks. Motivation. Tevfik Ko!ar

CSC Systems Programming Fall Lecture - XVIII Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 11 th, 2008

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

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

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

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

Roadmap. Concurrent Programming. Concurrent Programming. Motivation. Why Threads? Tevfik Ko!ar. CSC Systems Programming Fall 2010

Process Synchronization. studykorner.org

Using make. Using Variables in Makefiles. Implicit Rules. Static vs Dynamic Libraries. Libraries. Tevfik Ko!ar

Thread and Synchronization

CSC Systems Programming Fall Lecture - XXIII Final Review. Tevfik Koşar. Louisiana State University

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

Synchronization Primitives

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

Process Synchronization

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

POSIX Threads. Paolo Burgio

Multithreading Programming II

Week 3. Locks & Semaphores

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

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

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

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

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

real time operating systems course

Locks and semaphores. Johan Montelius KTH

Locks and semaphores. Johan Montelius KTH

Synchronization. Semaphores implementation

Paralleland Distributed Programming. Concurrency

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

The Fundamentals. Port Assignments. Common Protocols. Data Encapsulation. Protocol Communication. Tevfik Ko!ar

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

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

W4118 Operating Systems. Instructor: Junfeng Yang

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

POSIX PTHREADS PROGRAMMING

Concurrent Server Design Multiple- vs. Single-Thread

Process Synchronization(2)

Shared Memory: Virtual Shared Memory, Threads & OpenMP

Parameter Passing in C. Pointer Arithmetic. Parameter Passing in C. Pointer Arithmetic. Pointer Arithmetic. Tevfik Ko!ar

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

Introduction to PThreads and Basic Synchronization

Threads. lightweight processes

Concurrent Programming

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

The mutual-exclusion problem involves making certain that two things don t happen at once. A non-computer example arose in the fighter aircraft of

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

CSC Systems Programming Fall Lecture - XV Network Programming - I. Tevfik Ko!ar. Louisiana State University. November 9 th, 2010

Process Synchronization(2)

Process Synchronization(2)

CSE 4/521 Introduction to Operating Systems

LSN 13 Linux Concurrency Mechanisms

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

Synchronization Mechanisms

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

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

CSCE 313: Introduction to Computer Systems

Multi-threaded Programming

Basic C Program: Print to stdout. Basic C Program. Basic C Program: Print to stdout. Header Files. Read argument and print. Read argument and print

Dining Philosophers, Semaphores

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

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

CS 153 Lab6. Kishore Kumar Pusukuri

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 1 September 29, 2014

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

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

Concurrency and Threads

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

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

Synchroniza+on II COMS W4118

Real Time Operating Systems and Middleware

Lecture 6. Process Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 6: Process Synchronization

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

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

Operating systems and concurrency (B08)

Lecture 18. Log into Linux. Copy two subdirectories in /home/hwang/cs375/lecture18/ $ cp r /home/hwang/cs375/lecture18/*.

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

9/30/2014. CS341: Operating System High Level Construct: Monitor Deadlock Conditions Prevention, Avoidance Detection and Recovery

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

Process Synchronization

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

Final Examination (Fall 2016) (Section 1)

Chapter 6 Process Synchronization

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

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

Synchronization Principles

CS370 Operating Systems

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

An Introduction to Parallel Systems

High-level Synchronization

This exam paper contains 8 questions (12 pages) Total 100 points. Please put your official name and NOT your assumed name. First Name: Last Name:

CSE 333 Section 9 - pthreads

Semaphores Semaphores: A Definition

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

Transcription:

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

Concurrency Issues 3 Synchronization Mechanism that allows the programmer to control the relative order in which operations occur in different threads or processes. 4

Critical Section 5 Busy Waiting 6

Suspend and Resume 7 Mutual Exclusion 8

Mutual Exclusion: Problem 1 9 Mutual Exclusion: Problem 2 10

Mutual Exclusion: Problem 2 11 Mutual Exclusion: Problem 3 12

Mutual Exclusion: Problem 3 13 Mutual Exclusion: Solution 14

POSIX Threads: MUTEX int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex); int pthread_mutex_destroy(pthread_mutex_t *mutex); a new data type named pthread_mutex_t is designated for mutexes a mutex is like a key (to access the code section) that is handed to only one thread at a time the attribute of a mutex can be controlled by using the pthread_mutex_init() function the lock/unlock functions work in tandem 15 #include <pthread.h>... pthread_mutex_t my_mutex; // should be of global scope... int main() {! int tmp;! // initialize the mutex! tmp = pthread_mutex_init( &my_mutex, NULL );! // create threads! pthread_mutex_lock( &my_mutex );! do_something_private();! pthread_mutex_unlock( &my_mutex );! return 0; } MUTEX Example Whenever a thread reaches the lock/unlock block, it first determines if the mutex is locked. If so, it waits until it is unlocked. Otherwise, it takes the mutex, locks the succeeding code, then frees the mutex and unlocks the code when it's done. 16

Semaphores Semaphore S integer variable Two standard operations modify wait() and signal() Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; } Less complicated Can only be accessed via two indivisible (atomic) operations 17 Semaphores Int sum = 0; Thread 1: int t; wait(sem) sum = sum + x; t = sum;. signal(sem); Thread 2: int t; wait(sem); sum = sum + y; t = sum; signal(sem); Use of semaphores for thread synchronization! 18

POSIX: Semaphores creating a semaphore: int sem_init(sem_t *sem, int pshared, unsigned int value); initializes a semaphore object pointed to by sem pshared is a sharing option; a value of 0 means the semaphore is local to the calling process gives an initial value value to the semaphore terminating a semaphore: int sem_destroy(sem_t *sem); frees the resources allocated to the semaphore sem usually called after pthread_join() an error will occur if a semaphore is destroyed for which a thread is waiting 19 POSIX: Semaphores (cont.) semaphore control: int sem_post(sem_t *sem); int sem_wait(sem_t *sem); sem_post atomically increases the value of a semaphore by 1, i.e., when 2 threads call sem_post simultaneously, the semaphore's value will also be increased by 2 (there are 2 atoms calling) sem_wait atomically decreases the value of a semaphore by 1; but always waits until the semaphore has a non-zero value first 20

Semaphore: Example #include <pthread.h> #include <semaphore.h>... void *thread_function( void *arg );... sem_t semaphore; // also a global variable just like mutexes... int main() {! int tmp;! // initialize the semaphore! tmp = sem_init( &semaphore, 0, 0 );! // create threads! pthread_create( &thread[i], NULL, thread_function, NULL );! while ( still_has_something_to_do() )! {!! sem_post( &semaphore );!! }! pthread_join( thread[i], NULL );! sem_destroy( &semaphore );! return 0; }! 21 Semaphore: Example (cont.) void *thread_function( void *arg ) {! sem_wait( &semaphore );! perform_task_when_sem_open();! pthread_exit( NULL ); } 22

Deadlock 23 Dining Philosophers Problem Five philosophers spend their time eating and thinking. They are sitting in front of a round table with spaghetti served. There are five plates at the table and five chopsticks set between the plates. Eating the spaghetti requires the use of two chopsticks which the philosophers pick up one at a time. Philosophers do not talk to each other. Semaphore chopstick [5] initialized to 1 24

Dining-Philosophers Problem (Cont.) The structure of Philosopher i: Do { wait ( chopstick[i] ); //lock wait ( chopstick[ (i + 1) % 5] ); //lock // eat signal ( chopstick[i] ); //unlock signal (chopstick[ (i + 1) % 5] ); //unlock // think } while (true) ; 25 To Prevent Deadlock Ensures mutual exclusion, but does not prevent deadlock Allow philosopher to pick up her chopsticks only if both chopsticks are available (i.e. in critical section) Use an asymmetric solution: an odd philosopher picks up first her left chopstick and then her right chopstick; and vice versa 26

Acknowledgments Advanced Programming in the Unix Environment by R. Stevens The C Programming Language by B. Kernighan and D. Ritchie Understanding Unix/Linux Programming by B. Molay Lecture notes from B. Molay (Harvard), T. Kuo (UT- Austin), G. Pierre (Vrije), M. Matthews (SC), B. Knicki (WPI), M. Shacklette (UChicago), J.Kim (KAIST), J. Schaumann (SIT), E. Cuansing (Purdue), and J. Vuori (JYU). 27