CS 2200 - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

CS 2200

Description:

unlocked we can. change status to. RUNNING. Same Timer Interrupt (Alarm) TCB. Context ... Unlocks scoreboard. Fills buffer row with random ints. Locks ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 58
Provided by: BillL161
Category:
Tags: unlocked

less

Transcript and Presenter's Notes

Title: CS 2200


1
CS 2200
  • Thread Synchronization

2
Whats wrong with Semaphores?
  • void wait(int s) / P /
  • while (S lt 0)
  • / spin /
  • s s - 1
  • void signal(int s) / V /
  • s s 1

3
Whats wrong with Semaphores?
  • void wait(int s) / P /
  • while (S lt 0)
  • / spin /
  • s s - 1
  • void signal(int s) / V /
  • s s 1

4
Solution
  • Block thread while waiting.
  • How?
  • Lets examine a simple threads package...

5
Typical API
  • Thread_create
  • Thread_yield
  • Thread_exit
  • Mutex_create
  • Mutex_lock
  • Mutex_unlock
  • Alarm

6
Program starts in main
  • Create linked list of thread control blocks
  • Context
  • Status
  • RUNNING
  • READY
  • DONE
  • BLOCKED
  • Pointer to mutex thread is waiting for.
  • Start timer (Request from OS)
  • Which will be handled by "Alarm"

7
Thread_Init
I even created a TCB for myself! But, I'm so
lonely...
8
Thread_create
  • Make a new TCB
  • malloc space for the new threads stack
  • Get current context
  • Modify context
  • stack address
  • stack size
  • starting address
  • Note Context is in TCB
  • Add TCB to linked list (Ready)

9
Thread_create (3 times)
I think I'll make me a mutex
10
Mutex_create
  • malloc new mutex variable (struct)
  • status UNLOCKED
  • Pointer to thread holding lock

11
Mutex_create
I just made a mutex!
12
Alarm
  • Reset alarm
  • Thread_yield

Thread_yield
  • If no other threads
  • return
  • Else
  • Make current threads status READY (if not DONE or
    BLOCKED)
  • Make next thread RUNNING
  • Context switch current with next

13
Timer Interrupt (Alarm)
14
Mutex_lock
  • If mutex is already locked
  • Mark thread as blocked
  • Note in TCB mutex being waited on
  • Thread_yield
  • Else
  • Make mutex locked
  • Point mutex at thread holding lock

15
Lock Mutex
I have the mutex!
16
Timer Interrupt (Alarm)
17
Mutex_lock
  • If mutex is already locked
  • Mark thread as blocked
  • Note in TCB mutex being waited on
  • Thread_yield
  • Else
  • Make mutex locked
  • Point mutex at thread holding lock

18
Try to Lock Mutex (Fails)
  • If mutex is already locked
  • Mark thread as blocked
  • Note in TCB mutex being waited on
  • Thread_yield
  • Else
  • Make mutex locked
  • Point mutex at thread holding lock

I want the mutex!
19
Timer Interrupt (Alarm)
20
Timer Interrupt (Alarm)
21
Timer Interrupt (Alarm)
Note The yield routine can check each
blocked thread to make sure mutex is still locked
Still waiting!
22
Timer Interrupt (Alarm)
23
Timer Interrupt (Alarm)
Guess I don't need the mutex anymore
24
Mutex_unlock
  • Check to see if thread trying to unlock is the
    holder of the mutex
  • Set mutex status to unlock

25
Unlocks Mutex
26
Timer Interrupt (Alarm)
Since mutex is unlocked we can change status to
RUNNING
27
Same Timer Interrupt (Alarm)
Since mutex is unlocked we can change status to
RUNNING
28
Same Timer Interrupt (Alarm)
And give the mutex to the requestor
29
Timer Interrupt (Alarm)
I'm done!
30
Thread_exit
  • Make status DONE
  • Thread_yield

31
Thread Exits
32
etc.
33
Questions?
34
Consider
  • lock(m)
  • while(resource busy)
  • wait(c, m)
  • resource busy
  • unlock(m)
  • / doing work... /
  • lock(m)
  • resource available
  • unlock(m)
  • signal(c)

35
Example
  • pthreads program

36
1000
Scoreboard
10
sorter
sorter
sorter
7
sorter
sorter
producer
sorter
sorter
37
Operation
Scoreboard
  • main starts up operation
  • producer thread
  • Locks scoreboard
  • Waits for row to be available
  • Marks row as in use
  • Unlocks scoreboard
  • Fills buffer row with random ints
  • Locks scoreboard
  • Marks row as sortable
  • Unlocks scoreboard

Row
Status
0
AVAILABLE
1
AVAILABLE
2
AVAILABLE
3
AVAILABLE
4
AVAILABLE
5
AVAILABLE
...
...
AVAILABLE 0 define FILLING 1 define SORTABLE
2 define SORTING 3
38
Operation
Scoreboard
  • main starts up operation
  • producer thread
  • Locks scoreboard
  • Waits for row to be available
  • Marks row as in use
  • Unlocks scoreboard
  • Fills buffer row with random ints
  • Locks scoreboard
  • Marks row as sortable
  • Unlocks scoreboard

Row
Status
0
FILLING
1
AVAILABLE
2
AVAILABLE
3
AVAILABLE
4
AVAILABLE
5
AVAILABLE
...
...
AVAILABLE 0 define FILLING 1 define SORTABLE
2 define SORTING 3
39
Operation
Scoreboard
  • main starts up operation
  • producer thread
  • Locks scoreboard
  • Waits for row to be available
  • Marks row as in use
  • Unlocks scoreboard
  • Fills buffer row with random ints
  • Locks scoreboard
  • Marks row as sortable
  • Unlocks scoreboard

Row
Status
0
SORTABLE
1
AVAILABLE
2
AVAILABLE
3
AVAILABLE
4
AVAILABLE
5
AVAILABLE
...
...
AVAILABLE 0 define FILLING 1 define SORTABLE
2 define SORTING 3
40
Operation
Scoreboard
  • sorter threads
  • Lock scoreboard
  • Wait for row to be sortable
  • Mark row as sorting
  • Unlock scoreboard
  • Sort row (using bubblesort)
  • Lock scoreboard
  • Mark row as available

Row
Status
0
SORTABLE
1
AVAILABLE
2
AVAILABLE
3
AVAILABLE
4
AVAILABLE
5
AVAILABLE
...
...
41
Operation
Scoreboard
  • sorter threads
  • Lock scoreboard
  • Wait for row to be sortable
  • Mark row as sorting
  • Unlock scoreboard
  • Sort row (using bubblesort)
  • Lock scoreboard
  • Mark row as available

Row
Status
0
SORTING
1
AVAILABLE
2
AVAILABLE
3
AVAILABLE
4
AVAILABLE
5
AVAILABLE
...
...
42
Operation
Scoreboard
  • sorter threads
  • Lock scoreboard
  • Wait for row to be sortable
  • Mark row as sorting
  • Unlock scoreboard
  • Sort row (using bubblesort)
  • Lock scoreboard
  • Mark row as available

Row
Status
0
AVAILABLE
1
AVAILABLE
2
AVAILABLE
3
AVAILABLE
4
AVAILABLE
5
AVAILABLE
...
...
43
  • / td -- Thread Demo /
  • include ltpthread.hgt
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • define SIZE 1000 / Size of sort buffers
    /
  • define SCOUNT 7 / Number of sorters
    /
  • define ROWS 10 / Number of buffers
    /
  • define STEPS 22 / Buffers full of data to
  • sort
    /
  • define NONEFOUND -1 / Used in searching
    /

44
  • / Allowable states for row buffers (in
    scoreboard) /
  • enum AVAILABLE, FILLING, SORTABLE, SORTING
  • enum NO, YES
  • static int dataROWSSIZE / The buffers
    /
  • static int available / Num of buffers
  • available to fill
    /
  • static int sortable / How many are
  • avail to sort
    /
  • static int scoreboardROWS / Row access
  • scoreboard /
  • static int run / Flag used to
  • shutdown
  • gracefully /

45
  • / Scoreboard mutex lock /
  • static pthread_mutex_t scorelock
  • / The producer can work! /
  • static pthread_cond_t pcanwork
  • / A sorter can work! /
  • static pthread_cond_t sorterworkavail
  • / Function prototypes /
  • static void producer()
  • static void sorter()
  • void sort(int)

Creating necessary mutex and condition variables
46
  • int main(int argc, char argv)
  • pthread_t producer_id
  • pthread_t sorter_idSCOUNT
  • int i
  • available ROWS
  • sortable 0
  • run YES

Threads have id numbers!
47
  • pthread_mutex_init(scorelock, NULL)
  • pthread_cond_init(pcanwork, NULL)
  • pthread_cond_init(sorterworkavail, NULL)
  • for (i0 i lt ROWS i)
  • scoreboardi AVAILABLE
  • pthread_create
  • (producer_id, NULL, producer,
    NULL)
  • for(i 0 i lt SCOUNT i)
  • pthread_create
  • (sorter_idi, NULL, sorter,
    NULL)
  • printf("maingt All threads running\n")
  • pthread_join(producer_id, NULL)

48
  • / After the producer is finished we send
    signals
  • to all sorters to wake up and see that
    they
  • should quit /
  • for(i 0 i lt SCOUNT i)
  • pthread_cond_signal(sorterworkavail)
  • for(i 0 i lt SCOUNT i)
  • pthread_join(sorter_idi, NULL)
  • printf("Normal Termination\n")
  • return 0

49
  • static void producer()
  • int pcount
  • int target
  • int i
  • for(pcount 0 pcount lt STEPS pcount)
  • pthread_mutex_lock(scorelock)
  • while(available 0)
  • pthread_cond_wait(pcanwork,
    scorelock)
  • target NONEFOUND
  • for(i0 ilt ROWS i)
  • if(scoreboardi AVAILABLE)
  • target i
  • available available - 1
  • break

This is the loop which controls the total
number of rows we process
50
  • pthread_mutex_unlock(scorelock)
  • if(target NONEFOUND)
  • printf(" Producer cannot find"
  • " available row!\n")
  • pthread_exit(NULL)
  • printf("pgt Filling row d\n", target)
  • for(i0 i lt SIZE i)
  • datatargeti rand()
  • printf("pgt Row d complete\n", target)
  • pthread_mutex_lock(scorelock)
  • scoreboardtarget SORTABLE
  • sortable sortable 1
  • pthread_mutex_unlock(scorelock)
  • pthread_cond_signal(sorterworkavail)

51
This means that we can quit once we finish all
sorting
  • run NO
  • return NULL
  • / pthread_exit(NULL) /

52
  • static void sorter()
  • int i
  • int target
  • pthread_t me
  • me pthread_self()
  • while(1)
  • pthread_mutex_lock(scorelock)
  • while(sortable 0 run YES)
  • pthread_cond_wait
  • (sorterworkavail,
    scorelock)

53
  • / If the producer says stop and there is
    no
  • work...exit /
  • if(run NO available ROWS)
  • printf(" Sgt x Exiting..."
  • "prod done no filled rows\n",
    me)
  • pthread_mutex_unlock(scorelock)
  • pthread_exit(NULL)
  • target NONEFOUND
  • for(i 0 i lt ROWS i)
  • if(scoreboardi SORTABLE)
  • target i
  • sortable sortable - 1
  • scoreboardtarget SORTING
  • break

54
  • if(target NONEFOUND)
  • / We get here if the producer is
    finished
  • and some threads are being sorted
    but
  • none are available for sorting /
  • printf("Sgt x couldn't find thread to
    "
  • "sort.\n", me)
  • pthread_mutex_unlock(scorelock)
  • pthread_exit(NULL)
  • pthread_mutex_unlock(scorelock)
  • printf("Sgt x starting...\n", me)
  • sort(target)
  • printf("Sgt x finishing min d max
    d\n",
  • me, datatarget0,
  • datatargetSIZE-1)

55
  • pthread_mutex_lock(scorelock)
  • scoreboardtarget AVAILABLE
  • available available 1
  • pthread_mutex_unlock(scorelock)
  • pthread_cond_signal(pcanwork)

56
  • void sort(int target)
  • int outer
  • int inner
  • int temp
  • outer SIZE - 1
  • for(outer SIZE - 1 outer gt 0 outer--)
  • for(inner0 inner lt outer inner)
  • if(datatargetinner gt
  • datatargetinner1)
  • temp datatargetinner
  • datatargetinner
  • datatargetinner
    1
  • datatargetinner1 temp

Bubble
Sort
57
Questions?
Write a Comment
User Comments (0)
About PowerShow.com