CSCI 447 Operating Systems Filip Jagodzinski

Size: px
Start display at page:

Download "CSCI 447 Operating Systems Filip Jagodzinski"

Transcription

1 Filip Jagodzinski

2 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 posted after Friday s lab Lab 2 This Friday (prelude to Homework 2)

3 Review Two processes are independent if the write set of each is disjoint from both the read and write sets of the other

4 Review Two processes are independent if the write set of each is disjoint from both the read and write sets of the other a b c d e f P a P b Q: Are P a and P b independent?

5 Review Two processes are independent if the write set of each is disjoint from both the read and write sets of the other a b c d e f Write set Read set P a d d, a P b b P a P b Q: Are P a and P b independent? Q: Is P a s write set disjoint from the read and write sets of P b?

6 Review Two processes are independent if the write set of each is disjoint from both the read and write sets of the other a b c d e f Write set Read set P a d d, a P b b P a P b Q: Are P a and P b independent? Q: Is P a s write set disjoint from the read and write sets of P b?

7 Review Two processes are independent if the write set of each is disjoint from both the read and write sets of the other a b c d e f Write set Read set P a d d, a P b b P a P b Q: Are P a and P b independent? Yes Q: Is P a s write set disjoint from the read and write sets of P b? Q: Is P b s write set disjoint from the read and write sets of P a? Yes Yes

8 Review History : a possible ordering of instruction invocations among multiple processes executing on a multi processor computer

9 Review History : a possible ordering of instruction invocations among multiple processes executing on a multi processor computer One way to specify a history is using < i1 < i2 < i3

10 Review History : a possible ordering of instruction invocations among multiple processes executing on a multi processor computer One way to specify a history is using < i1 < i2 < i3 Unless otherwise stated, instructions are assumed to be executed atomically*, meaning, allowing to proceed to completion. A more technical definition of atomic is : a guarantee that an operation will complete without interruption or signal calls

11 Review Single-threaded process vs multi-threaded processes memory memory registers stack registers registers registers stack stack stack

12 Review Single-threaded process vs multi-threaded processes memory memory registers stack registers registers registers stack stack stack Advantages Responsiveness Resource Sharing Economy Scalability Disadvantages/Challenges Identifying tasks Balance Data splitting Data dependency

13 Review Single-threaded process vs multi-threaded processes memory memory registers stack registers registers registers stack stack stack Advantages Responsiveness Resource Sharing Economy Scalability Disadvantages/Challenges Identifying tasks Balance Data splitting Data dependency If 4 CPUs are recruited to sum the entries of the matrix, and each CPU is responsible for a unique color region, then no two CPUs contend for the same shared piece of information

14 Review Single-threaded process vs multi-threaded processes memory memory registers stack registers registers registers stack stack stack If 4 CPUs are recruited to calculate the product of two matrices x Advantages Responsiveness Resource Sharing Economy Scalability Disadvantages/Challenges Identifying tasks Balance Data splitting Data dependency????

15 Review Single-threaded process vs multi-threaded processes memory memory registers stack registers registers registers stack stack stack If 4 CPUs are recruited to calculate the product of two matrices x Advantages Responsiveness Resource Sharing Economy Scalability Disadvantages/Challenges Identifying tasks Balance Data splitting Data dependency 2x6 + 3x8 = 36 Q: Which data does the red CPU rely on? 36

16 Review Single-threaded process vs multi-threaded processes memory memory registers stack registers registers registers stack stack stack If 4 CPUs are recruited to calculate the product of two matrices x Q: Which data does the blue CPU rely on? 2x7 + 3x9 = 41 Advantages Responsiveness Resource Sharing Economy Scalability Disadvantages/Challenges Identifying tasks Balance Data splitting Data dependency 36 41

17 Review Single-threaded process vs multi-threaded processes memory memory registers stack registers registers registers stack stack stack If 4 CPUs are recruited to calculate the product of two matrices x x6 + 3x8 = 36 2x7 + 3x9 = 41 Advantages Responsiveness Resource Sharing Economy Scalability Disadvantages/Challenges Identifying tasks Balance Data splitting Data dependency Both the red & blue CPUs need access to the 2 and 3, so one CPU must wait

18 Review Single-threaded process vs multi-threaded processes memory memory registers stack registers registers registers stack stack stack If 4 CPUs are recruited to calculate the product of two matrices x x6 + 3x8 = 36 2x7 + 3x9 = 41 Advantages Responsiveness Resource Sharing Economy Scalability Disadvantages/Challenges Identifying tasks Balance Data splitting Data dependency 2,3

19 Review m threads executing n atomic actions each m n possible histories 3 1? A1 B1 C1

20 Review m threads executing n atomic actions each m n possible histories A1 B1 C1 A1 B1 C1 A1 C1 B1 B1 C1 A1 B1 A1 C1 C1 A1 B1 C1 B1 A1

21 Review m threads executing n atomic actions each m n possible histories ? A1 A2 B1 B2

22 Review m threads executing n atomic actions each m n possible histories ? A1 A2 B1 B2 A1 A2 B1 B2 Identify the instructions

23 Review m threads executing n atomic actions each m n possible histories ? A1 A2 B1 B2 A1 A2 B1 B2 Next enumerate all histories regardless if the A1 < A2 condition, and B1 < B2 conditions are met A1 A2 B1 B2 A1 A2 B2 B1 A1 B1 B2 A2 A1 B1 A2 B2 A1 B2 A2 B1 A1 B2 B1 A2 A2 A1 B1 B2 A2 A1 B2 B1 A2 B1 B2 A1 A2 B1 A1 B2 A2 B2 A1 B1 A2 B2 B1 A1 B1 A2 A1 B2 B1 A2 B2 A1 B1 A1 B2 A2 B1 A1 A2 B2 B1 B2 A2 A1 B1 B2 A1 A2 B2 A2 B1 A1 B2 A2 A1 B1 B2 B1 A1 A2 B2 B1 A2 A1 B2 A1 A2 B1 B2 A1 B1 A2

24 Review m threads executing n atomic actions each m n possible histories ? A1 A2 B1 B2 A1 A2 B1 B2 Then prune based on whether the A1 < A2 and B1 < B2 conditions are met A1 A2 B1 B2 A1 A2 B2 B1 A1 B1 B2 A2 A1 B1 A2 B2 A1 B2 A2 B1 A1 B2 B1 A2 A2 A1 B1 B2 A2 A1 B2 B1 A2 B1 B2 A1 A2 B1 A1 B2 A2 B2 A1 B1 A2 B2 B1 A1 B1 A2 A1 B2 B1 A2 B2 A1 B1 A1 B2 A2 B1 A1 A2 B2 B1 B2 A2 A1 B1 B2 A1 A2 B2 A2 B1 A1 B2 A2 A1 B1 B2 B1 A1 A2 B2 B1 A2 A1 B2 A1 A2 B1 B2 A1 B1 A2

25 Review m threads executing n atomic actions each m n possible histories A1 A2 B1 B2 A1 A2 B1 B2 Then prune based on whether the A1 < A2 and B1 < B2 conditions are met A1 A2 B1 B2 A1 A2 B2 B1 A1 B1 B2 A2 A1 B1 A2 B2 A1 B2 A2 B1 A1 B2 B1 A2 A2 A1 B1 B2 A2 A1 B2 B1 A2 B1 B2 A1 A2 B1 A1 B2 A2 B2 A1 B1 A2 B2 B1 A1 B1 A2 A1 B2 B1 A2 B2 A1 B1 A1 B2 A2 B1 A1 A2 B2 B1 B2 A2 A1 B1 B2 A1 A2 B2 A2 B1 A1 B2 A2 A1 B1 B2 B1 A1 A2 B2 B1 A2 A1 B2 A1 A2 B1 B2 A1 B1 A2

26 Review m threads executing n atomic actions each m n possible histories A1 A2 B1 B2 A1 A2 B1 B2 Then prune based on whether the A1 < A2 and B1 < B2 conditions are met A1 A2 B1 B2 A1 A2 B2 B1 A1 B1 B2 A2 A1 B1 A2 B2 A1 B2 A2 B1 A1 B2 B1 A2 A2 A1 B1 B2 A2 A1 B2 B1 A2 B1 B2 A1 A2 B1 A1 B2 A2 B2 A1 B1 A2 B2 B1 A1 B1 A2 A1 B2 B1 A2 B2 A1 B1 A1 B2 A2 B1 A1 A2 B2 B1 B2 A2 A1 B1 B2 A1 A2 We finished with : Is there a formula for calculating the count of possible histories among m threads with n instructions each. B2 A2 B1 A1 B2 A2 A1 B1 B2 B1 A1 A2 B2 B1 A2 A1 B2 A1 A2 B1 B2 A1 B1 A2

27 Review m threads executing n atomic actions each m n possible histories A1 A2 B1 B2 A1 A2 B1 B2 Then prune based on whether the A1 < A2 and B1 < B2 conditions are met A1 A2 B1 B2 A1 A2 B2 B1 A1 B1 B2 A2 A1 B1 A2 B2 A1 B2 A2 B1 A1 B2 B1 A2 A2 A1 B1 B2 A2 A1 B2 B1 A2 B1 B2 A1 A2 B1 A1 B2 A2 B2 A1 B1 A2 B2 B1 A1 B1 A2 A1 B2 B1 A2 B2 A1 B1 A1 B2 A2 B1 A1 A2 B2 B1 B2 A2 A1 B1 B2 A1 A2 We finished with : Is there a formula for calculating the count of possible histories among m threads with n instructions each. B2 A2 B1 A1 B2 A2 A1 B1 B2 B1 A1 A2 B2 B1 A2 A1 B2 A1 A2 B1 B2 A1 B1 A2

28 In-class exercise

29 In-class exercise We finished with : Is there a formula for calculating the count of possible histories among m threads with n instructions each. (mn)! (n!) m

30 Review User threads

31 Review User threads Be sure you know both the terminology (many-to-one, one-toone, and many-to-many), but also can list drawbacks/limitations and advantages of each approach

32 Today Concurrency versus Parallel Finish up Threads Chapter 5, Synchronization Peterson s Mutex

33 Concurrency versus parallel Q: Are concurrent and parallel synonyms? Q: Is it possible for a system to be executing something concurrently, but without parallelism?

34 Concurrency versus parallel Q: Are concurrent and parallel synonyms? Q: Is it possible for a system to be executing something concurrently, but without parallelism? The two terms are often used interchangeably even in technical manuscripts but there are subtle differences

35 Concurrency versus parallel Q: Are concurrent and parallel synonyms? Q: Is it possible for a system to be executing something concurrently, but without parallelism? The two terms are often used interchangeably even in technical manuscripts but there are subtle differences Concurrent means that two tasks are being executed (from the perspective of the user) at the same time regardless whether one or more CPUs are being used and the ORDER of execution is indeterminate (many possible histories) the OS selects scheduling, and one process may be waiting on another Parallel means that at least two processes are actively (nobody is waiting) working at the same time

36 Concurrency versus parallel Q: Are concurrent and parallel synonyms? Q: Is it possible for a system to be executing something concurrently, but without parallelism? The two terms are often used interchangeably even in technical manuscripts but there are subtle differences Concurrent means that two tasks are being executed (from the perspective of the user) at the same time regardless whether one or more CPUs are being used and the ORDER of execution is indeterminate (many possible histories) the OS selects scheduling, and one process may be waiting on another Parallel means that at least two processes are actively (nobody is waiting) working at the same time

37 Concurrency versus parallel Q: Are concurrent and parallel synonyms? Q: Is it possible for a system to be executing something concurrently, but without parallelism? cpu0 t1 t2 t3 t1 t2 t3 On a single CPU machine (that is NOT superscalar), concurrency is possible, but parallelism is not cpu0 cpu1 t1 t3 t1 t3 t1 t3 t2 t4 t2 t4 t2 t4 On a multiple CPU machine, concurrency AND parallelism is possible

38 Threads When designing multithreaded programs, special circumstance arise

39 Threads When designing multithreaded programs, special circumstance arise fork(): create a separate, duplicate process exec(): create a new process In a single threaded program, it is clear what happens when either of the above system calls are made But when there are multiple threads for a single process

40 Threads When designing multithreaded programs, special circumstance arise fork(): create a separate, duplicate process exec(): create a new process In a single threaded program, it is clear what happens when either of the above system calls are made But when there are multiple threads for a single process If a thread calls fork(), does the new process duplicate all threads, or does the new process run on a single thread? Some systems have multiple fork() routines, one that duplicates all threads, and another that duplicates the single thread that issued the fork()

41 Threads When designing multithreaded programs, special circumstance arise Q: What are the mechanisms by which a thread is killed (cancelled), perhaps mid-execution? Task : Describe a multi-thread programming scenario in which a thread might need to be terminated prior to completion

42 Threads When designing multithreaded programs, special circumstance arise Q: What are the mechanisms by which a thread is killed (cancelled), perhaps mid-execution? Task : Describe a multi-thread programming scenario in which a thread might need to be terminated prior to completion The thread to be terminated is referred to as a target thread

43 Threads Asynchronous cancellation Deferred cancellation

44 Threads Asynchronous cancellation Deferred cancellation A thread immediately terminates the target thread The target thread periodically checks in to find out if it should be terminated; if so, it does so in an orderly fashion Q: Why might asynchronous thread cancellation be problematic?

45 Threads Asynchronous cancellation Deferred cancellation A thread immediately terminates the target thread The target thread periodically checks in to find out if it should be terminated; if so, it does so in an orderly fashion Q: Why might asynchronous thread cancellation be problematic? Threads take on distinct roles in a computation/process, and information might need to be shared among threads. buffer thread 1 thread 2

46 Threads Asynchronous cancellation Deferred cancellation A thread immediately terminates the target thread The target thread periodically checks in to find out if it should be terminated; if so, it does so in an orderly fashion Q: Why might asynchronous thread cancellation be problematic? Threads take on distinct roles in a computation/process, and information might need to be shared among threads. Terminating a thread prematurely might impact the thread that is killed, AND the thread that was in the process of communication with the killed thread. buffer thread 1 thread 2

47 Threads Asynchronous cancellation Deferred cancellation A thread immediately terminates the target thread The target thread periodically checks in to find out if it should be terminated; if so, it does so in an orderly fashion In deferred cancellation, a target thread must first check whether the conditions for termination have been satisfied for example, via the use of a flag If the flag is set to yes to specify still writing to shared resource, and the thread is targeted for termination, the thread holds off until buffer thread 1 thread 2 iswriting : 0/1

48 Threads Asynchronous cancellation Deferred cancellation A thread immediately terminates the target thread The target thread periodically checks in to find out if it should be terminated; if so, it does so in an orderly fashion In deferred cancellation, a target thread must first check whether the conditions for termination have been satisfied for example, via the use of a flag If the flag is set to yes to specify still writing to shared resource, and the thread is targeted for termination, the thread holds off until buffer thread 1 thread 2 iswriting : 0/1

49 Synchronization With the use of threads and possible large counts of histories what can go wrong?

50 Synchronization With the use of threads and possible large counts of histories what can go wrong? Let s take a closer look at the producer / consumer problem we ve already briefly seen Q: What is the use of the in, and out variables? in = out = 0; while (true) { item = produce_item() while (((in + 1) % BUFFER_SIZE == out) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; } while (true) { while (in == out) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; consume_item(item) }

51 Synchronization With the use of threads and possible large counts of histories what can go wrong? Let s take a closer look at the producer / consumer problem we ve already briefly seen Q: What is the use of the in, and out variables? in = out = 0; while (true) { item = produce_item() while (((in + 1) % BUFFER_SIZE == out) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; } while (true) { while (in == out) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; consume_item(item) } The producer checks the value of in (in combination with modulus and the SIZE of the buffer), to see if something has already been produced which hasn t yet been consumed if the buffer is not full, an item is placed into the buffer

52 Synchronization With the use of threads and possible large counts of histories what can go wrong? Let s take a closer look at the producer / consumer problem we ve already briefly seen Q: What is the use of the in, and out variables? in = out = 0; while (true) { item = produce_item() while (((in + 1) % BUFFER_SIZE == out) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; } while (true) { while (in == out) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; consume_item(item) } The producer checks the value of in (in combination with modulus and the SIZE of the buffer), to see if something has already been produced which hasn t yet been consumed if the buffer is not full, an item is placed into the buffer

53 Synchronization With the use of threads and possible large counts of histories what can go wrong? Let s take a closer look at the producer / consumer problem we ve already briefly seen Q: What is the use of the in, and out variables? in = out = 0; while (true) { item = produce_item() while (((in + 1) % BUFFER_SIZE == out) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; } while (true) { while (in == out) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; consume_item(item) } After which the producer updates the value of in, so that on subsequent checks of the variable in, the produce will spin (do nothing)

54 Synchronization With the use of threads and possible large counts of histories what can go wrong? Let s take a closer look at the producer / consumer problem we ve already briefly seen Q: What is the use of the in, and out variables? in = out = 0; while (true) { item = produce_item() while (((in + 1) % BUFFER_SIZE == out) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; } while (true) { while (in == out) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; consume_item(item) } After which the producer updates the value of in, so that on subsequent checks of the variable in, the producer will spin (do nothing)

55 Synchronization With the use of threads and possible large counts of histories what can go wrong? Let s take a closer look at the producer / consumer problem we ve already briefly seen Q: What is the use of the in, and out variables? in = out = 0; while (true) { item = produce_item() while (((in + 1) % BUFFER_SIZE == out) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; } while (true) { while (in == out) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; consume_item(item) } The consumer, in the meantime, checks repeatedly if the value of in is equal to out. If yes, that means that as many items have been produced as have been consumed, in which case the consumer waits

56 Synchronization With the use of threads and possible large counts of histories what can go wrong? Let s take a closer look at the producer / consumer problem we ve already briefly seen Q: What is the use of the in, and out variables? in = out = 0; while (true) { item = produce_item() while (((in + 1) % BUFFER_SIZE == out) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; } while (true) { while (in == out) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; consume_item(item) } If in!= out, that means that more items have been produced than consumed, in which case the consumer retrieves the value in the buffer, and updates the value of out.

57 Synchronization With the use of threads and possible large counts of histories what can go wrong? A shortcoming of this approach is that the buffer can at most hold BUFFER_SIZE -1 elements The buffer is not fully utilized a consequence of using modulus in = out = 0; while (true) { item = produce_item() while (((in + 1) % BUFFER_SIZE == out) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; } while (true) { while (in == out) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; consume_item(item) }

58 Synchronization With the use of threads and possible large counts of histories what can go wrong? This can be fixed by introducing a variable, counter, which is incremented each time something is produced, and decremented, each time something is consumed in = out = 0; while (true) { item = produce_item() while (counter == BUFFER_SIZE) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; counter++; } while (true) { while (counter == 0) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; consume_item(item) } The counter variable now mediates adding to / removing from the buffer, which can be fully utilized

59 Synchronization With the use of threads and possible large counts of histories what can go wrong? The issue now is the increment and decrement of counter seems harmless But remember, these two threads are running concurrently (at the same time) Q: What could go wrong? in = out = 0; while (true) { item = produce_item() while (counter == BUFFER_SIZE) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; counter++; } while (true) { while (counter == 0) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; consume_item(item) }

60 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1

61 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1 a1: load count a2: add 1 a3: store count Register/ALU view b1: load count b2: subtract 1 b3: store count Remember that executing an instruction involves multiple architecture-level steps, including loading registers, loading ALUs, executing ALUs, fetching results from ALU, etc.

62 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1 a1: load count a2: add 1 a3: store count Register/ALU view b1: load count b2: subtract 1 b3: store count Remember that executing an instruction involves multiple architecture-level steps, including loading registers, loading ALUs, executing ALUs, fetching results from ALU, etc. Assume initial value of count = 4 What are a few of the possible instruction histories for Threads A and B?

63 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1 a1: load count a2: add 1 a3: store count Register/ALU view b1: load count b2: subtract 1 b3: store count Remember that executing an instruction involves multiple architecture-level steps, including loading registers, loading ALUs, executing ALUs, fetching results from ALU, etc. Assume initial value of count = 4 Notice that all of the instructions in both threads are executed sequentially... a1<a2<a3, and b1<b2<b3 What are the final values of count for these 4 histories? a1 < b1 < a2 < a3 < b2 < b3 a1 < a2 < b1 < b2 < b3 < a3 a1 < a2 < a3 < b1 < b2 < b3 b1 < b2 < a1 < b3 < a2 < a3

64 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1 a1: load count a2: add 1 a3: store count Register/ALU view b1: load count b2: subtract 1 b3: store count Remember that executing an instruction involves multiple architecture-level steps, including loading registers, loading ALUs, executing ALUs, fetching results from ALU, etc. Assume initial value of count = 4 Make sure you understand why this happened Task : Explain in your own words what caused this to happen a1 < b1 < a2 < a3 < b2 < b3 count = 3 a1 < a2 < a3 < b1 < b2 < b3 count = 4 a1 < a2 < b1 < b2 < b3 < a3 count = 5 b1 < b2 < a1 < b3 < a2 < a3 count = 5

65 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1 a1: load count a2: add 1 a3: store count Register/ALU view b1: load count b2: subtract 1 b3: store count Remember that executing an instruction involves multiple architecture-level steps, including loading registers, loading ALUs, executing ALUs, fetching results from ALU, etc. Assume initial value of count = 4 In most of these histories, both a1 and b1 fetched the value of count BEFORE the other process wrote back to it a1 < b1 < a2 < a3 < b2 < b3 count = 3 a1 < a2 < a3 < b1 < b2 < b3 count = 4 a1 < a2 < b1 < b2 < b3 < a3 count = 5 b1 < b2 < a1 < b3 < a2 < a3 count = 5

66 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1 a1: load count a2: add 1 a3: store count Register/ALU view b1: load count b2: subtract 1 b3: store count Remember that executing an instruction involves multiple architecture-level steps, including loading registers, loading ALUs, executing ALUs, fetching results from ALU, etc. Assume initial value of count = 4 Which is the desired final value of count? a1 < b1 < a2 < a3 < b2 < b3 count = 3 a1 < a2 < a3 < b1 < b2 < b3 count = 4 a1 < a2 < b1 < b2 < b3 < a3 count = 5 b1 < b2 < a1 < b3 < a2 < a3 count = 5

67 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1 a1: load count a2: add 1 a3: store count Register/ALU view b1: load count b2: subtract 1 b3: store count Remember that executing an instruction involves multiple architecture-level steps, including loading registers, loading ALUs, executing ALUs, fetching results from ALU, etc. Assume initial value of count = 4 What is the one property of the desired history that is different from the non-desirable histories? a1 < b1 < a2 < a3 < b2 < b3 count = 3 a1 < a2 < a3 < b1 < b2 < b3 count = 4 a1 < a2 < b1 < b2 < b3 < a3 count = 5 b1 < b2 < a1 < b3 < a2 < a3 count = 5

68 Synchronization Thread A Thread B i1: count = count + 1 i2: count = count - 1 a1: load count a2: add 1 a3: store count Register/ALU view b1: load count b2: subtract 1 b3: store count Remember that executing an instruction involves multiple architecture-level steps, including loading registers, loading ALUs, executing ALUs, fetching results from ALU, etc. Assume initial value of count = 4 What is the one property of the desired history that is different from the non-desirable histories? a1 < b1 < a2 < a3 < b2 < b3 count = 3 a1 < a2 < a3 < b1 < b2 < b3 count = 4 a1 < a2 < b1 < b2 < b3 < a3 count = 5 b1 < b2 < a1 < b3 < a2 < a3 count = 5

69 Synchronization Going back to the code the problem areas are the increment and decrement sections Intuitively, we want to permit only one of these to be executing at any one time we want to enforce atomic execution in = out = 0; while (true) { item = produce_item() while (counter == BUFFER_SIZE) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; counter++; } while (true) { while (counter == 0) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; consume_item(item) }

70 Synchronization Going back to the code the problem areas are the increment and decrement sections in = out = 0; Critical section A region of code for which access is controlled and/or coordinated by multiple processes while (true) { item = produce_item() while (counter == BUFFER_SIZE) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; counter++; Critical section } while (true) { while (counter == 0) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; Critical section consume_item(item) }

71 Synchronization Going back to the code the problem areas are the increment and decrement sections in = out = 0; Entry section We want to write code to control access into the critical section This is right BEFORE the critical section while (true) { item = produce_item() while (counter == BUFFER_SIZE) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; counter++; } Entry section while (true) { while (counter == 0) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; consume_item(item) Entry section }

72 Synchronization Going back to the code the problem areas are the increment and decrement sections Exit section We want to write code which in some way informs the other processes that access is now allowed This is right AFTER the critical section in = out = 0; while (true) { item = produce_item() while (counter == BUFFER_SIZE) {}/* do nothing */; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; counter++; } Exit section while (true) { while (counter == 0) {}/* do nothing */; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; consume_item(item) Exit section }

73 Synchronization A solution to the critical section problem must satisfy 3 criteria

74 Synchronization A solution to the critical section problem must satisfy 3 criteria Mutual exclusion : if a process is executing its critical section, no other process can be executing its critical section This is the most basic requirement

75 Synchronization A solution to the critical section problem must satisfy 3 criteria Mutual exclusion : if a process is executing its critical section, no other process can be executing its critical section Progress : If no process is executing its critical section, AND some process wants to enter its critical section, then only those processes NOT executing code in their other sections can decide who enters This helps to ensure that one of the processes which is actively waiting to enter its critical section is allowed to enter its critical section

76 Synchronization A solution to the critical section problem must satisfy 3 criteria Mutual exclusion : if a process is executing its critical section, no other process can be executing its critical section Progress : If no process is executing its critical section, AND some process wants to enter its critical section, then only those processes NOT executing code in their other sections can decide who enters Bounded Waiting : there must be a limit on the number of times that ANOTHER process is allowed to enter its critical section after a process has made a request to enter its critical section This ensures that everybody who is waiting has their turn at entering their critical section no one process can repeatedly enter indefinitely while some wait indefinitely

77 Synchronization Peterson s Solution do { flag[i] = true; turn = j; while (flag[j] && turn == j){}; Critical section flag[i] = false // other stuff (non critical) } while(true);

78 Synchronization Peterson s Solution do { flag[i] = true; turn = j; while (flag[j] && turn == j){}; Critical section flag[i] = false // other stuff (non critical) } while(true); Works for 2 processes/threads j = 1-i; Requires additional (shared) data : int turn; boolean flag[2];

79 Synchronization Peterson s Solution do { flag[i] = true; turn = j; while (flag[j] && turn == j){}; Critical section flag[i] = false // other stuff (non critical) } while(true); Works for 2 processes/threads j = 1-i; Requires additional (shared) data : int turn; boolean flag[2]; Two processes/threads are both running this code The j refers to the other process turn indicates whose turn it is to enter their section flag[] specifies if one of i or j are ready to enter their section Q: What is the entry condition/section? Q: What is the exit condition/section?

80 Synchronization Peterson s Solution do { flag[i] = true; turn = j; while (flag[j] && turn == j){}; Critical section flag[i] = false // other stuff (non critical) } while(true); Works for 2 processes/threads j = 1-i; Process i sets flag[i] to true (to specify, hey, I m ready to entry my critical section ) A process sets turn to j (the other process) Q: If both processes try to enter their critical section, what happens? Requires additional (shared) data : int turn; boolean flag[2];

81 Synchronization Peterson s Solution do { flag[i] = true; turn = j; while (flag[j] && turn == j){}; Critical section flag[i] = false // other stuff (non critical) } while(true); Works for 2 processes/threads j = 1-i; Requires additional (shared) data : int turn; boolean flag[2]; Process i sets flag[i] to true (to specify, hey, I m ready to entry my critical section ) A process sets turn to j (the other process) If both processes try to enter their critical section, although flag might be [1,1], because turn is a single shared value, only one of the processes will succeed in setting turn to the other process.

82 Synchronization Peterson s is a software solution to the critical section problem but it is not guaranteed to work because retrieval of the shared variable turn by two processes can be plagued by the same problem as increment (value = value + 1, for example) performed by multiple threads

83 Synchronization Peterson s is a software solution to the critical section problem but it is not guaranteed to work because retrieval of the shared variable turn by two processes can be plagued by the same problem as increment (value = value + 1, for example) performed by multiple threads Other solutions include: Kernel level instructions, such as test_and_set, and test_and_test_and_set, which act directly on hardware Kernel level compare_and_swap instrucions In the case of multiple processes/threads (not just two), test_and_set is used in combination with an array which holds information about ALL of the processes that are waiting to enter, along with a shared lock variable, that specifies if ANY process is in its critical section These kernel level operations are usually not accessible to users via system calls

84 Synchronization Most operating systems allow, via a system call, the use of a mutex this allows the application programmer to solve a critical section problem Mutex : What does this mean?

85 Synchronization Most operating systems allow, via a system call, the use of a mutex this allows the application programmer to solve a critical section problem Mutex : Mutual Exclusion This high level idea is the following : have the OS provide system calls for a lock that can be used to control access to a critical section.

86 Synchronization Most operating systems allow, via a system call, the use of a mutex this allows the application programmer to solve a critical section problem Mutex : Mutual Exclusion This high level idea is the following : have the OS provide system calls for a lock that can be used to control access to a critical section. do { // acquire lock Critical section // release lock // other stuff (remainder) } while(true);

87 Synchronization Most operating systems allow, via a system call, the use of a mutex this allows the application programmer to solve a critical section problem Mutex : Mutual Exclusion This high level idea is the following : have the OS provide system calls for a lock that can be used to control access to a critical section. do { // acquire lock Critical section // release lock // other stuff (remainder) } while(true); Multiple processes are running the code show left What is needed are system calls for acquiring a lock (which would grant access to the critical section), and releasing the lock (to specify that a process is done with their critical section)

88 Synchronization Most operating systems allow, via a system call, the use of a mutex this allows the application programmer to solve a critical section problem Mutex : Mutual Exclusion This high level idea is the following : have the OS provide system calls for a lock that can be used to control access to a critical section. do { // acquire lock Critical section acquire(){ while(!available) {}; available = false } // release lock // other stuff (remainder) } while(true); release(){ available = true } Q: What is the variable that is used to specify whether a process is in its critical section?

89 Up Next Lab 2 Implementation of Mutex using blitz

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Dept. of CSE, York Univ. 1

Dept. 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 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

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

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

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

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

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

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

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution 1. (15 points) A system with two dual-core processors has four processors available for scheduling. A CPU-intensive application (e.g., a program that spends most of its time on computation, not I/O or

More information

Synchronization Principles I

Synchronization 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 information

Chapter 7: Process Synchronization!

Chapter 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 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

CS370 Operating Systems Midterm Review

CS370 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 information

Synchronization API of Pthread Mutex: lock, unlock, try_lock CondVar: wait, signal, signal_broadcast. Synchronization

Synchronization 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 information

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems Chapter 5: Processes Chapter 5: Processes & Threads Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems, Silberschatz, Galvin and

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

Introduction to OS Synchronization MOS 2.3

Introduction to OS Synchronization MOS 2.3 Introduction to OS Synchronization MOS 2.3 Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Mahmoud El-Gayyar / Introduction to OS 1 Challenge How can we help processes synchronize with each other? E.g., how

More information

Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Synchronization. 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 information

Chapter 5: Process Synchronization

Chapter 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 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

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

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Synchronization. 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 information

Process Synchronization

Process 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 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

Chapter 5: Process Synchronization

Chapter 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 information

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

COP 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 information

Process Coordination

Process 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 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

Synchronization Spinlocks - Semaphores

Synchronization 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 information

Processes and Threads

Processes and Threads TDDI04 Concurrent Programming, Operating Systems, and Real-time Operating Systems Processes and Threads [SGG7] Chapters 3 and 4 Copyright Notice: The lecture notes are mainly based on Silberschatz s, Galvin

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

Process Synchronization

Process 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 information

Part II Process Management Chapter 6: Process Synchronization

Part 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 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

Operating Systems. Sina Meraji U of T

Operating Systems. Sina Meraji U of T Operating Systems Sina Meraji U of T 1 Announcement Check discussion board for announcements A1 is posted 2 Recap: Process Creation: Unix In Unix, processes are created using fork() int fork() fork() Creates

More information

Module 6: Process Synchronization

Module 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 information

Chapter 7: Process Synchronization. Background. Illustration

Chapter 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 information

Chapter 5: Process Synchronization

Chapter 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 information

CS370 Operating Systems

CS370 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 information

UNIT-II PROCESS SYNCHRONIZATION

UNIT-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 information

Chapter 4: Multithreaded

Chapter 4: Multithreaded Chapter 4: Multithreaded Programming Chapter 4: Multithreaded Programming Overview Multithreading Models Thread Libraries Threading Issues Operating-System Examples 2009/10/19 2 4.1 Overview A thread is

More information

Chapter 6: Process Synchronization

Chapter 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

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

Process Synchronization

Process 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 information

UNIT 2 Basic Concepts of CPU Scheduling. UNIT -02/Lecture 01

UNIT 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 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

Process Synchronization

Process 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 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

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008

Process Synchronization. Mehdi Kargahi School of ECE University of Tehran Spring 2008 Process Synchronization Mehdi Kargahi School of ECE University of Tehran Spring 2008 Producer-Consumer (Bounded Buffer) Producer Consumer Race Condition Producer Consumer Critical Sections Structure of

More information

Advance Operating Systems (CS202) Locks Discussion

Advance Operating Systems (CS202) Locks Discussion Advance Operating Systems (CS202) Locks Discussion Threads Locks Spin Locks Array-based Locks MCS Locks Sequential Locks Road Map Threads Global variables and static objects are shared Stored in the static

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

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

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

More information

Chapter 6: Process Synchronization

Chapter 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 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

CS370 Operating Systems Midterm Review. Yashwant K Malaiya Spring 2019

CS370 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 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 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads Chapter 4: Threads Objectives To introduce the notion of a

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

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

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

Threading and Synchronization. Fahd Albinali

Threading and Synchronization. Fahd Albinali Threading and Synchronization Fahd Albinali Parallelism Parallelism and Pseudoparallelism Why parallelize? Finding parallelism Advantages: better load balancing, better scalability Disadvantages: process/thread

More information

Lecture 5: Inter-process Communication and Synchronization

Lecture 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 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

Process Co-ordination OPERATING SYSTEMS

Process Co-ordination OPERATING SYSTEMS OPERATING SYSTEMS Prescribed Text Book Operating System Principles, Seventh Edition By Abraham Silberschatz, Peter Baer Galvin and Greg Gagne 1 PROCESS - CONCEPT Processes executing concurrently in the

More information

Real-Time Operating Systems M. 5. Process Synchronization

Real-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 information

CHAPTER 6: PROCESS SYNCHRONIZATION

CHAPTER 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 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

Remaining Contemplation Questions

Remaining Contemplation Questions Process Synchronisation Remaining Contemplation Questions 1. The first known correct software solution to the critical-section problem for two processes was developed by Dekker. The two processes, P0 and

More information

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne Chapter 4: Threads Silberschatz, Galvin and Gagne Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Linux Threads 4.2 Silberschatz, Galvin and

More information

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7)

Lecture Topics. Announcements. Today: Concurrency: Mutual Exclusion (Stallings, chapter , 5.7) Lecture Topics Today: Concurrency: Mutual Exclusion (Stallings, chapter 5.1-5.4, 5.7) Next: Concurrency: Deadlock and Starvation (Stallings, chapter 6.1, 6.6-6.8) 1 Announcements Self-Study Exercise #5

More information

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition, Chapter 3: Process-Concept, Silberschatz, Galvin and Gagne 2009 Chapter 3: Process-Concept Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2 Silberschatz, Galvin

More information

Announcements. Office hours. Reading. W office hour will be not starting next week. Chapter 7 (this whole week) CMSC 412 S02 (lect 7)

Announcements. 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 information

True/False: In Signal and Wait, the signaller finishes execution while the signalled process waits for its turn

True/False: In Signal and Wait, the signaller finishes execution while the signalled process waits for its turn Question Define the Rate Monotonic algorithm and the Earliest Deadline First algorithm. Give and example of both algorithms using execution history of processes. True/False: In Signal and Wait, the signaller

More information

Process Synchronization

Process 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 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

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

PESIT Bangalore South Campus

PESIT Bangalore South Campus INTERNAL ASSESSMENT TEST II Date: 04/04/2018 Max Marks: 40 Subject & Code: Operating Systems 15CS64 Semester: VI (A & B) Name of the faculty: Mrs.Sharmila Banu.A Time: 8.30 am 10.00 am Answer any FIVE

More information

Chapter 7: Process Synchronization. Background

Chapter 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 information

CS307: Operating Systems

CS307: Operating Systems CS307: Operating Systems Chentao Wu 吴晨涛 Associate Professor Dept. of Computer Science and Engineering Shanghai Jiao Tong University SEIEE Building 3-513 wuct@cs.sjtu.edu.cn Download Lectures ftp://public.sjtu.edu.cn

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

COMP 3430 Robert Guderian

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

More information

G52CON: Concepts of Concurrency

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

More information

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

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