Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1.

Similar presentations


Presentation on theme: "Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1."— Presentation transcript:

1 Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1

2 Outline Questions? Chapter 4: Threads Next time: – Assignment 1 due (processes.cpp, MyShell.java) – Assignment 2: Threads & Scheduling – Chapter 5: CPU Scheduling CSS 430: Operating Systems - Threads2

3 Chapter 4: Threads Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads CSS 430: Operating Systems - Threads3 Material derived, in part, from Operating Systems Concepts with Java, 8 th Ed. © 2009 Silberschatz, Galvin & Gagne

4 Single and Multithreaded Processes 4CSS 430: Operating Systems - Threads

5 Benefits Responsiveness Resource Sharing Economy Scalability 5CSS 430: Operating Systems - Threads

6 Multicore Programming Multicore systems put pressure on programmers, challenges include: – Dividing activities – Balance – Data splitting – Data dependency – Testing and debugging 6CSS 430: Operating Systems - Threads

7 Multithreaded Server Architecture 7CSS 430: Operating Systems - Threads

8 Concurrent Thread Execution CSS 430: Operating Systems - Threads8

9 User Threads Thread management done by user-level threads library Three primary thread libraries: – POSIX Pthreads – Win32 threads – Java threads 9CSS 430: Operating Systems - Threads

10 Kernel Threads Supported by the Kernel Examples – Windows XP/2000 – Solaris – Linux – Tru64 UNIX – Mac OS X 10CSS 430: Operating Systems - Threads

11 Multithreading Models Many-to-One One-to-One Many-to-Many CSS 430: Operating Systems - Threads11

12 Multithreading Models Many-to-One – Many user-level threads mapped to a single kernel thread – Examples: Solaris Green Threads, Gnu Portable Threads One-to-One – Each user-level thread maps to a separate kernel thread – Examples Windows NT/XP/2000 Linux Solaris 9 and later Many-to-Many – M user level threads mapped to N kernel threads – Examples Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package 12CSS 430: Operating Systems - Threads

13 Two-level Model Similar to M:N, except that it allows a user thread to be bound to kernel thread Examples – IRIX – HP-UX – Tru64 UNIX – Solaris 8 and earlier 13CSS 430: Operating Systems - Threads

14 Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing – Library entirely in user space – Kernel-level library supported by the OS CSS 430: Operating Systems - Threads14

15 Pthreads May be user-level or kernel-level A POSIX standard (IEEE 1003.1c) API for thread creation & synchronization API specifies behavior of the thread library, implementation varies Common in UNIX operating systems (Solaris, Linux, Mac OS X) CSS 430: Operating Systems - Threads15

16 Java Threads CSS 430: Operating Systems - Threads16 Java threads are managed by the JVM – 1 process, 1+ threads Java threads may be created by: – Extending the Thread class http://download.oracle.com/javase/6/docs/api/java/lang/Thread.htm l http://download.oracle.com/javase/6/docs/api/java/lang/Thread.htm l – Implementing the Runnable interface http://download.oracle.com/javase/6/docs/api/java/lang/Runnable.h tml http://download.oracle.com/javase/6/docs/api/java/lang/Runnable.h tml /usr/apps/CSS430/examples/threads public class MyShell extends Thread { … void run() { }

17 Java Threads - Producer-Consumer CSS 430: Operating Systems - Threads17 Main driver: Factory.java – Shared memory via queue variable – Instantiates & starts 2 Thread s /usr/apps/CSS430/examples/threads import java.util.Date; public class Factory { public static void main( String[] args ) { // create the message queue Channel queue = new MessageQueue (); // create the producer and consumer threads Thread producer = new Thread( new Producer(queue) ); Thread consumer = new Thread( new Consumer(queue) ); // start the threads producer.start(); consumer.start(); }

18 Java Threads - Producer-Consumer CSS 430: Operating Systems - Threads18 Shared memory via abstract data type: Channel.java /usr/apps/CSS430/examples/threads public interface Channel { public void send(E item); public E receive(); }

19 Java Threads - Producer-Consumer CSS 430: Operating Systems - Threads19 Implementation of abstract data type: MessageQueue.java /usr/apps/CSS430/examples/threads import java.util.Vector; public class MessageQueue implements Channel { private Vector queue; public MessageQueue() { queue = new Vector (); } public void send( E item ) { queue.addElement( item );// add to tail (end) } public E receive() { if ( queue.size() == 0 ) return null; else// remove from head (front) return queue.remove( 0 ); }

20 Java Threads - Producer-Consumer Implementation of producer thread: Producer.java CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 20 /usr/apps/CSS430/examples/threads import java.util.Date; class Producer implements Runnable { private Channel queue; public Producer( Channel queue ) { this.queue = queue; } @SuppressWarnings("unchecked") public void run() { Date message; while ( true ) { SleepUtilities.nap(); message = new Date(); // produce an item System.out.println( "Producer produced " + message ); queue.send(message); // enter it into the buffer }

21 Java Threads - Producer-Consumer Implementation of consumer thread: Consumer.java CSS 430: Operating Systems - Threads, Scheduling, Assignment 2 21 /usr/apps/CSS430/examples/threads import java.util.Date; class Consumer implements Runnable { private Channel queue; public Consumer( Channel queue ) { this.queue = queue; } @SuppressWarnings("unchecked") public void run() { Date message; while ( true ) { SleepUtilities.nap(); System.out.println( "Consumer wants to consume." ); message = queue.receive(); // consume an item from buffer if ( message != null ) System.out.println( "Consumer consumed " + message ); }

22 Java Thread States 22CSS 430: Operating Systems - Threads

23 Thread Cancellation Terminating a thread before it has finished Two general approaches: – Asynchronous cancellation terminates the target thread immediately – Deferred cancellation allows the target thread to periodically check if it should be cancelled CSS 430: Operating Systems - Threads23

24 Thread Cancellation Terminating a thread before it has finished Two general approaches: – Asynchronous cancellation terminates the target thread immediately Thread.stop(), Thread.destroy() – Deferred cancellation allows the target thread to periodically check if it should be cancelled isInterrupted(), try / catch( InterruptedException e ) CSS 430: Operating Systems - Threads24

25 Thread Cancellation in Java CSS 430: Operating Systems - Threads25 public class InterruptibleThread implements Runnable { private int threadNum; public InterruptibleThread( int threadNum ) { this.threadNum = threadNum; } public void run() { while ( true ) { System.out.printf( "Thread %d working\n", threadNum ); try { Thread.sleep( 500 ); } catch ( InterruptedException e ) { System.out.printf( "Thread %d interrupted\n", threadNum ); break; } // if not using Thread.sleep() or other method requiring try/catch // can also use if ( isInterrupted() ) … } System.out.printf( "Thread %d cleaning up & terminating\n", threadNum ); } /usr/apps/CSS430/examples/os-book/ch4/cancellation/InterruptibleThread.java

26 Thread Cancellation in Java CSS 430: Operating Systems - Threads26 public static void main( String[] args ) { Thread worker1 = new Thread( new InterruptibleThread( 1 ) ); Thread worker2 = new Thread( new InterruptibleThread( 2 ) ); worker1.start(); worker2.start(); // wait 2 seconds so workers can get started try { Thread.sleep( 2000 ); } catch ( InterruptedException e ) { } // Use deferred cancellation with worker1 worker1.interrupt(); // Use asynchronous cancellation with worker2 // NOTE: stop() has been deprecated & should be avoided // (except for demos) worker2.stop(); } /usr/apps/CSS430/examples/os-book/ch4/cancellation/InterruptibleThread.java

27 CSS 430: Operating Systems - Threads27 http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html Why is Thread.stop deprecated? Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. … [possibly leaving them in an inconsistent state] Why are Thread.suspend and Thread.resume deprecated? Thread.suspend is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results. What about Thread.destroy ? Thread.destroy has never been implemented. If it were implemented, it would be deadlock-prone in the manner of Thread.suspend.

28 Thread Pools Create a number of threads in a pool where they await work Advantages: – Usually slightly faster to service a request with an existing thread than create a new thread – Allows the number of threads in the application(s) to be bound to the size of the pool. CSS 430: Operating Systems - Threads28

29 Thread Pools Create a number of threads in a pool where they await work Advantages: – Usually slightly faster to service a request with an existing thread than create a new thread – Allows the number of threads in the application(s) to be bound to the size of the pool. CSS 430: Operating Systems - Threads29 private boolean[] tids; // Indicate which ids have been used private static final int DEFAULT_MAX_THREADS = 10000;

30 Operating System Examples Windows XP Threads Linux Threads (Tasks) CSS 430: Operating Systems - Threads30

31 Windows XP Threads 1:1 mapping, kernel-level Each thread contains – A thread id – Register set – Separate user and kernel stacks – Private data storage area Registers, stacks, & private storage area are known as the context of the threads The primary data structures of a thread include: – ETHREAD (executive thread block) – KTHREAD (kernel thread block) – TEB (thread environment block) CSS 430: Operating Systems - Threads31

32 Linux Threads Referred to as tasks rather than threads Thread creation is done through clone() system call clone() allows a child task to share the address space of the parent task (process) 32CSS 430: Operating Systems - Threads

33 For next time Assignments – Program 1 due – Program 2 assigned Readings – Chapter 5: CPU Scheduling CSS 430: Operating Systems - Threads33


Download ppt "Chapter 4: Threads Joe McCarthy CSS 430: Operating Systems - Threads1."

Similar presentations


Ads by Google