1threads what are they? why are they important? how are they implemented in oses? how to use...

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

Post on 19-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

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

1

ThreadsThreads

• What are they?

• Why are they important?

• How are they implemented in OSes?

• How to use threads? (in Java)

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

2

What are Threads?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).

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

3

Process ImageProcess 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

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

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)

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

5

A Little More Detailed Look at A Little More Detailed Look at Process ImagesProcess Images

thread

Single-threaded (traditional) process

Open files, open network connectionsdevices

User data ->User dataUser stackOS resources

PCB U K Stacks

U Prog U data OS Res

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

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 Prog U data OS Res

thread

TCB

U K Stacks

thread

PCB

TCB

U K Stacks

thread

PCB U K Stacks

U Prog U data OS Res

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

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 Prog U data OS Res

thread

TCB

U K Stacks

thread

PCB

TCB

U K Stacks

thread

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

8

Why Threads?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…)

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

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

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

10

PCB U K Stacks

U Prog U data OS 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 Prog U data OS Res

thread

TCB

U K Stacks

thread

PCB

TCB

U K Stacks

thread

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

11

In Summary: Why Threads?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.

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

12

How are Threads Implemented in OSes?How are Threads Implemented in OSes?

• Two major strategies to implement threads in OSes– User-level threads (UTs)– Kernel level threads (KTs)

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

13

User-level Threads (UTs)User-level Threads (UTs)

• Thread mgt done at user-level programs

• The kernel is not aware of the existence of threads.

Thread libraryUser level

Kernel level

UTs

KT

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

14

UTs: Pros and ConsUTs: 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.

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

15

Example ImplementationsExample Implementations

• Pthreads– POSIX thread package

• Ported on every Unix OSes

• Solaris– With the “green thread” package

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

16

Kernel-level Threads (KTs)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

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

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

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

18

• Many-to-many– Thread multiplexing– The number of KTs is a smaller or equal number of

UTs.

– Examples• HP-UX• IRIX• Tru64 Unix

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

19

Java ThreadsJava 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.

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

20

Code 1Code 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

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

21

Thread.start()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.

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

22

Thread Sleep and InterruptionThread Sleep and Interruption

• A sleeping thread can wake up via interruptions from other threads.– Call interrupt() on a target thread.

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

23

Code 2Code 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.

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

24

The Order of Thread ExecutionThe 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.