6-on-1(4) - Lecture Notes 12 Concurrent Programming using...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1 Lecture Notes 12 Concurrent Programming using POSIX Threads Anand Tripathi CSci 4061 Introduction to Operating Systems 2 Topics Introduction to Thread Programming Refer to Chapters 12 and 13 of Robbins and Robbins Posix thread programming primitives Creation of threads Mutex and Condition variables Semaphores Refer to the example programs on the webpage Study the example code 1a.hello/ and 1b.hello/ Producer-Consumer Example 3 Using POSIX Thread Library To use this library, #include <pthread.h> in your program. To compile, link with the pthread library: cc hello.c -o hello -lpthread On Solaris you should define _REENTRANT flag, either in program or at compile time: cc -D_REENTRANT hello.c -o hello -lpthread This will make sure that reentrant library code versions will be used. These versions are MT-safe . 4 POSIX Thread Programming User level thread library . Supports thread creation Synchronization using MUTEX CONDITION variables SEMAPHORES At the time of creation, different attributes can be assigned to: threads mutex/condition variables semaphores 5 Mutex and Condition Variables • These two data types together provide a synchronization facilty which is more basic than semaphores. – Original POSIX standard did not include semaphores. • Using these two one can implement semaphores. 6 Data Types in POSIX thread mutex for mutual exclusion condition variables It is a queue (list) in which thread is placed in the blocked state. A blocked thread is resumed when the condition variable is signaled various kind of attribute types used when initializing: thread mutex condition
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 7 Data Types in Thread Programming All POSIX thread function have the form: pthread[ _object ] _operation Most of the POSIX thread library functions return 0 for success and some error-number in case of a failure. Following are the important data types in POSIX library: pthread_t Thread ID for a thread object pthread_attr_t Thread attribute variable 8 Data Types in Thread Programming pthread_mutex_t Mutual exclusion lock variable pthread_cond_t Condition variable pthread_mutexattr_t Mutex lock variable attribute pthread_condattr_t Condition variable attribute 9 Threads and their Attributes pthread_create function is used to create a new thread. A thread is created with specification of certain attributes: Thread’ scope Detach state Stack address Stack size Scheduling Policy : set priority, default is inherit its creator’s priority 10 Bound and Unbound Threads A bound thread has one-to-one mapping to an LWP. The thread runs only on the specified LWP, and The LWP runs only that thread These threads are also called “globally scheduled”, I.e. scheduled by the kernel
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CSCI 4061 taught by Professor Tripathi during the Spring '08 term at Minnesota.

Page1 / 12

6-on-1(4) - Lecture Notes 12 Concurrent Programming using...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online