Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.

Slides:



Advertisements
Similar presentations
OPERATING SYSTEMS Threads
Advertisements

Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 AE4B33OSS Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows.
Chap 4 Multithreaded Programming. Thread A thread is a basic unit of CPU utilization It comprises a thread ID, a program counter, a register set and a.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
國立台灣大學 資訊工程學系 Chapter 4: Threads. 資工系網媒所 NEWS 實驗室 Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the.
Threads.
Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
Modified from Silberschatz, Galvin and Gagne ©2009 Lecture 7 Chapter 4: Threads (cont)
Chapter 4: Multithreaded Programming
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
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 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
4.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Chapter 4 Multithreaded Programming Objectives Objectives To introduce a notion of.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
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 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Objectives To introduce a notion of a thread.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Chapter 4: Threads READ 4.1 & 4.2 NOT RESPONSIBLE FOR 4.3 &
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Multithreaded Programming.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Operating System Principles Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4 Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Multithreaded Programming. 4.2 Multithreaded Programming n Overview n Multithreading Models n Thread Libraries n Threading Issues n Operating.
Chapter 4: Threads. From Processes to Threads 4.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Threads.
Thread. A basic unit of CPU utilization. It comprises a thread ID, a program counter, a register set, and a stack. It is a single sequential flow of control.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Chapter 4: Threads. 4.2 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2011Operating System Concepts Essentials – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Operating System Concepts Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CHAPTER 5: THREADS ( 线程 ) Overview Overview Multithreading Models (多线程模型) Multithreading Models (多线程模型) Thread Implementation Issues (线程实现事项) Thread Implementation.
Source: Operating System Concepts by Silberschatz, Galvin and Gagne.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads Modified from the slides of the text book. TY, Sept 2010.
Chapter 4: Threads.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
CS307 Operating Systems Threads Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2011.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Operating System Concepts
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
Lecture 5. Example for periority The average waiting time : = 41/5= 8.2.
Introduction to threads
OPERATING SYSTEM CONCEPT AND PRACTISE
Operating System (013022) Dr. H. Iwidat
Chapter 4: Multithreaded Programming
Operating System Concepts
Chapter 4: Threads.
Chapter 4: Threads.
Presentation transcript:

Multithreaded Programing

Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread  Win32  Java Threading Issues  The fork() & exec() system call  Cancellation  Signal Handling  Thread Pools  Thread-Specific Data  Scheduler Activations

Threads --an overview  Process can contain multiple threads of control  APIs for Pthreads, Win32 and Java thread libraries  Support of threads in WinXP and Linux Thread: A basic unit of CPU utilization. ThreadID, program counter, register set and a stack It shares its code and data sections with the other thread of the same process. A process can perform more than one task at a time if a process has multiple threads.

Why Threads (contd..) A web browser might have one thread to display images or text while another retrieves data from the network. Word processor and Web server Operating system kernels are multithreaded  A set of threads in Solaris kernel manage interrupt handling  A kernel thread in Linux manages amount of free memory

Threads (contd)

Threads (Benefits) Responsiveness  Program can continue even if part of it is blocked or is performing lengthy operation.  Web browser can still allow user interaction in one thread during an image load thread Resource sharing  Several different threads of activity within the same address space --share the code and data section Economy  Costly Memory and resource allocation for process  Comparatively time consuming to create and manage processes than threads  Solaris: creating a process 30 times slower  Context switching 5 times slower Utilization of multiprocessor architecture

Multithreading Models User Threads: managed without kernel support Kernel threads: managed directly by OS Relationship b/w user and kernel threads  Many-to-one model  One-to-One model  Many-to-Many model

Multithreading Models Many-to-One Thread management by thread library in user space Entire process will block if a thread makes a blocking system call One thread can access the kernel at a time Multiple threads cannot run in parallel on multiprocessors. Developer can create as many threads as he/she wishes Examples Solaris –Green threads (uses this model) GNU Portable threads

Multithreading Models One-to-One Maps each user thread to a kernel thread -- More concurrency Multiple threads can run in parallel on multiprocessors Drawbacks? Crating a user thread requires creating a kernel thread Overhead of creating kernel thread –Performance Implementation of this model may limit the number of supported threads Examples: Linux, Windows

Multithreading Models Many-to-Many This model multiplexes many user-level threads to a smaller or equal number of kernel threads. Developer can create as many user threads as necessary –corresponding kernel thread can run in parallel on a multiprocessor Examples: Solaris 9

Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread  Win32  Java Threading Issues  The fork() & exec() system call  Cancellation  Signal Handling  Thread Pools  Thread-Specific Data  Scheduler Activations

Thread Libraries provides an API for creating & managing threads Two ways of implementing thread libraries  Provide a library entirely in user space with no kernel support All code & data structures exist in user space Invoking a function in the library results in a local function call in user space –not system call  Implement a kernel-level library supported directly by the OS Code & data structures for library exist in kernel space. Invoking a function in the API for library results in a system call to the kernel Three main thread libraries  POSIX Pthread, Win32 and Java

Thread Libraries (contd..) Pthread Library  Thread extension of the POSIX standard –provided as either a user or kernel level library Win 32 Library  Kernel level library for windows Java thread API  thread creation and management in Java program

Pthreads POSIX standard (IEEE1003.1c) –API for thread creation and synchronization Just a specification of thread behavior –not an implementation OS designers may implement it as they wish. Solaris, Linux, MacOSX, Tr64 UNIX A multithreaded program for summation: sum = n  i=0 i

Win32 Threads Similar technique –windows.h Threads creation by CreateThread() Pthread_join() = WaitForSingleObject() Java Threads Fundamental model of program execution in a java prog. Rich set of features –creation & management of threads Every Java program has at least one thread+ Create a thread – start() Pthread_join() = join()

Threading Issues If one thread in a program calls fork(), does the new process duplicate al threads, or the new process single- threaded?  Two versions Thread Cancellation: searching database, web browser Target Thread – thread to be cancelled: Two scenarios  Asynchronous Cancellation: One thread immediately terminates the target thread  Deferred Cancellation: Target thread periodically checks whether it should terminate –termination in orderly fashion. If resources have been allocated to cancelled thread? A thread is cancelled while in the midst of updating data? Cancellation points

Threading Issues (contd) Consider multithreading in web server  Problems? Time required to create a thread prior to servicing a request Thread will be discarded once it has completed its work No bound in the number of threads concurrently active in the system  Solution ? Thread Pool: Threads sit and wait for work  Benefits? Servicing a request is comparatively faster Thread pool limit the number of threads that exist at any point Thread pool architectures can dynamically adjust the # of threads in the pool according to usage patterns Smaller pool --larger benefit –less memory consumption

Threading Issues (contd) Thread Specific Data:  Threads share the data of the process  Thread might need its own copy of certain data  TSD support in Win32, Pthreads & Java K LWP

Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread  Win32  Java Threading Issues  The fork() & exec() system call  Cancellation  Signal Handling  Thread Pools  Thread-Specific Data  Scheduler Activations Operating-System Examples