0% found this document useful (0 votes)
25 views

OS 3 InterProcessesCommunication

The document discusses inter-process communication and synchronization. It covers topics like race conditions, critical regions, process synchronization techniques including disabling interrupts, local variables, Dekker's solution, Peterson's solution, sleep and wakeup, producer-consumer problem, and semaphores.

Uploaded by

Miki Micah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

OS 3 InterProcessesCommunication

The document discusses inter-process communication and synchronization. It covers topics like race conditions, critical regions, process synchronization techniques including disabling interrupts, local variables, Dekker's solution, Peterson's solution, sleep and wakeup, producer-consumer problem, and semaphores.

Uploaded by

Miki Micah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Operating Systems

ECEG-5202

Inter-Processes Communication

Surafel Lemma Abebe (Ph. D.)


Outline
• Introduction
• Race condition
• Critical region
• Process synchronization

Surafel Lemma Abebe (Ph. D.) 2


Introduction
• Processes within a system may be independent or cooperating
• Reasons for cooperating processes
– Information sharing
– Computation speedup
– Modularity
– Convenience
• Cooperating processes frequently need to communicate => inter-process
communication (IPC)
– Communicate without using interrupts
• Issues related to IPC
– How one process can pass information to another process
– How to make sure two or more processes do not get in each other’s way
• E.g., two or more processes in an airline reservation system trying to take the last seat on
a plane for different customers
– Proper sequencing when dependencies are present
• E.g., Output of one process is an input for the other
• Question
– Do these issues apply to threads? Explain.
Surafel Lemma Abebe (Ph. D.) 3
Race condition
• Is where two or more processes are reading or writing some shared data and the
final result depends on who runs precisely when
• Undesirable behavior that can occur from inappropriate reliance on ordering of
operations
• Example: Print spooler
– Shared variables
• out points to the next file to be printed
• in points to the next free slot in the directory
– Processes A and B want to queue a file for printing
– Each process reads in and stores the value in a local variable called next_free_slot

Next_free_slot

Surafel Lemma Abebe (Ph. D.) Next_free_slot 4


Race condition

• What happens when a race condition occurs?
– System hangs
– System crashes
– Lost data
– Security problems
– Unpredictability

Surafel Lemma Abebe (Ph. D.) 5


Critical regions
• What shall we do to avoid race condition?
– Find some way to prohibit more than one process from reading and writing
the shared data at the same time
 i.e., conflicting sections should be mutually exclusive
• Part of the program where the shared memory is accessed
– Critical region/section

• Conditions to enable parallel processes cooperate correctly and efficiently


using shared data, i.e., requirements for a critical section implementation
1. No two processes may be simultaneously inside their critical section
2. No assumption may be made about the speeds or the number of CPUs
3. No process outside its critical section (including the entry and exit code)
may block other processes
4. No process should have to wait forever to enter its critical section

Surafel Lemma Abebe (Ph. D.) 6


Process synchronization
• Approaches to achieve mutual exclusion

• Disabling interrupt
– Disable all interrupts just after entering its critical
region and re-enable them just before leaving it
– No clock interrupts can occur
– Simple
– Not a good choice. Why?
• Gives user processes the power to turn off interrupts
• Doesn’t work in a system with multiprocessor
– Disabling interrupts affects only the CPU that executes the disable
instruction, other processes on other CPUs could access it

Surafel Lemma Abebe (Ph. D.) 7


Process synchronization…
• Local variables
– Uses a single, shared (lock) variable
• Set variable to 0 => indicates no process in the critical section
• Set variable to 1 => some process in the critical section
• Same problem as the printer spooler example above
– Using two shared variables
• Example: software solution (mutual exclusion with busy waiting)

– Does the above code work? Why?

Surafel Lemma Abebe (Ph. D.) 8


Process synchronization…
• Strict alteration
– A process enters a critical section, after checking value of turn
– If its not the processes turn, the process continuously tests turn
– Busy waiting
• Continuously test a variable until some value appears
• Should usually be avoided
– Problem
• Continuously testing a variable until some value appears is called busy
waiting
• When one of the processes are slower than the other. Violates requirement 3

Surafel Lemma Abebe (Ph. D.) 9


Process synchronization…
• Dekker’s solution
– First correct solution
– Combines the idea of
taking turns with the
idea of wants
– Idea
• Take turns where there
is contention, but when
there is no contention,
the requesting process
can enter
• Peterson’s solution
– Same as Dekker’s
solution but simpler

Surafel Lemma Abebe (Ph. D.) 10


Process synchronization…
• The TSL instruction
– Supported by the hardware
– Some computers, esp. those with multiprocessors have instructions
like
• TSL, RX, LOCK
– Test and Set Lock
• Reads the contents of the memory word lock into register RX and then
stores a nonzero value at the memory address lock
• Operations of reading the word and storing into RX are atomic operations
– CPU executing the TSL instruction locks the memory bus to prohibit other CPUs
from accessing memory until its done
• NB.
– Locking memory bus is different from disabling interrupts

Surafel Lemma Abebe (Ph. D.) 11


Process synchronization…
• Sleep and wakeup
– What is limitation of TSL and Peterson’s solutions?
• Requires busy waiting
• Priority inversion problem
– Two processes with different priority
– Low priority process gets access to critical section
– High priority process becomes ready to access the critical section
– The scheduling algorithm could block the process with low priority
» i.e., low priority process gets blocked in its critical section
– Inter-process communication primitives
• Sleep
– Causes the caller to block or suspend until another process wakes it up
• Wakeup
– Unblocks a process that is blocked or suspended

Surafel Lemma Abebe (Ph. D.) 12


Process synchronization…
• Sleep and wakeup…
– Producer-consumer problem
• Also known as bounded-buffer problem

Two processes share a common, fixed-size buffer. One of them, the


producer, puts information into the buffer, and the other one, the
consumer, takes it out

• Problem
1. Producer wants to put a new item in the buffer, but it is already full
» Soln.: Producer goes to sleep
2. Consumer wants to remove an item from the buffer and sees that the
buffer is empty
» Soln.: Consumer goes to sleep

Surafel Lemma Abebe (Ph. D.) 13


Process synchronization…
• Sleep and wakeup…
– Producer-consumer problem …

Surafel Lemma Abebe (Ph. D.) 14


Process synchronization…
• Sleep and wakeup…
– Producer-consumer problem …
• Race condition
– Buffer status: Empty
1. Consumer has just read count to see if it is 0
2. Scheduler decides to stop running the consumer temporarily and start running
the producer
3. Producer inserts an item in the buffer, increments count, and calls wakeup to
wake consumer (which is going to be lost)
4. Scheduler starts consumer, consumer sees count = 0 and sleeps
5. Producer fill up the buffer and also go to sleep
 Both will sleep forever
• Solution
– Add a wakeup waiting bit
» Set when a wakeup is sent to a process that is still awake
» A process will check this bit before going to sleep
• If on, process will stay awake and turn off the bit
• Problem
– One wakeup bit would be insufficient when there are more than one processes

Surafel Lemma Abebe (Ph. D.) 15


Process synchronization

• Semaphores
– Uses an integer variable called semaphore as a flag that can be
accessed through two atomic operations
• Semaphore = 0 => no wakeups were saved
• Semaphore > 0 => if one or more wakeups were pending
– Operations on semaphore
• Generalizations of sleep and wakeup
• down
– Checks to see if the value is greater than 0
– If semaphore > 0, process decrements value and continues
– If semaphore = 0, process goes to sleep without completing the down
• up
– Increments the value of the semaphore addressed
– One of the sleeping processes is chosen by the system randomly and allowed
to complete down
• Atomic operations
– Checking the value, changing it, and possibly going to sleep
Surafel Lemma Abebe (Ph. D.) 16
Process synchronization…
• Semaphores…
– Producer-consumer problem
• Semaphore could be used to solve
the lost-wakeup problem (sleep
wakeup problem example – see the
code on the side)
• How?
– Implement up and down in an
indivisible way
– Each semaphore should be protected
by a lock variable with TSL
instructions
» TSL prevent several CPUs from
accessing the semaphore for
short period of time

Surafel Lemma Abebe (Ph. D.) 17


Process synchronization…
• Semaphores…
– Producer-consumer problem…
• Semaphore use
– Mutual exclusion
» Mutex
– Synchronization
» Full and empty semaphores

Surafel Lemma Abebe (Ph. D.) 18


Process synchronization…
• Mutex
– Simplified version of semaphore
• No need to count
– Good for managing mutual exclusion to some shared resource or piece of
code
– Is a shared variable that can be in one of two states:
• Unlocked
• Locked
– 0 means unlocked and all other values means locked
– Call mutex lock to enter a critical region

Surafel Lemma Abebe (Ph. D.) 19


Process synchronization…
• Assumption in proposed solutions
– Requires for multiple processes to have access to at least some shared
memory
– BUT processes have disjoint address spaces

• Question
– How can they share the turn variable in Peterson’s algorithm, or
semaphores or a common buffer?

• Answer
1. Semaphores can be stored in the kernel and accessed only by means
of system calls
2. Most modern OSs offer a way for processes to share some portion
of their address space with other processes
3. Using shared files

Surafel Lemma Abebe (Ph. D.) 20


Process synchronization…
• Monitors
– Scenario (while using
semaphore in the next code)
• Buffer full
1. Swap empty and mutex
semaphores
2. Producer executes
3. Consumer executes
– What would happen?
• Deadlock

– Shows that one must be very


careful

– Solution
• Higher level synchronization
primitive: Monitor

Surafel Lemma Abebe (Ph. D.) 21


Process synchronization…
• Monitors…
– Are a collection of procedures, variables, and data
structures that are all grouped together in a special kind of
module or package
– Processes can call procedures in monitors but cannot
access the monitors internal data structure
Syntax: nameOfMonitor.procedureName

– Only one process can be active in a monitor at any instance


• When a process calls a monitor procedure
– Checks if any other process is in the monitor
– If so, the calling process will be suspended until the other finishes
– Otherwise, calling process enters the monitor

– Monitors are programming language constructs


– Compiler is responsible to achieve mutual exclusion on
monitor entries
• Could use mutex or binary semaphore

Surafel Lemma Abebe (Ph. D.) 22


Process synchronization…
• Monitors…
– Semantics
• The monitors procedures are accessible by all the processes of the
system
• The data in the monitor is shared among processes
• Variables of the monitor can only be accessed through the
procedures of the monitor

– We use conditionals using the “condition” type declaration


– Operations on variables of “condition” type
• Wait (variable): blocks the calling process on that specific
condition
• Signal(variable): wakes a process sleeping on a condition variable

Surafel Lemma Abebe (Ph. D.) 23


Process synchronization…
• Monitors…
– What happens after a signal?
– How to avoid having two active processes in the
monitor at the same time?
– Proposal
• Let the newly awakened process run, suspending the other
process in the monitor
• A process doing signal must exit the monitor immediately
• Let the signaler continue to run and allow the waiting
process to start running only after the signaler has exited the
monitor

Surafel Lemma Abebe (Ph. D.) 24



Process
Monitors…
synchronization…
– Producer consumer problem with monitors
– Difference of wait and signal from sleep and wakeup
• Problem:
– sleep and wakeup failed because while one process was trying to go to sleep, the other was
trying to wake it up
– This cant happen with monitors

Surafel Lemma Abebe (Ph. D.) 25


Process synchronization…
• Monitors…
– Java supports monitors
• Synchronized
– Java guarantees that once any thread has started executing
that method, no other thread will be allowed to start
executing any other synchronized method of that object

Surafel Lemma Abebe (Ph. D.) 26


Process synchronization…
• What do you need to know while solving a
synchronization problem?
– What are the processes that need to be blocked?
– What are the conditions for blocking?
– Who should unblock the process and when?

Surafel Lemma Abebe (Ph. D.) 27


Process synchronization…
• Classical IPC problem
– The dining philosophers problem
• A classical problem from Dijkstra
– 5 philosophers sitting at a round table
» Thinking or eating
– Each has a plate of spaghetti
– There is a fork between each two
– Need two forks to eat
– Acquires one fork at a time in either right to left or left to right
order
• What algorithm do you use for access to the shared
resource (the forks)?

Surafel Lemma Abebe (Ph. D.) 28

You might also like