Project: ProcessThread Synchronization - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Project: ProcessThread Synchronization

Description:

solve it using (1) mutex (2) software solution. compare performance ... pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER; locking mutex: thread blocks if already locked ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 19
Provided by: dc7
Category:

less

Transcript and Presenter's Notes

Title: Project: ProcessThread Synchronization


1
Project Process/Thread Synchronization
2
Assignment
  • Using Pthreads
  • set up a race condition
  • solve it using (1) mutex (2) software solution
  • compare performance
  • develop P/V using (1) mutex (2) software sol.
  • compare performance
  • implement bounded buffer (groups only)

3
Creating Pthreads
  • on Unix, compile as
  • gcc -lpthread prog.c
  • program structure
  • include ltstdlib.hgt
  • include ltpthread.hgt
  • / global declarations /
  • void foo(void arg)...
  • / func for thread /
  • int main()...

4
Creating Pthreads
  • to create a new thread
  • pthread_create(t, a, (void)f, p)
  • t variable of type pthread_t (name of new
    thread)
  • a variable of type pthread_attr_t (various
    attributes, e.g. RR scheduling)
  • f function to run by new thread
  • p parameter for f

5
Creating Pthreads
  • Example thread without attributes
  • thread function foo single argument of type
    void, returns void
  • void foo(void arg) ...
  • int main()
  • pthread_t th1
  • pthread_create(
  • th1, NULL, (void)foo, NULL )
  • ...

6
Creating Pthreads
  • using scheduling attribute (RR)
  • define attribute variable
  • pthread_attr_t atr
  • create attribute structure
  • pthread_attr_init(atr)
  • set attribute
  • pthread_attr_setschedpolicy(
  • atr, SCHED_RR)

7
Creating Pthreads
  • int main()
  • pthread_t th1
  • pthread_attr_t att
  • pthread_attr_init(att) pthread_attr_setschedp
    olicy(
  • att, SCHED_RR)
  • ...
  • pthread_create(
  • th1, att, (void)foo, NULL )
  • ...

8
Creating Pthreads
  • main needs to wait for child threads
  • int main()
  • pthread_t th1
  • ...
  • pthread_create(
  • th1, att, (void)foo, NULL )
  • ...
  • pthread_join(th1, NULL)

9
Setting up a race condition
  • create 2 concurrent threads both do
  • counter 0
  • do
  • tmp1 accnt1
  • tmp2 accnt2
  • r rand()
  • accnt1 tmp1 r
  • accnt2 tmp2 - r
  • counter
  • while ( accnt1 accnt2 0 )
  • print(counter)

10
Solving the Critical Section
  • Solution 1 use mutex locks of Pthreads
  • mutex is a variable of type pthread_mutex_t
  • static mutex with default attribute use init
    macro
  • pthread_mutex_t mu PTHREAD_MUTEX_INITIALIZER
  • locking mutex thread blocks if already locked
  • pthread_mutex_lock(mu)
  • unlocking mutex
  • pthread_mutex_unlock(mu)

11
Solving the Critical Section
  • Solution 2 Petersons software solution
  • th1 while (1)
  • c1 1
  • will_wait 1
  • while (c2 (will_wait1))
  • CS1 c1 0
  • th2 while (1)
  • c2 1
  • will_wait 2
  • while (c1 (will_wait2))
  • CS2 c2 0
  • CS is the code between first read and second write

12
Performance comparison
  • implement the two solutions
  • repeat the do loop n times, measure execution
    time of both solutions
  • repeat several times to get averages
  • how to measure time
  • at command line
  • time program_to_be_timed
  • inside program
  • include ltsys/time.hgt
  • gettimeofday()

13
Implementing P/V
  • Solution 1 use mutex locks of Pthreads
  • binary semaphores
  • Pb(sb) if sb1, set sb0 and proceed
  • else wait
  • Vb(sb) sb1

14
Implementing P/V
  • Solution 1 use mutex locks of Pthreads
  • P(s)
  • Pb(sb)
  • s s-1
  • if (s lt 0) Vb(sb) Pb(delay)
  • Vb(sb)
  • V(s)
  • Pb(sb)
  • s s1
  • if (s lt 0) Vb(delay) else Vb(sb)
  • Pb is like pthread_mutex_lock
  • Vb is line pthread_mutex_unlock
  • use two mutex variables to implement P/V

15
Implementing P/V
  • Solution 2 use software solution (CS)
  • each semaphore s is an integer
  • access to s is a CS
  • P test s (in CS)
  • if (sgt0) decrement s and proceed
  • if (s0) yield CPU
  • but yields cannot be issued inside CS
    (deadlock)!
  • V increment s

16
Performance comparison
  • implement the two solutions
  • for each solution test the following
  • single thread repeatedly execute P, V, P, V,
  • no blocking, just execution times
  • 2 threads th1 blocks on a single P th2 executes
    n V operations, s initialized to -n
  • tests permanent block of one thread
  • 2 threads th1 executes P, th2 executes V
    (repeatedly)
  • be creative come up with interesting tests

17
Bounded buffer problem
  • (groups only)
  • semaphore e n, f 0, b 1
  • Producer while (1)
  • read element from file 1
  • P(e)P(b) Add element to buffer V(b)V(f)
  • Consumer while (1)
  • P(f)P(b) Take element from buffer V(b)V(e)
  • write element to file 2
  • implement buffer as an array
  • producer and consumer each maintain current index
  • plot number of elements in buffer over time

18
Summary of tasks
  • demonstrate race condition
  • solve race condition using (1) mutex (2)
    software compare performance
  • implement P/V using (1) mutex (2) software
    compare performance
  • implement bounded buffer make sure elements
    fluctuates over time
  • document your design and results (NO LIVE DEMOS)
Write a Comment
User Comments (0)
About PowerShow.com