10/16/2015 1 Realizing Concurrency using the thread model B. Ramamurthy.

Slides:



Advertisements
Similar presentations
1 Processes and Threads Creation and Termination States Usage Implementations.
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.
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Threads By Dr. Yingwu Zhu. Review Multithreading Models Many-to-one One-to-one Many-to-many.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
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.
Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Threads Chapter 4. Modern Process & Thread –Process is an infrastructure in which execution takes place  (address space + resources) –Thread is a program.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
Processes and Threads.
1 Process States (1) Possible process states –running –blocked –ready Transitions between states shown.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Threads A thread (or lightweight process) is a basic unit of CPU.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Silberschatz, Galvin and Gagne ©2005 Modified by Dimitris Margaritis, Spring 2007 Chapter 4: Threads.
Department of Computer Science and Software Engineering
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.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
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.
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads A thread is an alternative model of program execution
Shahriar Pirnia Operating system سيستم عامل.
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.
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.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Tutorial 4. In this tutorial session we’ll see Threads.
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
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
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 4: Threads.
Realizing Concurrency using Posix Threads (pthreads)
Operating Systems Lecture 13.
Chapter 4: Threads.
Realizing Concurrency using the thread model
Implementing Threads in User Space
Realizing Concurrency using the thread model
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)
Tutorial 4.
Thread Model for Work Unit
Presentation transcript:

10/16/ Realizing Concurrency using the thread model B. Ramamurthy

10/16/ 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. Typically these problems are expected to handle many requests simultaneously. Example: multi- media, database applications, web applications.

10/16/ Topics to be Covered Objectives What are Threads? Thread implementation models POSIX threads Creating threads Using threads Summary

10/16/ Objectives To understand the thread model for realizing concurrency To study POSIX standard for threads called Pthreads. To study thread control primitives for creation, termination, join, synchronization, concurrency, and scheduling. To learn to design multi-threaded applications.

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

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

10/16/ Implementing Threads in User Space A user-level threads package

10/16/ Implementing Threads in the Kernel A threads package managed by the kernel

10/16/ Hybrid Implementations Multiplexing user-level threads onto kernel- level threads

10/16/ Scheduler Activations Goal – mimic functionality of kernel threads gain performance of user space threads Avoids unnecessary user/kernel transitions Kernel assigns virtual processors to each process lets runtime system allocate threads to processors Problem: Fundamental reliance on kernel (lower layer) calling procedures in user space (higher layer)

10/16/ Pop-Up Threads Creation of a new thread when message arrives (a) before message arrives (b) after message arrives Thread pools

10/16/ Thread Scheduling (1) Possible scheduling of user-level threads 50-msec process quantum threads run 5 msec/CPU burst B1, B2, B3

10/16/ Thread Scheduling (2) Possible scheduling of kernel-level threads 50-msec process quantum threads run 5 msec/CPU burst B1, B2, B3

10/16/ Thread as a unit of work A thread is a unit of work to a CPU. It is 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.

10/16/ Pthread Library Many thread models emerged: Solaris threads, win- 32 threads A POSIX standard (IEEE c) API for thread creation and synchronization. API specifies behavior of the thread library, implementation is up to development of the library. Simply a collection of C functions. One of the undergraduates from UB implemented a object-oriented version of the Pthreads called Zthreads: /projects/bina/zthread-1.5.1

10/16/ Posix Library Implementation in F. Mueller’s PaperPaper Language Application Language InterfaceC Language Application Posix thread library Unix Kernel Unix libraries User Level Kernel Level

10/16/ 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.

10/16/ 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++ -o executable file.cc -lpthread

10/16/ Thread’s local data Variables declared within a thread (function) are called local data. Local (automatic) 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.

10/16/ 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.

10/16/ 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.

10/16/ Summary We looked at Implementation of threads. thread-based concurrency. Pthread programming We will look at a pthread programming demo Study the details given in thread library link. See