Threads Assistant Professor DCS Operating System Concepts

Similar documents
Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Multithreading Pearson Education, Inc. All rights reserved.

Lecture Contents. 1. Overview. 2. Multithreading Models 3. Examples of Thread Libraries 4. Summary

4. Concurrency via Threads

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Chapter 4: Threads. Operating System Concepts 8 th Edition,

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Chapter 3 Process Description and Control

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Threads Chapter 4. Reading: 4.1,4.4, 4.5

Processes and Threads

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

OPERATING SYSTEM. Chapter 4: Threads

Chapter 4: Multi-Threaded Programming

CS420: Operating Systems

Threads. studykorner.org

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Multithreading. Reading: Silberschatz chapter 5 Additional Reading: Stallings chapter 4

Preview. The Thread Model Motivation of Threads Benefits of Threads Implementation of Thread

Process Management Rab Nawaz Jadoon

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Process Description and Control

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Threads Chapter 5 1 Chapter 5

Chapter 4: Threads. Chapter 4: Threads

SMD149 - Operating Systems

Chapter 4: Threads. Chapter 4: Threads

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

CS307: Operating Systems

Threads. CS3026 Operating Systems Lecture 06

CS 3305 Intro to Threads. Lecture 6

Processes and Threads

Chapter 4: Threads. Operating System Concepts 9 th Edition

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

Chapter 4 Multithreaded Programming

Definition Multithreading Models Threading Issues Pthreads (Unix)

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

Threads, SMP, and Microkernels. Chapter 4

Performance Throughput Utilization of system resources

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

CS360 Lecture 12 Multithreading

CSE 4/521 Introduction to Operating Systems

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Chapter 4: Multithreaded Programming

Chapter 4: Threads. Operating System Concepts 9 th Edition

ECE519 Advanced Operating Systems

CSCI-GA Operating Systems Lecture 3: Processes and Threads -Part 2 Scheduling Hubertus Franke

Process- Concept &Process Scheduling OPERATING SYSTEMS

Processes and Threads

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

Concurrency, Thread. Dongkun Shin, SKKU

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

Operating Systems: Internals and Design Principles. Chapter 4 Threads Seventh Edition By William Stallings

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

CS 450 Operating System Week 4 Lecture Notes

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Multithreaded Programming

4.8 Summary. Practice Exercises

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Rab Nawaz Khan Jadoon

CHAPTER 2: PROCESS MANAGEMENT

CISC2200 Threads Spring 2015

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Process Characteristics

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Threads, SMP, and Microkernels

Java Threads. Written by John Bell for CS 342, Spring 2018

CSE 306/506 Operating Systems Threads. YoungMin Kwon

Threads in Java (Deitel & Deitel)

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Distributed Systems Operation System Support

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

CSC 539: Operating Systems Structure and Design. Spring 2006

Threads. Thread Concept Multithreading Models User & Kernel Threads Pthreads Threads in Solaris, Linux, Windows. 2/13/11 CSE325 - Threads 1

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

Chapter 4 Threads, SMP, and

CSE 153 Design of Operating Systems Fall 2018

Threads. Threads (continued)

Lecture 2 Process Management

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Operating Systems Overview. Chapter 2

operating system Spring 2017 Prof.Dr. Hasan Balik Student Name : Walid.W. Ramadan mansour Student ID :

CPSC/ECE 3220 Fall 2017 Exam Give the definition (note: not the roles) for an operating system as stated in the textbook. (2 pts.

OS Design Approaches. Roadmap. OS Design Approaches. Tevfik Koşar. Operating System Design and Implementation

Operating Systems. Figure: Process States. 1 P a g e

Transcription:

Threads Rab Nawaz Jadoon DCS COMSATS Institute of Information Technology Assistant Professor COMSATS Lahore Pakistan Operating System Concepts

Definitions Threads In the previous discussion, a process was an executing program with a single thread of control. Most modern OS now provide features enabling a process to contain multiple threads of control. A thread is a basic unit of CPU utilization. It is a light weight process. If a process has multiple thread of control, it can perform more than one task at a time. Seamless execution of two or more sections of a program at the same time. Threads allows us to do this. 2

Process, threads What is the relationship of a thread, a process and a program? 3

Threads Writing multithreaded programs can be tricky, Although human mind can function concurrently, people find it difficult to jump between parallel thoughts. Why it is difficult to handled multithreaded applications or programs, just see the example, Open three books to page 1, and try reading the books concurrently, Read a few words from book 1, then from 2 and then from 3 rd one. Execute the same work repeatedly. After this experiment, you will appreciate some of the key challenges of the multithreading. 4

Threads i.e. switching between books, reading briefly, remembering your place in each book etc are hectic at the end. 5

Thread possesses a subset of the resources contained in a process, such as, Processor registers, Stack and other, TSD (thread specific data) Threads Such as signal masks (data that describes which signals a thread will not receive) Many OS supports threads, Win32 threads used by MS-32 bit Windows. C-threads these are created from a thread library in the MAC Microkernel. POSIX threads Pthread standards Java threads Thread Class / runnable interface 6

Threads The primary goad of POSIX is to allow Multithreaded programs to be portable across multiple OS platforms. POSIX has been implemented in a variety of OS, Including, Solaris Linux Windows XP etc. 7

Single threaded & multithreaded processes Threads 8

Question??? Why is it difficult to write portable multithreaded applications? ANSWER! There is no standard threading library that is implemented on all platforms. 9

Threads Many s/w packages that run on modern desktop PCs are mulithreaded. An application typically is implemented as a separate process with several threads of control. For example a web browser might have one thread displaying images while other retrieves data from the network. Next example is of word processor may have displaying graphics, another thread for responding the keystrokes from the user and a 3 rd thread for performing spelling and grammar checking in the background. 10

Benefits Responsiveness Threads Multithreading interactive application may allow a program to continue running even if a part of it is blocked or is performing a lengthy operation. For example, a multithreaded web browser could still allow user interaction in one thread while an image was being loaded in another thread. Resource sharing By default, threads share the memory and the resources of the process to which they belong. The benefits of sharing code and data is that it allows an application to have several different threads of activity within the same address space. 11

Economy Allocating memory and resources for process creation is costly. Threads It is more economical to create and context switch threads. In Solaris, creating a process is about thirty times slower than is a creating a thread, and context switching is about five time slower. 12

Utilization of multiprocessor architecture Threads The benefit of multithreading can be greatly increased in a multiprocessor architecture, where threads may be running parallel on different processor. A single threaded process can only run on one CPU, no matter how many are available. Multithreading on a multi CPU machine increases concurrency. 13

Different threads in a process are not independent as different processes. Threads All threads have exactly the same address space which means that they share the same global variables. 14

Thread states Like a traditional process, a thread can be in any one of several states, States Ready Running Waiting Sleeping Blocked dead Description The thread is ready to run and waiting for the CPU. The thread is executing on the CPU. The thread is waiting for some event to happen. The thread has been told to sleep for a time The thread is waiting for I/O to finish The thread is terminated 15

Born Thread life cycle (JAVA) 16

Thread stack 17

Thread states If a process is swapped out, all of its threads are swapped out, b/c they all share the same address space of the process. 18

Thread Operations Threads and processes have many operations in common such as, Create Exit Suspend Resume Sleep Wake Etc. 19

Threading Models Almost all OS s support one of three primary threading models, User level threads (ULTs) Kernel level threads (KLTs) Combination of ULTs and KLTs 20

User Level Threads These performs operation in user space. User Level Threads These are created by run time libraries that cannot access kernel primitives directly. These are transparent to the OS. Single execution context i.e. OS dispatches the multithreaded process as a unit, as opposed to dispatching each individual thread. For this reason ULTs are also called many to one thread mapping, bc OS maps all threads in a multithreaded process to a single execution context. 21

User level thread 22

User level threads In ULTs, OS is unaware that process contains multiple threads. In ULTs the multithreaded process continues until its quantum expires. ULTs are more portable b/c they do not rely on a particular OS s threading API. Application developers can tune the threading library s scheduling algorithm to meet the needs of specific applications. 23

ULTs ULTs presents an API to applications that is independent of the operating system APIs. Deficiencies Kernel views a multithreaded process as a single thread of control. For instance, ULTs do not scale well to multiprocessor systems, b/c the kernel can not simultaneously dispatch a process thread to multiple processors. So ULTs can result in suboptimal performance in multiprocessor systems. 24

Kernel Level threads The KLTs are used to address the limitations in ULTs. This is done by mapping each thread to its own execution context. As a result KLTs are often described as one to one thread mapping. Such mappings require the OS to provide each user thread with a kernel thread that the OS can dispatch. When a user requests a KLT using system calls defined by OS s API, the OS create a KLT that executes the user thread s instructions. 25

Kernel level thread 26

Benefits of KTLs The kernel can dispatch a process s threads to several processors at one, which can improve performance for application designed for concurrent execution. Kernel can manage each thread individually, meaning that OS can dispatch a process s ready threads even if one of its thread is blocked. 27

Difficiencies KLTs are not always the optimal solution for multithreaded applications. KLTs implementation is less efficient than ULTs implementation b/c scheduling and synchronization operations invoke the kernel which increases overhead. Software that employs KLTs, are less portable than the S/W that employs ULTs b/c The application programmer that uses KLTs must modify the program to use the thread API for each OS on which it runs. 28

Hybrid Threading This is a compromise between kernel-level ("1:1") and user-level ("N:1") threading. Many to many association mechanism. In general, "M:N" threading systems are more complex to implement than either kernel or user threads, because changes to both kernel and userspace code are required. In the M:N implementation, the threading library is responsible for scheduling user threads on the available schedulable entities; this makes context switching of threads very fast, as it avoids system calls. 29

Kernel Space User Space Hybrid Threading Model Process P1 Process P2 Process P3 T1 T2 T3 T4 T1 T2 T3 T1 T2 Thread Execution Context 30

Threads Implementation(JAVA) public class ThreadTester { public static void main(string args[]) { //create and name each thread. PrintThread thread1 = new PrintThread(); PrintThread thread2 = new PrintThread(); PrintThread thread3 = new PrintThread(); system.err.println("starting thread"); thread1.start(); thread2.start(); thread3.start(); system.err.println("threads Started, main ended"); }//end of main }//end class ThreadTester 31

Threads in JAVA // Class PrintThread controls thread execution. Class PrintThread extends Thread { private int sleeptime; //assign name to threads by calling super class constructor. public PrintThread ( Stirng name ) { } super(name); //pick random sleep time between 0 and 5 second sleeptime = ( int ) ( math.random ( ) * 5001); 32

pubic void run() { try { Threads in JAVA system.err.println(getname() + "going to sleep for " + sleep time + "milliseconds"); thread.sleep(sleeptime); } //end try catch(interruptedexception e) { e.printstacktrace(); } //end catch system.err.println(getname() + "done sleeping"); }//end method run }// end class PrintThread. 33

Output Starting threads Treads started, main ends thread1 going to sleep for 1217 milliseconds thread2 going to sleep for 3989 milliseconds thread3 going to sleep for 662 milliseconds thread3 done sleeping thread1 done sleeping thread2 done sleeping 34

35