COMP 3430 Robert Guderian

Size: px
Start display at page:

Download "COMP 3430 Robert Guderian"

Transcription

1 Operating Systems COMP 3430 Robert Guderian file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 1/76 1

2 Concurrency file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 2/76 2

3 Last week IPC signals, FIFOs Scheduling file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 3/76 3

4 This week Concurrent processes Relevant chapters: Chapter Chapter 30, 31 file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 4/76 4

5 Concurrency Definition: two or more events or circumstances happening or existing at the same time In CS: Running many processes and threads at the same time, and achieving a deterministic result We've seen this with threads already file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 5/76 5

6 Concurrency This week: Formalize concurrent data access, and concurrent resources See algorithms to control access file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 6/76 6

7 Threads Good tools to synchronize data Threads know about each other, share codebases! Buuuuuuut... file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 7/76 7

8 Processes Processes might not know about each other Two or more processes may require the same system resource These processes might not know about each other They don't share a codebase file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 8/76 8

9 Synchronizing processes How can we resolve this problem? IPC! Abstraction: all resources are memory, need to control access to the memory file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 9/76 9

10 Goals Processes must share access to a resource Must serialize access to the shared resource This is just locking - serial means one-at-a-time Mutex! Access to shared resources must be atomic All or nothing file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 10/76 10

11 Critical sections The resource that must be protected is a critical resource. The code that processes the resource is a critical section. file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 11/76 11

12 Locking problems First problem: deadlock Consider: Two processes require two locks to do their tasks... But, each one holds one of the locks file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 12/76 12

13 Locking problems Second: starvation A process greedily holding a lock "I'm going to use this again, so why unlock" - everything else waits. file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 13/76 13

14 How to make a lock How can we solve this problem? Iterate on ideas on how to build a lock... file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 14/76 14

15 Lock attempt 1 Assume we have 2 well-known processes accessing some critical resource. Have them take turns? file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 15/76 15

16 Lock attempt 1 Pass the lock // process id 0 while(turn!= 0) {/*nothing*/} // have the lock // so something // done critical section, pass the lock to the next process turn = 1; file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 16/76 16

17 Lock attempt 1 Did we do it? Mutex? Yes! Does it scale? no... Does it always work? What if a process crashes? Starvation! file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 17/76 17

18 Lock attempt 2 Concept: have an array of bool, one entry for each process More scalable! If any element is true, that process has the lock file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 18/76 18

19 Lock attempt 2 while (locks[1]) // other thread has the lock {/*zzzz*/} locks[0] = true; // take the lock // critical section - do work // CS done, pass the lock locks[0] = false; //free the lock file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 19/76 19

20 What could possibly go wrong? What happens if a context switch happens just before locks[0] = true; file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 20/76 20

21 Lock attempt 2 Mutex? No. The problem? Setting our flag AFTER checking their flag file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 21/76 21

22 Lock attempt 3 Try setting our flag before we check their flag Then, if we have the lock we have the lock Otherwise, leave our flag, and wait for their flag to clear flag[0] = true; while (flag[1]) {} // we have the lock! // Done CS, free lock: flag[0] = false; file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 22/76 22

23 Lock attempt 3 Mutex? Yes! Are we problem-free? What about deadlock? We're greedily holding any locks we already have Not atomic (grab some locks, not all, deadlock) file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 23/76 23

24 Lock attempt 4 If we fail getting the lock, give up the lock, sleep How? file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 24/76 24

25 Lock attempt 4 flag[0] = true; while(flag[1]){ flag[0] = false; // give up lock sleep(1); flag[0] = true; // try again! } // have lock flag[0] = false; file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 25/76 25

26 Lock attempt 4 How did we do? Mutex? Yes! Deadlock?... no? Livelock? Both try lock, both sleep 1s, both try lock, both sleep 1s... file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 26/76 26

27 Livelock Both processes are collect some of their locks, get block, sleep - in perfect synchronicity. Think: to shopping carts in an aisle of a store. Solution? Random sleep times - better, but not a guarantee. file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 27/76 27

28 Dekker's Algorithm Avoid deadlock by mixing all these ideas, including the concept of a turn! Process gets the lock, and waits for turn file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 28/76 28

29 Dekker psuedocode For task x flag[x] = true; while (flag[!x]) if (turn!= x) flag[x] = false; while (turn[!x]); flag[x] = true; // Got the lock! turn =!x; flag[x]=false; file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 29/76 29

30 Peterson's Algorithm A simplification on Dekker's: flag[x] = true turn!= x while (flag[!x] && turn!= x); // lock flag[x] = false file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 30/76 30

31 Peterson How does the while work? What if both processes set their flag at the same time? Turn choses who can go Turn is only used if both have their flag set file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 31/76 31

32 There's got to be a better way This is hard. And weird. Examples assumed 2 (or a known number of) processes. These are software solutions, called spinlocks. Hardware has evolved to help with these problems. file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 32/76 32

33 Hardware mutex The kernel + processor let us write a word atomically. A few different implementations: TAS Test-And-Set (e.g. Motorola 68000) CAS Compare-And-Swap (e.g. IBM 370 and Motorola 68000) XCHG exchange (e.g. x86) *LL/SC Load Linked and Store Conditional (e.g. MIPS & ARM) file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 33/76 33

34 Test-and-set (TAS) Remember, we can write atomically now! The pseudocode is: bool testandset(int n) { if (n==0) { // no flag n = 1 return true } return false } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 34/76 34

35 Test-and-set (TAS) int lock = 0; // lots of code... while (!testandset(getpid())); // have lock lock = 0; Now, in our code Does it matter how many processes are accessing this lock? No! Problems What if process doesn't free the lock? Still a spinlock file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 35/76 35

36 Compare-and-swap (CAS) The same idea Shared memory that we read & write to atomically Check for a value (probably 0, indicating lock is free), set value if the value is what we expected Difference: we send the expected value file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 36/76 36

37 Compare-and-swap (CAS) Psuedocode: bool CAS(int* test, int old, int new){ if (*test == old){ *test = new; return true; } return false; } (Some return the old value on success) file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 37/76 37

38 Compare-and-swap (CAS) How do we use it? int lock = 0; // lots of code... while (!compareandswap(&lock, 0, 1)); // have lock compareandswap(&lock, 1, 0) file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 38/76 38

39 CAS and TAS Are they good? Advantages: Any * of tasks Simple operation Atomic, handled by hardware file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 39/76 39

40 CAS and TAS But... Still a spinlock Starvation and Deadlock still possible - left up to programmers... Can we really trust programmers? Can we do better? file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 40/76 40

41 Have the OS deal with locks The OS can act as a broker for locks Using semaphores! What? Oxford,... what? [Traffic semaphore] ( A signaling system! file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 41/76 41

42 Semaphores Mutex is one use of the things it can do. Two types of semaphores: 1. Counting semaphore - allows n processes access to a critical resource. 2. 0/1 semaphore (binary semaphore) - only counts to 1... mutex! file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 42/76 42

43 Semaphores 3 operations: 1. initialize: initialize with a non-zero number the # of concurrent tasks that can access 2. wait: decrement the value of the semaphore, block if the value is negative 3. signal: increment the value if value <= 0, unblock a waiting task. if >= 0, there's no waiting tasks! file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 43/76 43

44 Semaphores - queues Semaphore needs a queue to hold blocked tasks. 2 models: 1. dequeue in any order Weak or asynchronous 2. queue tasks as FIFO Forces an order, potentially reduces concurrency (livelocks) file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 44/76 44

45 psuedo-semaphore Just a count, and a queue of blocked processes struct{ int count; queue Q; } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 45/76 45

46 psuedo-sema Wait function - remember, this is an OS-level function, so we can control the state of processes! wait (sema s) { s.count--; if (s.count < 0 ) // not available s.q.enter(task); block(task); // block this thread } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 46/76 46

47 psuedo-sema Signal function - someone freed the lock signal(sema s) { s.count++; if (s.count < 0) // is a prc waiting? s.q.leave() ready(task) // put on ready queue } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 47/76 47

48 pseudo-sema What code has to be atomic? Everything except setting the state of the process. file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 48/76 48

49 pseudo-use sema lock; lock.init(); lock.wait(); // critical section lock.signal(); file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 49/76 49

50 OS concepts The operating system manages resources, we abstract the resources to producers and consumers Producers make data for consumption/resources for use Consumers take data (process data)/use resources Called "Producer/Consumer model" file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 50/76 50

51 Producer/Consumer Problem 1 or more producers creating data/resources 1 or more consumers taking data/resources consumer can not read from an empty buffer file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 51/76 51

52 The bounded buffer Producer/consumers use a bounded buffer to share their data - FIFO queue on an array. Producers write to the 'in' pointer Consumers write to the 'out' pointer file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 52/76 52

53 Producer/consumer Producer pseudocode void produce(){ v = getnewdata(); buffer[in] = v; in++; } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 53/76 53

54 Producer/consumer Consumer pseudocode int consume(){ while (in <= out) {}; x = buffer[out++]; return x; } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 54/76 54

55 Locking? Of course Can't have two consume at the same time! But how? file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 55/76 55

56 Mutex Attempt 1: Lock the whole thing Lock the entire buffer - in, and out Works, but serializes all input/output "course grain" locking file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 56/76 56

57 Mutex Attempt 2: Use a semaphore Hey, we just learned about that, this is such a big surprise Has signalling, gets rid of busy wait How should we use it? file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 57/76 57

58 Semaphore solution v1 Use it as a mutex lock - producer sema lock; void produce(){ v = getnewdata(); lock.wait() buffer[in] = v; in++; lock.signal(); } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 58/76 58

59 Semaphore solution v1 int consume(){ lock.wait(); x = buffer[out++]; lock.signal(); return x; } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 59/76 59

60 Semaphore solution v1 Is it good? Mutex? Yes! It still agressively a mutex? Yes! Do we need to lock the buffer? No! file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 60/76 60

61 Semaphore solution v2 Use 2 locks, one for the buffer, one for consuming sema lock; sema numdata; void produce(){ v = getnewdata(); lock.wait() buffer[in] = v; in++; lock.signal(); numdata.signal(); } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 61/76 61

62 Semaphore solution v2 int consume(){ numdata.wait(); x = buffer[out++]; return x; } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 62/76 62

63 Semaphore solution v2 Mutex? Yes Cool? Yes! Problems? Finite buffer, could get filled. Solvable with more locks (of course) file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 63/76 63

64 Semaphore solution v3 What about... producer... sema inlock; void produce(){ v = getnewdata(); inlock.wait() buffer[in] = v; in++; inlock.signal(); } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 64/76 64

65 Semaphore solution v3 Consumer... sema outlock; int consume(){ outlock.wait(); x = buffer[out++]; outlock.signal(); return x; } file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 65/76 65

66 Semaphore solution v3 Mutex? Yes Aggressive locking? No file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 66/76 66

67 Bounded buffer wrap-up Interesting problem, because there is many solutions, most have tradeoffs. file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 67/76 67

68 Deadlock Deadlock - threads/processes blocking each other Can we be more formal What are the causes How do we avoid them? file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 68/76 68

69 Conditions 4 conditions for deadlock Mutual exclusion Hold-and-wait No preemption Circular wait Prevent ONE, and no deadlock can occur file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 69/76 69

70 Prevent mutex lock Ask youself some questions Lock smaller elements? Does THIS need to be locked? Does this NEED to be locked? Is there a lock-free solution? Replace with CAS? file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 70/76 70

71 Prevent hold-and-wait Get all locks at once, atomically If we can't get them all, give them all up. Sleep, hoping they will become available Terrible, but easy Or, list up-front what we need, don't wake until all locks are available Unrealistic! file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 71/76 71

72 Prevent "no preemption" Allow preemption! Take locks away from deadlocked processes... How do we preempt a process that is holding a lock? It's unsafe! pthread_mutex_trylock - don't have to be prempted! file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 72/76 72

73 Prevent circular wait This is where we can be clever! This is preventing livelock. Can we... Always lock in a certain order? ALWAYS lock L1, then L2, would never have L2 locked without L1 Called Partial ordering Draw the resource dependency graph file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 73/76 73

74 Other deadlock avoidance Scheduling: Never schedule P1 and P2 at the same time (multi- CPU) Clean up locks before context switch Wait, we can't control that file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 74/76 74

75 Other deadlock avoidance Detect and Recover - This is real-world Use an outside process to monitor for deadlocks If all processes are blocked, kill one Or, send a signal (SIGUSR1?) which tells one process to drop locks? file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 75/76 75

76 Concurrency, conclusion Locks are difficult to make Deadlock is hard to avoid Resources are hard to manage The OS manages locks, to aide with concurrency file:///users/robg/dropbox/teaching/ /slides/06_concurrency/index.html?print-pdf#/ 76/76 76

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

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

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

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

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

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

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28) Lecture Topics Today: Concurrency (Stallings, chapter 5.1-5.4, 5.7) Next: Exam #1 1 Announcements Self-Study Exercise #5 Project #3 (due 9/28) Project #4 (due 10/12) 2 Exam #1 Tuesday, 10/3 during lecture

More information

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1 Synchronization Disclaimer: some slides are adopted from the book authors slides with permission 1 What is it? Recap: Thread Independent flow of control What does it need (thread private)? Stack What for?

More information

Synchronization for Concurrent Tasks

Synchronization for Concurrent Tasks Synchronization for Concurrent Tasks Minsoo Ryu Department of Computer Science and Engineering 2 1 Race Condition and Critical Section Page X 2 Algorithmic Approaches Page X 3 Hardware Support Page X 4

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

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

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

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Process Synchronization, Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Fall 2017 Lecture 11 Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel Feedback Queue: Q0, Q1,

More information

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

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

Last Class: Deadlocks. Today

Last Class: Deadlocks. Today Last Class: Deadlocks Necessary conditions for deadlock: Mutual exclusion Hold and wait No preemption Circular wait Ways of handling deadlock Deadlock detection and recovery Deadlock prevention Deadlock

More information

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem? What is the Race Condition? And what is its solution? Race Condition: Where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular

More information

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

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

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Synchronization 6.1 Background 6.2 The Critical-Section Problem 6.3 Peterson s Solution 6.4 Synchronization Hardware 6.5 Mutex Locks 6.6 Semaphores 6.7 Classic

More information

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

More information

CS420: Operating Systems. Process Synchronization

CS420: Operating Systems. Process Synchronization Process Synchronization James Moscola Department of Engineering & Computer Science York College of Pennsylvania Based on Operating System Concepts, 9th Edition by Silberschatz, Galvin, Gagne Background

More information

Process Synchronization

Process Synchronization Process Synchronization Concurrent access to shared data may result in data inconsistency Multiple threads in a single process Maintaining data consistency requires mechanisms to ensure the orderly execution

More information

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks

More information

Lesson 6: Process Synchronization

Lesson 6: Process Synchronization Lesson 6: Process Synchronization Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

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

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6. Part Three - Process Coordination Chapter 6: Synchronization 6.1 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure

More information

Last Class: Monitors. Real-world Examples

Last Class: Monitors. Real-world Examples Last Class: Monitors Monitor wraps operations with a mutex Condition variables release mutex temporarily C++ does not provide a monitor construct, but monitors can be implemented by following the monitor

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University Semaphores 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) Synchronization

More information

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1 Thread Disclaimer: some slides are adopted from the book authors slides with permission 1 IPC Shared memory Recap share a memory region between processes read or write to the shared memory region fast

More information

Synchronization Principles

Synchronization Principles Synchronization Principles Gordon College Stephen Brinton The Problem with Concurrency Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms

More information

What's wrong with Semaphores?

What's wrong with Semaphores? Next: Monitors and Condition Variables What is wrong with semaphores? Monitors What are they? How do we implement monitors? Two types of monitors: Mesa and Hoare Compare semaphore and monitors Lecture

More information

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

More information

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems Concurrency 1 Concurrency Execution of multiple processes. Multi-programming: Management of multiple processes within a uni- processor system, every system has this support, whether big, small or complex.

More information

CS370 Operating Systems

CS370 Operating Systems CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L11 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Multilevel feedback queue:

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

Interprocess Communication By: Kaushik Vaghani

Interprocess Communication By: Kaushik Vaghani Interprocess Communication By: Kaushik Vaghani Background Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of execution depends on the

More information

Chapters 5 and 6 Concurrency

Chapters 5 and 6 Concurrency Operating Systems: Internals and Design Principles, 6/E William Stallings Chapters 5 and 6 Concurrency Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Concurrency When several processes/threads

More information

Process Synchronization

Process Synchronization TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Process Synchronization [SGG7] Chapter 6 Copyright Notice: The lecture notes are mainly based on Silberschatz s, Galvin

More information

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

Chapter 6: Synchronization. Operating System Concepts 8 th Edition, Chapter 6: Synchronization, Silberschatz, Galvin and Gagne 2009 Outline Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information

Synchronization for Concurrent Tasks

Synchronization for Concurrent Tasks Synchronization for Concurrent Tasks Minsoo Ryu Department of Computer Science and Engineering 2 1 Race Condition and Critical Section Page X 2 Synchronization in the Linux Kernel Page X 3 Other Synchronization

More information

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

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers Mutual exclusion & synchronization mutexes Unbounded buffer, 1 producer, N consumers out shared by all consumers mutex among consumers producer not concerned: can still add items to buffer at any time

More information

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

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy Operating Systems Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. AL-AZHAR University Website : eaymanelshenawy.wordpress.com Email : eaymanelshenawy@yahoo.com Reference

More information

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set? Last Class: Synchronization Review Synchronization Mutual exclusion Critical sections Example: Too Much Milk Locks Synchronization primitives are required to ensure that only one thread executes in a critical

More information

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor. Synchronization 1 Concurrency On multiprocessors, several threads can execute simultaneously, one on each processor. On uniprocessors, only one thread executes at a time. However, because of preemption

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

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018 Deadlock and Monitors CS439: Principles of Computer Systems February 7, 2018 Last Time Terminology Safety and liveness Atomic Instructions, Synchronization, Mutual Exclusion, Critical Sections Synchronization

More information

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Process Synchronisation (contd.) Operating Systems. Autumn CS4023 Operating Systems Autumn 2017-2018 Outline Process Synchronisation (contd.) 1 Process Synchronisation (contd.) Synchronization Hardware 6.4 (SGG) Many systems provide hardware support for critical section

More information

CSE 153 Design of Operating Systems

CSE 153 Design of Operating Systems CSE 153 Design of Operating Systems Winter 19 Lecture 7/8: Synchronization (1) Administrivia How is Lab going? Be prepared with questions for this weeks Lab My impression from TAs is that you are on track

More information

CSC501 Operating Systems Principles. Process Synchronization

CSC501 Operating Systems Principles. Process Synchronization CSC501 Operating Systems Principles Process Synchronization 1 Last Lecture q Process Scheduling Question I: Within one second, how many times the timer interrupt will occur? Question II: Within one second,

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

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

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems Synchronization CS 475, Spring 2018 Concurrent & Distributed Systems Review: Threads: Memory View code heap data files code heap data files stack stack stack stack m1 m1 a1 b1 m2 m2 a2 b2 m3 m3 a3 m4 m4

More information

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018 CIS 3207 - Operating Systems Synchronization based on Busy Waiting Professor Qiang Zeng Spring 2018 Previous class IPC for passing data Pipe FIFO Message Queue Shared Memory Compare these IPCs for data

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

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

Process Synchronization

Process Synchronization CSC 4103 - Operating Systems Spring 2007 Lecture - VI Process Synchronization Tevfik Koşar Louisiana State University February 6 th, 2007 1 Roadmap Process Synchronization The Critical-Section Problem

More information

Tasks. Task Implementation and management

Tasks. Task Implementation and management Tasks Task Implementation and management Tasks Vocab Absolute time - real world time Relative time - time referenced to some event Interval - any slice of time characterized by start & end times Duration

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

COMP 3430 Robert Guderian

COMP 3430 Robert Guderian Operating Systems COMP 3430 Robert Guderian file:///users/robg/dropbox/teaching/3430-2018/slides/04_threads/index.html?print-pdf#/ 1/58 1 Threads Last week: Processes This week: Lesser processes! file:///users/robg/dropbox/teaching/3430-2018/slides/04_threads/index.html?print-pdf#/

More information

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019 CS 31: Introduction to Computer Systems 22-23: Threads & Synchronization April 16-18, 2019 Making Programs Run Faster We all like how fast computers are In the old days (1980 s - 2005): Algorithm too slow?

More information

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang Process Synchronization CISC3595, Spring 2015 Dr. Zhang 1 Concurrency OS supports multi-programming In single-processor system, processes are interleaved in time In multiple-process system, processes execution

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 Clicker Question #1 public static void main(string[] args) { (new Thread(new t1())).start(); (new Thread(new t2())).start();}

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

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

Fall 2015 COMP Operating Systems. Lab 06

Fall 2015 COMP Operating Systems. Lab 06 Fall 2015 COMP 3511 Operating Systems Lab 06 Outline Monitor Deadlocks Logical vs. Physical Address Space Segmentation Example of segmentation scheme Paging Example of paging scheme Paging-Segmentation

More information

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University Synchronization I Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Today s Topics Synchronization problem Locks 2 Synchronization Threads cooperate

More information

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens Edsger s perspective Testing can only prove the presence of bugs Thread Synchronization: Foundations Properties Property: a predicate that is evaluated over a run of the program (a trace) every message

More information

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization Chapter 6: Process Synchronization Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Mutex Locks Semaphores Classic Problems of Synchronization

More information

Chapter 6 Synchronization

Chapter 6 Synchronization Chapter 6 Synchronization Images from Silberschatz Pacific University 1 My code is slow Don't worry about speed at this point Later solutions: use the optimizer with gcc: -O# # is 0,1,2,3 0 do not optimize

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

Interprocess Communication and Synchronization

Interprocess Communication and Synchronization Chapter 2 (Second Part) Interprocess Communication and Synchronization Slide Credits: Jonathan Walpole Andrew Tanenbaum 1 Outline Race Conditions Mutual Exclusion and Critical Regions Mutex s Test-And-Set

More information

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination.

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination. Times: Thursday 2017-06-22 at 19:00 to 20:50 (7 to 8:50PM) Duration: 1 hour 50 minutes (110 minutes) Exam ID: 3520593 Please print in pen: Waterloo Student ID Number: WatIAM/Quest Login Userid: Sections:

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

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212 Operating Systems Spring 2009-2010 Outline Process Synchronisation (contd.) 1 Process Synchronisation (contd.) 2 Announcements Presentations: will be held on last teaching week during lectures make a 20-minute

More information

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo CSE 120 Principles of Operating Systems Fall 2007 Lecture 6: Semaphores Keith Marzullo Announcements Homework #2 out Homework #1 almost graded... Discussion session on Wednesday will entertain questions

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

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

Process Synchronization (Part I)

Process Synchronization (Part I) Process Synchronization (Part I) Amir H. Payberah amir@sics.se Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 1 / 44 Motivation

More information

Semaphores. May 10, Mutual exclusion with shared variables is difficult (e.g. Dekker s solution).

Semaphores. May 10, Mutual exclusion with shared variables is difficult (e.g. Dekker s solution). Semaphores May 10, 2000 1 Introduction Mutual exclusion with shared variables is difficult (e.g. Dekker s solution). Generalising to an arbitrary number of processes is also nontrivial (e.g. Lamport s

More information

CMSC421: Principles of Operating Systems

CMSC421: Principles of Operating Systems CMSC421: Principles of Operating Systems Nilanjan Banerjee Assistant Professor, University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/421/ Principles of Operating

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

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

Process Synchronization Mechanisms

Process Synchronization Mechanisms Process Synchronization Mechanisms Tom Kelliher, CS 311 Mar 19, 2012 Announcements: From last time: 1. CPU scheduling. Outline: 1. Critical sections and cooperating processes. 2. Cooperating processes

More information

Operating Systems. Synchronization Based on Ch. 5 of OS Concepts by SGG

Operating Systems. Synchronization Based on Ch. 5 of OS Concepts by SGG Operating Systems Synchronization Based on Ch. 5 of OS Concepts by SGG Need to Synchronize We already saw that if we write on a shared data structure w/o synchronization bad things can happen. The parts

More information

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition Module 6: Process Synchronization 6.1 Silberschatz, Galvin and Gagne 2009 Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores

More information

Process/Thread Synchronization

Process/Thread Synchronization CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov February 14, 2013 Reading Assignment 7 Chapter 7 Deadlocks, due 2/21 2/14/13 CSE325: Synchronization

More information

Chapter 5 Asynchronous Concurrent Execution

Chapter 5 Asynchronous Concurrent Execution Chapter 5 Asynchronous Concurrent Execution Outline 5.1 Introduction 5.2 Mutual Exclusion 5.2.1 Java Multithreading Case Study 5.2.2 Critical Sections 5.2.3 Mutual Exclusion Primitives 5.3 Implementing

More information

Concurrency. Chapter 5

Concurrency. Chapter 5 Concurrency 1 Chapter 5 2 Concurrency Is a fundamental concept in operating system design Processes execute interleaved in time on a single processor Creates the illusion of simultaneous execution Benefits

More information

IV. Process Synchronisation

IV. Process Synchronisation IV. Process Synchronisation Operating Systems Stefan Klinger Database & Information Systems Group University of Konstanz Summer Term 2009 Background Multiprogramming Multiple processes are executed asynchronously.

More information

Concurrency: Deadlock and Starvation. Chapter 6

Concurrency: Deadlock and Starvation. Chapter 6 Concurrency: Deadlock and Starvation Chapter 6 Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involve conflicting needs for resources

More information

Locks and semaphores. Johan Montelius KTH

Locks and semaphores. Johan Montelius KTH Locks and semaphores Johan Montelius KTH 2018 1 / 40 recap, what s the problem : # include < pthread.h> volatile int count = 0; void * hello ( void * arg ) { for ( int i = 0; i < 10; i ++) { count ++;

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

Programming in Parallel COMP755

Programming in Parallel COMP755 Programming in Parallel COMP755 All games have morals; and the game of Snakes and Ladders captures, as no other activity can hope to do, the eternal truth that for every ladder you hope to climb, a snake

More information

Introduction to Operating Systems

Introduction to Operating Systems Introduction to Operating Systems Lecture 4: Process Synchronization MING GAO SE@ecnu (for course related communications) mgao@sei.ecnu.edu.cn Mar. 18, 2015 Outline 1 The synchronization problem 2 A roadmap

More information

Midterm Exam March 13, 2013 CS162 Operating Systems

Midterm Exam March 13, 2013 CS162 Operating Systems University of California, Berkeley College of Engineering Computer Science Division EECS Spring 2013 Anthony D. Joseph Midterm Exam March 13, 2013 CS162 Operating Systems Your Name: SID AND 162 Login:

More information

Announcements. CS 3204 Operating Systems. Schedule. Optimistic Concurrency Control. Optimistic Concurrency Control (2)

Announcements. CS 3204 Operating Systems. Schedule. Optimistic Concurrency Control. Optimistic Concurrency Control (2) Announcements CS 3204 Operating Systems Lecture 15 Godmar Back Project 2 due Tuesday Oct 17, 11:59pm Midterm Thursday Oct 12 Posted Midterm Announcement Posted Sample Midterm Reading assignment: Read Chapter

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

CSCI 447 Operating Systems Filip Jagodzinski

CSCI 447 Operating Systems Filip Jagodzinski Filip Jagodzinski Announcements Homework 2 you have 2 weeks start now Book questions including two custom ones A single programming task File names and directories, class conventions homework2-script,

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

Chapter 6: Process Synchronization. Module 6: Process Synchronization

Chapter 6: Process Synchronization. Module 6: Process Synchronization Chapter 6: Process Synchronization Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization

More information