240 likes | 533 Views
Threads. What are they? Why are they important? How are they implemented in OSes? How to use threads? (in Java). 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)
E N D
Threads • What are they? • Why are they important? • How are they implemented in OSes? • How to use threads? (in Java)
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).
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 Image Process Control Block User Program User Data/Stack Kernel Stack
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)
A Little More Detailed Look at Process Images Open files, open network connections devices U Prog U data OS Res PCB U K Stacks User data -> User data User stack OS resources thread Single-threaded (traditional) process
U Prog U data OS Res PCB TCB TCB TCB U K Stacks U K Stacks U K Stacks thread thread thread U Prog U data OS Res • The CPU is scheduled against threads. • Concurrency (pseudo-parallelism) within a process • Context switches occur among threads. PCB U K Stacks thread Multi-threaded process Single-threaded (traditional) process
U Prog U data OS Res PCB TCB TCB TCB U K Stacks U K Stacks U K Stacks thread thread thread • 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
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…)
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
U Prog U data OS Res PCB TCB TCB TCB U K Stacks U K Stacks U K Stacks thread thread thread • 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. U Prog U data OS Res PCB U K Stacks thread Multi-threaded process Single-threaded (traditional) process
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.
How are Threads Implemented in OSes? • Two major strategies to implement threads in OSes • User-level threads (UTs) • Kernel level threads (KTs)
User-level Threads (UTs) • Thread mgt done at user-level programs • The kernel is not aware of the existence of threads. UTs User level Thread library Kernel level KT
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.
Example Implementations • Pthreads • POSIX thread package • Ported on every Unix OSes • Solaris • With the “green thread” package
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. UTs User level Kernel level KTs KT
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
Many-to-many • Thread multiplexing • The number of KTs is a smaller or equal number of UTs. • Examples • HP-UX • IRIX • Tru64 Unix
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.
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
Thread.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.
Thread Sleep and Interruption • A sleeping thread can wake up via interruptions from other threads. • Call interrupt() on a target thread.
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.
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.