1 / 12

POSIX Thread Programming

POSIX Thread Programming. 2/14/97 B.Ramamurthy. Topics to be Covered. Objective POSIX threads What are Threads? Creating threads Using threads Summary. Objective. To study POSIX standard for threads called Pthreads.

cchase
Télécharger la présentation

POSIX Thread Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. POSIX Thread Programming 2/14/97 B.Ramamurthy B.Ramamurthy

  2. Topics to be Covered • Objective • POSIX threads • What are Threads? • Creating threads • Using threads • Summary B.Ramamurthy

  3. Objective • To study POSIX standard for threads called Pthreads. • To study thread control primitives for creation, termination, join, synchronization, concurrency, and scheduling. • To learn to design multi-threaded applications. B.Ramamurthy

  4. Creating threads • Always include pthread library: #include <pthread.h> • int pthread_create (pthread_t *threadp, const pthread_attr_t * attr, void *(* start routine)(void *), void *arg); • This creates a new thread of control that calls the function start_routine. • It return a zero if the creation is successful, and thread id in threadp (first parameter). • attr is to modify the attributes of the new thread. If it is NULL, default attributes are used. • The arg is passing arguments to the thread function. B.Ramamurthy

  5. Using threads 1. Declare a variable of type pthread_t 2. Define a function to be executed by the thread. 3. Create the thread using pthread_create Make sure creation is successful by checking the return value. 4. Pass any arguments need through’ arg (packing and unpacking arg list necessary.) 6. Compile: cc –o xyz xyz.c {options such as –lthread –lpthread} B.Ramamurthy

  6. Thread’s local data • Variables declared within a thread (function) are called local data. • Local (static) data associated with a thread are allocated on the stack. So these may be deallocated when a thread returns. • So don’t plan on using locally declared variables for returning arguments. Plan to pass the arguments thru argument list passed from the caller or initiator of the thread. B.Ramamurthy

  7. Thread termination (destruction) Implicit : Simply returning from the function executed by the thread terminates the thread. In this case thread’s completion status is set to the return value. • Explicit : Use thread_exit. Prototype: void thread_exit(void *status); The single pointer value in status is available to the threads waiting for this thread. B.Ramamurthy

  8. Waiting for thread exit • int pthread_join (pthread_t tid, void **statusp); • A call to this function makes a thread wait for another thread whose thread_id is specified by tid in the above prototype. • When the thread specified by tid exits its completion status is stored(returned) in statusp. B.Ramamurthy

  9. Example • See the multi_thr.c example from Pthreads book by Lewis and Berg. • This is available in /projects/bina/Pthreads • We will walk through the execution of this program to understand basic thread control. • Anything specified in Uppercase are functions/macro made by the authors. • Example: PTHREAD_CREATE B.Ramamurthy

  10. PTHREAD_CREATE • Is a robust version of the simple pthread_create of the posix library. {int err; if (err = pthread_create(new_thread_ID, attr, start_func, arg) { printf(%s\n”, strerror(err)); abort(); } B.Ramamurthy

  11. Other pthread functions • pthread_self : to get the “handle” for itself. • Synchronization: • pthread_mutex_t • pthread_mutex_lock (&lock) • pthread_mutex_unlock(&lock) • sema_t, sema_init, sema_wait, sema_post, sema_destroy B.Ramamurthy

  12. Summary • We will look at the basic thread operation in this lecture. • We will look into the details of critical section, need for synchronization, synchronization models in the next lecture. B.Ramamurthy

More Related