Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

Threads Programming Thread creation Synchronization.
Multi-core Programming Programming with Posix Threads.
Using synchronization primitives CS-3013 A-term Using Synchronization Primitives in C, C++, and Linux Kernel CS-3013 Operating Systems A-term 2008.
PTHREADS These notes are from LLNL Pthreads Tutorial
CONDITION VARIABLE. Announcements  Quiz  Getting the big picture  Programming assignments  Cooperation  Lecture is cut 20 mins short for Quiz and.
Chap. 23 Threads (1) Threads v.s. Fork (2) Basic Thread Functions #include int pthread_create ( pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
POSIX Threads HUJI Spring 2007.
Pthread II. Outline Join Mutex Variables Condition Variables.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
THREAD IMPLEMENTATION For parallel processing. Steps involved Creation Creates a thread with a thread id. Detach and Join All threads must be detached.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
Atomic Operations David Monismith cs550 Operating Systems.
04/10/25Parallel and Distributed Programming1 Shared-memory Parallel Programming Taura Lab M1 Yuuki Horita.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Copyright ©: University of Illinois CS 241 Staff1 Synchronization and Semaphores.
Programming with POSIX* Threads Intel Software College.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Condition Variables u A condition variable is used to wait until a particular condition is true  like in a monitor u Always use condition variables together.
Pthreads: A shared memory programming model
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
(p)Threads Libraries Math 442 es Jim Fix. Life cycle of a thread.
POSIX Threads HUJI Spring 2011.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
CS 360 pthreads Condition Variables for threads. Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining.
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
pThread synchronization
1 Programming with Shared Memory - 2 Issues with sharing data ITCS 4145 Parallel Programming B. Wilkinson Jan 22, _Prog_Shared_Memory_II.ppt.
回到第一頁 What are threads n Threads are often called "lightweight processes” n In the UNIX environment a thread: u Exists within a process and uses the process.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
CS 537 – Introduction to Operating Systems
Principles of Operating Systems Lecture 11
Threads in C Caryl Rahn.
Shared-Memory Programming with Threads
Threads Threads.
Netprog: Threads Programming
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Pthreads void *sleeping(void *); /* thread routine */
PTHREADS These notes are from LLNL Pthreads Tutorial
Thread synchronization
Recitation 14: Proxy Lab Part 2
Lecture 14: Pthreads Mutex and Condition Variables
PTHREADS AND SEMAPHORES
Pthread Prof. Ikjun Yeom TA – Mugyo
Synchronization Primitives – Semaphore and Mutex
Lecture 14: Pthreads Mutex and Condition Variables
Programming with Shared Memory - 2 Issues with sharing data
Lecture 20: Synchronization
POSIX Threads(pthreads)
Threads CSE 2431: Introduction to Operating Systems
Presentation transcript:

Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid, NULL, sleeping, &time) ; } void *sleeping(int * sleep_time) { printf(“thread sleeping for %d secs\n”, *sleep_time) ; }

Pthreads Problem: When main exits the process exits. main() { int time = 2 ; pthread_create(&tid, NULL, sleeping, &time) ; pthread_join(tid, void NULL); } void *sleeping(int * sleep_time) { printf(“thread sleeping for %d secs\n”, *sleep_time) ; }

pthread_t tids[10] ; main() { for (j = 0 ; j < 10 ; j++) pthread_create(&tids[i], NULL, sleeping, &time) ; for(j = 0 ; j < 10 ; j++) pthread_join(tid[i], NULL) ; }

All theads share: global variables file descriptors static variables within creating function. Local variables are private to each thread. void *sleeping(int * sleep_time) { static int tootoo = 10 ; printf(“thread sleeping for %d secs\n”, *sleep_time) ; tootoo++ ; //will have final value of 20. }

Synchronization Constructs mutex and condition variables: Main() { pthread_mutex_t my_mute ; //initialize pthread_mutex_init(&my_mute, NULL) ; pthread_mutex_lock(&my_mute);//blocks caller till mutex unlocked // by owner -critical code pthread_mutex_unlock(&my_mute);

You can also test a mutex to see if it is available pthread_mutex_trylock(&my_mute) ; If multiple threads blocked on mutex, one will be chosen Based on the scheduling algorithm.

Condition variable allows threads to wait on some event and resume when the event has occurred. Condition variable associated with a mutex! Thread must acquire mutex before calling pthread_cond_wait(). pthread_mutex_t my_mute ; pthread_cond_t condalisa ; pthread_mutex_init(&mymute) ; thread_cond_init(&condalisa) ;

pthread_mutex_lock(&my_mute) ; pthread_cond_wait(&condalisa, &my_mute); This call to wait atomically checks the condition and blocks (if necessary). If the thread blocks, the mutex is automatically released. Woken up with call to pthread_cond_signal ; pthread_cond_signal(&condalisa) ; //wakes up one //thread. pthread_cond_broadcast (&condalisa) ; //wakes up all threads