Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1Threads What are they? Why are they important? How are they implemented in OSes? How to use threads? (in Java)"— Presentation transcript:

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

2 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 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 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 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 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 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 8 Why Threads? Finer-grained concurrency –Old, good days: coarse-grained concurrency Writing a document with an editor, while sending/fetching emails –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 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 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 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 12 How are Threads Implemented in OSes? Two major strategies to implement threads in OSes –User-level threads (UTs) –Kernel level threads (KTs)

13 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 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 15 Example Implementations Pthreads –POSIX thread package Ported on every Unix OSes Solaris –With the “green thread” package

16 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 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 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 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 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

21 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 22 Thread Sleep and Interruption A sleeping thread can wake up via interruptions from other threads. –Call interrupt() on a target thread.

23 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 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.


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

Similar presentations


Ads by Google