Interprocess Communication - PowerPoint PPT Presentation

Loading...

PPT – Interprocess Communication PowerPoint presentation | free to download - id: be511-Zjk3O



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Interprocess Communication

Description:

Consider the example in the book of a print spooler. When a process wants to print a file, it enters the name in the spooler directory. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 23
Provided by: derek94
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Interprocess Communication


1
Interprocess Communication
  • Processes/threads often work together to
    accomplish some task.
  • This usually means that the processes/threads
    need to communicate.

2
Process Communication Issues
  • How do we pass information between processes?
  • How do we ensure the processes do not get in each
    others way during critical activities?
  • Proper sequencing when processes have
    dependencies upon each other.

3
Race Conditions/Shared Data Problem
  • Consider the example in the book of a print
    spooler.
  • When a process wants to print a file, it enters
    the name in the spooler directory. Another
    process periodically checks to see if there are
    any files to be printed, if so, prints them and
    removes their file names from the spooler
    directory.

4
Printer Spooler/Race Condition
Process A locals
Next file to be printed
next_free_slot 7
Process A interrupted after reading in and B runs
Next free slot
P.B file
8
P.A file
Processes reading or writing some shared data and
the final result depends on who runs when
Process B locals
next_free_slot 7
5
Avoiding Race Conditions
  • Accessing the shared memory should be mutually
    exclusive to one process at a time.
  • This shared memory area is known as a critical
    section.

6
Conditions for Parallel Process to cooperate
correctly and efficiently using shared data
  • No two processes simultaneously in critical
    section
  • No assumptions made about speeds or numbers of
    CPUs
  • No process running outside its critical region
    may block another process
  • No process must wait forever to enter its
    critical region

7
What we would like to happen
8
Easy Approach Disable Interrupts
  • Disable Interrupts before accessing critical
    section and then re-enable afterwards
  • Ensures the process will not be interrupted while
    accessing the shared memory
  • Can be problematic if the process does not
    re-enable interrupts
  • Better mechanisms for mutual exclusion

9
Using a Lock Variable
  • Use variable called lock to indicate if a
    critical section is currently being accessed
  • If lock is set then wait otherwise enter
    critical section
  • What happens if a process is interrupted from
    running just after reading the lock is not set?

10
Atomic Instructions
  • Atomic instructions are those that are performed
    together and are considered indivisible.
  • As we just saw performing a test on the lock is
    not guaranteed to complete before another
    process is scheduled.
  • if (lock 0)
  • lock 1
  • enter critical section
  • The C style code translates to many assembly
    instructions
  • Another process could be scheduled after
    executing any of these assembly (ie the read of
    lock)

11
Strict Alternation
Assume turn is initially 0
  • Proposed solution to critical region
    problem
  • (a) Process 0. (b) Process 1.

12
Strict Alternation
  • So, what happens when Process 1 is slow in
    executing?
  • Process 0 eventually is blocked waiting on
    Process 1 to complete its noncritical section.
  • This violates our condition of no process running
    outside its critical region may block another
    process

13
Petersons Solution
Processes must call enter_region before entering
critical section and must call leave_region upon
leaving
14
Assume we start with Process 0 in the running
state about to execute enter_region, Process 1 is
in ready state with its logical PC stored in the
PCB
Critical Section Code
Other Code
Interrupt occurs switching to other process (may
be a clock interrupt)
Process 0 is able to enter Into the critical
section since turn 1 and process 0
Process 1 must busy wait since while statement
is TRUE
Process 1 is now able to enter the CS
since Interested0False
Process 0 PC
Process 1 PC
Bolded arrows indicate the process is in the
running state otherwise PC is stored in the
associated PCB along with locals
0
interested1
1
0
interested0
1
other
1
other
0
0
turn
0
process
0
process
1
1
Globals of interest
Process 0 locals
Process 1 locals
15
Is Petersons solution correct?
  • It is correct in terms of our four conditions for
    parallel processes to cooperate correctly and
    efficiently using shared data but it requires
    busy waiting if the process cannot enter the
    critical section.

16
Priority Inversion
  • Priority Inversion can take place when a low
    priority process holds a shared resource that is
    required by a high priority process. The high
    priority process is then blocked until the
    resource is released by the low priority process.
  • Obviously the priorities have been inverted.

17
Prevent Busy Waiting with Blocking Calls
  • Sleep Causes the caller to suspend until
    another process wakes it up.
  • Wakeup wakeups specified process

18
Producer-Consumer using sleep and wakeup
Producer
Consumer
0 1 2
N
What happens if consumer is interrupted after
reading count as 0?
19
How do we fix this problem?
  • Add a bit to indicate if a process is already
    awake and check it before issuing a sleep?
  • Obviously we need a mechanism to keep count of
    saved wakeups

20
Semaphore
  • Semaphores introduced to keep track of wakeups.
  • Dijkstra proposed the semaphore have two
    operations
  • down checks if Semaphore gt0, if so it
    decrements the count. Otherwise sleep.
  • up simply increments semaphore count and if a
    process was sleeping on a down it obtains
    semaphore and completes down operation.
  • up and down are atomic instructions

21
Producer-Consumer with Semaphores
Note mutex is a special kind of semaphore
called a binary semaphore. It can have a value of
0 or 1
22
Semaphores Uses
  • To provide mutual exclusion to a shared resource.
  • Obviously, we can imagine the count of
    semaphore corresponding to the number of shared
    identical resources.
  • To provide synchronization
  • full and empty were used in this way
About PowerShow.com