EE512 System Programming - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

EE512 System Programming

Description:

... to solving synchronization problems and avoiding race conditions. ... Problem: Program the barber and the customers, without getting into race conditions. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 34
Provided by: core3
Category:

less

Transcript and Presenter's Notes

Title: EE512 System Programming


1
EE512 System Programming
Lecture 3 Interproces Communication
Sept. 8, 2009 Prof. Kyu Ho Park http//core.kaist.
ac.kr
Notice Tutorial 1 for Project1 on Sept. 10,
2009
1
2
fork() system call
  • int main()
  • pid_t pid
  • char message
  • int n
  • printf(fork example program start\n)
  • pid fork()
  • switch(pid) case -1 perror(fork failed)
  • case 0 message I am a child break
  • defualt messageI am the parent break
  • exit(0)

3
IPC Problems
  • The 3 Issues of IPC
  • 1. How one process can pass information to
    another.
  • 2. How to make sure that two or more processes
    do not get into others way in competing to get a
    same resource ? Mutual Exclusion.
  • 3. How to make proper sequencing when
    dependencies are present among processes such as
    producers and consumers ? Synchronization.

4
Interprocess CommunicationRace Conditions
  • Two processes want to access shared memory at
    same time

5
Race Conditions
  • 1.Process_A is running
  • Read in
  • Store in to next_free_slot
  • 2.Process_A is switched to Process_B
  • 3.Process_B is running
  • Read in
  • Store in to next_free_slot
  • Write a filename at the slot 7
  • 4.Process_B is switched to Process_A
  • 1.

5.Process_A is running Write a filename to
print at the slot 7
6
Race Conditions, Critical Section(or Region)and
Mutual Exclusion
  • Race conditions Two or more processes are
    competing together to get certain shared
    resources.
  • Critical Section( or Critical Region) That part
    of the program where the shared memory is
    accessed.
  • Mutual Exclusion Making sure that if one
    process is using a shared resource the other
    processes will be excluded from doing the same
    thing.

7
Critical Regions (1)
  • Four conditions to provide mutual exclusion
  • 1. No two processes simultaneously in critical
    region
  • 2. No assumptions made about speeds or numbers of
    CPUs
  • 3. No process running outside its critical region
    may block another process
  • 4. No process must wait forever to enter its
    critical region

8
Critical Regions (2)
  • Mutual exclusion using critical regions

9
Mutual Exclusion with Busy-Waiting
  • Disabling Interrupts
  • The simplest solution is to have each process
    disable all interrupts just after entering its
    critical section and re-enable them just before
    leaving it.
  • This approach is generally unattractive because
    it is unwise to give user processes the power to
    turn off interrupts.
  • Suppose that one of them did it, and never
    turned them on again. ???
  • Disabling interrupts is often a useful technique
    within the OS but is not appropriate as a general
    mutual exclusion mechanism for user processes.

10
Mutual Exclusion by Busy-WaitingLock Variables
P1
P0
  • while (true)
  • acquire lock
  • critical section
  • release lock
  • remainder section

while (true) acquire lock critical
section release lock remainder
section
What will happen in this case?
11
Mutual Exclusion with Busy Waiting
  • Proposed solution to critical region problem
  • (a) Process 0. (b) Process 1.

12
Strict Alternation
//1 msec for execution
//10min for execution
What will happen in this case? Spin lock A
lock that uses busy waiting.
13
Mutual Exclusion with Busy Waiting
  • Peterson's solution for achieving mutual exclusion

14
TSL Instruction
  • TSL RX, LOCK Test Set Lock
  • It reads the contents of the memory word LOCK
    into register RX and then stores a nonzero value
    at the memory address LOCK.
  • LOCK shared variable
  • 0 any process may set it to 1 using the TSL
    and read or write the shared memory.
  • When it is done, the process sets LOCK back
    to 0.

15
Mutual Exclusion with Busy Waiting
  • Entering and leaving a critical region using the
  • TSL instruction

16
Sleep and Wakeup
  • The drawback of Petersons method and the
    solution by TSL ?
  • Busy-Waiting
  • H Process with High Priority
  • L Process with Low Priority
  • When H becomes ready to run with L in CS,
  • H now begins to run. What will happen?

17
Sleep and Wakeup
18
Producer-Consumer Problem
  • Is there any racing problem in the previous
    Producer-Consumer problem?
  • 1.Consumer reads count0
  • 2.SchedulerProducer running, Consumer ready
  • 3.Producer inserts an item in the buffer, and
    count
  • 4.Producer calls wakeup to wake the consumer up.
  • 5.Consumer next runs, and goes to sleep(Why?).
  • 6.Sooner or later the producer will fill up the
    buffer and also go to sleep. Both will sleep
    forever.
  • Which one causes this problem and How to solve?

19
Semaphore
  • Synchronization tool that does not require
    busy-waiting.
  • Semaphore S integer variable.
  • Two standard operations to modify S
  • acquire( ) or down( )// originally P( )
  • release( ) or up( ) // originally V( )
  • acquire( )
  • while value lt 0
  • value--
  • release( )
  • value

20
Semaphore
  • Binary Semaphore? for MUTEX
  • Counting Semaphore? for Synchronization
  • acquire( )
  • value--
  • if(value lt0)
  • add this process P
  • to waiting list

release() value if( value gt 0) remove
P from the waiting list wakeup( P)
21
Semaphore
  • Checking the value, changing it, and possibly
    going to sleep, is all done as a single,
    invisible atomic action.
  • This atomicity is absolutely essential to solving
    synchronization problems and avoiding race
    conditions.
  • How can we implement down( ) and up( ) in
    indivisible way?

22
Semaphores
  • The producer-consumer problem using semaphores

23
Mutexes
  • Implementation of mutex_lock and mutex_unlock

24
Classical IPC ProblemDining Philosophers
ProblemDijkstra, 1965
  • The life of a philosopher consists of alternate
    period eating and thinking.
  • When a philosopher gets hungry, she tries to
    acquire her left and right fork, one at a time,
    in either order.
  • If successful in acquiring two forks, she eats
    for a while, then puts down the forks, and
    continues to think.
  • Question Can you write a program for each
    philosopher that never gets stuck?

25
Classical IPC ProblemsDining Philosophers (1)
  • Philosophers eat/think
  • Eating needs 2 forks
  • Pick one fork at a time
  • How to prevent deadlock

Deadlock
26
Dining Philosophers (2)
  • Any Problem?

27
Dining Philosophers (3)
  • Solution to dining philosophers problem (part 1)

28
Dining Philosophers (4)
  • Solution to dining philosophers problem (part 2)

29
Readers and Writers Problem Courtois, 1971
  • The Dining Philosophers problem is useful for
    modeling processes that are competing for
    exclusive access to a limited number of
    resources.
  • This Readers and Writers problem models access to
    a database ,for example, an airline reservation
    system with many competing processes wishing to
    read and write it.

30
The Readers and Writers Problem
  • A solution to the readers and writers problem

31
Sleeping Barber Problem
  • The barber shop has one barber, one barber chair,
    and n chairs for waiting customers.
  • If there are no customers present, the barber
    sits down in barber chair and fall asleep as in
    the Fig.
  • When a customer arrives, he has to wake up the
    sleeping barber.
  • If additional customers arrive while the barber
    is cutting a customers hair, they either sit
    down if there are empty chairs or leave the shop
    if all chairs are full.
  • Problem Program the barber and the customers,
    without getting into race conditions.

32
The Sleeping Barber Problem (1)
33
The Sleeping Barber Problem (2)
Solution to sleeping barber problem.
Write a Comment
User Comments (0)
About PowerShow.com