Upload
carsyn
View
23
Download
2
Embed Size (px)
DESCRIPTION
Operating Systems Lecture 14 Threads 2 Read Ch 5.4 - 5.8. User and Kernel Level Threads. User level threads : are implemented with user thread libraries. The kernel is unaware of the threads. Kernel level threads: Implemented by the Operating systems. Questions about User Level Threads. - PowerPoint PPT Presentation
Citation preview
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.1Operating System Concepts
Operating Systems
Lecture 14Threads 2
Read Ch 5.4 - 5.8
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.2Operating System Concepts
User and Kernel Level Threads
User level threads: are implemented with user thread libraries. The kernel is unaware of the threads.
Kernel level threads: Implemented by the Operating systems.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.3Operating System Concepts
Questions about User Level Threads
When one thread opens a file, do the other threads see the file?
Does the OS choose the thread, the process or both to execute?
When one thread is blocked in a process (e.g. for I/O) what happens to the other threads?
When a process is swapped, what happens to the ready threads?
What are the key thread states?
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.4Operating System Concepts
Questions about Kernel Level Threads
When one thread opens a file, do the other threads see the file?
Does the OS choose the thread, the process or both to execute?
When one thread is blocked in a process (e.g. for I/O) what happens to the other threads?
When a process is swapped, what happens to the ready threads?
What are the key thread states?
What is the difference between a threaded kernel and kernel threads?
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.5Operating System Concepts
fork( ) and exec( ) with threads
If one thread in a multi-threaded program calls fork( ), does the new process duplicate all threads or only the thread that made the call?
It depends on the version of UNIX.Some UNIX versions have 2 versions of fork( )--
one that duplicates all threads andone that duplicates only one thread.
What happens if exec( ) is called from a thread?The same as before--the entire process is replaced.
If exec( ) is called immediately after fork( ) then it is not necessary to duplicate all threads.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.6Operating System Concepts
Thread Cancellation
Thread cancellation is the task of terminating a thread before it is completed.
The thread to be cancelled is called the target thread. Asynchronous cancellation: One thread immediately terminates
the target thread. This can cause problems: Cancellation may occur while the thread is in the middle of
updating shared data. The OS may not reclaim all resources from the cancelled
thread. Deferred cancellation: Target thread can periodically check to
determine whether it should terminate This allows the target thread an opportunity to terminate itself in
an orderly fashion. Threads are only terminated at cancellation points.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.7Operating System Concepts
Signal Handling
A signal in UNIX is used to notify a process that an event has occurred.
A signal can be synchronous or asynchronous, depending on the event that generated it.
Response to a signal:A signal is generated by the occurrence of an event. The generated signal is delivered to a process.Once delivered the signal must be handled. It is
handled one of two ways: The default signal handler (in the kernel) A user-defined signal handler.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.8Operating System Concepts
Signals and Threads
Options for delivering signals to a multi-threaded process: Deliver the signal to the thread to which the signal applies.
E.g. a divide by zero generates an asynchronous signal. Deliver the signal to every thread in the process
E.g. when the user hits <Control>-C Deliver the signal to certain threads in the process.
some threads can specify which signals they will accept and which they will block.
Typically, the signal is delivered only to the first thread that is not blocking it.
Assign a specific thread to receive all signals for the process (Solaris 2) A special thread gets the signals and then delivers them to
the first thread not blocking the signal.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.9Operating System Concepts
Thread Pools
The problem with allowing a process(e.g. a multi-threading web server) to create as many threads as it wants: It takes time to create a thread prior to handling a service request. Unlimited number of threads could exhaust system resources.
Solution: Thread pools. A thread pool contains a limited number of threads created at process
startup. When the program needs a thread, it takes one from the pool. When the thread is done with its service, it is returned to the pool. If no thread is available, the program must wait for one to be returned
to the pool. Benefits of thread pools:
Faster--the process doesn't have to create the threads each time. Limits the number of threads.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.10Operating System Concepts
Thread specific data
Threads share most of their data with other threads. A thread may need its own data for certain tasks.
This is called thread-specific data. Example: Transaction processing.
Each transaction may be handled by a separate thread.
Each transaction may have a unique ID. Most thread libraries provide support for thread-
specific data.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.11Operating System Concepts
Pthreads
Pthreads is the POSIX standard defining an API for thread creation and synchronization.
It is a specification for thread behavior. Different operating systems may implement it in different ways.
Pthreads are generally used in UNIX-based systems.
Pthreads are considered user level threads.
Example: create a separate thread that computes the summation of the first n integers.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.12Operating System Concepts
#include <pthread.h>#include <stdio.h>
int sum; /* this data is shared by the thread(s) */void *runner(void *param); /* the thread */
main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of attributes for the thread */
/*some error checking here */
pthread_attr_init(&attr); /* get the default attributes */
pthread_create(&tid,&attr,runner,argv[1]); /* create the thread */
pthread_join(tid,NULL); /* now wait for the thread to exit */
printf("sum = %d\n",sum);}
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.13Operating System Concepts
The thread function
void *runner(void *param) { int upper = atoi(param); int i; sum = 0;
if (upper > 0) { for (i = 1; i <= upper; i++) sum += i; }
pthread_exit(0);}
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.14Operating System Concepts
Solaris 2 threads
Solaris 2 uses an intermediate level of threads, called lightweight processes (LWPs), between the user level threads and the kernel level threads.
Each LWP is attached to a kernel level thread. User threads are multiplexed among LWPs.
Bound threads are permanently attached to an LWP. Only one thread is attached to that LWP.
Unbound threads are not permanently attached to an LWP. These are multiplexed among the available LWPs.
The PCB includes a linked list of LWPs associated with the process.
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.15Operating System Concepts
Diagram of Solaris 2 threads
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.16Operating System Concepts
Solaris 2 process
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.17Operating System Concepts
Windows 2000 threads
Windows applications run as a single process containing one or more threads.
Windows implements the one-to-one mapping. Each thread contains
- a thread id
- register set
- separate user and kernel stacks
- private data storage area
Silberschatz, Galvin and Gagne 2002Modified for CSCI 399, Royden, 20055.18Operating System Concepts
Linux and Java threads
Linux threads: Linux refers to them 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) A fork( ) command creates a separate process with a copy of
the address space of the parent. A clone( ) command creates a process with a pointer to the
address space of the parent.
Java Threads: Java threads may be created by:
Extending Thread class Implementing the Runnable interface.
Java threads are managed by the JVM.