Download Threads (continued)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Burroughs MCP wikipedia , lookup

Berkeley Software Distribution wikipedia , lookup

Plan 9 from Bell Labs wikipedia , lookup

Unix security wikipedia , lookup

Linux adoption wikipedia , lookup

Distributed operating system wikipedia , lookup

Smallfoot wikipedia , lookup

Spring (operating system) wikipedia , lookup

Linux kernel wikipedia , lookup

DNIX wikipedia , lookup

Security-focused operating system wikipedia , lookup

Process management (computing) wikipedia , lookup

Thread (computing) wikipedia , lookup

Transcript
Threads (continued)
CS-3013, Operating Systems
C-Term 2008
(Slides include materials from Operating System Concepts, 7th ed., by Silbershatz, Galvin,
& Gagne and from Modern Operating Systems, 2nd ed., by Tanenbaum)
CS-3013 C-term 2008
Threads
1
Review
• Threads introduced because
• Processes are heavyweight in Windows and Linux
• Difficult to develop concurrent applications when
address space is unique per process
• Thread — a particular execution of a
program within the context of a WindowsUnix-Linux process
• Multiple threads in same address space at same time
CS-3013 C-term 2008
Threads
2
This problem …
• … is partly an artifact of
• Unix, Linux, and Windows
and of
• Big, powerful processors (e.g., Pentium, Athlon)
• … tends to occur in most large systems
• … is infrequent in small-scale systems
• PDAs, cell phones
• Closed systems (i.e., controlled applications)
CS-3013 C-term 2008
Threads
3
Characteristics
• A thread has its own
• Program counter, registers, PSW
• Stack
• A thread shares
• Address space, heap, static data, program code
• Files, privileges, all other resources
with all other threads of the same process
CS-3013 C-term 2008
Threads
4
Address Space for Multiple Threads
thread 1 stack
SP (T1)
0xFFFFFFFF
thread 2 stack
SP
SP (T2)
thread 3 stack
SP (T3)
Virtual
address space
heap
static data
0x00000000
CS-3013 C-term 2008
PC
PC (T2)
PC (T1)
PC (T3)
code
(text)
Threads
5
Who creates and manages threads?
• User-level implementation
– done with function library (e.g., POSIX)
– Runtime system – similar to process
management except in user space
– Windows NT – fibers: a user-level thread
mechanism ???
• Kernel implementation – new system calls
and new entity to manage
– Linux: lightweight process (LWP)
– Windows NT & XP: threads
CS-3013 C-term 2008
Threads
6
Mutual Exclusion within Threads
extern void thread_yield();
extern int TestAndSet(int &i);
/* sets the value of i to 1 and returns the
previous value of i. */
void enter_critical_region(int &lock) {
while (TestAndSet(lock) == 1)
thread_yield(); /* give up processor */
};
void leave_critical_region(int &lock) {
lock = 0;
};
CS-3013 C-term 2008
Threads
7
Reading Assignment
• Silbertshatz
– Chapter 4 – “Threads”
• Robert Love, Linux Kernel Development
– Chapter 3 – “Process Management”
CS-3013 C-term 2008
Threads
8
Kernel Threads
• Supported by the Kernel
• OS maintains data structures for thread state and
does all of the work of thread implementation.
• Examples
•
•
•
•
•
Solaris
Tru64 UNIX
Mac OS X
Windows 2000/XP/Vista
Linux version 2.6
CS-3013 C-term 2008
Threads
9
Kernel Threads (continued)
• OS schedules threads instead of processes
• Benefits
– Overlap I/O and computing in a process
– Creation is cheaper than processes
– Context switch can be faster than processes
• Negatives
– System calls (high overhead) for operations
– Additional OS data space for each thread
CS-3013 C-term 2008
Threads
10
Threads – supported by processor
• E.g., Pentium 4 with Hyperthreading™
• www.intel.com/products/ht/hyperthreading_more.htm
• Multiple processor cores on a single chip
• True concurrent execution within a single process
• Requires kernel thread support
• Re-opens old issues
• Deadlock detection
• Critical section management of synchronization primitives
(especially in OS kernel)
CS-3013 C-term 2008
Threads
11
Unix Processes vs. Threads
• On a 700 Mhz Pentium running Linux
– Processes:
• fork()/exit(): 250 microsec
– Kernel threads:
• pthread_create()/pthread_join(): 90 microsec
– User-level threads:
• pthread_create()/pthread_join(): 5 microsec
CS-3013 C-term 2008
Threads
12
POSIX pthread Interface
• Data type:– pthread_t
• int pthread_create(pthread_t *thread, const
pthread_attr_t *attr, void*(*start_routine)
(void), void *arg) ;
– creates a new thread of control
– new thread begins executing at start_routine
• pthread_exit(void *value_ptr)
– terminates the calling thread
• pthread_join(pthread_t thread, void **value_ptr);
– blocks the calling thread until the thread specified terminates
• pthread_t pthread_self()
– Returns the calling thread's identifier
CS-3013 C-term 2008
Threads
13
Threads and “small” operating systems
• Many “small” operating systems provide a
common address space for all concurrent
activities
• Each concurrent execution is like a LinuxUnix thread
• But its often called a process!
• pthread interface and tools frequently used
for managing these processes
• …
CS-3013 C-term 2008
Threads
14
Java Threads
• Thread class
• Thread worker = new Thread();
• Configure it
• Run it
• public class application extends Thread {
… }
• Methods to
•
•
•
•
Run, wait, exit
Cancel, join
Synchronize
…
CS-3013 C-term 2008
Threads
15
Threading Issues
• Semantics of fork() and exec() system
calls for processes
• Thread cancellation
• Signal handling
• Kernel thread implementations
– Thread pools
– Thread specific data
– Scheduler activations
CS-3013 C-term 2008
Threads
16
Semantics of fork() and exec()
• Does fork() duplicate only the calling
thread or all threads?
– Easy if user-level threads
– Not so easy with kernel-level threads
• Linux has special clone() operation – only forking
thread is created in child process
• Windows XP has something similar
CS-3013 C-term 2008
Threads
17
Thread Cancellation
• Terminating a thread before it has finished
• Reason:–
• Some other thread may have completed the joint
task
• E.g., searching a database
• Issue:–
• Other threads may be depending cancelled thread
for resources, synchronization, etc.
• May not be able to cancel one until all can be
cancelled
CS-3013 C-term 2008
Threads
18
Thread Cancellation (continued)
• Two general approaches:
– Asynchronous cancellation terminates the target
thread immediately
– Deferred cancellation allows the target thread
to periodically check if it should cancel itself
• pthreads provides cancellation points
CS-3013 C-term 2008
Threads
19
Signal Handling
• Signals are used in Unix-Linux to notify process that a
particular event has occurred — e.g.
–
–
–
–
–
Divide-by-zero
Illegal memory access, stack overflow, etc.
CTL-C typed, or kill command issued at console
Timer expiration; external alarm
…
• A signal handler is used to process signals
– Signal is generated by particular event
– Signal is delivered to a process
– Signal is handled by a signal handler
• All processes provided with default signal handler
• Applications may install own handlers for specific signals
CS-3013 C-term 2008
Threads
20
Signal Handling Options
• Deliver signal to specific thread to which it applies
• E.g., illegal memory access, divide-by-zero, etc.
• Deliver signal to every thread in the process
• CTL-C typed
• Deliver signal to certain threads in the process
• I.e., threads that have agreed to receive such signals
(or not blocked them)
• Assign a specific thread to receive all signals for
the process
• …
CS-3013 C-term 2008
Threads
21
Kernel Thread Implementations
• Linux
• Windows
• Others
CS-3013 C-term 2008
Threads
22
Modern Linux Thread Implementation
• Implemented directly in kernel
• Primary unit of scheduling and computation
implemented by Linux 2.6 kernel
• “A thread is just a special kind of process.”
• Robert Love, Linux Kernel Development, p.23
• Every thread has its own task_struct in
kernel
• …
CS-3013 C-term 2008
Threads
23
Definition
• Task (from point of view of Linux kernel):–
– Process
– Thread
– Kernel thread (see later)
CS-3013 C-term 2008
Threads
24
Modern Linux Threads (continued)
• Process task_struct has pointer to own memory
& resources
• Thread task_struct has pointer to process’s
memory & resources
• Kernel thread task_struct has null pointer to
memory & resources
• fork() and pthread_create() are library
functions that invoke clone() system call
• Arguments specify what kind of clone
• …
CS-3013 C-term 2008
Threads
25
Modern Linux Threads (continued)
• Threads are scheduled independently of
each other
• Threads can block independently of each
other
• Even threads of same process
• Threads can make their own kernel calls
• Kernel maintains a small kernel stack per thread
• During kernel call, kernel is in process context
CS-3013 C-term 2008
Threads
26
Process Context
0xFFFFFFFF
Kernel Code and Data
Kernel Space
stack
SP2
(dynamically allocated)
stack
SP1
(dynamically allocated)
Virtual
User Space
heap
address space
(dynamically allocated)
static data
PC
code
(text)
0x00000000
32-bit Linux & Win XP – 3G/1G user space/kernel space
CS-3013 C-term 2008
Threads
27
Modern Linux Threads (continued)
• Multiple threads can be executing in kernel
at same time
• When in process context, kernel can
• sleep on behalf of its thread
• take pages faults on behalf of its thread
• move data between kernel and process address space
on behalf of thread
•…
CS-3013 C-term 2008
Threads
28
Linux Kernel Threads
• Kernel has its own threads
• No associated process context
• Supports concurrent activity within kernel
• Multiple devices operating at one time
• Multiple application activities at one time
• Multiple processors in kernel at one time
• A useful tool
• Special kernel thread packages, synchronization
primitives, etc.
• Useful for complex OS environments
CS-3013 C-term 2008
Threads
29
Windows XP Threads
• Much like to Linux 2.6 threads
•
•
•
•
Primitive unit of scheduling defined by kernel
Threads can block independently of each other
Threads can make kernel calls
…
• Process is a higher level (non-kernel)
abstraction
• See Silbershatz, §22.3.2.2
CS-3013 C-term 2008
Threads
30
Other Implementations of Kernel Threads
• Many-to-Many
• Many-to-One
• One-to-One
CS-3013 C-term 2008
Threads
31
Thread Pools (Implementation
technique)
• 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 application(s)
to be bounded by size of pool
CS-3013 C-term 2008
Threads
32
Many-to-Many Model
• Allows many user level threads to be
mapped to many kernel threads
• Allows the operating system to create a
sufficient number of kernel threads
• Solaris prior to version 9
• Windows NT/2000 with the Thread/Fiber
package
CS-3013 C-term 2008
Threads
33
Many-to-Many Model
CS-3013 C-term 2008
Threads
34
Two-level Model
• Similar to M:M, except it allows a specific
thread to be bound to one kernel thread
• Examples
•
•
•
•
IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
CS-3013 C-term 2008
Threads
35
Two-level Model
CS-3013 C-term 2008
Threads
36
Many-to-One
• Many user-level threads mapped to single
kernel thread
• Examples:
• Solaris Green Threads
• GNU Portable Threads
CS-3013 C-term 2008
Threads
37
Many-to-One Model
CS-3013 C-term 2008
Threads
38
One-to-One
• Each user-level thread maps to kernel thread
• Examples
• Windows NT/XP/2000
• Linux
• Solaris 9 and later
CS-3013 C-term 2008
Threads
39
One-to-one Model
CS-3013 C-term 2008
Threads
40
Threads – Summary
• Threads were invented to counteract the
heavyweight nature of Processes in Unix,
Windows, etc.
• Provide lightweight concurrency within a
single address space
• Have evolved to become primitive abstraction
defined by kernel
• Fundamental unit of scheduling in Linux, Windows, etc
CS-3013 C-term 2008
Threads
41
Reading Assignment
• Silbertshatz
– Chapter 4 – “Threads”
• Robert Love, Linux Kernel Development
– Chapter 3 – “Process Management”
CS-3013 C-term 2008
Threads
42
Questions?
CS-3013 C-term 2008
Threads
43