hw1 and synchronization queuing n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
HW1 and Synchronization & Queuing PowerPoint Presentation
Download Presentation
HW1 and Synchronization & Queuing

Loading in 2 Seconds...

play fullscreen
1 / 57

HW1 and Synchronization & Queuing - PowerPoint PPT Presentation


  • 105 Views
  • Uploaded on

HW1 and Synchronization & Queuing. CS241 Discussion Section Spring 2009 Week 7. TA Review. Optional TA Review Session The TAs will hold an optional review session to answer last-minute exam questions: Saturday, March 14, 2009 2pm – 4pm 2405 SC. Outline.

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 'HW1 and Synchronization & Queuing' - penda


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
hw1 and synchronization queuing
HW1 andSynchronization & Queuing
  • CS241 Discussion Section
  • Spring 2009
  • Week 7
ta review
TA Review

Optional TA Review Session

The TAs will hold an optional review session to answer last-minute exam questions: Saturday, March 14, 2009 2pm – 4pm 2405 SC

outline
Outline
  • HW1 Discussion
  • Synchronization problems
    • Producer-consumer
    • Dining Philosophers
  • Queueing theory
slide4
HW1
  • a) int *x, q=0;
  • *x = q; (a): ________
  • b) int b[2]; *b = 20; (b): ________
  • c) char greet[80]; strcpy (‘‘Hello’’, greet); (c): ________
slide5
HW1
  • a) int *x, q=0;
  • *x = q; (a): Incorrect
  • b) int b[2]; *b = 20; (b): Correct
  • c) char greet[80]; strcpy (‘‘Hello’’, greet); (c): Incorrect
slide6
HW1
  • d) int *p, q[2]; p = malloc (sizeof (int));
  • *p = 3;
  • q[2]=*p; (d): ________
  • e) int *x, y; x = &y; *x = 10; (e): ________
slide7
HW1
  • d) int *p, q[2]; p = malloc (sizeof (int));
  • *p = 3;
  • q[2]=*p; (d): Incorrect
  • e) int *x, y; x = &y; *x = 10; (e): Correct
slide8
HW1
  • Which scheduling policy
  • a) minimizes average task waiting time? _______
  • b) minimizes average task response time? ________
  • c) suffers the convoy effect? ________
  • d) has the largest context switch overhead? _______
  • e) may suffer a starvation effect? ______
slide9
HW1
  • Which scheduling policy
  • a) minimizes average task waiting time? ___SJF_____
  • b) minimizes average task response time? ____RR_____
  • c) suffers the convoy effect? ____FIFO_____
  • d) has the largest context switch overhead? ____RR_____
  • e) may suffer a starvation effect? ___SJF___
slide10
HW1
  • while (x > 0) {};
  • x ++; /* assume this line is atomic */
  • execute critical section;
  • x – –; /* assume this line is atomic */
  • Mutual exclusion:
  • Progress:
slide11
HW1
  • while (x > 0) {};
  • x ++; /* assume this line is atomic */
  • execute critical section;
  • x – –; /* assume this line is atomic */
  • Mutual exclusion: No
  • Progress: Yes
slide12
HW1
  • x2 = 1; x1 = 1;
  • while (x1 != 0) {}; while (x2 != 0) {};
  • critical section; critical section;
  • x2 = 0; x1 = 0;
  • Mutual exclusion:
  • Progress:
slide13
HW1
  • x2 = 1; x1 = 1;
  • while (x1 != 0) {}; while (x2 != 0) {};
  • critical section; critical section;
  • x2 = 0; x1 = 0;
  • Mutual exclusion: Yes
  • Progress: No
slide14
HW1
  • while (y = = 1) {}; while (y = = 0) {};
  • y = 1; y = 0;
  • critical section; critical section;
  • Mutual exclusion:
  • Progress:
slide15
HW1
  • while (y = = 1) {}; while (y = = 0) {};
  • y = 1; y = 0;
  • critical section; critical section;
  • Mutual exclusion: No
  • Progress: No
slide16
HW 1
  • Function g() calls function f().
  • Function g() creates a POSIX thread that executes function f().
  • Kill –
  • Exec –
  • Exit –
slide17
HW 1
  • Function g() calls function f().
    • control passes from function g() to function f() then returns to g() when f() ends.
  • b) Function g() creates a POSIX thread that executes function f().
  • when the new thread is created, f() executes concurrently with g().
  • Kill – sends a signal to a process
  • Exec – runs a new program in the current address space
  • Exit – terminates a unix process
slide18
HW1
  • - Change behavior of SIGUSR1
  • Block SIGINT
  • Which of these?
  • sleep alarm sigwait
  • sigprocmask sigaction sigsuspend
slide19
HW1
  • - Change behavior of SIGUSR1
  • Block SIGINT
  • Which of these?
  • sleep alarm sigwait
  • sigprocmask sigaction sigsuspend
slide20
HW1
  • a) The turnaround time minus the _______ time is equal to the waiting time.
  • b) The turnaround time is the time from the process creation time to its _____ time.
  • b) The total time a process spends in queues is called ______ time
  • d) Response time is the interval from ______ time to _____ time
slide21
HW1
  • a) The turnaround time minus the execution time is equal to the waiting time.
  • b) The turnaround time is the time from the process creation time to its finish time.
  • b) The total time a process spends in queues is called waiting time
  • d) Response time is the interval from arrival time to start time
example 1 producer consumer problem
Example 1:Producer-Consumer Problem
  • Producers insert items
  • Consumers remove items
  • Shared bounded buffer
    • e.g. a circular buffer with an insert and a removal pointer.
producer consumer
Producer-Consumer

insertPtr

removePtr

producer consumer1
Producer-Consumer

insertPtr

removePtr

producer consumer2
Producer-Consumer

insertPtr

removePtr

producer consumer3
Producer-Consumer

insertPtr

removePtr

producer consumer4
Producer-Consumer

insertPtr

removePtr

producer consumer5
Producer-Consumer

insertPtr

removePtr

producer consumer6
Producer-Consumer

insertPtr

removePtr

producer consumer7
Producer-Consumer

BUFFER FULL: Producer must be blocked!

insertPtr

removePtr

producer consumer8
Producer-Consumer

insertPtr

removePtr

producer consumer9
Producer-Consumer

removePtr

insertPtr

producer consumer10
Producer-Consumer

removePtr

insertPtr

producer consumer11
Producer-Consumer

removePtr

insertPtr

producer consumer12
Producer-Consumer

removePtr

insertPtr

producer consumer13
Producer-Consumer

removePtr

insertPtr

producer consumer14
Producer-Consumer

removePtr

insertPtr

producer consumer15
Producer-Consumer

BUFFER EMPTY: Consumer must be blocked!

removePtr

insertPtr

challenge
Challenge
  • Need to prevent:
  • Buffer Overflow
    • Producer writing when there is no storage
  • Buffer Underflow
    • Consumer reading nonexistent data
  • Race condition
    • Two processes editing the list at the same time
synchronization variables
Synchronization variables
  • Create these variables to prevent these problems:
  • items semaphore
    • Counts how many items are in the buffer
    • Cannot drop below 0
  • slots semaphore
    • Counts how may slots are available in the buffer
    • Cannot drop below 0
  • list mutex
    • Makes buffer access mutually exclusive
producer consumer example
Producer-Consumer Example
  • ds7-problem1.c shows an example implementation for one producer and one consumer, but without synchronization code.
  • Running it shows
    • Buffer underflows
      • Nonsense data is consumed
    • Buffer overflows
      • Unconsumed data is overwritten
dining philosopher challenge
Dining Philosopher Challenge
  • { Think | Eat }
  • N Philosophers circular table with N chopsticks
  • To eat the Philosopher must first pickup two chopsticks
  • ith Philosopher needs ith&i+1stchopstick
  • Only put down chopstick when Philosopher has finished eating
  • Devise a solution which satisfies mutual exclusion but avoids starvation and deadlock
the simple implementation
The simple implementation
  • while(true) {
  • think()
  • lock(chopstick[i])
  • lock(chopstick[(i+1) % N])
  • eat()
  • unlock(chopstick[(i+1) % N])
  • unlock(chopstick[i])
  • }
  • Does this work?
deadlocked
Deadlocked!
  • When every philosopher has picked up his left chopstick, and no philosopher has yet picked up his right chopstick, no philosopher can continue.
  • Each philosopher waits for his right neighbor to put a chopstick down, which he will never do.
  • This is a deadlock.
formal requirements for deadlock
Formal Requirements for Deadlock
  • Mutual exclusion
    • Exclusive use of chopsticks
  • Hold and wait condition
    • Hold 1 chopstick, wait for next
  • No preemption condition
    • Cannot force another to undo their hold
  • Circular wait condition
    • Each waits for next neighbor to put down chopstick
  • The simple implementations satisfies all of these.
problems for week 7
Problems for Week 7
  • 1) ds7-problem1.c contains producer-consumer code.
    • Use the provided semaphores and mutexes to prevent buffer overflows, underflows, and race conditions.
    • Think: When should the consumer block? When should the producer block?
problems for week 7 contd
Problems for Week 7 (contd)
  • 2) ds7-problem2.c contains dining philosophers code.
    • Alter the program to prevent deadlock. There are multiple ways to do this.
    • Think: What are the conditions of deadlock? Can any of them be removed?
queuing diagram for processes
Queuing Diagram for Processes

Exit

Start

Ready Queue

CPU

Time Slice

Event

Event Queue

queueing theory
Queueing Theory
  • Steady state
  • Poisson arrival with l constant arrival rate (customers per unit time) each arrival is independent.
  • P( t£t ) = 1- e–lt

0

0.5

1

t

Av λ

analysis of queueing behavior
Analysis of Queueing Behavior

Probability n customers arrive in time interval t is:

e–lt (lt) n/ n!

Assume random service times (also Poisson): m constant service rate (customers per unit time)

queuing diagram for processes1
Queuing Diagram for Processes

Exit

Start

ARRIVAL RATE l

SERVICE RATE m

Time Slice

SERVICE RATE m1

ARRIVAL RATE l1

Event Queue

useful facts from queuing theory
Useful Facts From Queuing Theory

Wq= mean time a customer spends in the queue

l = arrival rate

Lq = l Wq number of customers in queue

W = mean time a customer spends in the system

L = l W ( Little's theorem) number of customers in the system

In words – average length of queue is arrival

rate times average waiting time

analysis of single server queue
Analysis of Single Server Queue
  • Server Utilization: r = l/m

Time in System: W = 1/(m-l)

Time in Queue: Wq = r/(m-l)

Number in Queue (Little): Lq = r2/(1-r)

poisson arrival service rates sum
Poisson Arrival & ServiceRates Sum

ARRIVAL RATE l1

Server

Input Queue

SERVICE RATE m

ARRIVAL RATE l2

l =l1+l2

ta review1
TA Review

Optional TA Review Session

The TAs will hold an optional review session to answer last-minute exam questions: Saturday, March 14, 2009 2pm – 4pm 2405 SC