Process synchronization iii
Download
1 / 17

Process Synchronization III - PowerPoint PPT Presentation


  • 323 Views
  • Updated On :

CS423UG Operating Systems Process Synchronization III Indranil Gupta Lecture 10 Sep 16, 2005 Today’s Agenda Classical synchronization problems Producer-Consumer problem Bounded Buffer Problem Reader Writer Problem Dining Philosophers Problem

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Process Synchronization III' - Faraday


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Process synchronization iii l.jpg

CS423UG Operating Systems

Process Synchronization III

Indranil Gupta

Lecture 10

Sep 16, 2005


Today s agenda l.jpg
Today’s Agenda

  • Classical synchronization problems

    • Producer-Consumer problem

    • Bounded Buffer Problem

    • Reader Writer Problem

    • Dining Philosophers Problem

    • Sleeping Barber Problem

  • IPC

CS 423UG- Operating Systems, Indranil Gupta


Iii reader writer problem l.jpg
III. Reader-Writer Problem

  • A reader: read data

  • A writer: write data

  • Rules:

    • Multiple readers may read the data simultaneously

    • Only one writer can write the data at any time

    • A reader and a writer cannot access data simultaneously

  • Locking table: whether any two can be in the critical section simultaneously

CS 423UG- Operating Systems, Indranil Gupta


Reader writer solution l.jpg
Reader-Writer Solution

  • Does it work? Why?

  • Ask Systematic questions, and look at What if ? scenarios…

  • Play the devil’s advocate!

    Semaphore mutex, wrt; // shared and initialized to 1;

    int readcount; // shared and initialized to 0

    // Writer // Reader

    Down(mutex);

    readcount:=readcount+1;

    Down(wrt); if readcount == 1 then Down(wrt);

    ...... Up(mutex);

    writing performed ....

    ..... reading performed

    Down(mutex);

    Up(wrt); readcount:=readcount-1;

    if readcount == 0 then Up(wrt);

    Up(mutex);

CS 423UG- Operating Systems, Indranil Gupta


Iv dining philosophers an intellectual game l.jpg
IV. Dining Philosophers: an intellectual game

0

  • N philosophers and N forks

  • Philosophers eat/think

  • Eating needs 2 forks

  • Pick one fork at a time

1

0

4

1

2

4

3

3

2

N=5

CS 423UG- Operating Systems, Indranil Gupta


Does this solve the dining philosophers problem l.jpg
Does this solve the Dining Philosophers Problem?

A non-solution to the dining philosophers problem

  • Deadlock: everyone picks up their left fork first, then waits… => Starvation!

CS 423UG- Operating Systems, Indranil Gupta


Dining philosophers solution l.jpg
Dining Philosophers Solution

(s[n] inited

to 0’s)

CS 423UG- Operating Systems, Indranil Gupta


Dining philosophers solution8 l.jpg
Dining Philosophers Solution

Can prove that this solution is deadlock-free and starvation-free

CS 423UG- Operating Systems, Indranil Gupta


V the sleeping barber problem l.jpg
V. The Sleeping Barber Problem

  • N customer chairs (waiting chairs)

  • One barber who can cut one customer’s hair at any time

  • No waiting customer => barber sleeps

  • Customer enters =>

    • If all waiting chairs full, customer leaves

    • Otherwise, if barber asleep, wake up barber and make him work

    • Otherwise, barber is busy – wait in a chair

CS 423UG- Operating Systems, Indranil Gupta


The sleeping barber solution 1 l.jpg
The Sleeping Barber Solution (1)

CS 423UG- Operating Systems, Indranil Gupta


The sleeping barber solution 2 l.jpg
The Sleeping Barber Solution (2)

CS 423UG- Operating Systems, Indranil Gupta


The sleeping barber solution 3 l.jpg
The Sleeping Barber Solution (3)

Solution to sleeping barber problem.

CS 423UG- Operating Systems, Indranil Gupta


Epilogue ipc interprocess communication l.jpg
Epilogue: IPC (Interprocess Communication)

  • Real life analogy: email!

    • Can two processes on a machine send “email” to each other?

  • IPC: An indirect way of coordination, where the OS (and/or network) helps processes!

  • An IPC primitive: Message Passing

    • Send (destination, &message)

    • Receive (source, &message)

    • Message size: Fixed or Variable size.

CS 423UG- Operating Systems, Indranil Gupta


Message passing l.jpg
Message Passing

Example: Unix pipes

CS 423UG- Operating Systems, Indranil Gupta


Ipc indirect communication implicit coordination provided through library system calls l.jpg
IPC=Indirect Communication: Implicit Coordination provided through library/system calls

send(A,message) /* send a message to mailbox A */

receive(A,message) /* receive a message from mailbox A */

  • Mailbox is an abstract object into which a message can be placed in, or removed from.

Example: Unix message queues

CS 423UG- Operating Systems, Indranil Gupta


Advantage of indirect communication l.jpg
Advantage of Indirect Communication through library/system calls

  • Allows greater variety of schemes:

    • two processes per link

    • 1 link per pair of processes

    • Uni- or bi-directional

    • allow 1 process to receive a message from a link

    • allow 1 process to receive all messages from a link

  • What if a process sends when queue is full? Receive when queue is empty?

    • Pipe and message queue implementations themselves use sempahores/mutexes within them!

Unix: msgget() and msgsnd() for message queues

CS 423UG- Operating Systems, Indranil Gupta


Reminders l.jpg
Reminders through library/system calls

  • Reading for this week’s lectures were Sections 2.3-2.4

  • Reading for next week: Chapter 3 (full)

  • MP1 due next Monday

CS 423UG- Operating Systems, Indranil Gupta


ad