1Threads What are they? Why are they important? How are they implemented in OSes? How to use threads? (in Java)

Slides:



Advertisements
Similar presentations
Threads. Objectives To introduce the notion of a thread — a fundamental unit of CPU utilization that forms the basis of multithreaded computer systems.
Advertisements

Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Chapter 4: Multithreaded Programming
Chapter 5 Threads os5.
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.
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads.
Chapter 4: Multithreaded Programming
Course: Operating Systems Instructor: Umar Kalim NUST Institute of Information Technology, Pakistan Operating Systems.
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.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Threads CSCI 444/544 Operating Systems Fall 2008.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
Process Concept An operating system executes a variety of programs
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 &
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Threads.
9/13/20151 Threads ICS 240: Operating Systems –William Albritton Information and Computer Sciences Department at Leeward Community College –Original slides.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
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.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
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 Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java 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.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 13 Threads Read Ch 5.1.
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. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads.
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.
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.
Multithreaded Programing. Outline Overview of threads Threads Multithreaded Models  Many-to-One  One-to-One  Many-to-Many Thread Libraries  Pthread.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts What is Thread “Thread is a part of a program.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 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.
Department of Computer Science and Software Engineering
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Lecture 3 Threads Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
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.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads  Overview  Multithreading Models  Threading Issues  Pthreads.
Threads by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Introduction to threads
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Chapter 5: Threads Overview Multithreading Models Threading Issues
Threads & multithreading
Chapter 4: Threads.
Operating System Concepts
Chapter 4: Threads.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Multithreaded Programming
Chapter 4: Threads.
Presentation transcript:

1Threads What are they? Why are they important? How are they implemented in OSes? How to use threads? (in Java)

2 What are Threads? Recap: What are processes? –A process is a container (or execution environment) for a program in execution. Threads (threads of execution/control) –A finer-grained container (or execution env) for a program in execution, than a process. A traditional process has a single thread of execution. A process can contain multiple concurrent threads (of execution).

3 Process Image Created for each process Each process image consists of 4 elements –User program The program to be executed. –User data The mutable part of memory. Includes program data and a user stack. –Used to keep track of procedure calls and parameter passing between procedures. –Kernel stack Used to keep track of system calls and parameter passing. –Process control block (PCB) Data needed by an OS to control a process (a set of attributes of a process) Process Control Block User Program User Data/Stack Kernel Stack Process Image

4 PCB: the most important data structure in an OS –Process identification PID –CPU state information The contents of CPU registers Program counter –Indicates the address of the next instruction to be executed. –Process control information Event that a process is waiting on. Scheduling info –Current scheduling priority (pri) –User mode priority (usrpri) –CPU utilization (cpu) –Nice value (user-controllable adjustment factor; nice)

5 A Little More Detailed Look at Process Images thread Single-threaded (traditional) process Open files, open network connections devices User data -> User data User stack OS resources PCBU K Stacks U ProgU dataOS Res

6 thread Single-threaded (traditional) process Multi-threaded process The CPU is scheduled against threads. Concurrency (pseudo-parallelism) within a process –Context switches occur among threads. TCB U K Stacks U ProgU dataOS Res thread TCB U K Stacks thread PCB TCB U K Stacks thread PCBU K Stacks U ProgU dataOS Res

7 Thread Control Block (TCB) –Thread ID –The contents of CPU registers –Program counter Indicates the address of the next instruction to be executed. –Event that a thread is waiting on. –Scheduling info Different threads can share the memory and the resources used by a process. –User program –User data –OS resources TCB U K Stacks U ProgU dataOS Res thread TCB U K Stacks thread PCB TCB U K Stacks thread

8 Why Threads? Finer-grained concurrency –Old, good days: coarse-grained concurrency Writing a document with an editor, while sending/fetching s –Now: finer-grained concurrency A program is expected to do different things at the same time. –Word »Displaying text and images »Responding to keystrokes/mouse input from a user »Retreating data within the text »Accessing the MS web site to look for fancy document templates »Performing spelling and grammar checking in the background –Web browser »Displaying text and images »Responding to keystrokes/mouse input »Checking and downloading software updates –iTunes »Playing music »Downloading music and its info (album’s cover, song titles, lyrics…)

9 A program is expected to do the same or similar things at the same time –Web server »Accepts and parses an HTTP request »Finds a target file »Makes an HTTP message (header, payload, etc.) »Returns a target file with the HTTP message Why not use multiple processes? –It was in common use 10 to 15 years ago before threading technology is well developed and it became popular

10 PCBU K Stacks U ProgU dataOS Res thread Single-threaded (traditional) process Multi-threaded process Process-creation is heavyweight. –Time-consuming and resource intensive. Creating a process is 30 times slower than creating a thread. Process switching is 5 times slower than thread switching. TCB U K Stacks U ProgU dataOS Res thread TCB U K Stacks thread PCB TCB U K Stacks thread

11 In Summary: Why Threads? Responsiveness –Threads allow a program to continue running even if a part of it is blocked for I/O or is performing a long operation. Resource sharing –Threads share the memory and the resources of a process that they belong to. Efficiency –Allocating memory and resources for process creation is costly. –Switching processes is heavyweight.

12 How are Threads Implemented in OSes? Two major strategies to implement threads in OSes –User-level threads (UTs) –Kernel level threads (KTs)

13 User-level Threads (UTs) Thread mgt done at user-level programs The kernel is not aware of the existence of threads. Thread library User level Kernel level UTs KT

14 UTs: Pros and Cons Pros –Threads do not need to change their modes between the user mode to kernel mode. No overhead of mode switches –Thread scheduling can be application specific. Apps can tailor their sched algorithms without disturbing the underlying OS scheduler. –The thread library can be very portable. No changes required to the underlying kernel. Cons –When a thread calls a system call, the thread AND all the other threads in the same process are blocked.

15 Example Implementations Pthreads –POSIX thread package Ported on every Unix OSes Solaris –With the “green thread” package

16 Kernel-level Threads (KTs) No thread mgt at user-level System calls (APIs) provided to the kernel thread mgt facility. One-to-one –Even if a thread calls a system call, the kernel schedules all other threads in the same process to a CPU. –With multiple processors, the kernel can schedule different threads in the same process to different CPUs. User level Kernel level UTs KT KTs

17 –The number of UTs is same as the number of KTs. –Example implementations Linux Windows (Win32) Solaris Hybrid of UT-only and KT-one-to-one –Solaris

18 Many-to-many –Thread multiplexing –The number of KTs is a smaller or equal number of UTs. –Examples HP-UX IRIX Tru64 Unix

19 Java Threads All Java programs comprise at least one thread of control. –main() runs as a single thread in a JVM. A thread is implicitly created when a JVM starts. Need to explicitly create additional threads. 4 things to do: –Define a class implementing the Runnable interface (java.lang) public abstract void run(); –Write a threaded task in run() in the class –Instantiate a thread and assign a Runnable object to the thread –Start (call start() on) the instantiated thread. run() is executed on the thread.

20 Code 1 HelloWorld.java GreetingRunnable.java Output: –Mon Mar 26 15:14:43 EDT 2007 Hello World –Mon Mar 26 15:14:44 EDT 2007 Hello World –Mon Mar 26 15:14:45 EDT 2007 Hello World –Mon Mar 26 15:14:46 EDT 2007 Hello World –Mon Mar 26 15:14:47 EDT 2007 Hello World –Mon Mar 26 15:14:48 EDT 2007 Hello World –Mon Mar 26 15:14:49 EDT 2007 Hello World –Mon Mar 26 15:14:50 EDT 2007 Hello World –Mon Mar 26 15:14:51 EDT 2007 Hello World –Mon Mar 26 15:14:52 EDT 2007 Hello World

21Thread.start() Creating a Thread object does not create a new thread; rather, it is start() that actually create it. Start() –Allocates memory and initialies a new thread in a JVM. –Calls run() of a specified Runnable object. Do not call run() directly. start() calls run() on our behalf.

22 Thread Sleep and Interruption A sleeping thread can wake up via interruptions from other threads. –Call interrupt() on a target thread.

23 Code 2 HelloWorld2.java and GreetingRunnable.java Output: –Mon Mar 26 15:28:45 EDT 2007 Goodbye World –Mon Mar 26 15:28:45 EDT 2007 Hello World –Mon Mar 26 15:28:46 EDT 2007 Hello World –Mon Mar 26 15:28:46 EDT 2007 Goodbye World –Mon Mar 26 15:28:47 EDT 2007 Hello World –Mon Mar 26 15:28:47 EDT 2007 Goodbye World –Mon Mar 26 15:28:48 EDT 2007 Goodbye World –Mon Mar 26 15:28:48 EDT 2007 Hello World –Mon Mar 26 15:28:49 EDT 2007 Goodbye World –Mon Mar 26 15:28:49 EDT 2007 Hello World –Mon Mar 26 15:28:50 EDT 2007 Goodbye World –Mon Mar 26 15:28:50 EDT 2007 Hello World –Mon Mar 26 15:28:51 EDT 2007 Goodbye World –Mon Mar 26 15:28:51 EDT 2007 Hello World –Mon Mar 26 15:28:52 EDT 2007 Hello World –Mon Mar 26 15:28:52 EDT 2007 Goodbye World –Mon Mar 26 15:28:53 EDT 2007 Hello World Two message sets (Hello and Goodbye) are not exactly interleaved.

24 The Order of Thread Execution The JVM thread scheduler gives NO guarantee about the order in which threads are executed. There are always slight variations in thread execution times, –especially when calling OS system calls (typically I/O related system calls) Expect that the order of thread execution is somewhat random.