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.

Slides:



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

Threads. Readings r Silberschatz et al : Chapter 4.
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.
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.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
SMP threads an Introduction to Posix Threads. Technical Definition 1.Independent stream of instructions that can be scheduled to run by an operating system.
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.
Many-SC Programming Model Jaejin Lee Center for Manycore Programming Seoul National University
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
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
04/10/25Parallel and Distributed Programming1 Shared-memory Parallel Programming Taura Lab M1 Yuuki Horita.
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
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.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
What is a thread? process: an address space with 1 or more threads executing within that address space, and the required system resources for those threads.
Includes slides from course CS194 at UC Berkeley, by prof. Katherine Yelick Shared Memory Programming Pthreads: an overview Ing. Andrea Marongiu
Programming with POSIX* Threads Intel Software College.
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.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
(p)Threads Libraries Math 442 es Jim Fix. Life cycle of a thread.
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.
Threads A thread is an alternative model of program execution
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.
2.2 Threads  Process: address space + code execution  There is no law that states that a process cannot have more than one “line” of execution.  Threads:
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
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.
Realizing Concurrency using the thread model
CS 537 – Introduction to Operating Systems
Realizing Concurrency using the thread model
PThreads.
Shared-Memory Programming with Threads
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Chapter 4: Threads.
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
CSE 333 – Section 9 Threads.
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Multithreading Tutorial
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

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 has its own stack Each thread has its own instruction pointer Each thread has access to shared data Why? –See server example above –Parallelism – may be able to speed up some computation (sum of X numbers) multi-CPU system or dual/quad core CPU

Webserver Thread 0 int conns=0; main() { while(1) { waitForConnection() runThread(threadMain) } Thread 1 threadMain() { int run=1; conns++; while(run) { recv() send() } close() conns -- ; } Web browser Thread 2 threadMain() { int run=1; conns++; while(run) { recv() send() } close() conns -- ; } Thread 3 threadMain() { int run=1; conns++; while(run) { recv() send() } close() conns -- ; } Can you spot the problem(s)?

Synchronization Mutex: Mutually exclusive –only one thread can have it –use it to guard shared data –lock/unlock –how will this fix the previous example?

Pthreads POSIX threads –Portable Operating System Interface for uniX –this is a particular API to a threading library specifies the functions to provide implementation is up to the developer #include pthread_t thread1; pthread_mutex_t guardConns;

pthreads int pthread_create (pthread_t* thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg) pthread_attr_t sets attributes of the thread, can be set to NULL for now start_routine should be a void* foo(void*) int pthread_join( pthread_t th, void **thread_return) return value of the thread is stored in thread_return (if it is not NULL) void pthread_exit(void*)

Mutexes int pthread_mutex_init( pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr) mutexattr may be NULL to accept the default settings int pthread_mutex_lock(pthread_mutex_t *mutex) int pthread_mutex_unlock(pthread_mutex_t *mutex)