Concurrency: Signaling and Condition Variables

Size: px
Start display at page:

Download "Concurrency: Signaling and Condition Variables"

Transcription

1 Concurrency: Signaling and Condition Variables Questions Answered in this Lecture: How do we make fair locks perform better? How do we notify threads of that some condition has been met? Hale CS450 Thanks to Remzi & Andrea Arapci-Dusseau for slide material 1

2 Recall: Ticket Lock Implementation typedef struct lock_t { int ticket; int turn; void lock_init(lock_t *lock) { lock->ticket = 0; lock->turn = 0; void acquire(lock_t *lock) { int myturn = FAA(&lock->ticket); while (lock->turn!= myturn); // spin void release (lock_t *lock) { FAA(&lock->turn); Hale CS450 2

3 Spinlock Performance Fast when - many CPUs - locks held a short time - advantage: avoid context switch Slow when - one CPU - locks held a long time - disadvantage: spinning is wasteful Hale CS450 3

4 CPU Scheduler is Ignorant lock unlock lock spin spin spin spin spin A B C D A B C D CPU scheduler may run B instead of A even though B is waiting for A Hale CS450 4

5 Ticket Lock with yield() typedef struct lock_t { int ticket; int turn; void lock_init(lock_t *lock) { lock->ticket = 0; lock->turn = 0; void acquire(lock_t *lock) { int myturn = FAA(&lock->ticket); while (lock->turn!= myturn) yield(); void release (lock_t *lock) { FAA(&lock->turn); Hale CS450 5

6 yield() Instead of spin lock unlock lock spin spin spin spin spin no yield: A B C D A B C D lock unlock lock yield: A A B Hale CS450 6

7 Spinlock Performance Waste Without yield: O(threads * time_slice) With yield: O(threads * context_switch) So even with yield, spinning is slow with high thread contention Next improvement: Block and put thread on waiting queue instead of spinning Hale CS450 7

8 How do we evaluate lock performance? Fairness: do some processes wait more than others? another way: do processes get lock in order requested? Performance: it depends Is the lockcontended? (many threads attempting to acquire lock) Or is it uncontended? (likely to get the lock if we request it) Hale CS450 8

9 Why is yield() useful? How can it affect performance? lock unlock lock spin spin spin spin spin no yield: A B C D A B C D lock unlock lock yield: A A B Hale CS450 9

10 Lock implementation: block when waiting Lock implementation removes waiting threads from the ready queue (e.g. with park() and unpark()) Scheduler only runs ready threads Separates concerns (programmer doesn t need to deal with yield vs not/spinning issue) Quiz: where should locks be implemented? Hale CS450 10

11 Example Ready: {A, B, C, D Waiting: { Running: { Hale CS450 11

12 Example Ready: {B, C, D Waiting: { Running: {A lock() A Hale CS450 12

13 Example Ready: {A, C, D Waiting: { Running: {B lock() A B Hale CS450 13

14 Example Ready: {A, C, D Waiting: {B Running: { lock() lock() A B Hale CS450 14

15 Example Ready: {A, D Waiting: {B Running: {C lock() lock() A B C Hale CS450 15

16 Example Ready: {A, C Waiting: {B Running: {D lock() lock() A B C D Hale CS450 16

17 Example Ready: {A, C Waiting: {B, D Running: { lock() lock() lock() A B C D Hale CS450 17

18 Example Ready: {C Waiting: {B, D Running: {A lock() lock() lock() A B C D A Hale CS450 18

19 Example Ready: {A Waiting: {B, D Running: {C lock() lock() lock() A B C D A C Hale CS450 19

20 Example Ready: {C Waiting: {B, D Running: {A lock() lock() lock() A B C D A C A Hale CS450 20

21 Example lock() lock() lock() Ready: {C, B, D Waiting: { Running: {A unlock() A B C D A C A Hale CS450 21

22 Example lock() lock() lock() Ready: {C, B, D Waiting: { Running: {A unlock() A B C D A C A Hale CS450 22

23 Example lock() lock() lock() Ready: {B, D, A Waiting: { Running: {C unlock() A B C D A C A C Hale CS450 23

24 Example Ready: {D, A, C Waiting: { Running: {B lock() lock() unlock() lock() A B C D A C A B Hale CS450 24

25 What do we get? Threads aren t contending on the lock when they shouldn t be (mostly) Fewer context switches Hale CS450 25

26 Lock: block when waiting typedef struct { bool lock = false; bool guard = false; queue_t q; lock_t; 1. Why do we need guard? 2. Why is it OK to spin on guard? 3. In release(), why not set lock=false? 4. What is the race condition? void acquire(lock_t *l) { while (TAS(&l->guard, true)); if (l->lock) { enqueue(l->q, tid); l->guard = false; park(); // blocked else { l->lock = true; l->guard = false; void release(lock_t *l) { while (TAS(&l->guard, true)); if (qempty(l->q)) l->lock=false; else unpark(dequeue(l->q)); l->guard = false; Hale CS450 26

27 time Race! void acquire(lock_t *l) { while (TAS(&l->guard, true)); if (l->lock) { enqueue(l->q, tid); l->guard = false; Thread 1 Thread 2 park(); // blocked else { l->lock = true; l->guard = false; void release(lock_t *l) { while (TAS(&l->guard, true)); if (qempty(l->q)) false! l->lock=false; else unpark(dequeue(l->q)); l->guard = false; bad! Hale CS450 27

28 Lock: the fix typedef struct { bool lock = false; bool guard = false; queue_t q; lock_t; 1. Why does this fix the race? 2. What does the scheduler need to do here? 3. Why can t we just hold the guard when we park()? void acquire(lock_t *l) { while (TAS(&l->guard, true)); if (l->lock) { enqueue(l->q, tid); setpark(); // notify of plan l->guard = false; park(); // unless unpark() else { l->lock = true; l->guard = false; void release(lock_t *l) { while (TAS(&l->guard, true)); if (qempty(l->q)) l->lock=false; else unpark(dequeue(l->q)); l->guard = false; Hale CS450 28

29 Spin-waiting vs. Blocking Each approach has tradeoffs: Uniprocessor Waiting process is scheduled -> process holding lock isn t Waiting process should always be descheduled Associate queue of waiters with each lock Multiprocessor Waiting process is scheduled -> process holding lock might also be Spin or block depends on how long (!) before lock is released Lockreleased quickly? spin() Lock taken for long time? block() we ll define this relative to the context switch time Hale CS450 29

30 Oracle Suppose we know how long (!) a lock will be held And suppose we make our decision to block or spin based on ", the cost of a context switch #$%&'( = * %,./&( % >, 12'$3 BUT: we have to know the future! Hale CS450 30

31 Applying Bounds The theory: bound our worst-case performance using actual wait time over optimal (oracle) Consider:! # What would the oracle do? What can we do (without knowing future)?! > # What would the oracle do? What can we do? We pay 2C, because we wait an extra C. 2C/C = 2 => 2-competitive algorithm! Hale CS450 31

32 Concurrency Goals Mutual Exclusion Keep two threads fromexecuting in a critical section concurrently We solved this with locks Dependent Events We want a thread to wait until some particular event has occurred Or some condition has been met Solved with condition variables and semaphores Hale CS450 32

33 I just dropped in to see what condition my condition was in Hale CS450 33

34 Example: join() pthread_t p1, p2; pthread_create(&p1, NULL, mythread, A ); pthread_create(&p2, NULL, mythread, B ); // join waits for the threads to finish pthread_join(p1, NULL); pthread_join(p2, NULL); printf( Main: done\n [balance: %d]\n, balance); return 0; Hale CS450 34

35 Condition Variables CV: queue of waiting threads B waits for a signal on CV before running wait(cv, ); A sends signal() on CV when time for B to run signal(cv, ); Hale CS450 35

36 API wait(cond_t * cv, mutex_t * lock) assumes lock is held when wait() is called (why?) puts caller to sleep + releases the lock (atomically) when awoken, reacquires lock before returning signal(cond_t * cv) wake a single waiting thread (if >= 1 thread is waiting) if there is no waiting thread, NOP Hale CS450 36

37 Join Implementation: Attempt #1 parent void thread_join() { // x cond_wait(&c, &m); // y // z child void thread_exit() { cond_signal(&c); // a // b // c Hale CS450 37

38 Join Implementation: Attempt #1 parent void thread_join() { // x cond_wait(&c, &m); // y // z child void thread_exit() { cond_signal(&c); // a // b // c Example schedule: parent x y z child a b c Hale CS450 38

39 Join Implementation: Attempt #1 parent void thread_join() { // x cond_wait(&c, &m); // y // z child void thread_exit() { cond_signal(&c); // a // b // c Example (bad) schedule: parent child a b c x y parent will wait forever! Hale CS450 39

40 Rule of Thumb #1 Always associate some state with the CV CVs are used to signal() when state changes If state is as required, don t need to wait for a signal() Hale CS450 40

41 Join Implementation: Attempt #2 parent void thread_join() { // w if (!done) // x cond_wait(&c, &m);// y // z child void thread_exit() { done = 1 // a cond_signal(&c); // b Hale CS450 41

42 Join Implementation: Attempt #2 parent void thread_join() { // w if (!done) // x cond_wait(&c, &m);// y // z Example schedule: parent child a b w x child void thread_exit() { done = 1 // a cond_signal(&c); // b fixed! y z Hale CS450 42

43 Join Implementation: Attempt #2 parent void thread_join() { // w if (!done) // x cond_wait(&c, &m);// y // z Example (bad) schedule: parent child w x a b child void thread_exit() { done = 1 // a cond_signal(&c); // b sleeps forever! y Hale CS450 43

44 Join Implementation: Attempt #3 (correct) parent void thread_join() { // w if (!done) // x cond_wait(&c, &m);// y // z child void thread_exit() { // a done = 1 // b cond_signal(&c); // c // d Hale CS450 44

45 Join Implementation: Attempt #3 (correct) parent void thread_join() { // w if (!done) // x cond_wait(&c, &m);// y // z Example chedule: parent child w x y a b child void thread_exit() { // a done = 1 // b cond_signal(&c); // c // d c d z fixed! Hale CS450 45

46 Signaling between threads Bounded Buffer (producer-consumer queue) Hale CS450 46

47 Signaling between threads Bounded Buffer (producer-consumer queue) t1: put() Hale CS450 47

48 Signaling between threads Bounded Buffer (producer-consumer queue) t1: put() Hale CS450 48

49 Signaling between threads Bounded Buffer (producer-consumer queue) t2: take() Hale CS450 49

50 Signaling between threads Bounded Buffer (producer-consumer queue) t2: take() Hale CS450 50

51 Signaling between threads Bounded Buffer (producer-consumer queue) Hale CS450 51

52 Signaling between threads Bounded Buffer (producer-consumer queue) t2: take()??? Hale CS450 52

53 Signaling between threads Bounded Buffer (producer-consumer queue) wait queue t2 What condition are we waiting on? Hale CS450 53

54 Signaling between threads Bounded Buffer (producer-consumer queue) wait queue t2 t1: put() Hale CS450 54

55 Signaling between threads Bounded Buffer (producer-consumer queue) wait queue t2 t1: put() What should happen? Hale CS450 55

56 Signaling between threads Bounded Buffer (producer-consumer queue) wait queue t2 t1: signal() Hale CS450 56

57 Signaling between threads Bounded Buffer (producer-consumer queue) wait queue t2: take() Hale CS450 57

58 Signaling between threads Bounded Buffer (producer-consumer queue) wait queue t2: take() Hale CS450 58

59 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) Hale CS450 59

60 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) most recent put Hale CS450 60

61 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) We re full Hale CS450 61

62 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t2:put() Hale CS450 62

63 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t2 Hale CS450 63

64 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t2 t1: take() Hale CS450 64

65 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t2 Hale CS450 65

66 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t2 t1: signal() Hale CS450 66

67 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t1: take() Hale CS450 67

68 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t2:put() Hale CS450 68

69 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t2:put() Hale CS450 69

70 The queue is a circular queue Also sometimes called a ring buffer Bounded Buffer (producer-consumer queue) wait queue t2:put() wraparound property Hale CS450 70

71 Bounded Buffer Reads and writes to buffer require locking when buffers are full, writers wait() when buffers are empty, readers wait() Many programming languages expose this abstraction directly chan in Go channel in Julia Python requires a queue() with a lock() Rust might use CircularBuffer() and CondVar() maybe crossbeam_channel() Hale CS450 71

72 Bounded Buffer Producers generate data Consumers take data and process it Very frequent situation encountered in systems programming General strategy: use CVs to notify: waiting readers when data is available waiting writers when slots are free Hale CS450 72

73 Example Easy case: 1 consumer thread 1 producer thread 1 shared buffer (max slots=1) numfill is buffer slots currently filled let s start with some code that doesn t quite work Hale CS450 73

74 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 74

75 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 75

76 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 76

77 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 77

78 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 78

79 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 79

80 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 80

81 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 81

82 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 82

83 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNABLE] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 83

84 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNABLE] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 84

85 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNABLE] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 85

86 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNABLE] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 86

87 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNABLE] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 87

88 [SLEEPING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNABLE] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 88

89 [SLEEPING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 89

90 [SLEEPING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 90

91 [SLEEPING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 91

92 [SLEEPING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 92

93 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 93

94 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 94

95 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 95

96 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 96

97 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 97

98 [RUNNABLE] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [RUNNING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 98

99 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS450 99

100 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS

101 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 0 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS

102 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [SLEEPING] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS

103 [RUNNING] void *producer (void *arg) { for (int i = 0; i < loops; i++) { while (numfull == max) do_fill(i); numfull = 1 [RUNNABLE] void *consumer (void *arg) { while (1) { while (numfull == 0) int tmp = do_get(); printf( %d\n, tmp); Hale CS

104 What about two consumers? Can you find a bad schedule? Hale CS

105 void *producer (void *arg) { for (int i = 0; i < loops; i++) { // p1 while (numfull == max) //p2 //p3 do_fill(i); // p4 // p5 // p6 Producer: Consumer1: [RUNNING] wait() wait() c1 c2 c3 signal() p1 p2 p3 p4 p5 p6 p1 p2 p3 void *consumer (void *arg) { while (1) { // c1 wait() Consumer2: c1 c2 c3 c2 c4 c5 [RUNNABLE] while (numfull == 0) // c2 int tmp = do_get(); // c4 // c5 // c6 printf( %d\n, tmp); // c7 signal() does last signal wake producer or consumer1?? Hale CS

106 How to wake the right thread? One solution: wake allthreads! Hale CS

107 API wait(cond_t * cv, mutex_t * lock) assumes lock is held when wait() is called (why?) puts caller to sleep + releases the lock (atomically) when awoken, reacquires lock before returning signal(cond_t * cv) wake a single waiting thread (if >= 1 thread is waiting) if there is no waiting thread, NOP broadcast(cond_t * cv) wake all waiters if no waiting threads, NOP Hale CS

108 Summary Rules of thumb Keep state in addition to CVs Always wait() or signal() with lock held Recheck state assumptions when waking up from waiting Hale CS

[537] Locks and Condition Variables. Tyler Harter

[537] Locks and Condition Variables. Tyler Harter [537] Locks and Condition Variables Tyler Harter Review: using and designing basic locks Do it. Problem 1 Lock Evaluation How to tell if a lock implementation is good? Lock Evaluation How to tell if a

More information

Implementing Locks. Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau)

Implementing Locks. Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau) Implementing Locks Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau) Lock Implementation Goals We evaluate lock implementations along following lines Correctness Mutual exclusion: only one

More information

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

Announcements. Class feedback for mid-course evaluations Receive  about survey to fill out until this Friday Announcements Project 2: Part 2a will be graded this week Part 2b take longer since we compare all graphs Project 3: Shared memory segments Linux: use shmget and shmat across server + client processes

More information

Condition Variables & Semaphores

Condition Variables & Semaphores Condition Variables & Semaphores Nima Honarmand (Based on slides by Prof. Andrea Arpaci-Dusseau) Review: Concurrency Objectives Mutual Exclusion A & B don t run at the same time Solved using locks Ordering

More information

Concurrency: Mutual Exclusion (Locks)

Concurrency: Mutual Exclusion (Locks) Concurrency: Mutual Exclusion (Locks) Questions Answered in this Lecture: What are locks and how do we implement them? How do we use hardware primitives (atomics) to support efficient locks? How do we

More information

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #2 SOLUTIONS

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #2 SOLUTIONS CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #2 SOLUTIONS This exam is closed book, closed notes. All cell phones must be turned off. No calculators may be used. You have two hours

More information

CS 537 Lecture 11 Locks

CS 537 Lecture 11 Locks CS 537 Lecture 11 Locks Michael Swift 10/17/17 2004-2007 Ed Lazowska, Hank Levy, Andrea and Remzi Arpaci-Dussea, Michael Swift 1 Concurrency: Locks Questions answered in this lecture: Review: Why threads

More information

Concurrency: Locks. Announcements

Concurrency: Locks. Announcements CS 537 Introduction to Operating Systems UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department Concurrency: Locks Andrea C. Arpaci-Dusseau Remzi H. Arpaci-Dusseau Questions answered in this lecture:

More information

[537] Semaphores. Tyler Harter

[537] Semaphores. Tyler Harter [537] Semaphores Tyler Harter Producer/Consumer Problem Producers generate data (like pipe writers). Consumers grab data and process it (like pipe readers). Producer/consumer problems are frequent in systems.

More information

Locks. Dongkun Shin, SKKU

Locks. Dongkun Shin, SKKU Locks 1 Locks: The Basic Idea To implement a critical section A lock variable must be declared A lock variable holds the state of the lock Available (unlocked, free) Acquired (locked, held) Exactly one

More information

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University Condition Variables Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu EEE3052: Introduction to Operating Systems, Fall 2017, Jinkyu Jeong (jinkyu@skku.edu)

More information

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

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017 CS 471 Operating Systems Yue Cheng George Mason University Fall 2017 1 Review: Sync Terminology Worksheet 2 Review: Semaphores 3 Semaphores o Motivation: Avoid busy waiting by blocking a process execution

More information

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

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Operating Systems Lecture 4 - Concurrency and Synchronization Adrien Krähenbühl Master of Computer Science PUF - Hồ Chí Minh 2016/2017 Mutual exclusion Hardware solutions Semaphores IPC: Message passing

More information

Lecture 5: Synchronization w/locks

Lecture 5: Synchronization w/locks Lecture 5: Synchronization w/locks CSE 120: Principles of Operating Systems Alex C. Snoeren Lab 1 Due 10/19 Threads Are Made to Share Global variables and static objects are shared Stored in the static

More information

Systèmes d Exploitation Avancés

Systèmes d Exploitation Avancés Systèmes d Exploitation Avancés Instructor: Pablo Oliveira ISTY Instructor: Pablo Oliveira (ISTY) Systèmes d Exploitation Avancés 1 / 32 Review : Thread package API tid thread create (void (*fn) (void

More information

Operating System Labs. Yuanbin Wu

Operating System Labs. Yuanbin Wu Operating System Labs Yuanbin Wu CS@ECNU Operating System Labs Project 4 Due: 10 Dec. Oral tests of Project 3 Date: Nov. 27 How 5min presentation 2min Q&A Operating System Labs Oral tests of Lab 3 Who

More information

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs CSE 451: Operating Systems Winter 2005 Lecture 7 Synchronization Steve Gribble Synchronization Threads cooperate in multithreaded programs to share resources, access shared data structures e.g., threads

More information

CS 537: Introduction to Operating Systems (Summer 2017) University of Wisconsin-Madison Department of Computer Sciences.

CS 537: Introduction to Operating Systems (Summer 2017) University of Wisconsin-Madison Department of Computer Sciences. CS 537: Introduction to Operating Systems (Summer 2017) University of Wisconsin-Madison Department of Computer Sciences Midterm Exam 2 July 21 st, 2017 3 pm - 5 pm There are sixteen (16) total numbered

More information

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4 Temporal relations CSE 451: Operating Systems Autumn 2010 Module 7 Synchronization Instructions executed by a single thread are totally ordered A < B < C < Absent synchronization, instructions executed

More information

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization Hank Levy Levy@cs.washington.edu 412 Sieg Hall Synchronization Threads cooperate in multithreaded programs to share resources, access shared

More information

Operating Systems. Synchronization

Operating Systems. Synchronization Operating Systems Fall 2014 Synchronization Myungjin Lee myungjin.lee@ed.ac.uk 1 Temporal relations Instructions executed by a single thread are totally ordered A < B < C < Absent synchronization, instructions

More information

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions CMSC 330: Organization of Programming Languages Multithreaded Programming Patterns in Java CMSC 330 2 Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to

More information

[537] Concurrency Bugs. Tyler Harter

[537] Concurrency Bugs. Tyler Harter [537] Concurrency Bugs Tyler Harter Review Semaphores CV s vs. Semaphores CV rules of thumb: - Keep state in addition to CV s - Always do wait/signal with lock held - Whenever you acquire a lock, recheck

More information

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004 Lecture 8: Semaphores, Monitors, & Condition Variables 8.0 Main Points: Definition of semaphores Example of use

More information

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430 Implementing Mutual Exclusion Sarah Diesburg Operating Systems CS 3430 From the Previous Lecture The too much milk example shows that writing concurrent programs directly with load and store instructions

More information

Reminder from last time

Reminder from last time Concurrent systems Lecture 2: More mutual exclusion, semaphores, and producer-consumer relationships DrRobert N. M. Watson 1 Reminder from last time Definition of a concurrent system Origins of concurrency

More information

G52CON: Concepts of Concurrency

G52CON: Concepts of Concurrency G52CON: Concepts of Concurrency Lecture 11: Semaphores I" Brian Logan School of Computer Science bsl@cs.nott.ac.uk Outline of this lecture" problems with Peterson s algorithm semaphores implementing semaphores

More information

CS 153 Design of Operating Systems Winter 2016

CS 153 Design of Operating Systems Winter 2016 CS 153 Design of Operating Systems Winter 2016 Lecture 9: Semaphores and Monitors Some slides from Matt Welsh Summarize Where We Are Goal: Use mutual exclusion to protect critical sections of code that

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Objectives Introduce Concept of Critical-Section Problem Hardware and Software Solutions of Critical-Section Problem Concept of Atomic Transaction Operating Systems CS

More information

CONCURRENCY: DEADLOCK. Shivaram Venkataraman CS 537, Spring 2019

CONCURRENCY: DEADLOCK. Shivaram Venkataraman CS 537, Spring 2019 CONCURRENCY: DEADLOCK Shivaram Venkataraman CS 537, Spring 2019 ADMINISTRIVIA Midterm is on Wednesday 3/13 at 5.15pm, details on Piazza Venue: If your last name starts with A-L, go to VanVleck B102 else

More information

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

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois 1 Synchronization primitives Mutex locks Used for exclusive access to a shared resource (critical section) Operations:

More information

Synchronization 1. Synchronization

Synchronization 1. Synchronization Synchronization 1 Synchronization key concepts critical sections, mutual exclusion, test-and-set, spinlocks, blocking and blocking locks, semaphores, condition variables, deadlocks reading Three Easy Pieces:

More information

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown

Lecture 9: Multiprocessor OSs & Synchronization. CSC 469H1F Fall 2006 Angela Demke Brown Lecture 9: Multiprocessor OSs & Synchronization CSC 469H1F Fall 2006 Angela Demke Brown The Problem Coordinated management of shared resources Resources may be accessed by multiple threads Need to control

More information

Last Class: Synchronization

Last Class: Synchronization Last Class: Synchronization Synchronization primitives are required to ensure that only one thread executes in a critical section at a time. Concurrent programs Low-level atomic operations (hardware) load/store

More information

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks CSci 4061 Introduction to Operating Systems Synchronization Basics: Locks Synchronization Outline Basics Locks Condition Variables Semaphores Basics Race condition: threads + shared data Outcome (data

More information

CS533 Concepts of Operating Systems. Jonathan Walpole

CS533 Concepts of Operating Systems. Jonathan Walpole CS533 Concepts of Operating Systems Jonathan Walpole Introduction to Threads and Concurrency Why is Concurrency Important? Why study threads and concurrent programming in an OS class? What is a thread?

More information

Administrivia. Review: Thread package API. Program B. Program A. Program C. Correct answers. Please ask questions on Google group

Administrivia. Review: Thread package API. Program B. Program A. Program C. Correct answers. Please ask questions on Google group Administrivia Please ask questions on Google group - We ve had several questions that would likely be of interest to more students x86 manuals linked on reference materials page Next week, guest lecturer:

More information

Synchronization I. Jo, Heeseung

Synchronization I. Jo, Heeseung Synchronization I Jo, Heeseung Today's Topics Synchronization problem Locks 2 Synchronization Threads cooperate in multithreaded programs To share resources, access shared data structures Also, to coordinate

More information

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ! Last Class: CPU Scheduling! Scheduling Algorithms: FCFS Round Robin SJF Multilevel Feedback Queues Lottery Scheduling Review questions: How does each work? Advantages? Disadvantages? Lecture 7, page 1

More information

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

Java Threads. Introduction to Java Threads

Java Threads. Introduction to Java Threads Java Threads Resources Java Threads by Scott Oaks & Henry Wong (O Reilly) API docs http://download.oracle.com/javase/6/docs/api/ java.lang.thread, java.lang.runnable java.lang.object, java.util.concurrent

More information

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011 Synchronization CS61, Lecture 18 Prof. Stephen Chong November 3, 2011 Announcements Assignment 5 Tell us your group by Sunday Nov 6 Due Thursday Nov 17 Talks of interest in next two days Towards Predictable,

More information

C09: Process Synchronization

C09: Process Synchronization CISC 7310X C09: Process Synchronization Hui Chen Department of Computer & Information Science CUNY Brooklyn College 3/29/2018 CUNY Brooklyn College 1 Outline Race condition and critical regions The bounded

More information

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today Atomic Read-Modify-Write instructions Examples of Read-Modify-Write Goals for Today" CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables" Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors

More information

Condition Variables. parent: begin child parent: end

Condition Variables. parent: begin child parent: end 22 Condition Variables Thus far we have developed the notion of a lock and seen how one can be properly built with the right combination of hardware and OS support. Unfortunately, locks are not the only

More information

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write Goals for Today CS162 Operating Systems and Systems Programming Lecture 5 Atomic instruction sequence Continue with Synchronization Abstractions Semaphores, Monitors and condition variables Semaphores,

More information

Concurrency and Synchronisation. Leonid Ryzhyk

Concurrency and Synchronisation. Leonid Ryzhyk Concurrency and Synchronisation Leonid Ryzhyk Textbook Sections 2.3 & 2.5 2 Concurrency in operating systems Inter-process communication web server SQL request DB Intra-process communication worker thread

More information

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 Process creation in UNIX All processes have a unique process id getpid(),

More information

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading Threads Figure 1: Multi-Threading Figure 2: Multi-Threading Concurrency What it is 1. Two or more threads of control access a shared resource. Scheduler operation must be taken into account fetch-decode-execute-check

More information

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling. Background The Critical-Section Problem Background Race Conditions Solution Criteria to Critical-Section Problem Peterson s (Software) Solution Concurrent access to shared data may result in data inconsistency

More information

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657 Process & Thread Management II CIS 657 Queues Run queues: hold threads ready to execute Not a single ready queue; 64 queues All threads in same queue are treated as same priority Sleep queues: hold threads

More information

Process & Thread Management II CIS 657

Process & Thread Management II CIS 657 Process & Thread Management II CIS 657 Queues Run queues: hold threads ready to execute Not a single ready queue; 64 queues All threads in same queue are treated as same priority Sleep queues: hold threads

More information

CS5460: Operating Systems

CS5460: Operating Systems CS5460: Operating Systems Lecture 9: Implementing Synchronization (Chapter 6) Multiprocessor Memory Models Uniprocessor memory is simple Every load from a location retrieves the last value stored to that

More information

28. Locks. Operating System: Three Easy Pieces

28. Locks. Operating System: Three Easy Pieces 28. Locks Oerating System: Three Easy Pieces AOS@UC 1 Locks: The Basic Idea Ensure that any critical section executes as if it were a single atomic instruction. w An examle: the canonical udate of a shared

More information

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code Old Producer Process Code Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Our

More information

CS 111. Operating Systems Peter Reiher

CS 111. Operating Systems Peter Reiher Operating System Principles: Mutual Exclusion and Asynchronous Completion Operating Systems Peter Reiher Page 1 Outline Mutual Exclusion Asynchronous Completions Page 2 Mutual Exclusion Critical sections

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Sections 2.3 & 2.4 Textbook 2 Making Single-Threaded Code Multithreaded Conflicts between threads over the use of a global variable 3 Inter- Thread and Process Communication

More information

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works?

OS Structure. User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions. Definition, examples, how it works? Midterm Review OS Structure User mode/ kernel mode (Dual-Mode) Memory protection, privileged instructions System call Definition, examples, how it works? Other concepts to know Monolithic kernel vs. Micro

More information

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization? Chapter 6: Process [& Thread] Synchronization CSCI [4 6] 730 Operating Systems Synchronization Part 1 : The Basics Why is synchronization needed? Synchronization Language/Definitions:» What are race conditions?»

More information

TCSS 422: OPERATING SYSTEMS

TCSS 422: OPERATING SYSTEMS TCSS 422: OPERATING SYSTEMS OBJECTIVES Introduction to threads Concurrency: An Introduction Wes J. Lloyd Institute of Technology University of Washington - Tacoma Race condition Critical section Thread

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Multithreading Multiprocessors Description Multiple processing units (multiprocessor) From single microprocessor to large compute clusters Can perform multiple

More information

Operating Systems (2INC0) 2017/18

Operating Systems (2INC0) 2017/18 Operating Systems (2INC0) 2017/18 Condition Synchronization (07) Dr. Courtesy of Prof. Dr. Johan Lukkien System Architecture and Networking Group Agenda Condition synchronization motivation condition variables

More information

CS3733: Operating Systems

CS3733: Operating Systems Outline CS3733: Operating Systems Topics: Synchronization, Critical Sections and Semaphores (SGG Chapter 6) Instructor: Dr. Tongping Liu 1 Memory Model of Multithreaded Programs Synchronization for coordinated

More information

Condition Variables. Dongkun Shin, SKKU

Condition Variables. Dongkun Shin, SKKU Condition Variables 1 Why Condition? cases where a thread wishes to check whether a condition is true before continuing its execution 1 void *child(void *arg) { 2 printf("child\n"); 3 // XXX how to indicate

More information

Synchronization I. To do

Synchronization I. To do Synchronization I To do q Race condition, critical regions q Locks and concurrent data structures q Next time: Condition variables, semaphores and monitors Cooperating processes Cooperating processes need

More information

CS 153 Design of Operating Systems Winter 2016

CS 153 Design of Operating Systems Winter 2016 CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization Administrivia Homework 1 Due today by the end of day Hopefully you have started on project 1 by now? Kernel-level threads (preemptable

More information

Multiprocessors and Locking

Multiprocessors and Locking Types of Multiprocessors (MPs) Uniform memory-access (UMA) MP Access to all memory occurs at the same speed for all processors. Multiprocessors and Locking COMP9242 2008/S2 Week 12 Part 1 Non-uniform memory-access

More information

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10 MULTITHREADING AND SYNCHRONIZATION CS124 Operating Systems Fall 2017-2018, Lecture 10 2 Critical Sections Race conditions can be avoided by preventing multiple control paths from accessing shared state

More information

Synchronization 1. Synchronization

Synchronization 1. Synchronization Synchronization 1 Synchronization key concepts critical sections, mutual exclusion, test-and-set, spinlocks, blocking and blocking locks, semaphores, condition variables, deadlocks reading Three Easy Pieces:

More information

[537] Locks. Tyler Harter

[537] Locks. Tyler Harter [537] Locks Tyler Harter Review: Threads+Locks CPU 1 CPU 2 running thread 1 running thread 2 RAM PageDir A PageDir B CPU 1 CPU 2 running thread 1 running thread 2 RAM PageDir A PageDir B Virt Mem (PageDir

More information

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

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T Operating Systems Operating Systems Summer 2017 Sina Meraji U of T More Special Instructions Swap (or Exchange) instruction Operates on two words atomically Can also be used to solve critical section problem

More information

Condition Variables. Dongkun Shin, SKKU

Condition Variables. Dongkun Shin, SKKU Condition Variables 1 Why Condition? cases where a thread wishes to check whether a condition is true before continuing its execution 1 void *child(void *arg) { 2 printf("child\n"); 3 // XXX how to indicate

More information

CS 318 Principles of Operating Systems

CS 318 Principles of Operating Systems CS 318 Principles of Operating Systems Fall 2017 Midterm Review Ryan Huang 10/12/17 CS 318 Midterm Review 2 Midterm October 17 th Tuesday 9:00-10:20 am at classroom Covers material before virtual memory

More information

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives Jonathan Walpole Computer Science Portland State University 1 What does a typical thread API look

More information

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2. Learning Outcomes Concurrency and Synchronisation Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

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

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10) Synchronization CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10) 1 Outline Critical region and mutual exclusion Mutual exclusion using busy waiting Sleep and

More information

PROCESS SYNCHRONIZATION

PROCESS SYNCHRONIZATION PROCESS SYNCHRONIZATION Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization

More information

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages The Dining Philosophers Problem CMSC 0: Organization of Programming Languages Threads Classic Concurrency Problems Philosophers either eat or think They must have two forks to eat Can only use forks on

More information

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University CS 333 Introduction to Operating Systems Class 3 Threads & Concurrency Jonathan Walpole Computer Science Portland State University 1 The Process Concept 2 The Process Concept Process a program in execution

More information

OS Structure. User mode/ kernel mode. System call. Other concepts to know. Memory protection, privileged instructions

OS Structure. User mode/ kernel mode. System call. Other concepts to know. Memory protection, privileged instructions Midterm Review OS Structure User mode/ kernel mode Memory protection, privileged instructions System call Definition, examples, how it works? Other concepts to know Monolithic kernel vs. Micro kernel 2

More information

Example Threads. compile: gcc mythread.cc -o mythread -lpthread What is the output of this program? #include <pthread.h> #include <stdio.

Example Threads. compile: gcc mythread.cc -o mythread -lpthread What is the output of this program? #include <pthread.h> #include <stdio. Example Threads #include #include int num = 0; void *add_one(int *thread_num) { num++; printf("thread %d num = %d\n", *thread_num, num); } void main() { pthread_t thread; int my_id

More information

Concurrency and Synchronisation

Concurrency and Synchronisation Concurrency and Synchronisation 1 Learning Outcomes Understand concurrency is an issue in operating systems and multithreaded applications Know the concept of a critical region. Understand how mutual exclusion

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Threads Synchronization Refers to mechanisms allowing a programmer to control the execution order of some operations across different threads in a concurrent

More information

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Operating Systems. Thread Synchronization Primitives. Thomas Ropars. 1 Operating Systems Thread Synchronization Primitives Thomas Ropars thomas.ropars@univ-grenoble-alpes.fr 2017 2 Agenda Week 42/43: Synchronization primitives Week 44: Vacation Week 45: Synchronization

More information

Midterm I February 28 th, 2019 CS162: Operating Systems and Systems Programming

Midterm I February 28 th, 2019 CS162: Operating Systems and Systems Programming Spring 2019 University of California, Berkeley College of Engineering Computer Science Division EECS John Kubiatowicz Midterm I February 28 th, 2019 CS162: Operating Systems and Systems Programming Your

More information

Synchronization COMPSCI 386

Synchronization COMPSCI 386 Synchronization COMPSCI 386 Obvious? // push an item onto the stack while (top == SIZE) ; stack[top++] = item; // pop an item off the stack while (top == 0) ; item = stack[top--]; PRODUCER CONSUMER Suppose

More information

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems The Dining Philosophers Problem Philosophers either eat or think They must have two forks to eat Can only use forks

More information

FINE-GRAINED SYNCHRONIZATION. Operating Systems Engineering. Sleep & Wakeup [chapter #5] 15-Jun-14. in the previous lecture.

FINE-GRAINED SYNCHRONIZATION. Operating Systems Engineering. Sleep & Wakeup [chapter #5] 15-Jun-14. in the previous lecture. Operating Systems Engineering Sleep & Wakeup [chapter #5] By Dan Tsafrir, 2014-04-02 1 in the previous lecture. When critical section is very short FINE-GRAINED SYNCHRONIZATION 2 1 in this lecture. When

More information

Dealing with Issues for Interprocess Communication

Dealing with Issues for Interprocess Communication Dealing with Issues for Interprocess Communication Ref Section 2.3 Tanenbaum 7.1 Overview Processes frequently need to communicate with other processes. In a shell pipe the o/p of one process is passed

More information

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable) CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable) Past & Present Have looked at two constraints: Mutual exclusion constraint between two events is a requirement that

More information

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

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018 Lecture In the lecture on March 13 we will mainly discuss Chapter 6 (Process Scheduling). Examples will be be shown for the simulation of the Dining Philosopher problem, a solution with monitors will also

More information

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems : Organization of Programming Languages Threads Classic Concurrency Problems The Dining Philosophers Problem Philosophers either eat or think They must have two forks to eat Can only use forks on either

More information

Today: Synchronization. Recap: Synchronization

Today: Synchronization. Recap: Synchronization Today: Synchronization Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical section

More information

Midterm Exam Amy Murphy 19 March 2003

Midterm Exam Amy Murphy 19 March 2003 University of Rochester Midterm Exam Amy Murphy 19 March 2003 Computer Systems (CSC2/456) Read before beginning: Please write clearly. Illegible answers cannot be graded. Be sure to identify all of your

More information

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

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018 Deadlock and Monitors CS439: Principles of Computer Systems September 24, 2018 Bringing It All Together Processes Abstraction for protection Define address space Threads Share (and communicate) through

More information

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst Operating Systems CMPSCI 377 Spring 2017 Mark Corner University of Massachusetts Amherst What is a Monitor? Ties data and the synchronization operations together Monitors guarantee mutual exclusion, i.e.,

More information

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack What is it? Recap: Thread Independent flow of control What does it need (thread private)? Stack What for? Lightweight programming construct for concurrent activities How to implement? Kernel thread vs.

More information

Achieving Synchronization or How to Build a Semaphore

Achieving Synchronization or How to Build a Semaphore Achieving Synchronization or How to Build a Semaphore CS 241 March 12, 2012 Copyright University of Illinois CS 241 Staff 1 Announcements MP5 due tomorrow Jelly beans... Today Building a Semaphore If time:

More information

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

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift CS 537 Lecture 8 Monitors Michael Swift Two Classes of Synchronization Problems Uniform resource usage with simple scheduling constraints No other variables needed to express relationships Use one semaphore

More information

CSE 153 Design of Operating Systems

CSE 153 Design of Operating Systems CSE 153 Design of Operating Systems Winter 2018 Lecture 10: Monitors Monitors A monitor is a programming language construct that controls access to shared data Synchronization code added by compiler, enforced

More information