12/22/2015 1 Thread Model for Realizing Concurrency B. Ramamurthy.

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.
Day 10 Threads. Threads and Processes  Process is seen as two entities Unit of resource allocation (process or task) Unit of dispatch or scheduling (thread.
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
Lecture 4: Concurrency and Threads CS 170 T Yang, 2015 Chapter 4 of AD textbook.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.
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.
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.
Process Concept An operating system executes a variety of programs
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.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
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.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
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.
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.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
Department of Computer Science and Software Engineering
© 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
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.
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.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
CMSC 421 Spring 2004 Section 0202 Part II: Process Management Chapter 5 Threads.
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
Day 12 Threads.
Threads in C Caryl Rahn.
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 4: Threads.
Nachos Threads and Concurrency
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
Threads and Concurrency
Operating System Concepts
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
CS510 Operating System Foundations
Tutorial 4.
Presentation transcript:

12/22/ Thread Model for Realizing Concurrency B. Ramamurthy

12/22/ Introduction A thread refers to a thread of control flow: an independent sequence of execution of program code. Threads are powerful. As with most powerful tools, if they are not used appropriately thread programming may be inefficient. Thread programming has become viable solution for many problems with the advent of multiprocessors and client-server model of computing. Low overhead compared to processes makes thread model a viable choice for realizing concurrency in embedded and realtime system

Page 3 Implementation of Processes

Page 4 Process Control Block (PCB) Information associated with each process Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information

Page 5 Process Control Block (PCB)

12/22/ Threads A thread is a unit of work to a CPU. It is a strand of control flow. A traditional UNIX process has a single thread that has sole possession of the process’s memory and resources. Threads within a process are scheduled and execute independently. Many threads may share the same address space. Each thread has its own private attributes: stack, program counter and register context.

12/22/ Pthread Library a POSIX standard (IEEE c) API for thread creation and synchronization. This API specifies behavior of the thread library, implementation is up to development of the library Simply a collection of C function.

12/22/ Creating threads Always include pthread library: #include int pthread_create (pthread_t *tp, const pthread_attr_t * attr, void *(* start_routine)(void *), void *arg); This creates a new thread of control that calls the function start_routine. It returns a zero if the creation is successful, and thread id in tp (first parameter). attr is to modify the attributes of the new thread. If it is NULL default attributes are used. The arg is passing arguments to the thread function.

12/22/ Using threads 1. Declare a variable of type pthread_t 2. Define a function to be executed by the thread. 3. Create the thread using pthread_create Make sure creation is successful by checking the return value. 4. Pass any arguments need through’ arg (packing and unpacking arg list necessary.) 5. #include at the top of your header. 6. Compile: g++ file.c -lpthread -o executable

12/22/ Thread’s local data Variables declared within a thread (function) are called local data. Local (static) data associated with a thread are allocated on the stack. So these may be deallocated when a thread returns. So don’t plan on using locally declared variables for returning arguments. Plan to pass the arguments thru argument list passed from the caller or initiator of the thread.

12/22/ Thread termination (destruction) Implicit : Simply returning from the function executed by the thread terminates the thread. In this case thread’s completion status is set to the return value. Explicit : Use thread_exit. Prototype: void thread_exit(void *status); The single pointer value in status is available to the threads waiting for this thread.

12/22/ Waiting for thread exit int pthread_join (pthread_t tid, void * *statusp); A call to this function makes a thread wait for another thread whose thread id is specified by tid in the above prototype. When the thread specified by tid exits its completion status is stored and returned in statusp.

12/22/ The Thread Model (a) Three processes each with one thread (b) One process with three threads

12/22/ Per process vs per thread items Items shared by all threads in a process Items private to each thread

Many Threads - One Process Thread 1 Thread 2 Low Memory High Memory Time TCB 1 TCB 2 Second thread starts here First thread resumes

User Level Threads Threads first developed in user libraries OS unaware Any blocking call blocks entire process!

Kernel Level Threads Threads recognized as useful Functions added to kernel Blocking call blocks only 1 thread Simplifies programming model Threads can use multiple CPUs Require interrupt for service

12/22/ Summary We looked at thread-based concurrency. Pthread programming We will look at a pthread programming demo Study the details given in thread link.