CS 241 Section Week - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

CS 241 Section Week

Description:

CS 241 Section Week – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 33
Provided by: Alej198
Category:

less

Transcript and Presenter's Notes

Title: CS 241 Section Week


1
CS 241 Section Week 6(10/02/08)
2
Topics This Section
  • MP 3
  • MP 4
  • Semaphores
  • Problems
  • Recap of Classical Synchronization Problems
  • HW2

3
MP 3
4
MP3 Review
  • You need to fill in 3 statistics functions
  • float average_response_time()
  • First run time - arrival time
  • float average_wait_time()
  • Completion time arrival time running time
  • float average_turnaround_time()
  • Completion time arrival time
  • Your job queue may need to maintain time
    information in order to compute these three
    functions
  • Complication occurs when you have job completion
    and job arrival at the same time
  • How to compute the response time?

5
MP 4
6
MP4 Forward
  • The scenario

7
MP4 Forward
  • Communication is done through shared memories
  • client_resultclient_id
  • You need to deal with three semaphores
  • queue_semaphore
  • To protect access to the queue
  • server_notify_semaphore
  • Client notify the server when data is added to
    the queue
  • client_notify_semaphore
  • Array of semaphores
  • Client ID is used to access the semaphores
  • Server notify individual client a response is
    ready

8
Semaphores
9
Example
  • int N 1000000
  • int x 0
  • int main(int argc, char argv)
  • pthread_t threadCountUp,
    threadCountDown
  • pthread_create(threadCountUp,
    NULL, countUp, NULL)
  • pthread_create(threadCountDown,
    NULL, countDown, NULL)

10
Example
  • void countUp()
  • int i
  • for (i 0 i lt N i)
  • int c x
  • c
  • x c

void countDown() int i for (i 0 i lt N
i) int c x c-- x c
11
Semaphores
  • Thread1 did x N times.
  • Thread2 did x-- N times.
  • Ideal result x is at its initial value.
  • Actual result x is off a little.

12
Semaphores
  • To fix this
  • A thread must read, update, and write x back to
    memory without any other threads interacting with
    x.
  • This concept is an atomic operation.

13
Semaphores
  • Conceptually, we would want an atomic scope
  • void countUp() atomic int c
    x c x c //
    But this doesnt exist

14
Semaphores
  • Semaphores provides a locking mechanism to give
    us atomicity.
  • void countUp() sem_wait(sema) int c
    x c x c sem_post(sema)

15
Semaphores
  • Semaphores provides a locking mechanism to give
    us atomicity.
  • void countUp() sem_wait(sema)
    LOCKS int c x c x c
    sem_post(sema) UNLOCKS

16
Semaphores
  • To use a semaphore, you have to define it. Three
    steps to defining a semaphore
  • 1. Include the header file
  • include ltsemaphore.hgt

17
Semaphores
  • To use a semaphore, you have to define it. Three
    steps to defining a semaphore
  • 2. Declare the semaphore
  • sem_t sema (Declare this in a global scope.)

18
Semaphores
  • To use a semaphore, you have to define it. Three
    steps to defining a semaphore
  • 3. Initialize the semaphore
  • sem_init(sema, 0, 1)

19
Semaphores
  • sem_init(sema, 0, 1)
  • sema Your declared sem_t.
  • 0 0 Thread Sync
  • 1 Total of one thread
    inside a locked
    section of code.

20
Semaphores
  • Three steps to starting them
  • Include include ltsemaphore.hgt
  • Define sem_t sema
  • Init sem_init(sema, 0, 1)

21
Semaphores
  • Two functions to use them
  • Acquiring the lock
    sem_wait(sema)
  • Releasing the lock
    sem_post(sema)

22
Semaphores
  • Example Revisited sem_wait() read x
    x context sw
    ? sema_wait() write x ? thread
    blocked sem_post() unlocked
    ? //

23
Problems
24
Problem 1 - Use semaphores to ensure order
  • machex3.c creates two threads to print out Hello
    World.
  • Use semaphores to ensure that that World\nHello
    is never printed instead of Hello World.

int main(int argc, char argv) pthread_t
hello, world pthread_create(hello, NULL,
hello_thread, NULL) pthread_create(world,
NULL, world_thread, NULL) pthread_join(hello,
NULL) pthread_join(world, NULL) return 0
void hello_thread(void arg) sleep(2) fprint
f(stderr, "Hello ") void world_thread(void
arg) sleep(1) fprintf(stderr,
"World!\n")
25
Problem 1 - Use semaphores to ensure order
sem_t sem int main(int argc, char
argv) pthread_t hello, world sem_init(sem
, 0, 0) pthread_create(hello, NULL,
hello_thread, NULL) pthread_create(world,
NULL, world_thread, NULL) pthread_join(hello,
NULL) pthread_join(world, NULL) return 0
void hello_thread(void arg) sleep(2) fprint
f(stderr, "Hello ") sem_post(sem) void
world_thread(void arg) sleep(1) sem_wait(s
em) fprintf(stderr, "World!\n") sem_post(sem)

26
Problem 2 Two semaphores
  • machex4.c creates two threads that both want
    access to two semaphores.
  • If you run this program, the program appears to
    stop running after a bit. Why?

sem_t sem1, sem2 int main(int argc, char
argv) pthread_t t1, t2 sem_init(sem1, 0,
1) sem_init(sem2, 0, 1) pthread_create(t1,
NULL, p1, NULL) pthread_create(t2, NULL, p2,
NULL) pthread_join(t1, NULL) pthread_join(t2,
NULL) return 0
27
Problem 2 Two semaphores
void p1(void arg) while (1)
printf("p1 sem_wait(sem1)\n") sem_wait(se
m1) printf("p1 sem_wait(sem2)\n") sem_wait(
sem2) printf("p1 locked\n") printf("p1
sem_post(sem2)\n") sem_post(sem2) printf("p
1 sem_post(sem1)\n") sem_post(sem1) printf
("p1 unlocked\n") return NULL
void p2(void arg) while (1)
printf("p2 sem_wait(sem2)\n") sem_wait(se
m2) printf("p2 sem_wait(sem1)\n") sem_wait(
sem1) printf("p2 locked\n") printf("p2sem_p
ost(sem1)\n") sem_post(sem1) printf("p2
sem_post(sem2)\n") sem_post(sem2) printf("p
2 unlocked\n") return NULL
28
Problem 2 Two semaphores
sem1_wait()
sem2_wait()
sem1_wait()
sem2_wait()
DEADLOCK!
How to fix it??
29
Classical Synchronization Problems
30
Producer consumer problem
  • Producers insert items
  • Consumers remove items
  • Shared bounded buffer
  • Three semaphores
  • Slots initialize to N, to prevent buffer
    overflow
  • Items initialize to 0, to prevent buffer
    underflow
  • Mutex to protect accesses to shared buffer
    pointers.

31
Reader writer problem
  • Multiple readers can read the data simultaneously
  • Only one writer can write the data at any time
  • A reader and a writer cannot in critical section
    together.
  • One global reader counter and 2 semaphores
  • Semaphore wrt to enforce mutual exclusion
  • Semaphore x to protect access to the global
    reader counter

32
HW2
  • Homework 2 due on next Monday lecture 10/6
Write a Comment
User Comments (0)
About PowerShow.com