Difference between revisions of "High-level Synchronization and IPC"

From Soma-notes
Jump to navigation Jump to search
(first edit, will "wiki-ize" on preceeding edits)
 
(finished "wiki-izing")
Line 1: Line 1:
Dinning Philosophers Problem:
=High-Level Synchronization and IPC=
- Thought experiment used to understand synchronization primitives


Five philosophers are dining at a frugal Chinese restaurant where they are only provided with one chopstick each. They need two chopsticks to pick up the noodles.
==Dinning Philosophers Problem==
Whenever they are hungry they pick up two chopsticks to eat.
When they are done eating they put down the chopsticks.
The philosophers do not talk to each other about eating, only high-minded ideas.
Have to define a strategy to make sure that no philosopher starves to death.
We can think of each chopstick as a semaphore.


Problems:
[[Image:DiningPhilosophers.jpg]]


Starvation:
* Thought experiment used to understand synchronization primitives
- one (or more) philosopher is never able to get two chopsticks and dies.
* Five philosophers are dining at a frugal Chinese restaurant where they are only provided with one chopstick each. They need two chopsticks to pick up the noodles in order to eat.
** Whenever they are hungry they pick up two chopsticks to eat.
** When they are done eating they put down the chopsticks.
** The philosophers do not talk to each other about eating, only high-minded ideas.
* Have to define a strategy to make sure that no philosopher starves to death.
* We can think of each chopstick as a semaphore.


Deadlock
===Problems===
- All philosophers have one chopstick and are waiting for another philosopher to put one down
* '''Starvation'''
** One (or more) philosopher is never able to get two chopsticks and dies.
* '''Deadlock'''
** All philosophers have one chopstick and are waiting for another philosopher to put one down and they all starve to death.


===Possible Solutions===
* Could use a scheme involving timeouts, but we want a ''PERFECT'' solution
* Could use synchronization; philosophers either grab one chopstick or none, this illustrates '''AND synchronization'''


How could AND synchronization be implemented?
* Could turn multiple semaphores into one by abstracting the semaphores into 1 class.
* In general this is done by encapsulating the resources and using another process/thread/program to hand them out


Possible Solutions:
==Limitations of Semaphores==
- Could use a scheme involving timeouts, but we want a PERFECT solution
- Could use synchronization; philosophers either grab one chopstick or none, this illustrates AND synchronization


How could AND synchronization be implemented?
Semaphores are hard to use...
- could turn multiple semaphores into one by abstracting the semaphores into 1 class.
** If the programmer forgets to grab the semaphore this can lead to corruption.
- In general this is done by encapsulating the resources and using another process/thread/program to hand them out
** If the programmer never releases the semaphore this can lead to the program locking.
 
Limitations of Semaphores:
- Semaphores are hard to use
    - if the programmer forgets to grab the semaphore this can lead to corruption
    - if the programmer never releases the semaphore this can lead to the program locking


The underlying problem is that the down and up need to be matched, with non-standard exits this can be difficult.
The underlying problem is that the down and up need to be matched, with non-standard exits this can be difficult.


But in C it's all you've got...
But in C it's all you've got...
 
* Solution: write a program to check the code (eg: Stanford Checker)
Solution: write a program to check the code (eg: Stanford Checker)
** Problem: not reliable, can produce many false positives
Problem: not reliable, can produce many false positives


How about building synchronization into the language as a base level construct?
How about building synchronization into the language as a base level construct?
- Java's synchronized takes care of semaphores
* Java's ''synchronized''
    - hides semaphores from developer
** Hides semaphores from developer
    - developer still has to decide where to synchronize
** Developer still has to decide where to synchronize
    - Don't want to  synchronize all your code, this leads to single threaded behaviour
** Don't want to  synchronize all your code, this leads to single threaded behaviour


General  idea: Monitor Object
General  idea: Monitor Object
- An object instance where only one thread can be executing certain pieces of code at once.
* An object instance where only one thread can be executing certain pieces of code at once.
- Provides exclusive access to variables
* Provides exclusive access to variables


Events:
==Events and Message Passing==
- message queue
 
- when do they occur?
===Message Passing===
    - user does something
* Uses buffers
    - device does something
* Message passing is used in large contexts
- synchronization based on waiting for an event
 
===Events===
* Message queue
* When do they occur?
** User does something
** Device does something
* Synchronization can be based around waiting for an event to occur.


Message Passing
- uses buffers
- message passing is used in large contexts


When an event occurs:
When an event occurs:
- it has to be stored (in a buffer)
* It has to be stored (in a buffer)
- might not be processed right away
* It might not be processed right away
    - what if the event is no longer relevant?
** What if the event is no longer relevant?
      - Could implement events with timeouts
*** Could implement events with timeouts
    - what if no one's listening for the event?
** What if no one's listening for the event?
        - Could drop the event
*** Could drop the event
            - In the case of a file open if the event is dropped there will be a leak
** The challenge is determining which events should be dropped and which should be queued.
*** In the case of a file open if the event is dropped there will be a leak
*** In the case of mouse movement when no application is listening we want to drop it so that if an application starts listening to mouses events the mouse doesn't go crazy


Message passing in a WAN
==Message passing in a WAN==


Message passing in a WAN is unreliable. Packets can be dropped.
Message passing in a WAN is unreliable. Packets can be dropped.


In some cases we can lose the data
In some cases it is acceptable to lose the data
- streaming video
* Streaming video
- VOIP
* VOIP
- cell phones
* Cell phones


In others we need a reliable transmission
In others we need a reliable transmission
- downloading content
* Downloading content
 
TCP/IP
- a mechanism to resent dropped packets
- if we build on top of TCP/IP we can assume reliable tranmission
 
NAT (network address translation)
- Used in local networks, to the outside world the whole network appears as a single address, NAT devices remember outgoing requests to route the response to the correct party.
- Firewalls will automatically drop messages that do not have a corresponding outgoing request
    - Firewalls will often only let certain types of traffic through, often only TCP/IP
      - Application developers may want to use UDP but may be forced to use TCP/IP to get through the firewall
 


===TCP/IP===
* A mechanism to resent dropped packets
* If we build on top of TCP/IP we can assume reliable transmission


Further Reading:
===NAT (Network Address Translation)===
http://en.wikipedia.org/wiki/Dining_philosophers_problem
* Used in local networks, to the outside world the whole network appears as a single address, NAT devices remember outgoing requests to route the response to the correct party.
http://en.wikipedia.org/wiki/Monitor_(synchronization)
* Firewalls will automatically drop messages that do not have a corresponding outgoing request
** Firewalls will often only let certain types of traffic through, often only TCP/IP
*** Application developers may want to use UDP but may be forced to use TCP/IP to get through the firewall

Revision as of 00:31, 13 October 2007

High-Level Synchronization and IPC

Dinning Philosophers Problem

DiningPhilosophers.jpg

  • Thought experiment used to understand synchronization primitives
  • Five philosophers are dining at a frugal Chinese restaurant where they are only provided with one chopstick each. They need two chopsticks to pick up the noodles in order to eat.
    • Whenever they are hungry they pick up two chopsticks to eat.
    • When they are done eating they put down the chopsticks.
    • The philosophers do not talk to each other about eating, only high-minded ideas.
  • Have to define a strategy to make sure that no philosopher starves to death.
  • We can think of each chopstick as a semaphore.

Problems

  • Starvation
    • One (or more) philosopher is never able to get two chopsticks and dies.
  • Deadlock
    • All philosophers have one chopstick and are waiting for another philosopher to put one down and they all starve to death.

Possible Solutions

  • Could use a scheme involving timeouts, but we want a PERFECT solution
  • Could use synchronization; philosophers either grab one chopstick or none, this illustrates AND synchronization

How could AND synchronization be implemented?

  • Could turn multiple semaphores into one by abstracting the semaphores into 1 class.
  • In general this is done by encapsulating the resources and using another process/thread/program to hand them out

Limitations of Semaphores

Semaphores are hard to use...

    • If the programmer forgets to grab the semaphore this can lead to corruption.
    • If the programmer never releases the semaphore this can lead to the program locking.

The underlying problem is that the down and up need to be matched, with non-standard exits this can be difficult.

But in C it's all you've got...

  • Solution: write a program to check the code (eg: Stanford Checker)
    • Problem: not reliable, can produce many false positives

How about building synchronization into the language as a base level construct?

  • Java's synchronized
    • Hides semaphores from developer
    • Developer still has to decide where to synchronize
    • Don't want to synchronize all your code, this leads to single threaded behaviour

General idea: Monitor Object

  • An object instance where only one thread can be executing certain pieces of code at once.
  • Provides exclusive access to variables

Events and Message Passing

Message Passing

  • Uses buffers
  • Message passing is used in large contexts

Events

  • Message queue
  • When do they occur?
    • User does something
    • Device does something
  • Synchronization can be based around waiting for an event to occur.


When an event occurs:

  • It has to be stored (in a buffer)
  • It might not be processed right away
    • What if the event is no longer relevant?
      • Could implement events with timeouts
    • What if no one's listening for the event?
      • Could drop the event
    • The challenge is determining which events should be dropped and which should be queued.
      • In the case of a file open if the event is dropped there will be a leak
      • In the case of mouse movement when no application is listening we want to drop it so that if an application starts listening to mouses events the mouse doesn't go crazy

Message passing in a WAN

Message passing in a WAN is unreliable. Packets can be dropped.

In some cases it is acceptable to lose the data

  • Streaming video
  • VOIP
  • Cell phones

In others we need a reliable transmission

  • Downloading content

TCP/IP

  • A mechanism to resent dropped packets
  • If we build on top of TCP/IP we can assume reliable transmission

NAT (Network Address Translation)

  • Used in local networks, to the outside world the whole network appears as a single address, NAT devices remember outgoing requests to route the response to the correct party.
  • Firewalls will automatically drop messages that do not have a corresponding outgoing request
    • Firewalls will often only let certain types of traffic through, often only TCP/IP
      • Application developers may want to use UDP but may be forced to use TCP/IP to get through the firewall