• a posix standard (ieee 1003.1c) api

17

Click here to load reader

Upload: srinu63

Post on 18-Nov-2014

234 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: • a Posix Standard (Ieee 1003.1c) API

pthreads

Page 2: • a Posix Standard (Ieee 1003.1c) API

pthreads

• A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization

• API specifies behavior of the thread library. Thread implementation is up to the development of the library.

• Common in UNIX operating systems

Page 3: • a Posix Standard (Ieee 1003.1c) API

Linux threads

• Linux refers to them as tasks rather than threads

• Thread creation is done through clone() system call

• Clone() allows a child task to share the address space of the parent task (process)

Page 4: • a Posix Standard (Ieee 1003.1c) API

Pthreads:

• pthread_create – to create a thread.– Arguments:

• 1: pthread_t * - name of the thread• 2: pthread_attr_t * - attributes of the thread• 3: void *(*start_routine)(void *) – name of the function• 4: void * arg – arguments to the function

• Different attributes are– Detach state– Scheduling policy– Schedule param– Inherit sched– scope– Stack size

Page 5: • a Posix Standard (Ieee 1003.1c) API

• pthread_join – called by the main task• pthread_exit – called by the thread to

return the exit status to the main task.

Page 6: • a Posix Standard (Ieee 1003.1c) API

Scenario

//Main threadPthread_create()

Pthread_join()

//Thread

Function()

{

Pthread_exit()}

Page 7: • a Posix Standard (Ieee 1003.1c) API

Thread attributes

• Pthread_attr_init()• Pthread_attr_set***()• Pthread_attr_get***()• Pthread_attr_destroy()

Page 8: • a Posix Standard (Ieee 1003.1c) API

set*** / get***

• ***:– detachstate()– schedpolicy()– schedparam()– inheritsched()– scope()– stacksize()

Page 9: • a Posix Standard (Ieee 1003.1c) API

Example detachstate

// main taskpthread_attr_init()pthread_attr_setdetachstate()

pthread_create()pthread_attr_destroy()

// threadfunction{

}

Page 10: • a Posix Standard (Ieee 1003.1c) API

Cancelling a thread

• pthread_cancel()• pthread_setcancelstate()• pthread_setcanceltype()

– Asynchronous

– Deferred• Pthread_join(), pthread_cond_wait(),

pthread_cond_timewait(), pthread_testcancel(), sem_wait(), sigwait() – should be called by the main task to collect the exit status

Page 11: • a Posix Standard (Ieee 1003.1c) API

usage

// main taskPthread_create()

Pthread_cancel()

Pthread_join()

// threadFunction(){Pthread_setcancelstate()Pthread_setcanceltype()

/// Jump to exit

Pthread_exit()}

Page 12: • a Posix Standard (Ieee 1003.1c) API

semaphores

• Binary semaphores– Mutually exclusive binary semaphores

• Initial value = 1– Signaling semaphores

• Initial value = 0

• Counting semaphores– Initial value = n

• Implementation– Sem_init– Sem_wait– Sem_post– Sem_destroy

Page 13: • a Posix Standard (Ieee 1003.1c) API

Mutex

• Similar to Mutually exclusive binary semaphore

• Initial value is 1 by default• Implementation

– Pthread_mutex_init()– Pthread_mutex_lock()

– Pthread_mutex_unlock()– Pthread_mutex_destroy()

Page 14: • a Posix Standard (Ieee 1003.1c) API

• Pthread_mutex_init()– Arguments:

• Mutex name• Pthread_mutexattr_t – mutex attribute

– PTHREAD_MUTEX_DEFAULT or PTHREAD_MUTEX_NORMAL

– PTHREAD_MUTEX_ERRORCHECK

– PTHREAD_MUTEX_RECURSIVE

Page 15: • a Posix Standard (Ieee 1003.1c) API

Types of mutex• PTHREAD_MUTEX_DEFAULT or PTHREAD_MUTEX_NORMAL

Results in a deadlock if the same pthread tries to lock it a second time using the pthread_mutex_lock subroutine without first unlocking it. This is the default type.

• PTHREAD_MUTEX_ERRORCHECKAvoids deadlocks by returning a non-zero value if the same thread

attempts to lock the same mutex more than once without first unlocking the mutex.

• PTHREAD_MUTEX_RECURSIVEAllows the same pthread to recursively lock the mutex using the

pthread_mutex_lock subroutine without resulting in a deadlock or getting a non-zero return value from pthread_mutex_lock. The same pthread has to call the pthread_mutex_unlock subroutine the same number of times as it called pthread_mutex_lock subroutine in order to unlock the mutex for other pthreads to use

Page 16: • a Posix Standard (Ieee 1003.1c) API

lock:pthread_t thread = pthread_self();lock mutexif (owner == thread)

recursionCount++;else {

while (recursionCount)wait on condition

recursionCount = 1owner = thread

}unlock mutex

unlock:lock mutexrecursionCount--if (!recursionCount) {

owner = NULLsignal condition

}unlock mutex

struct RecursiveLock {pthread_mutex_t mutex;pthread_cond_t condition;unsigned int recursionCount;pthread_t owner;

};

Page 17: • a Posix Standard (Ieee 1003.1c) API

sequence

• Variables:– Pthread_mutex_t m; name of the mutex

– Pthread_mutexattr_t a ; what type of mutex

• Function calling sequence– pthread_mutexattr_init (&a); – Pthread_mutexattr_settype(&a, TYPE ) ;

– Pthread_mutex_init(&m, &a) ;– Pthread_mutexattr_destroy(&a);