Download presentation
Presentation is loading. Please wait.
1
Interprocess Communication Race Conditions
Two processes want to access shared memory at same time
2
Race Condition: Race condition
Two or more processes are accessing some shared data and the final result depends on which order did they execute.
3
Concetps Mutual exclusion Critical region
Prohibit more than one process from reading and writing the shared data at the same time Critical region Part of the program where the shared memory is accessed is called as critical region or critical section.
4
Critical Section Problem
Four criteria that are desirable to solve race condition problem : No two processes simultaneously in critical region that is nothing but mutual exclusion. No assumptions made about speeds or numbers of CPUs. No process running outside its critical region may block another process i.e. progress No process must wait forever to enter its critical region i.e bounded wait.
5
Critical Section Problem
do { entry section critical section exit section remainder section Draw this on the board!! (So I can have it to refer to when discussing requirements of critical sections) } while (TRUE); General structure of a typical process Pi 5 5
6
Mutual exclusion using critical regions
7
Mutual Exclusion with Busy Waiting
Disable interrupt After entering critical region, disable all interrupts. Since clock is just an interrupt, no CPU preemption can occur. Disabling interrupt is useful for OS itself, but not for user programs. Disable interrupt does not work with multicore systems as it will disabled interrupts from one of the processor remaining can continue working and hence can access shared data.
8
Mutual Exclusion with busy waiting
Lock variable A software solution A single, shared variable (lock), before entering critical region, programs test the variable, if 0, no contest; if 1, the critical region is occupied This leads to same problem that we are trying to solve as lock is shared variable.
9
Mutual Exclusion with Busy Waiting : strict alternation
Proposed solution to critical region problem (a) Process (b) Process 1.
10
Concepts Busy waiting Spin lock
Continuously testing a variable until some value appears in it is called busy waiting. Spin lock A lock that uses busy waiting is called as spin lock.
11
Mutual Exclusion with Busy Waiting (2) : a workable method
Peterson's solution for achieving mutual exclusion
12
Mutual Exclusion with Busy Waiting (3)
Entering and leaving a critical region using the TSL instruction
13
Sleep and wakeup Drawback of Busy waiting
A lower priority process has entered critical region A higher priority process comes and preempts the lower priority process, it wastes CPU in busy waiting, while the lower priority don’t come out Priority inversion/deadlock Block instead of busy waiting Wakeup sleep
14
Producer-consumer problem
Two processes share a common, fixed-sized buffer Producer puts information into the buffer Consumer takes information from buffer A simple solution
15
Producer-consumer problem with fatal race condition
Sleep and Wakeup Producer-consumer problem with fatal race condition
16
Producer-Consumer Problem
What can be the problem? Signal missing Shared variable: counter Same old problem caused by concurrency When consumer read count with a 0 but didn’t fall asleep in time, then the signal will be lost
17
Semaphore Proposed by Dijkstra, introducing a new type of variable
Atomic Action A single, indivisible action Down (P) Check a semaphore to see whether it’s 0, if so, sleep; else, decrements the value and go on Up (v) Check the semaphore If processes are waiting on the semaphore, OS will chose on to proceed, and complete its down Consider as a sign of number of resources
18
Semaphore Solve producer-consumer problem
Full: counting the slots that are full; initial value 0 Empty: counting the slots that are empty, initial value N Mutex: prevent access the buffer at the same time, initial value 0 (binary semaphore) Synchronization/mutual exclusion
19
The producer-consumer problem using semaphores
20
Implementation of mutex_lock and mutex_unlock
Mutexes Implementation of mutex_lock and mutex_unlock
21
Figure 2-30. Some of the Pthreads calls relating to mutexes.
Mutexes in Pthreads (1) Figure Some of the Pthreads calls relating to mutexes. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
22
Mutexes in Pthreads (2) Figure Some of the Pthreads calls relating to condition variables. Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
23
Message Passing Information exchange between machines.
Two primitives functions Send(destination, &message) Receive(source,&message) Lots of design issues Message may get loss. Authentication-how does a process know the identity of the sender ?
24
Producer Consumer Using Message Passing
Consumer sends N empty messages to producer Producer fills message with data and sends to consumer.
25
Producer-Consumer Problem with Message Passing (1)
. . . .
26
Producer-Consumer Problem with Message Passing (2)
. . .
27
Message Passing Approaches
Have unique ID for address of recipient process Mailbox : It is place to buffer a certain number of messages. No buffering-sending process blocks until the receive happens. Receiver blocks until send occurs (Rendezvous) MPI
28
Barriers Barriers are intended for synchronizing groups of processes
Often used in scientific computations.
29
Classic IPC Problems Dining philosopher problem Reader Writer problem
A philosopher either eat or think If goes hungry, try to get two forks and eat Reader Writer problem Models access to a database
30
Dining Philosophers Problem (1)
Lunch time in the Philosophy Department.
31
Dining Philosophers Problem (2)
A nonsolution to the dining philosophers problem.
32
Dining Philosophers Problem (3)
. . . A solution to the dining philosophers problem.
33
Dining Philosophers Problem (4)
. . . . . . A solution to the dining philosophers problem.
34
Dining Philosophers Problem (5)
. . . A solution to the dining philosophers problem.
35
The Readers and Writers Problem (1)
. . . A solution to the readers and writers problem.
36
The Readers and Writers Problem (2)
. . . A solution to the readers and writers problem.
37
Reader-Writer Problem
What is the disadvantage of the solution? Writer faces the risk of starvation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.