1 CMSC 15400 Laundry List P/W/F, Exam, etc. Instructors: HSG, HH, MW.

Slides:



Advertisements
Similar presentations
Programming with Threads November 26, 2003 Topics Shared variables The need for synchronization Synchronizing with semaphores Thread safety and reentrancy.
Advertisements

Carnegie Mellon 1 Synchronization: Basics : Introduction to Computer Systems 23 rd Lecture, Nov. 16, 2010 Instructors: Randy Bryant and Dave O’Hallaron.
– 1 – , F’02 Traditional View of a Process Process = process context + code, data, and stack shared libraries run-time heap 0 read/write data Program.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Synchronization April 29, 2008 Topics Shared variables The need for synchronization Synchronizing with semaphores Thread safety and reentrancy Races and.
Concurrent HTTP Proxy with Caching Ashwin Bharambe Monday, Dec 4, 2006.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Carnegie Mellon 1 Synchronization: Basics / : Introduction to Computer Systems 23 rd Lecture, Jul 21, 2015 Instructors: nwf and Greg Kesden.
Programming with Threads April 30, 2002 Topics Shared variables The need for synchronization Synchronizing with semaphores Synchronizing with mutex and.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
1 Process Synchronization Andrew Case Slides adapted from Mohamed Zahran, Clark Barrett, Jinyang Li, Randy Bryant and Dave O’Hallaron.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
SYNCHRONIZATION-2 Slides from: Computer Systems: A Programmer's Perspective, 2nd Edition by Randal E. Bryant and David R. O'Hallaron, Prentice Hall, 2011.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Week 16 (April 25 th ) Outline Thread Synchronization Lab 7: part 2 & 3 TA evaluation form Reminders Lab 7: due this Thursday Final review session Final.
Synchronizing Threads with Semaphores
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
SYNCHRONIZATION. Today  Threads review  Sharing  Mutual exclusion  Semaphores.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 12: Thread-safe Data Structures, Semaphores.
Carnegie Mellon Introduction to Computer Systems /18-243, fall nd Lecture, Nov. 17 Instructors: Roger B. Dannenberg and Greg Ganger.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Concurrency Threads Synchronization Thread-safety of Library Functions Anubhav Gupta Dec. 2, 2002 Outline.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Chien-Chung Shen CIS/UD
Threads Synchronization Thread-safety of Library Functions
CS703 – Advanced Operating Systems
Synchronization: Basics
Instructors: Randal E. Bryant and David R. O’Hallaron
Process Synchronization
Programming with Threads
Threads Threads.
Instructor: Randy Bryant
Recitation 14: Proxy Lab Part 2
Programming with Threads
Concurrent Programming
Programming with Threads
Programming with Threads Dec 6, 2001
Programming with Threads Dec 5, 2002
Threading And Parallel Programming Constructs
Synchronization and Semaphores
Jonathan Walpole Computer Science Portland State University
CS703 - Advanced Operating Systems
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Concurrency: Mutual Exclusion and Process Synchronization
Synchronization Primitives – Semaphore and Mutex
Synchronization: Basics CSCI 380: Operating Systems
Programming with Threads
Programming with Threads
CSE 153 Design of Operating Systems Winter 19
Synchronization: Advanced CSCI 380: Operating Systems
CS333 Intro to Operating Systems
Programming with Shared Memory - 2 Issues with sharing data
Lecture 20: Synchronization
Instructor: Brian Railing
Lecture 19: Threads CS April 3, 2019.
Presentation transcript:

1 CMSC Laundry List P/W/F, Exam, etc. Instructors: HSG, HH, MW

2 CMSC P/F/W form Don’t forget to fill out this form if you want a P/F/W:

3 CMSC Exam Tuesday, May 31    Check everyday! Time: Attend your lab time slot (no switching!)  (or we will deduct your points) Materials  mm5, os1-4, nt1-3, mem1-2, sy1-3 Office hoursM  Sunday (likely above 2pm) and Monday (all day)  Check the website above and Piazza  Come prepared w/ specific questions (Lec #X, Tag #Y, Slide #Z) How to prepare?  Check the links above

4 CMSC Passing argument to peer threads Three ways to pass “connfd” argument to echo_thread  The first approach has an unintended sharing  Main thread and peer thread has read-write data races int connfd; // in main’s stack while (1) { connfd = accept(listenfd, …; pthread_create(…, &connfd); // ref } void *echo_thread(void *vargp) { int connfd = *(vargp);... }

5 CMSC Passing argument to peer threads Two correct ways  Make sure each thread gets an argument exclusive for itself  i.e. don’t the same memory location Which one is more powerful?  Pass a pointer (can pass more data with pointer to a struct) while(1) { int *connfdp = malloc(sizeof(int)); *connfdp = accept(listenfd, …); pthread_create(…, connfdp); } void *echo_thread(void *vargp) { int connfd = *(vargp);... free(vargp); // !free  leaks! } int connfd; while(1) { connfd = accept(listenfd, …); pthread_create(…, connfd); } void *echo_thread(void *vargp) { int connfd = (int)(vargp);... }

6 CMSC Synchronization with Semaphores Section 12 Instructors: HSG, HH, MW

7 CMSC Semaphores void increment() { lock(L); cnt++; // updating shared var, in CS unlock(L); void increment() { P(semaphore); cnt++; // updating shared var, in CS V(semaphore); (semaphore is the origin of lock)

8 CMSC Semaphores sem_* are the real functions P() and V() are wrappers Last lecture: lock *L; init(L) lock(L) unlock(L) Real usage: sem_t *s = malloc(sem_t); sem_init(s, 0, value); sem_wait(s); sem_post(s) This Lecture: (simplified) (same) s = val; P(s) V(s)

9 CMSC Semaphore Semaphore: non-negative global integer synchronization variable sem_t *s = malloc(sem_t); int main() { sem_init(s, 0, 1); // initialize s  val=1; // create threads } int cnt = 0; void increment() { sem_wait(s) // P(s) cnt++; // updating shared var, in CS sem_post(s); // V(s) }

10 CMSC Semaphore Operations 10 Semaphore: sem_t s;  non-negative global integer synchronization variable  (see man pages) sem_init(s, pshared, value)  Semaphore initially contains a non-negative integer value  User cannot read or write value directly after initialization sem_t *s = malloc(sem_t); sem_init(s, 0, 1); // initialize s  val=1;

11 CMSC Semaphore Operations 11 sem_wait(s) or P(s)  P() for “test” in Dutch (proberen)  Wait/block until value of sem is > 0, then decrement sem value  Details: put current thread to s  waitqueue;  (OS manage the wait and wake) sem_wait(s) { while (s  val == 0) block(s); s  val--; } P(s) { sem_wait(s); } void increment() { sem_wait(s); // P(s) cnt++; // critical sec. sem_post(s); // V(s) }

12 CMSC Semaphore Operations 12 sem_post(s) or V(s)  V() for “increment” in Dutch (verhogen)  Increment value of semaphore  Details:  wake(s) checks if someone is waiting on the s  waitqueue void increment() { sem_wait(s); // P(s) cnt++; // critical sec. sem_post(s); // V(s) } sem_post(s) { s  val++; wake(s); } V(s) { sem_post(s); }

13 CMSC (The code above is just the high-level logic. The actual implementation is more complex) OS kernel guarantees that operations inside P() and V() are executed atomically  No data races inside P() and V()  Only one P or V operation at a time can modify s Semaphores ACQUIRE THE LOCK: sem_wait(s) { while (s  val == 0) block(s); s  val--; } P(s) { sem_wait(s); } RELEASE THE LOCK: sem_post(s) { s  val++; wake(s); } V(s) { sem_post(s); }

14 CMSC Simplified (for lecture) Don’t worry about s  waitqueue and s->val and other details Let’s just simplify semaphore S as non-negative integer P(s) { while (s  val == 0) block(s); s  val--; } V(s) { s  val++; wake(s); } P(s) { while (s == 0) block(); // sleep s--; } V(s) { s++; }

15 CMSC Example 1 15 What happens if semaphore s is initialized to 2?  Scenario: Three threads call P(s)  … P(s) { while (s == 0) block(); s--; } V(s) { s++; } 3 threads: t1 / t2 / t3 P(s); cnt++; // critical sec V(s);

16 CMSC Example 2: mutex with semaphores 16 (Mutex: mutual exclusion)  Only one thread in critical section  We can achieve mutex with semaphores To what value should S be initialized? … What happens if S is accidentally initialized to 0? … Mutex with semaphore s: P(s); cnt++; // critical sec. V(s);

17 CMSC Binary semaphores 17 Last example can be seen as binary semaphores Binary semaphore is sufficient for mutual exclusion  semaphore whose value is always 0 or 1 General semaphore is also called counting semaphore Scheduling semaphores, S > 1  E.g. ps -e | sort | grep something  (FYI only)

18 CMSC Another way: pthread_mutex Allocate and Initialize  pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;  pthread_mutex_init(& mylock, NULL); Acquire  Acquire exclusion access to lock; Wait if lock is not available  pthread_mutex_lock(&mylock); // just like P() Release  Release exclusive access to lock  pthread_mutex_unlock(&mylock); // just like V() Difference vs. Semaphore  pthread mutex: no value in initialization

19 CMSC EXTRA: Thread Safety Functions called from a thread must be thread-safe Def: A function is thread-safe iff it will always produce correct results when called repeatedly from multiple concurrent threads Classes of thread-unsafe functions:  Class 1: Functions that do not protect shared variables  Class 2: Functions that keep state across multiple invocations  Class 3: Functions that return a pointer to a static variable  Class 4: Functions that call thread-unsafe functions  (You need to provide locks yourself before calling thread-unsafe functions)

20 CMSC Thread-Safe Library Functions All functions in the Standard C Library (at the back of your K&R text) are thread-safe  Examples: malloc, free, printf, scanf,sem_wait/post  No data races inside these functions Most Unix system calls are thread-safe, with a few exceptions: Thread-unsafe functionClassReentrant version asctime 3asctime_r ctime 3ctime_r gethostbyaddr 3gethostbyaddr_r gethostbyname 3gethostbyname_r inet_ntoa 3(none) localtime 3localtime_r rand 2rand_r

21 CMSC Deadlock

22 CMSC Deadlock: Why does it happen? 22 Every entity is waiting for resource held by another entity None release until it gets what it is waiting for

23 CMSC Deadlock example 23 Two threads access two shared variables, A and B Variable A is protected by lock x, variable B by lock y How to add lock and unlock statements? Naively do:  Before use A, lock X … and … after use A, release X  Before use B, lock Y … and … after use B, release Y Thread 1 A += 10; B += 20; A += B; A += 30; Thread 2 B += 10; A += 20; A += B; B += 30;

24 CMSC Example (cont’d) 24 What can go wrong?  T1 get X and concurrently T2 gets Y  Then, T1 wants y, T2 wants X int A, B; sem x, y; // init to 1 Thread 1 P(x); A += 10; P(y); B += 20; A += B; V(y); A += 30; V(x); Thread 2 P(y); B += 10; P(x); A += 20; A += B; V(x); B += 30; V(y); wait y wait x

25 CMSC Deadlock Spinning wheel  Almost 0% CPU utilization What is my computer doing?  Possibly a deadlock  Solution: Restart the app/OS How about 100% CPU utilization? What’s wrong?

26 CMSC Detection 26 Thread 1 P(x); A += 10; ? P(y); B += 20; ? A += B; ? V(y); A += 30; ? V(x); Thread 2 P(y); B += 10; ? P(x); A += 20; ? A += B; ? V(x); B += 30; ? V(y); Steps:  List all pairs of mutexes that T1 and T2 can hold simultaneously  mutexes = locks = semaphores  Is there an overlap between the sets of pairs of mutexes?  If yes, are the mutexes (within the pair) locked in the same order?  Is there a potential for deadlock? Yes, if locked not in the same order

27 CMSC Deadlock prevention: Lock ordering 27 If multiple locks are used, threads should acquire them in the same order Order in which locks released does not matter much  (Threads are potentially blocked only when acquiring locks) Thread 1 P(x); A += 10; P(y); B += 20; A += B; V(y); A += 30; V(x); Thread 2 P(y); *** B += 10; P(x); *** A += 20; A += B; V(x); B += 30; V(y); Thread 1 P(x); A += 10; P(y); B += 20; A += B; V(y); A += 30; V(x); Thread 2 P(x); *** P(y); *** B += 10; A += 20; A += B; V(x); B += 30; V(y);

28 CMSC Summary Users: faster, faster, faster! Old days: single threaded program (slow) Parallelism (multi-processors) and concurrency (overlapping I/Os) Process is to heavyweight, threads was invented Shared data + concurrent updates  data races Locks (atomicity, mutual exclusion,..) If get bad performance, must make independent “embarrassingly parallel” subtasks, don’t synchronize too often Wrong ordering of locks  deadlock Use locks in order

29 CMSC Last slides: See “final-systems.ptx”

30 CMSC Extra

31 CMSC Summary Programmers need a clear model of how variables are shared by threads Variables shared by multiple threads must be protected to ensure mutually exclusive access Semaphores are a fundamental mechanism for enforcing mutual exclusion

32 CMSC C semaphore operations Pthreads functions: #include int sem_init(sem_t *s, 0, unsigned int val);} /* s = val */ int sem_wait(sem_t *s); /* P(s) */ int sem_post(sem_t *s); /* V(s) */ CS:APP wrapper functions: #include "csapp.h” void P(sem_t *s); /* Wrapper function for sem_wait */ void V(sem_t *s); /* Wrapper function for sem_post */

33 CMSC badcnt.c : Improper synchronization int cnt = 0; int main(int argc, char **argv) { int niters = atoi(argv[1]); pthread_t tid1, tid2; Pthread_create(&tid1, NULL, thread, &niters); Pthread_create(&tid2, NULL, thread, &niters); Pthread_join(tid1, NULL); Pthread_join(tid2, NULL); /* Check result */ if (cnt != (2 * niters)) printf("BOOM! cnt=%d\n”, cnt); else printf("OK cnt=%d\n", cnt); exit(0); } void *thread(void *vargp) { int i, niters = *((int *)vargp); for (i = 0; i < niters; i++) cnt++; return NULL; } How can we fix this using semaphores?

34 CMSC goodcnt.c: Proper synchronization Define and initialize a mutex for the shared variable cnt: int cnt = 0; /* Counter */ sem_t mutex; /* Semaphore that protects cnt */ sem_init(&mutex, 0, 1); /* mutex = 1 */ Surround critical section with P and V: for (i = 0; i < niters; i++) { P(&mutex); cnt++; V(&mutex); } linux>./goodcnt OK cnt=20000 linux>./goodcnt OK cnt=20000 linux> Advantages? Safe and correct Disadvantages? Remember try your best to create independent subtasks

35 CMSC Unsafe region Why mutexes work Provide mutually exclusive access to shared variable by surrounding critical section with P and V operations on semaphore s (initially set to 1) Semaphore invariant creates a forbidden region that encloses unsafe region that cannot be entered by any trajectory. H1H1 P(s)V(s)T1T1 Thread 1 Thread 2 L1L1 U1U1 S1S1 H2H2 P(s) V(s) T2T2 L2L2 U2U2 S2S Initially s = 1 Forbidden region

36 CMSC Recap Basic idea:  Associate a unique semaphore mutex, initially 1, with each shared variable (or related set of shared variables).  Surround corresponding critical sections with P(mutex) and V(mutex) operations. Terminology:  Binary semaphore: semaphore whose value is always 0 or 1  Mutex: binary semaphore used for mutual exclusion  P operation: “locking” the mutex  V operation: “unlocking” or “releasing” the mutex  “Holding” a mutex: locked and not yet unlocked  Counting semaphore: used as a counter for set of available resources

37 CMSC Representing deadlock 37 Vertices:  Threads in system  Resources (locks) Edges:  Indicate relationships Resource-Allocation Graph T1T2 wants held by y x wants held by