Auburn University http://www.eng.auburn.edu/~xqin COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Threads, SMP, and Microkernels
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
1 Chapter 4 Threads Threads: Resource ownership and execution.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Threads, SMP, and Microkernels Chapter 4. 2 Outline n Threads n Symmetric Multiprocessing (SMP) n Microkernel n Linux Threads.
Threads, Thread management & Resource Management.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
CS333 Intro to Operating Systems Jonathan Walpole.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Threads A thread is an alternative model of program execution
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
1 Threads, SMP, and Microkernels Chapter 4. 2 Process Resource ownership - process includes a virtual address space to hold the process image Scheduling/execution-
COMP7330/7336 Advanced Parallel and Distributed Computing OpenMP: Programming Model Dr. Xiao Qin Auburn University
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
COMP7330/7336 Advanced Parallel and Distributed Computing POSIX Thread API Dr. Xiao Qin Auburn University
回到第一頁 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.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
COMP7330/7336 Advanced Parallel and Distributed Computing POSIX Thread API Dr. Xiao Qin Auburn University
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.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
CS 6560: Operating Systems Design
Realizing Concurrency using the thread model
PThreads.
Day 12 Threads.
Concurrency, Processes and Threads
Threads in C Caryl Rahn.
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Linux Processes & Threads
Chapter 4 Threads.
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Threads, SMP, and Microkernels
Realizing Concurrency using the thread model
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Lecture 4- Threads, SMP, and Microkernels
Threads and Concurrency
Threads Chapter 4.
Operating System Concepts
Jonathan Walpole Computer Science Portland State University
Concurrency, Processes and Threads
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
Presentation transcript:

Auburn University http://www.eng.auburn.edu/~xqin COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu Feedback on homework 3. TBC=8 Slides are adopted from Drs. Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar

What is a thread? A thread is a lightweight process

What is a process? Unit of resource ownership (A process is allocated?) a virtual address space to hold the process image control of some resources (files, I/O devices...) Unit of dispatching (A process is an execution path through one or more programs:) execution may be interleaved with other processes the process has an execution state and a dispatching priority Reference: http://www.cs.cf.ac.uk/Dave/C/node29.html Unit of resource ownership-- A process is allocated:a virtual address space to hold the process image control of some resources (files, I/O devices...) Unit of dispatching- A process is an execution path through one or more programs:execution may be interleaved with other processes the process has an execution state and a dispatching priority If we treat these two characteristics as being independent (as does modern OS theory): The unit of resource ownership is usually referred to as a process or task. This Processes have: a virtual address space which holds the process image. protected access to processors, other processes, files, and I/O resources. The unit of dispatching is usually referred to a thread or a lightweight process. Thus a thread: Has an execution state (running, ready, etc.) Saves thread context when not running Has an execution stack and some per-thread static storage for local variables Has access to the memory address space and resources of its process all threads of a process share this when one thread alters a (non-private) memory item, all other threads (of the process) sees that a file open with one thread, is available to others Reference: http://www.cs.cf.ac.uk/Dave/C/node29.html

Threads have some advantages of (multi) processes Creation/Termination Less time to create a new thread than a process, because the newly created thread uses the current process address space. Less time to terminate a thread than a process. (Why?) Reference: http://www.cs.cf.ac.uk/Dave/C/node29.html Unit of resource ownership-- A process is allocated:a virtual address space to hold the process image control of some resources (files, I/O devices...) Unit of dispatching- A process is an execution path through one or more programs:execution may be interleaved with other processes the process has an execution state and a dispatching priority If we treat these two characteristics as being independent (as does modern OS theory): The unit of resource ownership is usually referred to as a process or task. This Processes have: a virtual address space which holds the process image. protected access to processors, other processes, files, and I/O resources. The unit of dispatching is usually referred to a thread or a lightweight process. Thus a thread: Has an execution state (running, ready, etc.) Saves thread context when not running Has an execution stack and some per-thread static storage for local variables Has access to the memory address space and resources of its process all threads of a process share this when one thread alters a (non-private) memory item, all other threads (of the process) sees that a file open with one thread, is available to others

Threads have some advantages of (multi) processes switch and communication Q1 Why it takes less time to switch between two threads within the same process? because the newly created thread uses the current process address space. Why the communication overheads among multiple threads is low? The threads share everything: address space. Data produced by one thread is immediately available to all the other threads. Reference: http://www.cs.cf.ac.uk/Dave/C/node29.html Unit of resource ownership-- A process is allocated:a virtual address space to hold the process image control of some resources (files, I/O devices...) Unit of dispatching- A process is an execution path through one or more programs:execution may be interleaved with other processes the process has an execution state and a dispatching priority If we treat these two characteristics as being independent (as does modern OS theory): The unit of resource ownership is usually referred to as a process or task. This Processes have: a virtual address space which holds the process image. protected access to processors, other processes, files, and I/O resources. The unit of dispatching is usually referred to a thread or a lightweight process. Thus a thread: Has an execution state (running, ready, etc.) Saves thread context when not running Has an execution stack and some per-thread static storage for local variables Has access to the memory address space and resources of its process all threads of a process share this when one thread alters a (non-private) memory item, all other threads (of the process) sees that a file open with one thread, is available to others

Multithreading Reference: http://www.cs.cf.ac.uk/Dave/C/node29.html Unit of resource ownership-- A process is allocated:a virtual address space to hold the process image control of some resources (files, I/O devices...) Unit of dispatching- A process is an execution path through one or more programs:execution may be interleaved with other processes the process has an execution state and a dispatching priority If we treat these two characteristics as being independent (as does modern OS theory): The unit of resource ownership is usually referred to as a process or task. This Processes have: a virtual address space which holds the process image. protected access to processors, other processes, files, and I/O resources. The unit of dispatching is usually referred to a thread or a lightweight process. Thus a thread: Has an execution state (running, ready, etc.) Saves thread context when not running Has an execution stack and some per-thread static storage for local variables Has access to the memory address space and resources of its process all threads of a process share this when one thread alters a (non-private) memory item, all other threads (of the process) sees that a file open with one thread, is available to others Reference: http://www.cs.cf.ac.uk/Dave/C/node29.html

Single-threaded vs. Multithreaded Models Q2: What are their differences? Reference: http://www.cs.cf.ac.uk/Dave/C/node29.html Unit of resource ownership-- A process is allocated:a virtual address space to hold the process image control of some resources (files, I/O devices...) Unit of dispatching- A process is an execution path through one or more programs:execution may be interleaved with other processes the process has an execution state and a dispatching priority If we treat these two characteristics as being independent (as does modern OS theory): The unit of resource ownership is usually referred to as a process or task. This Processes have: a virtual address space which holds the process image. protected access to processors, other processes, files, and I/O resources. The unit of dispatching is usually referred to a thread or a lightweight process. Thus a thread: Has an execution state (running, ready, etc.) Saves thread context when not running Has an execution stack and some per-thread static storage for local variables Has access to the memory address space and resources of its process all threads of a process share this when one thread alters a (non-private) memory item, all other threads (of the process) sees that a file open with one thread, is available to others Reference: http://www.cs.cf.ac.uk/Dave/C/node29.html

Thread Basics All memory in the logical machine model of a thread is globally accessible to every thread. The stack corresponding to the function call is generally treated as being local to the thread for liveness reasons. A logical machine model with both global memory (default) and local memory (stacks). Such a flat model may result in very poor performance since memory is physically distributed in typical machines.

Overview of Programming Models Process based models assume that all data associated with a process is private, by default, unless otherwise specified. Lightweight processes and threads assume that all memory is global. Directive based programming models extend the threaded model by facilitating creation and synchronization of threads.

Example A thread is a single stream of control in the flow of a program. A program like: for (row = 0; row < n; row++) for (column = 0; column < n; column++) c[row][column] = dot_product( get_row(a, row), get_col(b, col)); can be transformed to: create_thread( dot_product(get_row(a, row), get_col(b, col))); Q3: What is the difference between the above two code segments? The for loop has n^2 iterations, each of which can be computed independently. The thread is an instance of a function that returns before the function has finished executing.

A logical machine model The logical machine model of a thread-based programming paradigm.

Thread Basics Threads provide software portability. Inherent support for latency hiding. (Why?) Scheduling and load balancing. Ease of programming and widespread use.

The POSIX Thread API Commonly referred to as Pthreads, POSIX has emerged as the standard threads API, supported by most vendors. The concepts discussed here are largely independent of the API and can be used for programming with other thread APIs (NT threads, Solaris threads, Java threads, etc.) as well.

Thread Basics: Creation Pthreads provides two basic functions for specifying concurrency in a program: #include <pthread.h> int pthread_create ( pthread_t *thread_handle, const pthread_attr_t *attribute, void * (*thread_function)(void *), void *arg); int pthread_join ( pthread_t thread, void **ptr); The function pthread_create invokes function thread_function as a thread When an attribute object is not specified, it is NULL, and the default thread is created with the following attributes: It is unbounded It is nondetached It has a default stack and stack size It inhetits the parent's priority pthread_join - wait for thread termination. The value passed to pthread_exit is returned in the located pointed by ptr. The function above makes sure that its parent thread does not terminate until it is done. This function is called from within the parent thread and the first argument is the thread ID of the thread to wait on and the second argument is the return value of the thread on which we want to the parent thread to wait. If we are not interested in the return value then we can set this pointer to be NULL. Reference: http://www.thegeekstuff.com/2012/04/terminate-c-thread/

Thread Basics: Termination A thread can terminate in three ways : If the thread returns from its start routine. pthread_join() If it is canceled by some other thread. The function used here is pthread_cancel(). If its calls pthread_exit() function from within itself. pthread_join - wait for thread termination The function above makes sure that its parent thread does not terminate until it is done. This function is called from within the parent thread and the first argument is the thread ID of the thread to wait on and the second argument is the return value of the thread on which we want to the parent thread to wait. If we are not interested in the return value then we can set this pointer to be NULL. Reference: http://www.thegeekstuff.com/2012/04/terminate-c-thread/

An Example: Hello World #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <pthread.h> void *thread_function(void *arg); char message[] = "Hello World"; int main() { int res; pthread_t a_thread; void *thread_result; ...... Show the fork-and-join model on the whiteboard See programming/thread/thread1.c

An Example: Hello World (cont.) ...... res = pthread_create(&a_thread, NULL, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } printf("Waiting for thread to finish...\n"); res = pthread_join(a_thread, &thread_result); perror("Thread join failed"); printf("Thread joined, it returned %s\n", (char *)thread_result); printf("Message is now %s\n", message); exit(EXIT_SUCCESS); Show the fork-and-join model on the whiteboard

Thread Basics: Creation and Termination (Example) Q4: How do threads communicate communicate with the parent process? #include <pthread.h> #include <stdlib.h> #define MAX_THREADS 512 void *compute_pi (void *); int hits[MAX_THREADS], total_hits, total_misses, ... main() { ... pthread_t p_threads[MAX_THREADS]; pthread_attr_t attr; pthread_attr_init (&attr); for (i=0; i< num_threads; i++) { hits[i] = i; pthread_create(&p_threads[i], &attr, compute_pi, (void *) &hits[i]); } pthread_join(p_threads[i], NULL); total_hits += hits[i]; Show the fork-and-join model on the whiteboard

Thread Basics: Creation and Termination (Example) void *compute_pi (void *s) { int seed, i, *hit_pointer; double rand_no_x, rand_no_y; int local_hits; hit_pointer = (int *) s; seed = *hit_pointer; local_hits = 0; for (i = 0; i < sample_points_per_thread; i++) { rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1); rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1); if (((rand_no_x - 0.5) * (rand_no_x - 0.5) + (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25) local_hits ++; seed *= i; } *hit_pointer = local_hits; pthread_exit(0);