Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.

Slides:



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

1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
Multi-core Programming Programming with Posix Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
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.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
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.
The University of Adelaide, School of Computer Science
Thread Synchronization with Semaphores
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
Programming with POSIX* Threads Intel Software College.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
Pthreads: A shared memory programming model
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
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.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
POSIX Threads HUJI Spring 2011.
Pthreads.
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.
Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.
CSC 360, Instructor: Kui Wu Thread & PThread. CSC 360, Instructor: Kui Wu Agenda 1.What is thread? 2.User vs kernel threads 3.Thread models 4.Thread issues.
Threads A thread is an alternative model of program execution
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
POSIX THREADS. What is a thread? Multiple stands of execution in a single program are called threads. In other words, a thread is a sequence of control.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
CS 537 – Introduction to Operating Systems
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Shared-Memory Programming with Threads
Threads Threads.
Netprog: Threads Programming
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Thread Programming.
Linux Processes & Threads
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Realizing Concurrency using Posix Threads (pthreads)
CSE 333 – Section 9 Threads.
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Thread Programming.
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
POSIX Threads(pthreads)
Presentation transcript:

Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals

Overview Threads are concurrent lines of execution within a process. Sometimes called “lightweight processes” All the threads of a process are in its memory space, but each thread has its own Thread ID Stack Register values Set of blocked and pending signals Thread specific data errno variable There also must be a scheduling policy for thread execution

Overview A typical UNIX process contains only a single thread of execution Multiple threads allow us to take advantage of parallel programming without the overhead of a new process May make programming for asynchronous events easier

POSIX Threads pthreads are the optional POSIX standardization for threads Must include and link against the pthread library -lpthread when compiling a C/C++ program

Thread Identification Each thread has an identifier Only unique within the process to which the thread belongs IDs can be represented by pthread_t which can be a struct Implementation of struct is system dependant A thread can get its own ID structure with pthread_t pthread_self(void); Compare two thread IDs with int pthread_equal(pthread_t t1, pthread_t t2);

Thread Creation int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); Arguments thread points to the new thread ID pthread_attr_t set of thread attributes. Use NULL for default attributes start_routine is a function pointer to the function that the new thread will begin executing when it is created arg is the single argument that may be passed to the start routine. If you need to pass multiple parameters, bundle them as a struct and pass that

Thread Termination If any thread calls exit, _exit or Exit, the entire process will terminate Thread may terminate due to exec 3 ways a single thread can terminate Return from the start routine Call pthread_exit void pthread_exit(void *value_ptr); Thread cancelled by another thread with int pthread_cancel(pthread_t thread);

Joining and Detaching int pthread_join(pthread_t thread, void **value_ptr); int pthread_detach(pthread_t thread); Join is similar to waitpid. Allows a thread to wait on another to terminate Termination status of thread waited on returned in value_ptr if specified in call to pthread_exit pthread_detach allows us to put a thread in the detached state

Joinable Attribute We can specify that a newly created thread is joinable or detached when we create the thread with the pthread_attr_t parameter Threads created as detached can not be joined Default is joinable Detached thread resources are released as soon as thread terminates Creating a thread as detached prevents synchronization (since we can’t join it)

Mutexes Mutex – “mutual exclusion” Only one thread at a time can own a particular mutex variable A thread aquires the mutex by “locking” it. When it is done, it releases it by “unlocking” it Mutex variable is of type pthread_mutex_t

Mutexes int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); mutex is a pointer to a variable of type pthread_mutex_t attr is a pointer to a type that holds attributes for non default mutexes. We can pass in NULL for a default mutex

Mutex Use Declare mutex variable pthread_mutex_t myMutex; Initialize pthread_mutex_create(&myMutex, NULL); A thread locks the mutex pthread_mutex_lock(&myMutex); Thread executes critical section and then releases the mutext pthread_mutex_unlock(&myMutex); When mutex not longer needed, it can be destroyed pthread_mutex_destroy(&myMutex);

Condition Variables Allows threads to synchronize based on value of data Avoids busy loop where threads acquire lock, check value of a variable and repeat until the value changes Condition variables should always be protected by mutex locks

Condition Variables int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); int pthread_cond_destroy(pthread_cond_t *cond); int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t * mutex); int pthread_cond_broadcast(pthread_cond_t *cond); Notify all threads waiting on this condition int pthread_cond_signal(pthread_cond_t *cond); Notify a single thread waiting on this condition See page for example

Threads and Signals Signals are delivered to a single thread If signal is the result of hardware fault, it is usually sent to the thread that caused it Otherwise signal sent to arbitrary thread Each thread has its own signal mask int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset); Signal dispositions shared between all threads in a process

Threads and Signals int sigwait(const sigset_t *set, int *sig); Allows a thread to wait for a signal from the set given by set parameter The particular signal received is returned through the sig parameter To prevent race condition, the signal being waited on must be blocked before calling this function int pthread_kill(pthread_t thread, int sig); Sends a signal to a specified thread