CSS430 Threads1 Textbook Ch4 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.

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.
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.
Threads.
5.1 Silberschatz, Galvin and Gagne ©2003 Operating System Concepts with Java Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 5: Threads Benefits User and Kernel Threads Multithreading Models Solaris.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java 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
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 CS 170 TY, Sept 2011.
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  Applied Operating System Concepts Module 5: Threads 9/29/03+ Overview Benefits User and Kernel Threads Multithreading.
CS238 Lecture 5 Threads Dr. Alan R. Davis. Threads Definitions Benefits User and Kernel Threads Multithreading Models Solaris 2 Threads Java Threads.
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Silberschatz, Galvin and Gagne ©2007 Chapter 4: Threads.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Objectives Thread definitions and relationship to process Multithreading.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Chapter 4: Threads. 4.2CSCI 380 Operating Systems Chapter 4: Threads Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Chapter 4: Multithreaded Programming. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Multithreaded Programming Overview.
Multithreading Allows application to split itself into multiple “threads” of execution (“threads of execution”). OS support for creating threads, terminating.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 4 Operating Systems.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 4: Threads CS 170 T Yang, Sept 2012.
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.
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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
CSS430 Threads Textbook Chapter 4
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Multithreading Models Threading Issues Pthreads Solaris 2 Threads.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and 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: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 4: Threads Overview Multithreading Models Threading Issues.
Chapter 4: Threads. 2 Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux 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.
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.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Outline n Overview n Multithreading.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 4: Threads.
Saurav Karmakar. Chapter 4: Threads  Overview  Multithreading Models  Thread Libraries  Threading Issues  Operating System Examples  Windows XP.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1.
CSS430 Threads1 Textbook Ch5 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
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
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.
14.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 4: Multithreaded Programming.
OPERATING SYSTEM CONCEPT AND PRACTISE
Concurrency, Processes and Threads
Chapter 4: Threads.
Modified by H. Schulzrinne 02/15/10 Chapter 4: Threads.
Concurrency, Processes and Threads
Chapter 4: Threads.
Chapter 4: Threads CSS503 Systems Programming
Presentation transcript:

CSS430 Threads1 Textbook Ch4 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.

CSS430 Threads2 Thread Concepts Operating System code files data code files data code files data Process-oriented computing Operating System Multithreaded computing code files data code files data thread process

CSS430 Threads3 Single vs Multithreaded Processes Processes Carries everything such as an address space, code, data, and files Heavyweight in terms of creation/termination/context switching Threads Shares an address space, code, data and files if they belong to the same process Lightweight in terms of creation/termination/context switching Then, what is uniquely carried by each thread?

CSS430 Threads4 Benefits 1. Responsiveness 2. Deadlock avoidance 3. Scalability, (i.e., Utilization of multiprocessor architecture) 4. Resource sharing and economy Threads share an address space, files, code, and data Avoid resource consumption Perform much a faster context swtich

CSS430 Threads5 Benefit 1: Responsiveness Multithreaded Server Clients Web Server DB1 http pages DB2 CGI

CSS430 Threads6 Benefit 2: Deadlock avoidance Process 1 Process 2 Bounded buffer send receive send receive A sequence of send and then receive does not work! Send and receive must be executed concurrently with threads.

CSS430 Threads7 Benefit 3: Utilization of multicore multiprocessor architecture  Place code, files and data in the main memory.  Distribute threads to each of CPUs, and  Let them execute in parallel. code files data time = t time = t + x time = t + 2x

CSS430 Threads8 Discussions 1 1.Why can threads perform their context switch much faster than processes? What data structure must threads carry by their own resource? What data structure can they share? What if we use processes rather than threads for the previous three cases? 1.Responsiveness 2.Deadlock avoidance 3.Utilization of multiprocessor architecture 2. Provide one programming example of multithreading giving improved performance over a single-threaded solution. 3. Provide one programming example of multithreading that would not improve performance over a single-threaded solution.

CSS430 Threads9 CSS430 ThreadOS Processes are managed by your real operating system, and cannot be handled by our ThreadOS. Threads are multiple execution entities inside a process, ThreadOS is a process, and thus ThreadOS executes multiple java application programs with threads.

CSS430 Threads10 User and Kernel Threads User Threads Thread Management Done by User-Level Threads Library Examples - POSIX Pthreads - Win32 threads - Java threads Kernel does not care how many user threads exist. Kernel Threads Supported by the Kernel Examples - Linux - Windows XP/ Solaris lightweight processes

CSS430 Threads11 Many-to-One Model Many user-level threads mapped to a single kernel thread. Used on systems that do not support kernel threads. Examples: Solaris Green Threads, GNU Portable Threads Advantage: fast Disadvantage: 1. Non preemption or very complicated to handle preemption and I/O 2. No use of MP architecture

CSS430 Threads12 One-to-One Model Each user-level thread maps to kernel thread. Examples - Windows NT/XP/ Linux - Solaris 9 or later Advantage: everything is supported by OS. Using MP architecture. Disadvantage: slower than many-to-one model. too many threads do not help.

CSS430 Threads13 Many-to-Many Model Covering the shortage of the previous two models Examples: Windows XP’s fiber library, AIX Pthreads

CSS543Lecture 5: Overview of Threads14 Process Structure Kernel schedules each light-weight process separately stdin stdout stderr priority Intr. mask registers Signal Dispatch Table Memory Map File Descriptors CPU Status Directory Entry UID GID EUID EGID Process ID TTY From MultiThreads Text: p stdin stdout stderr priority Intr. mask registers Signal Dispatch Table Memory Map File Descriptors LWP1 Directory Entry UID GID EUID EGID Process ID TTY priority Intr. mask registers LWP2 LWP ID Traditional UNIX ProcessMultithreaded Process

CSS543Lecture 5: Overview of Threads15 JVM Java Threads Signal Dispatch Table Memory Map File Descriptors Directory Entry UID GID EUID EGID Process ID TTY Thread Structures Thead library runs in a user space. All threads shared the same address space. Each thread maintains its own information and stack space. Once a thread is mapped to a LWP, it runs on a CPU. LWP1 priority Intr. mask registers LWP ID LWP2 priority Intr. mask registers LWP ID Thread2 priority PC Thread ID SP Other regs Thread3 priority PC Thread ID SP Other regs Thread1 priority PC Thread ID SP Other regs code + global data heap red zone stack thread info red zone stack thread info red zone stack thread info Thread2 Thread3 Thread1 Kernel mode User mode Process Thread to LWP mapping Pthreads Win32 threads

CSS430 Threads16 Pthreads 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 Common in UNIX operating systems (Solaris, Linux, Mac OS X)

CSS430 Threads17 Pthreads $ ls MyThread.java ThreadFunc.java $ javac MyThread.java $ java MyThread hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! Master synched with slave $ Compilation and Execution MyThread.java ThreadFunc.java public class MyThread { public static void main( String args[] ) { String arg = args[0]; ThreadFunc child = new ThreadFunc( arg ); child.start( ); for ( int i = 0; i < 10; i++ ) { try { Thread.sleep( 1000 ); } catch ( InterruptedException e ) { }; System.out.println( "I'm a master: " + arg ); } try { child.join( ); } catch ( InterruptedException e ) { }; System.out.println( "Master synched with slave" ); } public class ThreadFunc extends Thread { public ThreadFunc( String param ) { this.param = param; } public void run( ) { for ( int i = 0; i < 5; i++ ) { try { Thread.sleep( 2000 ); } catch ( InterruptedException e ) { }; System.out.println( "I'm a slave: " + param ); } String param; }

CSS430 Threads18 Java Threads Java threads may be created by: 1.Extending thread class 2.Implementing the Runnable interface

CSS430 Threads19 Thread Class Extension class Worker1 extends Thread { public void run() { while ( true ) System.out.println(“I am a Worker Thread”); }

CSS430 Threads20 Thread Creation 1 public class First { public static void main(String args[]) { First main = new First( ); } First( ) { Worker1 runner = new Worker1(); runner.start(); while ( true ) System.out.println(“I am the main thread”); }

CSS430 Threads21 Runnable Interface Implementation // This Runnable interface has been defined by system public interface Runnable { public abstract void run(); } // You have to actually implement the run method. class Worker2 implements Runnable extends myBaseClass { public void run() { while ( true ) System.out.println(“I am a Worker Thread”); }

CSS430 Threads22 Thread Creation 2 public class Second { public static void main(String args[]) { Second main = new Second( ) } Second( ) { Runnable runner = new Worker2(); Thread thrd = new Thread(runner); thrd.start(); while ( true ) System.out.println(“I am the main thread”); }

Java Threads Producer-Consumer CSS430 Threads23

Java Threads Producer CSS430 Threads24

Java Threads Consumer CSS430 Threads25

CSS430 Threads26 Java Thread Management setPriority( ) – changes this thread’s priority. suspend() – suspends execution of the currently running thread. sleep() – puts the currently running thread to sleep for a specified amount of time. resume() – resumes execution of a suspended thread. stop() – stops execution of a thread. Suspend, resume, and stop are currently deprecated in the specification. (However, we will use for Assignment 2.) For more information, visit:

CSS430 Threads27 Java Thread States

CSS430 Threads28 Discussions 2 1. Why does the latest Java compiler deprecate the use of resume, suspend, and stop? Consider an undesired situation incurred when those three functions are used. 2. Code a Java thread corresponding to a Pthread example on page 16. If we omit a join( ) statement in the both version, what happened to them. Do we observe any difference between Pthread and Java?

CSS430 Threads29 Exercises (No turn-in) Consider the following five options to implement synchronization between producer and a consumer, both accessing the same bounded buffer. When we run a producer and a consumer on shared-memory-based dual-processor computer, which of the following implementation is the fastest? Justify your selection. Also select the slowest implementation and justify your selection. (After you have studied synchronization, come back here and solve this exercise.) (1)User the many-to-one thread mapping model, allocate a user thread to a producer and a consumer respectively, and let them synchronize with each other using test-and-set instructions. (2)Use the many-to-one thread mapping model, allocate a user thread to a producer and a consumer respectively, and let them synchronize with each other using semaphore. (3)User the one-to-one thread mapping model, allocate a user thread, (i.e., a kernel thread) to a producer and a consumer respectively, and let them synchronize with each other using test-and- set instructions. (4)User the one-to-one thread mapping model, allocate a user thread, ( i.e., a kernel thread) to a producer and a consumer respectively, and let them synchronize with each other using semaphores. (5)Allocate a different process to a producer and a consumer respectively, and let them synchronize with each other using semaphores. (Note that a bounded buffer is mapped onto the shared memory allocate by those processes through shmget and shmat.)