Thread Programming.

Slides:



Advertisements
Similar presentations
Threads Relation to processes Threads exist as subsets of processes Threads share memory and state information within a process Switching between threads.
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Threads. Readings r Silberschatz et al : Chapter 4.
1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
Professor: Shu-Ching Chen TA: Hsin-Yu Ha.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int c;
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
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.
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
Unix Threads operating systems. User Thread Packages pthread package mach c-threads Sun Solaris3 UI threads Kernel Threads Windows NT, XP operating systems.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads Changes by MA Doman 2013.
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.
PRINCIPLES OF OPERATING SYSTEMS Lecture 6: Processes CPSC 457, Spring 2015 May 21, 2015 M. Reza Zakerinasab Department of Computer Science, University.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
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,
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
CS333 Intro to Operating Systems Jonathan Walpole.
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.
Pthreads.
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
CISC2200 Threads Fall 09. Process  We learn the concept of process  A program in execution  A process owns some resources  A process executes a program.
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
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Realizing Concurrency using the thread model
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University.
Threads Threads.
Boost String API & Threads
Copyright ©: Nahrstedt, Angrave, Abdelzaher
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 4: Threads.
Linux Processes & Threads
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Realizing Concurrency using the thread model
Multithreading Tutorial
Realizing Concurrency using the thread model
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
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.
POSIX Threads(pthreads)
Presentation transcript:

Thread Programming

Topics Thread Thread NPTL thread library commands Thread Safety Thread libraries Threading issues Linux Threads

4 Reasons for threads Express logically concurrent tasks Run work in the background Exploit multiple processors Manage I/O devices

Process (fork) vs Thread creation Fork() is relatively expensive to launch Difficult to share information between processes Faster creation (10x or better.. Sharing information is easy.. Just copy information into a shared heap. -> That requires synchronization between threads

Thread Advantages Sharing data between threads is easier Thread Creation is faster Used extensively in Operating Systems

Thread Disadvantages First let’s look at threads in memory space

Stack and Stack Frames square (int x) STACK doCalc(int val) { return x*x } doCalc(int val) { printf (“square is %d\n”, square(val) } main (int x, int y) { key = 9999 doCalc (key) } STACK Frames for C run-time start up functions Frame for doCalc Frame for square Stack for main

4 Threads executing in process in linux argv, environ Stack for main thread Stack for thread 3 Stack for thread 2 Stack for thread 1 Shared libraries, shared memory Code is re-entrant Heap Uninitialized Data (BSS) Initialized Data Thread 3 executing here Text (program code) Main thread executing here Thread 1 executing here Thread 2 executing here

Threads share Process ID and parent process ID Controlling terminal Credentials Open files and locks Resource limits CPU times consumed.

Attributes distinct for Threads Thread ID Thread-specific data Real-time scheduling policy CPU affinity Stack (local variables and function call linkage) Processor registers in switc

Thread resource Shared State Per-Thread State Per-Thread State Thread Control Block Heap Thread Control Block Stack Information Stack Information Saved Registers Saved Registers Global Variables Thread Metadata Thread Metadata Code Thread Stack Thread Stack

Thread Lifecycle

Thread Safety A function is said to be thread-safe if it can be safely invoked by multiple threads at the same time. Conversely, if a function is not thread-safe, then we can’t call it from one thread while it is being executed in another thread.

Two threads call yield

Thread Disadvantages Must ensure thread safety Bug in one thread can exhibit itself in ALL threads Threads are competing for finite virtual address space.

Compiling Thread Programs On Linux compile with the -pthread option. The effects include _REENTRANT preprocessor macro is defined. Links with libpthread lib

Creating Threads Thread points to buffer for unique thread ID #include <pthread.h> int pthread_create( pthread_t *thread. const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)); Thread points to buffer for unique thread ID If attr is NULL, the default attributes are used. Upon its creation, the thread executes start_routine, with arg as its sole argument.

Thread ID #include <pthread.h> pthread_t pthread_self(void)); Each thread within a process is uniquely identified by the Thread Id This is returned on pthread_create()

Thread Termination #include <pthread.h> void pthread_exit(void *retval)); The thread’s start function returns with return value Thread calls pthread_exit() Thread is cancelled by external program Main thread performs a return (causes all threads to terminate immediately)

Thread Cancellation #include <pthread.h> void pthread_cancel(pthread_t thread)); Threads can protect themselves from cancellation by setting its flag. When cancellation is enabled, it is done only when the thread reaches its next cancellation point.

Joining with a Terminated Thread #include <pthread.h> int pthread_join(pthread_ t thread, void **retval); Returns 0 on success or a positive error number on error Waits for the thread to terminate.

Detaching a Thread When we don’t care to wait for a thread to finish. #include <pthread.h> int pthread_detach (pthread_ t thread); When we don’t care to wait for a thread to finish.

Create Threads example https://computing.llnl.gov/tutorials/pthreads/ http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm

To see thread information ps -m

ps m –o pid,tid,command