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

Size: px
Start display at page:

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

Transcription

1 CS 471 Operating Systems Yue Cheng George Mason University Fall

2 Review: Sync Terminology Worksheet 2

3 Review: Semaphores 3

4 Semaphores o Motivation: Avoid busy waiting by blocking a process execution until some condition is satisfied o Two operations are defined on a semaphore variable s: sem_wait(s) (also called P(s) or down(s)) sem_post(s) (also called V(s) or up(s)) 4

5 Semaphore as a General Synchronization Tool o Semaphores provide a general synchronization mechanism beyond the critical section problem o Example problem: Execute B in P j only after A executed in P i o Use semaphore flag initialized to 0 o Code: P i : P j :!! A sem_wait(flag) sem_post(flag) B 5

6 Join with Semaphore sem_t s; void *child(void *arg) { printf( child\n ); sem_post(&s); } int main(int argc, char *argv[]) { sem_init(&s, 0); pthread_t c; printf( parent: begin\n ); pthread_create(c, NULL, child, NULL); sem_wait(&s); printf( parent: end\n ); return 0; } 6

7 Classical Problems of Synchronization o Producer-Consumer Problem Semaphore version Condition Variable A CV-based version o Readers-Writers Problem o Dining-Philosophers Problem 7

8 Producer-Consumer Problem o o o o The bounded-buffer producer-consumer problem assumes that there is a buffer of size N The producer process puts items to the buffer area The consumer process consumes items from the buffer The producer and the consumer execute concurrently producer consumer 8

9 Example: UNIX Pipes o A pipe may have many writers and readers o Internally, there is a finite-sized buffer o Writers add data to the buffer o Readers remove data from the buffer 9

10 Example: UNIX Pipes start Buffer end 10

11 Example: UNIX Pipes start Write Buffer end 11

12 Example: UNIX Pipes start Buffer end 12

13 Example: UNIX Pipes start Write Buffer end 13

14 Example: UNIX Pipes start Buffer end 14

15 Example: UNIX Pipes start Read Buffer end 15

16 Example: UNIX Pipes start Buffer end 16

17 Example: UNIX Pipes start Write Buffer end 17

18 Example: UNIX Pipes start Buffer end 18

19 Example: UNIX Pipes Read start Buffer end 19

20 Example: UNIX Pipes Read start Buffer end 20

21 Example: UNIX Pipes Read start Buffer Note: reader must wait end 21

22 Example: UNIX Pipes Write start Buffer end 22

23 Example: UNIX Pipes Write start Buffer end 23

24 Example: UNIX Pipes Write start Buffer Note: writer must wait end 24

25 Example: UNIX Pipes o Implementation Reads/writes to buffer require locking When buffers are full, writers (producers) must wait When buffers are empty, readers (consumers) must wait 25

26 Example: UNIX Pipes % ps aux less Demo Pipe % cat file grep <str> Pipe 26

27 Producer-Consumer Model: o Shared data: sem_t full, empty; Parameters o Initially: full = 0 /* The number of full buffers */ empty = MAX /* The number of empty buffers */ 27

28 First Attempt: MAX = 1 Put and Get routines 28

29 First Attempt: MAX = 10? Put and Get routines 29

30 First Attempt: MAX = 10? fill = 0 empty = 10 Producer 0: Running Producer 1: Runnable 30

31 First Attempt: MAX = 10? fill = 0 empty = 9 Producer 0: Running Producer 1: Runnable void put(int value) { buffer[fill] = value; fill = (fill + 1) % MAX; } 31

32 First Attempt: MAX = 10? fill = 0 empty = 9 Producer 0: Running Producer 1: Runnable void put(int value) { buffer[fill] = value; Interrupted fill = (fill + 1) % MAX; } 32

33 First Attempt: MAX = 10? fill = 0 empty = 9 Producer 0: Sleeping Producer 1: Runnable void put(int value) { buffer[fill] = value; Interrupted fill = (fill + 1) % MAX; } 33

34 First Attempt: MAX = 10? fill = 0 empty = 9 Producer 0: Runnable Producer 1: Running void put(int value) { buffer[fill] = value; Interrupted fill = (fill + 1) % MAX; } 34

35 First Attempt: MAX = 10? fill = 0 Overwrite! empty = 8 Producer 0: Runnable Producer 1: Running void put(int value) { buffer[fill] = value; Interrupted fill = (fill + 1) % MAX; } void put(int value) { buffer[fill] = value; fill = (fill + 1) % MAX; } 35

36 One More Parameter: A mutex lock o Shared data: sem_t full, empty; o Initially: full = 0; /* The number of full buffers */ empty = MAX; /* The number of empty buffers */ mutex = 1; /* Semaphore controlling the access to the buffer pool */ 36

37 Add Mutual Exclusion 37

38 Add Mutual Exclusion What if consumer gets to run first?? 38

39 Adding Mutual Exclusion Producer 0: Runnable Consumer 0: Running 39

40 Adding Mutual Exclusion Producer 0: Runnable Consumer 0: Runnable Consumer 0 is waiting for full to be greater than 0 40

41 Adding Mutual Exclusion Producer 0: Running Consumer 0: Runnable Consumer 0 is waiting for full to be greater than 0 41

42 Adding Mutual Exclusion Deadlock!! Producer 0: Running Consumer 0: Runnable Producer 0 gets stuck at acquiring mutex which has been locked by Consumer 0! Consumer 0 is waiting for full to be greater than 0 42

43 Deadlocks o A set of threads are said to be in a deadlock state when every thread in the set is waiting for an event that can be caused only by another thread in the set A typical deadlock dependency graph 43

44 Conditions for Deadlock o Mutual exclusion Threads claim exclusive control of resources that require (e.g., a thread grabs a lock) o Hold-and-wait Threads hold resources allocated to them while waiting for additional resources o No preemption Resources cannot be forcibly removed from threads that are holding them o Circular wait There exists a circular chain of threads such that each holds one or more resources that are being requests by next thread in chain 44

45 Correct Mutual Exclusion Mutex wraps just around critical section! Mutex wraps just around critical section! 45

46 Producer-Consumer Solution o Make sure that 1. The producer and the consumer do not access the buffer area and related variables at the same time 2. No item is made available to the consumer if all the buffer slots are empty 3. No slot in the buffer is made available to the producer if all the buffer slots are full 46

47 Condition Variables 47

48 Condition Variables A parent waiting for its child 48

49 Spin-based Approach Using a shared variable, parent spins until child set it to 1 49

50 Spin-based Approach Using a shared variable, parent spins until child set it to 1 What s the problem of this approach? 50

51 Condition Variables (CV) o Definition: An explicit queue that threads can put themselves when some condition is not as desired (by waiting on the condition) Other thread can wake one of those waiting threads to allow them to continue (by signaling on the condition) o Pthread CV 51

52 CV-based Approach???? 52

53 Broken Implementation 1 53

54 Broken Implementation 1 If parent comes after child, parent sleeps forever 54

55 Broken Implementation 2 55

56 Broken Implementation 2 No mutual exclusion, hence child may signal before parent calls cond_wait(). In this case, parent sleeps forever! 56

57 Trap 1 When Using CV Condition Variable wait wait thread thread 57

58 Trap 1 When Using CV thread signal Condition Variable wait wait thread thread 58

59 Trap 1 When Using CV Condition Variable wait thread 59

60 Trap 1 When Using CV Condition Variable wait thread Only one thread gets a signal 60

61 Trap 2 When Using CV Condition Variable 61

62 Trap 2 When Using CV thread signal Condition Variable 62

63 Trap 2 When Using CV Condition Variable 63

64 Trap 2 When Using CV Condition Variable wait thread 64

65 Trap 2 When Using CV Condition Variable wait thread waits forever 65

66 Trap 2 When Using CV Condition Variable wait thread waits forever Signal lost if nobody waiting at that time 66

67 Guarantee Upon signal, there has to be at least one thread waiting; If there are threads waiting, at least one thread will wake thread signal Condition Variable wait wait thread thread 67

68 CV-based Parent-wait-for-child Approach 68

69 CV-based Parent-wait-for-child Approach Good Rule of Thumb Always do 1. wait and 2. signal while holding the lock To prevent lost signal 69

[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

[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

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

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks Synchronization II Today Condition Variables Semaphores Monitors and some classical problems Next time Deadlocks Condition variables Many times a thread wants to check whether a condition is true before

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

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

Synchronization II. q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks Synchronization II q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks Condition variables Locks are not enough to build concurrent programs Many times a thread

More information

Semaphores Semaphores: A Definition

Semaphores Semaphores: A Definition 23 Semaphores As we know now, one needs both locks and condition variables to solve a broad range of relevant and interesting concurrency problems. The first person who realized this years ago was Edsger

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

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

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

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

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

Condition Variables. Figure 29.1: A Parent Waiting For Its Child

Condition Variables. Figure 29.1: A Parent Waiting For Its Child 29 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

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

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources 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 No efficient solution Involve conflicting

More information

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

CS 25200: Systems Programming. Lecture 26: Classic Synchronization Problems CS 25200: Systems Programming Lecture 26: Classic Synchronization Problems Dr. Jef Turkstra 2018 Dr. Jeffrey A. Turkstra 1 Announcements Lab 5 posted this evening Web server Password protection SSL cgi-bin

More information

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

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science High Performance Computing Lecture 21 Matthew Jacob Indian Institute of Science Semaphore Examples Semaphores can do more than mutex locks Example: Consider our concurrent program where process P1 reads

More information

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings Chapter 6 Concurrency: Deadlock and Starvation Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall Deadlock

More information

Process Synchronization. studykorner.org

Process Synchronization. studykorner.org Process Synchronization Semaphore Implementation Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time The main disadvantage of the semaphore definition

More information

Semaphores. attention to the previous chapters. And even remembering a few things. You have, right?!

Semaphores. attention to the previous chapters. And even remembering a few things. You have, right?! 30 Semaphores As we know now 1, one needs both locks and condition variables to solve a broad range of relevant and interesting concurrency problems. One of the first people to realize this years ago was

More information

W4118 Operating Systems. Instructor: Junfeng Yang

W4118 Operating Systems. Instructor: Junfeng Yang W4118 Operating Systems Instructor: Junfeng Yang Outline Semaphores Producer-consumer problem Monitors and condition variables 2 Semaphore motivation Problem with lock: mutual exclusion, but no ordering

More information

[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

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

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; } Semaphore Semaphore S integer variable Two standard operations modify S: wait() and signal() Originally called P() and V() Can only be accessed via two indivisible (atomic) operations wait (S) { while

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

Concept of a process

Concept of a process Concept of a process In the context of this course a process is a program whose execution is in progress States of a process: running, ready, blocked Submit Ready Running Completion Blocked Concurrent

More information

Lecture 13 Condition Variables

Lecture 13 Condition Variables Lecture 13 Condition Variables Contents In this lecture, you will learn Condition Variables And how to use CVs to solve The Producer/Consumer (Bounded Buffer) Problem Review Thus far we have developed

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

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

Midterm Exam. October 20th, Thursday NSC

Midterm Exam. October 20th, Thursday NSC CSE 421/521 - Operating Systems Fall 2011 Lecture - XIV Midterm Review Tevfik Koşar University at Buffalo October 18 th, 2011 1 Midterm Exam October 20th, Thursday 9:30am-10:50am @215 NSC Chapters included

More information

Concurrency: Signaling and Condition Variables

Concurrency: Signaling and Condition Variables 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

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

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

30. Condition Variables

30. Condition Variables 30. Condition Variables Oerating System: Three Easy Pieces AOS@UC 1 Condition Variables There are many cases where a thread wishes to check whether a condition is true before continuing its execution.

More information

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 Classical Synchronization Problems 1 1 This lecture Goals: Topics Introduce classical synchronization problems Producer-Consumer Problem Reader-Writer Problem Dining Philosophers Problem Sleeping Barber

More information

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

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017 Data Race Shared Data: 465 1 8 5 6 209? tail A[] thread switch Enqueue(): A[tail] = 20; tail++; A[tail] = 9; tail++; Thread 0 Thread

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

Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer Problem.

Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer Problem. CSCI-375 Operating Systems Lab #5 Semaphores, Producer/Consumer Problem October 19, 2016 Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer

More information

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

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012 Pre-lab #2 tutorial ECE 254 Operating Systems and Systems Programming May 24, 2012 Content Concurrency Concurrent Programming Thread vs. Process POSIX Threads Synchronization and Critical Sections Mutexes

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

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

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

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores  Review: Multi-Threaded Processes CSC 1600: Chapter 6 Synchronizing Threads with Semaphores " Review: Multi-Threaded Processes" 1 badcnt.c: An Incorrect Program" #define NITERS 1000000 unsigned int cnt = 0; /* shared */ int main() pthread_t

More information

Synchroniza+on II COMS W4118

Synchroniza+on II COMS W4118 Synchroniza+on II COMS W4118 References: Opera+ng Systems Concepts (9e), Linux Kernel Development, previous W4118s Copyright no2ce: care has been taken to use only those web images deemed by the instructor

More information

Concurrency, Thread. Dongkun Shin, SKKU

Concurrency, Thread. Dongkun Shin, SKKU Concurrency, Thread 1 Thread Classic view a single point of execution within a program a single PC where instructions are being fetched from and executed), Multi-threaded program Has more than one point

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

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

Concurrency: Deadlock and Starvation

Concurrency: Deadlock and Starvation Concurrency: Deadlock and Starvation Chapter 6 E&CE 354: Processes 1 Deadlock Deadlock = situation in which every process from a set is permanently blocked, i.e. cannot proceed with execution Common cause:

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

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

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization CS-345 Operating Systems Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization Threads A thread is a lightweight process A thread exists within a process and uses the process resources. It

More information

Process Synchronization(2)

Process Synchronization(2) EECS 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Semaphores Problems with the software solutions.

More information

OS Process Synchronization!

OS Process Synchronization! OS Process Synchronization! Race Conditions! The Critical Section Problem! Synchronization Hardware! Semaphores! Classical Problems of Synchronization! Synchronization HW Assignment! 3.1! Concurrent Access

More information

Process Synchronization(2)

Process Synchronization(2) CSE 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Computer Science and Engineering York University Semaphores Problems with the software solutions. Not easy

More information

CSE Traditional Operating Systems deal with typical system software designed to be:

CSE Traditional Operating Systems deal with typical system software designed to be: CSE 6431 Traditional Operating Systems deal with typical system software designed to be: general purpose running on single processor machines Advanced Operating Systems are designed for either a special

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

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

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

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 1

Classical Synchronization Problems. Copyright : University of Illinois CS 241 Staff 1 1 Classical Synchronization Problems Copyright : University of Illinois CS 241 Staff 1 1 This lecture Goals: Introduce classical synchronization problems Topics Producer-Consumer Problem Reader-Writer Problem

More information

Process Synchronization(2)

Process Synchronization(2) EECS 3221.3 Operating System Fundamentals No.6 Process Synchronization(2) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Semaphores Problems with the software solutions.

More information

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation

Processes The Process Model. Chapter 2. Processes and Threads. Process Termination. Process Creation Chapter 2 Processes The Process Model Processes and Threads 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling Multiprogramming of four programs Conceptual

More information

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

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018 CS 31: Intro to Systems Misc. Threading Kevin Webb Swarthmore College December 6, 2018 Classic thread patterns Agenda Pthreads primitives and examples of other forms of synchronization: Condition variables

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

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

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

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

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

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

POSIX / System Programming

POSIX / System Programming POSIX / System Programming ECE 650 Methods and Tools for Software Eng. Guest lecture 2017 10 06 Carlos Moreno cmoreno@uwaterloo.ca E5-4111 2 Outline During today's lecture, we'll look at: Some of POSIX

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

Chapter 2 Processes and Threads

Chapter 2 Processes and Threads MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads The Process Model Figure 2-1. (a) Multiprogramming of four programs. (b) Conceptual model of four independent,

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

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

Dining Philosophers, Semaphores

Dining Philosophers, Semaphores CS 220: Introduction to Parallel Computing Dining Philosophers, Semaphores Lecture 27 Today s Schedule Dining Philosophers Semaphores Barriers Thread Safety 4/30/18 CS 220: Parallel Computing 2 Today s

More information

Synchronization: Advanced

Synchronization: Advanced Synchronization: Advanced CS 485G-006: Systems Programming Lecture 35: 27 Apr 2016 1 Enforcing Mutual Exclusion Question: How can we guarantee a safe trajectory? Answer: We must synchronize the execution

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

ANKARA UNIVERSITY COMPUTER ENGINEERING DEPARTMENT BLM334-COM334 PROJECT

ANKARA UNIVERSITY COMPUTER ENGINEERING DEPARTMENT BLM334-COM334 PROJECT ANKARA UNIVERSITY COMPUTER ENGINEERING DEPARTMENT BLM334-COM334 PROJECT Due date: 08.05.2013 Lab Hours You re expected to implement Producer-Consumer Problem that is described below. (Any form of cheating

More information

Concurrent Programming

Concurrent Programming Indian Institute of Science Bangalore, India भ रत य व ज ञ न स स थ न ब गल र, भ रत SE 292: High Performance Computing [3:0][Aug:2014] Concurrent Programming Yogesh Simmhan Adapted from Intro to Concurrent

More information

Operating systems. Lecture 12

Operating systems. Lecture 12 Operating systems. Lecture 12 Michał Goliński 2018-12-18 Introduction Recall Critical section problem Peterson s algorithm Synchronization primitives Mutexes Semaphores Plan for today Classical problems

More information

Semaphores Semaphores: A Definition

Semaphores Semaphores: A Definition 31 Semaphores As we know now, one needs both locks and condition variables to solve a broad range of relevant and interesting concurrency problems. One of the first people to realize this years ago was

More information

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008. CSC 4103 - Operating Systems Spring 2008 Lecture - XII Midterm Review Tevfik Ko!ar Louisiana State University March 4 th, 2008 1 I/O Structure After I/O starts, control returns to user program only upon

More information

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

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1 Synchronization and Semaphores Copyright : University of Illinois CS 241 Staff 1 Synchronization Primatives Counting Semaphores Permit a limited number of threads to execute a section of the code Binary

More information

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives. CS 5523 Operating Systems: Concurrency and Synchronization Thank Dr. Dakai Zhu and Dr. Palden Lama for providing their slides. Lecture Outline Problems with concurrent access to shared data Ø Race condition

More information

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio Fall 2017 1 Outline Inter-Process Communication (20) Threads

More information

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies

Processes The Process Model. Chapter 2 Processes and Threads. Process Termination. Process States (1) Process Hierarchies Chapter 2 Processes and Threads Processes The Process Model 2.1 Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling Multiprogramming of four programs Conceptual

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

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

CPS 310 first midterm exam, 10/6/2014

CPS 310 first midterm exam, 10/6/2014 CPS 310 first midterm exam, 10/6/2014 Your name please: Part 1. More fun with fork and exec* What is the output generated by this program? Please assume that each executed print statement completes, e.g.,

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

Resource management. Real-Time Systems. Resource management. Resource management

Resource management. Real-Time Systems. Resource management. Resource management Real-Time Systems Specification Implementation Verification Mutual exclusion is a general problem that exists at several levels in a real-time system. Shared resources internal to the the run-time system:

More information

Chapter 6 Concurrency: Deadlock and Starvation

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles Chapter 6 Concurrency: Deadlock and Starvation Seventh Edition By William Stallings Operating Systems: Internals and Design Principles When two trains

More information

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

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2 Plan Demos Project: Due» Demos following week (Wednesday during class time) Next Week» New phase of presentations» Deadlock, finish synchronization Course Progress:» History, Structure, Processes, Threads,

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

CS 261 Fall Mike Lam, Professor. Threads

CS 261 Fall Mike Lam, Professor. Threads CS 261 Fall 2017 Mike Lam, Professor Threads Parallel computing Goal: concurrent or parallel computing Take advantage of multiple hardware units to solve multiple problems simultaneously Motivations: Maintain

More information

Process Synchronization

Process Synchronization Process Synchronization Concurrent access to shared data in the data section of a multi-thread process, in the shared memory of multiple processes, or in a shared file Although every example in this chapter

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

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

Problem Set 2. CS347: Operating Systems

Problem Set 2. CS347: Operating Systems CS347: Operating Systems Problem Set 2 1. Consider a clinic with one doctor and a very large waiting room (of infinite capacity). Any patient entering the clinic will wait in the waiting room until the

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

Recitation 14: Proxy Lab Part 2

Recitation 14: Proxy Lab Part 2 Recitation 14: Proxy Lab Part 2 Instructor: TA(s) 1 Outline Proxylab Threading Threads and Synchronization 2 ProxyLab ProxyLab is due in 1 week. No grace days Late days allowed (-15%) Make sure to submit

More information

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

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective? CS 470 Spring 2019 POSIX Mike Lam, Professor Multithreading & Pthreads MIMD

More information

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

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology Process Synchronization: Semaphores CSSE 332 Operating Systems Rose-Hulman Institute of Technology Critical-section problem solution 1. Mutual Exclusion - If process Pi is executing in its critical section,

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

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