Process Synchronization (Part I)
|
|
- Jordan Stevenson
- 5 years ago
- Views:
Transcription
1 Process Synchronization (Part I) Amir H. Payberah Amirkabir University of Technology (Tehran Polytechnic) Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 1 / 44
2 Motivation Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 2 / 44
3 Background Processes can execute concurrently. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 3 / 44
4 Background Processes can execute concurrently. Concurrent access to shared data may result in data inconsistency. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 3 / 44
5 Background Processes can execute concurrently. Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 3 / 44
6 Producer-Consumer Problem Providing a solution to the producer-consumer problem that fills all the buffers. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44
7 Producer-Consumer Problem Providing a solution to the producer-consumer problem that fills all the buffers. Having an integer counter that keeps track of the number of full buffers. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44
8 Producer-Consumer Problem Providing a solution to the producer-consumer problem that fills all the buffers. Having an integer counter that keeps track of the number of full buffers. Initially, counter is set to 0. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44
9 Producer-Consumer Problem Providing a solution to the producer-consumer problem that fills all the buffers. Having an integer counter that keeps track of the number of full buffers. Initially, counter is set to 0. The producer produces a new buffer: increment the counter Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44
10 Producer-Consumer Problem Providing a solution to the producer-consumer problem that fills all the buffers. Having an integer counter that keeps track of the number of full buffers. Initially, counter is set to 0. The producer produces a new buffer: increment the counter The consumer consumes a buffer: decrement the counter Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 4 / 44
11 Producer Producer while (true) { /* produce an item in next produced */ while (counter == BUFFER_SIZE); /* do nothing */ buffer[in] = next_produced; in = (in + 1) % BUFFER_SIZE; } counter++; Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 5 / 44
12 Consumer Consumer while (true) { while (counter == 0); /* do nothing */ next_consumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; /* consume the item in next consumed */ } Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 6 / 44
13 Race Condition counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 7 / 44
14 Race Condition counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 counter-- could be implemented as register2 = counter register2 = register2-1 counter = register2 Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 7 / 44
15 Race Condition counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 counter-- could be implemented as register2 = counter register2 = register2-1 counter = register2 Consider this execution interleaving with count = 5 initially: S0: producer: register1 = counter: register1 = 5 S1: producer: register1 = register1 + 1: register1 = 6 S2: consumer: register2 = counter: register2 = 5 S3: consumer: register2 = register2-1: register2 = 4 S4: producer: counter = register1: counter = 6 S5: consumer: counter = register2: counter = 4 Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 7 / 44
16 The Critical-Section (CS) Problem Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 8 / 44
17 The Critical-Section Problem (1/2) Consider system of n processes {p 0, p 1,, p n 1 }. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 9 / 44
18 The Critical-Section Problem (1/2) Consider system of n processes {p 0, p 1,, p n 1 }. Each process has CS segment of code. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 9 / 44
19 The Critical-Section Problem (1/2) Consider system of n processes {p 0, p 1,, p n 1 }. Each process has CS segment of code. Process may be changing common variables, updating table, writing file, etc. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 9 / 44
20 The Critical-Section Problem (1/2) Consider system of n processes {p 0, p 1,, p n 1 }. Each process has CS segment of code. Process may be changing common variables, updating table, writing file, etc. When one process in CS, no other may be in its CS. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 9 / 44
21 The Critical-Section Problem (2/2) CS problem is to design protocol to solve this. Each process must ask permission to enter CS in entry section, may follow CS with exit section, then remainder section. General structure of process P i. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 10 / 44
22 CS Problem Solution Requirements (1/3) Mutual Exclusion: if process P i is executing in its CS, then no other processes can be executing in their CSs. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 11 / 44
23 CS Problem Solution Requirements (2/3) Progress: if no process is executing in its CS and there exist some processes that wish to enter their CS, then the selection of the processes that will enter the CS next cannot be postponed indefinitely. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 12 / 44
24 CS Problem Solution Requirements (3/3) Bounded Waiting: a bound must exist on the number of times that other processes are allowed to enter their CSs after a process has made a request to enter its CS and before that request is granted. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 13 / 44
25 CS Handling in OS Two approaches depending on if kernel is preemptive or nonpreemptive. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 14 / 44
26 CS Handling in OS Two approaches depending on if kernel is preemptive or nonpreemptive. Preemptive: allows preemption of process when running in kernel mode. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 14 / 44
27 CS Handling in OS Two approaches depending on if kernel is preemptive or nonpreemptive. Preemptive: allows preemption of process when running in kernel mode. Non-preemptive: runs until exits kernel mode, blocks, or voluntarily yields CPU. Essentially free of race conditions in kernel mode. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 14 / 44
28 Peterson s Solution Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 15 / 44
29 Peterson s Solution Two-process solution. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 16 / 44
30 Peterson s Solution Two-process solution. The two processes share two variables: int turn boolean flag[2] Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 16 / 44
31 Peterson s Solution Two-process solution. The two processes share two variables: int turn boolean flag[2] turn: indicates whose turn it is to enter the CS. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 16 / 44
32 Peterson s Solution Two-process solution. The two processes share two variables: int turn boolean flag[2] turn: indicates whose turn it is to enter the CS. flag: indicates if a process is ready to enter the CS, i.e., flag[i] = true implies that process P i is ready. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 16 / 44
33 Algorithm for Process P i Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 17 / 44
34 CS Requirements Provable that the three CS requirement are met: Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 18 / 44
35 CS Requirements Provable that the three CS requirement are met: 1 Mutual exclusion is preserved: P i enters CS only if: either flag[j] = false or turn = i Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 18 / 44
36 CS Requirements Provable that the three CS requirement are met: 1 Mutual exclusion is preserved: P i enters CS only if: either flag[j] = false or turn = i 2 Progress requirement is satisfied. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 18 / 44
37 CS Requirements Provable that the three CS requirement are met: 1 Mutual exclusion is preserved: P i enters CS only if: either flag[j] = false or turn = i 2 Progress requirement is satisfied. 3 Bounded-waiting requirement is met. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 18 / 44
38 Synchronization Hardware Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 19 / 44
39 Synchronization Hardware Many systems provide hardware support for implementing the CS code. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 20 / 44
40 Synchronization Hardware Many systems provide hardware support for implementing the CS code. Uniprocessors: could disable interrupts: running code without preemption. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 20 / 44
41 Synchronization Hardware Many systems provide hardware support for implementing the CS code. Uniprocessors: could disable interrupts: running code without preemption. Modern machines provide atomic hardware instructions: atomic = non-interruptible Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 20 / 44
42 Solution to CS Problem Using Locks Protecting CS via locks. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 21 / 44
43 Atomic Instructions test and set compare and swap Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 22 / 44
44 test and set Instruction Executed atomically. Returns the original value of passed parameter. Set the new value of passed parameter to true. boolean test_and_set(boolean *target) { boolean rv = *target; *target = true; return rv; } Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 23 / 44
45 Solution Using test and set Shared boolean variable lock, initialized to false. do { while (test_and_set(&lock)); /* do nothing */ /* critical section */ lock = false; /* remainder section */ } while (true); Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 24 / 44
46 compare and swap Instruction Executed atomically. Returns the original value of passed parameter value. Set the value the value of the passed parameter new value but only if value == expected. int compare_and_swap(int *value, int expected, int new_value) { int temp = *value; if (*value == expected) *value = new_value; } return temp; Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 25 / 44
47 Solution Using compare and swap Shared integer lock initialized to 0. do { while (compare_and_swap(&lock, 0, 1)!= 0); /* do nothing */ /* critical section */ lock = 0; /* remainder section */ } while (true); Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 26 / 44
48 compare and swap and compare and swap Problem Although these algorithms satisfy the mutual-exclusion requirement, they do not satisfy the bounded-waiting requirement. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 27 / 44
49 Mutex Locks Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 28 / 44
50 Mutex Locks (1/2) Previous solutions are complicated and generally inaccessible to application programmers. OS designers build software tools to solve CS problem. Simplest is mutex lock. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 29 / 44
51 Mutex Locks (2/2) Protect a CS by first acquire() a lock then release() the lock. Boolean variable indicating if lock is available or not. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 30 / 44
52 Mutex Locks (2/2) Protect a CS by first acquire() a lock then release() the lock. Boolean variable indicating if lock is available or not. Calls to acquire() and release() must be atomic. Usually implemented via hardware atomic instructions. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 30 / 44
53 Mutex Locks (2/2) Protect a CS by first acquire() a lock then release() the lock. Boolean variable indicating if lock is available or not. Calls to acquire() and release() must be atomic. Usually implemented via hardware atomic instructions. But this solution requires busy waiting. This lock therefore called a spinlock. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 30 / 44
54 acquire() and release() acquire() { while (!available); /* busy wait */ } available = false; release() { available = true; } Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 31 / 44
55 Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 32 / 44
56 Initializing Mutexes Mutexes are represented by the pthread mutex t object. /* define and initialize a mutex named mutex */ pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 33 / 44
57 Locking Mutexes pthread mutex lock() locks (acquires) a pthreads mutex. #include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *mutex); Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 34 / 44
58 Unlocking Mutexes pthread mutex unlock() unlocks (releases) a pthreads mutex. #include <pthread.h> int pthread_mutex_unlock(pthread_mutex_t *mutex); Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 35 / 44
59 Mutex Example static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int withdraw(struct account *account, int amount) { pthread_mutex_lock(&mutex); const int balance = account->balance; if (balance < amount) { pthread_mutex_unlock(&mutex); return -1; } account->balance = balance - amount; pthread_mutex_unlock(&mutex); disburse_money(amount); return 0; } Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 36 / 44
60 Condition Variable Mutex is very resource consuming since the thread would be continuously busy in this activity. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 37 / 44
61 Condition Variable Mutex is very resource consuming since the thread would be continuously busy in this activity. A condition variable is a way to achieve the same goal without polling. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 37 / 44
62 Condition Variable Mutex is very resource consuming since the thread would be continuously busy in this activity. A condition variable is a way to achieve the same goal without polling. A condition variable allows one thread to inform other threads about changes in the state of a shared variable. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 37 / 44
63 Waiting on Condition Variables pthread cond wait() must be called after pthread mutex lock() and before pthread mutex unlock(). pthread cond wait() release the mutex lock while it is waiting, so that pthread cond signal(), which is also called in the mutex should get access and can give signal to waiting thread to awake. #include <pthread.h> int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 38 / 44
64 Signaling on Condition Variables This routine is used to wakeup the thread which is waiting for any condition to occur. If in any case pthread cond signal() calls first before the execution of pthread cond wait() then it cannot awake the thread which is waiting. #include <pthread.h> int pthread_cond_signal(pthread_cond_t *cond); Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 39 / 44
65 Condition Variable Calling Format static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; s = pthread_mutex_lock(&mtx); while (/* Check that shared variable is not in state we want */ ) pthread_cond_wait(&cond, &mtx); /* Now shared variable is in desired state; do some work */ s = pthread_mutex_unlock(&mtx); Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 40 / 44
66 Condition Variable Example pthread_mutex_t count_lock; pthread_cond_t count_nonzero; unsigned count; decrement_count() { pthread_mutex_lock(&count_lock); while (count == 0) pthread_cond_wait(&count_nonzero, &count_lock); count = count - 1; pthread_mutex_unlock(&count_lock); } increment_count() { pthread_mutex_lock(&count_lock); if (count == 0) pthread_cond_signal(&count_nonzero); count = count + 1; pthread_mutex_unlock(&count_lock); } Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 41 / 44
67 Summary Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 42 / 44
68 Summary Access to shared data Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44
69 Summary Access to shared data Race condition Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44
70 Summary Access to shared data Race condition The critical-section problem Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44
71 Summary Access to shared data Race condition The critical-section problem Requirements: mutual-exclusion, progress, bounding waiting Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44
72 Summary Access to shared data Race condition The critical-section problem Requirements: mutual-exclusion, progress, bounding waiting Peterson solution Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44
73 Summary Access to shared data Race condition The critical-section problem Requirements: mutual-exclusion, progress, bounding waiting Peterson solution Synchronization hardware Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44
74 Summary Access to shared data Race condition The critical-section problem Requirements: mutual-exclusion, progress, bounding waiting Peterson solution Synchronization hardware Mutex lock and condition variable Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 43 / 44
75 Questions? Acknowledgements Some slides were derived from Avi Silberschatz slides. Amir H. Payberah (Tehran Polytechnic) Process Synchronization 1393/7/14 44 / 44
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 informationChapter 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 informationProcess 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 informationChapter 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 informationCS370 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 informationLesson 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 informationChapter 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 informationChapter 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 informationProcess 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 informationChapter 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 informationSynchronization 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 informationChapter 5: Process Synchronization
Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013 Operating System Concepts 9th Edition Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution
More informationChapter 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 informationCS420: 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 informationChapter 5: Process Synchronization
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 informationDept. of CSE, York Univ. 1
EECS 3221.3 Operating System Fundamentals No.5 Process Synchronization(1) Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University Background: cooperating processes with shared
More informationSynchronization 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 informationChapter 5: Process Synchronization
Chapter 5: Process Synchronization Silberschatz, Galvin and Gagne 2013! Chapter 5: Process Synchronization Background" The Critical-Section Problem" Petersons Solution" Synchronization Hardware" Mutex
More informationCS370 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 informationOperating 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 informationChapter 7: Process Synchronization!
Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors 7.1 Background Concurrent access to shared
More informationProcess Synchronization
Process Synchronization Daniel Mosse (Slides are from Silberschatz, Galvin and Gagne 2013 and Sherif Khattab) Chapter 5: Process Synchronization Background The Critical-Section Problem Peterson s Solution
More informationProcess/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 informationSynchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution
Race Condition Synchronization CSCI 315 Operating Systems Design Department of Computer Science A race occurs when the correctness of a program depends on one thread reaching point x in its control flow
More informationModule 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 informationChapter 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 informationProcess/Thread Synchronization
CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov March 1, 2011 The image cannot be displayed. Your computer may not have enough memory to open the image,
More informationSynchronization Spinlocks - Semaphores
CS 4410 Operating Systems Synchronization Spinlocks - Semaphores Summer 2013 Cornell University 1 Today How can I synchronize the execution of multiple threads of the same process? Example Race condition
More informationChapter 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 informationChapter 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 informationCS370 Operating Systems
CS370 Operating Systems Colorado State University Yashwant K Malaiya Spring 1018 L10 Synchronization Slides based on Text by Silberschatz, Galvin, Gagne Various sources 1 1 FAQ Development project: You
More informationChapter 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 informationModule 6: Process Synchronization
Module 6: Process Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic
More informationChapter 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 informationChapter 7: Process Synchronization. Background. Illustration
Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris
More informationChapter 6: Process Synchronization
Module 6: Process Synchronization Chapter 6: Process Synchronization Background! The Critical-Section Problem! Peterson s Solution! Synchronization Hardware! Semaphores! Classic Problems of Synchronization!
More informationSynchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization
CS341: Operating System Lect20 : 16 th Sept 2014 Dr. A. Sahu Dept of Comp. Sc. & Engg. Indian Institute of Technology Guwahati Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait,
More informationProcess Synchronization
Process Synchronization Organized By: Vinay Arora V.A. Disclaimer This is NOT A COPYRIGHT MATERIAL Content has been taken mainly from the following books: Operating Systems Concepts By Silberschatz & Galvin,
More informationChapter 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 informationProcess Synchronization
Process Synchronization Chapter 6 2015 Prof. Amr El-Kadi Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly
More informationCHAPTER 6: PROCESS SYNCHRONIZATION
CHAPTER 6: PROCESS SYNCHRONIZATION The slides do not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams. TOPICS Background
More informationProcess Synchronization
Chapter 7 Process Synchronization 1 Chapter s Content Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors 2 Background
More informationProcess Synchronization
CS307 Process Synchronization Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2018 Background Concurrent access to shared data may result in data inconsistency
More informationProcess 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 informationChapter 7: Process Synchronization. Background
Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Critical Regions Monitors Synchronization in Solaris
More informationSynchronization Principles I
CSC 256/456: Operating Systems Synchronization Principles I John Criswell University of Rochester 1 Synchronization Principles Background Concurrent access to shared data may result in data inconsistency.
More informationReal-Time Operating Systems M. 5. Process Synchronization
Real-Time Operating Systems M 5. Process Synchronization Notice The course material includes slides downloaded from: http://codex.cs.yale.edu/avi/os-book/ and (slides by Silberschatz, Galvin, and Gagne,
More informationChapter 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 informationCOP 4225 Advanced Unix Programming. Synchronization. Chi Zhang
COP 4225 Advanced Unix Programming Synchronization Chi Zhang czhang@cs.fiu.edu 1 Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating
More informationProcess 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 informationBackground. 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 informationIV. 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 informationChapter 6 Synchronization
Chapter 6 Synchronization Da-Wei Chang CSIE.NCKU Source: Abraham Silberschatz, Peter B. Galvin, and Greg Gagne, "Operating System Concepts", 9th Edition, Wiley. 1 Outline Background The Critical-Section
More informationCSCI 447 Operating Systems Filip Jagodzinski
Filip Jagodzinski Announcements Reading Task Should take 30 minutes-ish maximum Homework 2 Book questions including two custom ones will be posted to the course website today Programming tasks will be
More informationCSE Opera,ng System Principles
CSE 30341 Opera,ng System Principles Synchroniza2on Overview Background The Cri,cal-Sec,on Problem Peterson s Solu,on Synchroniza,on Hardware Mutex Locks Semaphores Classic Problems of Synchroniza,on Monitors
More informationMaximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Thread Scheduling Operating Systems Examples Java Thread Scheduling Algorithm Evaluation CPU
More informationRecap: 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 informationCSE 4/521 Introduction to Operating Systems
CSE 4/521 Introduction to Operating Systems Lecture 7 Process Synchronization II (Classic Problems of Synchronization, Synchronization Examples) Summer 2018 Overview Objective: 1. To examine several classical
More informationLecture 3: Synchronization & Deadlocks
Lecture 3: Synchronization & Deadlocks Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating
More informationProcess Coordination
Process Coordination Why is it needed? Processes may need to share data More than one process reading/writing the same data (a shared file, a database record, ) Output of one process being used by another
More informationBackground. 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 informationSynchronization. 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 informationPart II Process Management Chapter 6: Process Synchronization
Part II Process Management Chapter 6: Process Synchronization 1 Process Synchronization Why is synchronization needed? Race Conditions Critical Sections Pure Software Solutions Hardware Support Semaphores
More informationChapter 6 Process Synchronization
Chapter 6 Process Synchronization Cooperating Process process that can affect or be affected by other processes directly share a logical address space (threads) be allowed to share data via files or messages
More informationUNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01
1 UNIT 2 Basic Concepts of CPU Scheduling UNIT -02/Lecture 01 Process Concept An operating system executes a variety of programs: **Batch system jobs **Time-shared systems user programs or tasks **Textbook
More informationProcess 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 informationSynchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han Announcements HW #3 is coming, due Friday Feb. 25, a week+ from now PA #2 is coming, assigned about next Tuesday Midterm is tentatively
More informationPRINCIPLES OF OPERATING SYSTEMS
Department of Computer Science, University of Calgary PRINCIPLES OF OPERATING SYSTEMS Tutorial 9/10: Concurrency CPSC 457, Spring 2015 June 1-2, 2015 Announcement Midterm exam is rescheduled. The exam
More informationProcess Synchronization - I
CSE 421/521 - Operating Systems Fall 2013 Lecture - VIII Process Synchronization - I Tevfik Koşar University at uffalo September 26th, 2013 1 Roadmap Process Synchronization Race Conditions Critical-Section
More informationChapter 6: Synchronization
Chapter 6: Synchronization Module 6: Synchronization Background The Critical-Section Problem Peterson s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization
More informationCS 153 Lab6. Kishore Kumar Pusukuri
Outline Mutex vs Condition Variables Unlocking and locking mutex leads spinning or polling, wastes CPU time. We could sleep for some amount of time, but we do not know how long to sleep. A mutex is for
More informationCS370 Operating Systems Midterm Review
CS370 Operating Systems Midterm Review Yashwant K Malaiya Fall 2015 Slides based on Text by Silberschatz, Galvin, Gagne 1 1 What is an Operating System? An OS is a program that acts an intermediary between
More informationCS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019
CS370 Operating Systems Midterm Review Yashwant K Malaiya Spring 2019 1 1 Computer System Structures Computer System Operation Stack for calling functions (subroutines) I/O Structure: polling, interrupts,
More informationWhat 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 informationSynchronization Mechanisms
Synchronization Mechanisms CSCI 4061 Introduction to Operating Systems Instructor: Abhishek Chandra Mutex Locks Enforce protection and mutual exclusion Condition variables Allow atomic checking of conditions
More informationSynchronization 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 informationSynchronization 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 informationThread. 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 informationCSci 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 informationUNIT II PROCESS MANAGEMENT 9
UNIT II PROCESS MANAGEMENT 9 Processes-Process Concept, Process Scheduling, Operations on Processes, Interprocess Communication; Threads- Overview, Multicore Programming, Multithreading Models; Windows
More informationLocks and semaphores. Johan Montelius KTH
Locks and semaphores Johan Montelius KTH 2017 1 / 41 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 informationLocks. 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 informationProcess 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 informationConcurrency: Mutual Exclusion and
Concurrency: Mutual Exclusion and Synchronization 1 Needs of Processes Allocation of processor time Allocation and sharing resources Communication among processes Synchronization of multiple processes
More informationLecture 5: Inter-process Communication and Synchronization
Lecture 5: Inter-process Communication and Synchronization Real-Time CPU Scheduling Periodic processes require the CPU at specified intervals (periods) p is the duration of the period d is the deadline
More informationRoadmap. Shared Variables: count=0, buffer[] Producer: Background. Consumer: while (1) { Race Condition. Race Condition.
CSE / - Operating Systems Fall 0 Lecture - VIII Process Synchronization - I Tevfik Koşar Roadmap Process Synchronization s Critical-Section Problem Solutions to Critical Section Different Implementations
More informationSynchronization COMPSCI 386
Synchronization COMPSCI 386 Obvious? // push an item onto the stack while (top == SIZE) ; stack[top++] = item; // pop an item off the stack while (top == 0) ; item = stack[top--]; PRODUCER CONSUMER Suppose
More informationChapter 5: Synchronization 1
1 Start of Lecture: January 25, 2014 2 Reminders Assignment 1 is due this Friday at 6:00 p.m. Couple comments about Exercise 1: Thought questions: be honest and sincere about questions you have while reading;
More informationProcess 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 informationChapter 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 informationData 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 informationUNIT-II PROCESS SYNCHRONIZATION
THREADS: OVERVIEW: A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. It shares with other threads belonging to the same process its
More informationMutual Exclusion and Synchronization
Mutual Exclusion and Synchronization Concurrency Defined Single processor multiprogramming system Interleaving of processes Multiprocessor systems Processes run in parallel on different processors Interleaving
More informationProcess Synchronization
Process Synchronization Reading: Silberschatz chapter 6 Additional Reading: Stallings chapter 5 EEL 358 1 Outline Concurrency Competing and Cooperating Processes The Critical-Section Problem Fundamental
More informationAnnouncements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)
Office hours Announcements W office hour will be 10-11 not 11-12 starting next week Reading Chapter 7 (this whole week) 1 Problems with the Producer-Consumer Shared Memory Solution Consider the three address
More informationCS3733: 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 informationIntroduction 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 informationLocks 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 informationPart III Synchronization Software and Hardware Solutions
Part III Synchronization Software and Hardware Solutions Spring 2018 Computers are useless. They can only give answers. 1 Pablo Picasso Software Solutions for Two Processes Suppose we have two processes
More informationOperating Systems Antonio Vivace revision 4 Licensed under GPLv3
Operating Systems Antonio Vivace - 2016 revision 4 Licensed under GPLv3 Process Synchronization Background A cooperating process can share directly a logical address space (code, data) or share data through
More information