0% found this document useful (0 votes)
18 views29 pages

OS 3 InterProcessesCommunication

This document discusses inter-process communication and process synchronization. It covers issues that can arise when processes need to share data, such as race conditions. It then describes methods processes can use to synchronize access to shared resources, including critical sections, local variables, Dekker's solution, Peterson's solution, and semaphores. Semaphores in particular use atomic operations on an integer value to control access to shared resources and avoid issues like lost wakeups that can occur with simple sleep/wakeup approaches.
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)
18 views29 pages

OS 3 InterProcessesCommunication

This document discusses inter-process communication and process synchronization. It covers issues that can arise when processes need to share data, such as race conditions. It then describes methods processes can use to synchronize access to shared resources, including critical sections, local variables, Dekker's solution, Peterson's solution, and semaphores. Semaphores in particular use atomic operations on an integer value to control access to shared resources and avoid issues like lost wakeups that can occur with simple sleep/wakeup approaches.
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/ 29

Operating Systems

ECEG-5202
INTER-PROCESSES
COMMUNICATION
Outline
▪Introduction
▪Race condition
▪Critical region
▪Process synchronization

November 28, 2022 INTERPROCESSES COMMUNICATION 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?

November 28, 2022 INTERPROCESSES COMMUNICATION 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
Next_free_slot
◦ 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

November 28, 2022 INTERPROCESSES COMMUNICATION 4


Race condition…
What happens when a race condition occurs?
◦ System hangs
◦ System crashes
◦ Lost data
◦ Security problems
◦ Unpredictability

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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?

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 13


Process synchronization…
Sleep and wakeup…
◦ Producer-consumer problem …

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 17


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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 20


Process synchronization…
1
Classical IPC problem
◦ The dining philosophers problem
◦ A classical problem from Dijkstra
5 2
◦ 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 4 3
◦ What algorithm do you use for access to
the shared resource (the forks)?

November 28, 2022 INTERPROCESSES COMMUNICATION 21


Process synchronization…
Monitors
◦ Scenario
◦ When all philosophers want to eat
◦ Could happen in Buffer full memory
◦ What would happen?
◦ Deadlock

◦ Shows that one must be very careful

◦ Solution
◦ Higher level synchronization primitive: Monitor

November 28, 2022 INTERPROCESSES COMMUNICATION 22


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

November 28, 2022 INTERPROCESSES COMMUNICATION 23


Process synchronization…
Monitors…
◦ Semantics
◦ The monitor’s 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

November 28, 2022 INTERPROCESSES COMMUNICATION 24


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

November 28, 2022 INTERPROCESSES COMMUNICATION 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

November 28, 2022 INTERPROCESSES COMMUNICATION 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?

November 28, 2022 INTERPROCESSES COMMUNICATION 27


Process synchronization…
Classical IPC problems
The Readers and Writers Problem
◦ Two classes of processes
◦ Readers, which can work concurrently.
◦ Writers, which need exclusive access.
◦ Must prevent 2 writers from being concurrent.
◦ Must prevent a reader and a writer from being concurrent.
◦ Must permit readers to be concurrent when no writer is active.
◦ Perhaps want fairness (e.g., freedom from starvation).
◦ Variants
◦ Writer-priority readers/writers.
◦ Reader-priority readers/writers.

November 28, 2022 INTERPROCESSES COMMUNICATION 28


Acknowledgment
These slides are adopted from the slides of
Surafel Lemma Abebe (Ph. D.)

Here, I would like to acknowledge and thank him for allowing me to


customize and use the slides for this course.

November 28, 2022 PROCESSES 29

You might also like