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

Chapter 4 IPC New 2

Uploaded by

Henok Zeleke
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)
8 views

Chapter 4 IPC New 2

Uploaded by

Henok Zeleke
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/ 25

1

Chapter 4:
Inter process communication(IPC)
• Inter process communication
• Race condition
• Critical section
• Mutual Exclusion
• Classic Problems
2
Inter process Communication

 The communication between processes in the control of the


OS is called as Inter-process Communication or simply IPC.
 Processes frequently need to communicate with other
processes/threads to share.
 Messages

 Semaphores

 Shared memory
3
IPC- In distributed environment

Distributed computing systems make


use of these facilities/resources to Process 1 Process 2
provide application programming
interface (API) which allows IPC to be
programmed at a higher level of data
abstraction.
(e.g., send and receive)
Distributed computing requires information to sender receiver
be exchanged among independent
processes.
Operations provided in an archetypal 4
Inter-process Communications API

• Connect (sender address, receiver address),


• For connection-oriented communication.
• Send ( [receiver], message)
• Receive ( [sender], message storage object)
• Disconnect (connection identifier),
• For connection-oriented communication
5
IPC – Event Diagram

Process B
Process A

time
request 1

response 1

request 2
interprocess communication
execution flow
response2
process blocked

Event diagram for a protocol


6
IPC – unicast and multicast

 In distributed computing, two or more processes engage in IPC using a


protocol agreed upon by the processes.
 A process may be a sender at some points during a protocol, a receiver
at other points.
P2 P2 P3 ... P4

m
m m m

P1 P1

unicast multicast
Socket communication Publish/Subscribe Message model,
7
Why we need to study-IPC

 First – How one process can pass information to another.


 APIs in distributed computing
 Second – conflict resolution when processes are engaging in critical
activities.
 Protocols and related issues.
 Third- proper sequencing when dependencies are present.
 Data production and printing
 Two of the above issues are equally well works for threads and the first
one is even more simple.
 Same problems exist- same solutions apply
8
Race Condition

 Where two or more processes are reading or writing some shared


data/shared memory and the final result depends on who runs
precisely.
 To protect against the race condition, ensure that only one process at
a time can be manipulating the shared data/variable.
9
Critical Section

 Each process has a segment of code called critical section (CS) in


which the process may be changing common variables, writing file and
so on.
 If we could arrange matters such that no two processes were ever in
their critical sections at the same time, we could avoid race conditions.
 Each process must request permission to enter its critical section i.e. entry
section. The entry section may be followed by exit section. The
remaining code is remainder section.
 Find some way to prohibit more than one process from reading and
writing the shared data at the same time.(mutual exclusion).
10
Critical Section….
11
Mutual Exclusion

 A way of making sure that if one process is using a shared modifiable


data, the other processes will be excluded from doing the same thing.

 Note that mutual exclusion needs to be enforced only when processes


access shared modifiable data.

 Rather it needs to support concurrent programming.


Mutual Exclusion Conditions 12

1. No two processes may at the same moment inside their critical


sections.

2. No assumptions are made about relative speeds of processes or


number of CPUs.

3. No process outside its critical section should block other processes.

4. No process should wait arbitrary long to enter its critical section.


13
Proposals for Achieving mutual exclusion

1. Disabling Interrupts
2. Lock Variable
3. Strict Alternation
4. Peterson’s algorithm
5. TSL instruction
14
Disabling Interrupts(Hardware Solution)

 Each process disables all interrupts just after entering in its critical section
and re-enable all interrupts just before leaving critical section.
 Has two drawbacks
 Multi-processor

 Re-enabling will not be performed


 Conclusion- good if it’s done by the kernel it self
15
Lock Variables(software solution)

 Consider having a single, shared lock variable initialized to 0.


 When a process wants to enter its CS, it first tests the lock.
 If the lock is 0, the process sets it to 1 and enters the CS.
 If the lock is already 1, the process just waits until it becomes 0.
 The problem is like we have in spooler directory.
Strict Alternation 16

 The integer variable 'turn’,


 Processes inspect turn
 E.g. Process A inspect turn, finds it to be 0, and enters in its critical section. Process
B also finds it to be 0 and sits in a loop continually testing 'turn' to see when it
becomes 1.
 Continuously testing a variable waiting for some value to appear is
called the Busy-Waiting- (waste CPU time)
 A lock that uses busy waiting is spin lock.
17
Strict Alternation

 No process should allow to use the shared data in a successive


manner.
18
Petersons algorithm

 A Dutch mathematician (T. Dekker,1965) comes up with the solution of


combining the idea of taking turns and lock variable.
 It avoids strict alteration
 In 1981 G.L peterson comes with a simple algorithm to achieve mutual
exclusion.
 Each process calls CS_entry with its own process number, 0 or 1, as
parameter before entering to their cs.
 The process calls CS_exit to indicate that it is done and to allow the
other process to enter.
19

 What if both processes claim to enter to their critical region almost simultaneously
 Needs busy waiting, hence CPU time wastage is there.
20
TSL instruction

 Proposed solution with a little help by the underling hardware.


 Used specially in multi-processor environment.
 Have an instruction TSL RX, LOCK – Test and Set lock
 It reads the content of the memory word lock into register RX. Then
stores a nonzero value at the memory address lock.
 This operation is guaranteed to be indivisible.
 The CPU executing this instruction locks the memory bus in order not to be
accessed by other processes.
21
TSL instruction….

enter_region:
TSL REGISTER,LOCK |copy lock to register and set lock to 1
CMP REGISTER,#0 | was lock zero?
JNE enter_region | if it was non zero, lock was set, so loop
RET | return to caller; critical region entered
leave_region:
MOVE LOCK,#0 | store a 0 in lock
RET | return to caller
22
Sleep and Wakeup

 Avoid priority inversion problem and CPU time wastage in TSL and
Peterson’s case.

 Uses a pair of sleep and wakeup.


 Sleep is a system call that causes the caller to be sleep (suspended) until….

 Wakeup call has one parameter, the process to be awakened.

 Alternatively both sleep and wakeup use one parameter. A memory address that
much up both sleeps and wakeups.
23
Producer- Consumer problem

 A.K.A. bounded-buffer problem.


 Two processes share a common fixed size buffer.
 Producer puts information in to the buffer
 Consumer takes out of information from the buffer.
 Race condition occurs
 When the producer finds the buffer full
 When the consumer finds the buffer empty
 In such cases both processes will be put into sleep forever.
24
25

End of chapter 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy