Parallel Programming with PThreads - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Parallel Programming with PThreads

Description:

program counter. register set. stack space. Threads share: memory space. code section ... Using semaphores. Two processes P1 and P2. Statements S1 and S2. S2 ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 19
Provided by: quinn5
Category:

less

Transcript and Presenter's Notes

Title: Parallel Programming with PThreads


1
Parallel Programming with PThreads
2
Threads
  • Sometimes called a lightweight process
  • smaller execution unit than a process
  • Consists of
  • program counter
  • register set
  • stack space
  • Threads share
  • memory space
  • code section
  • OS resources(open files, signals, etc.)

3
Threads
  • A process is defined to have at least one thread
    of execution
  • A process may launch other threads which execute
    concurrently with the process
  • Switching between threads is faster
  • No memory management issues, etc.
  • Mutual exclusion problems

4
Threads
Process 0
Process 1
regs
regs
mem
code
code
mem
All threads in a process share the same memory
space
5
POSIX Threads
  • Thread API available on many OSs
  • include ltpthread.hgt
  • cc myprog.c o myprog -lpthread
  • Thread creation
  • int pthread_create(pthread_t thread,
    pthread_attr_t attr, void
    (start_routine)(void ), void arg)
  • Thread termination
  • void pthread_exit(void retval)
  • Waiting for Threads
  • int pthread_join(pthread_t th, void
    thread_return)

6
include ltpthread.hgt include ltstdio.hgt int
print_message_function( void ptr ) int x
1 main() pthread_t thread1, thread2
int thread1result, thread2result
char message1 "Hello" char message2
"World" pthread_attr_t
pthread_attr_default NULL
printf("Begin\n") pthread_create(
thread1, pthread_attr_default,
(void)print_message_function, (void)
message1) pthread_create(thread2,
pthread_attr_default,
(void)print_message_function, (void)
message2) pthread_join(thread1, (void
)thread1result) printf("End thread1
with d\n", thread1result)
pthread_join(thread2, (void )thread2result)
printf("End thread2 with d\n",
thread2result) exit(0)
int print_message_function( void ptr )
char message message (char ) ptr
printf("s ", message)
fflush(stdout) return x
7
(No Transcript)
8
Synchronization Primitives
  • int pthread_mutex_init( pthread_mutex_t
    mutex_lock, const pthread_mutexattr_t
    lock_attr)
  • int pthread_mutex_lock( pthread_mutex_t
    mutex_lock)
  • int pthread_mutex_unlock( pthread_mutex_t
    mutex_lock)
  • int pthread_mutex_trylock( pthread_mutex_t
    mutex_lock)

9
include ltpthread.hgt void find_min(void
list_ptr) pthread_mutex_t minimum_value_lock int
minimum_value, partial_list_size main() minim
um_value MIN_INT pthread_init() pthread_mute
x_init(minimum_value_lock, NULL) /inititaliz
e lists etc, create and join threads/ void
find_min(void list_ptr) int
partial_list_ptr, my_min MIN_INT,
i partial_list_ptr (int )list_ptr for (i
0 i lt partial_list_size i) if
(partial_list_ptri lt my_min) my_min
partial_list_ptri pthread_mutex_lock(minimum_v
alue_lock) if (my_min lt minimum_value) minimum
_value my_min pthread_mutex_unlock(minimum_val
ue_lock) pthread_exit(0)
10
Semaphores
  • Synchronization tool provided by the OS
  • Integer variable and 2 operations
  • Wait(s) while (s lt 0) do noop
    /sleep/ s s - 1
  • Signal(s) s s 1
  • All modifications to s are atomic

11
The critical section problem
Shared semaphore mutex 1
repeat wait(mutex) critical
section signal(mutex) remainder
section until false
12
Using semaphores
  • Two processes P1 and P2
  • Statements S1 and S2
  • S2 must execute only after S1

13
Bounded Buffer Solution
Shared semaphore empty n, full 0, mutex 1
repeat produce an item in nextp wait(empty) w
ait(mutex) add nextp to the buffer signal(mut
ex) signal(full) until false
repeat wait(full) wait(mutex) remove an
item from buffer place it in nextc signal(mutex
) signal(empty) consume the item in
nextc until false
14
Readers - Writers (priority?)
Shared Semaphore mutex1, wrt 1 Shared
integer readcount 0
wait(mutex) readcount readcount 1 if
(readcount 1) wait(wrt) signal(mutex) read
the data wait(mutex) readcount readcount -
1 if (readcount 0) signal(wrt) signal(mutex)

wait(wrt) write to the data object signal(wrt)
15
Readers Writers (priority?)
outerQ, rsem, rmutex, wmutex, wsem 1
wait (outerQ) wait (rsem) wait
(rmutex) readcnt if (readcnt 1) wait
(wsem) signal(rmutex) signal
(rsem) signal (outerQ) READ wait (rmutex)
readcnt-- if (readcnt 0) signal
(wsem) signal (rmutex)
wait (wsem) writecnt if (writecnt
1) wait (rsem) signal (wsem) wait
(wmutex) WRITE signal (wmutex) wait (wsem)
writecnt-- if (writecnt 0) signal
(rsem) signal (wsem)
16
Unix Semaphores
  • Are a generalization of the counting semaphores
    (more operations are permitted).
  • A semaphore includes
  • the current value S of the semaphore
  • number of processes waiting for S to increase
  • number of processes waiting for S to be 0
  • System calls
  • semget creates an array of semaphores
  • semctl allows for the initialization of
    semaphores
  • semop performs a list of operations one on each
    semaphore (atomically)

17
Unix Semaphores
  • Each operation to be done is specified by a value
    sop.
  • Let S be the semaphore value
  • if sop gt 0 (signal operation)
  • S is incremented and process awaiting for S to
    increase are awaken
  • if sop 0
  • If S0 do nothing
  • if S!0, block the current process on the event
    that S0
  • if sop lt 0 (wait operation)
  • if S gt sop then S S - sop then if S
    lt0 wait

18
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com