You are on page 1of 18

Operating Systems

Lecture 14
Threads 2
Read Ch 5.4 - 5.8

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.1 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.2 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.3 Modified for CSCI 399, Royden, 2005
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 2002
Operating System Concepts 5.4 Modified for CSCI 399, Royden, 2005
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 and
one 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 2002


Operating System Concepts 5.5 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.6 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.7 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.8 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.9 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.10 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.11 Modified for CSCI 399, Royden, 2005
#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 2002
Operating System Concepts 5.12 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.13 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.14 Modified for CSCI 399, Royden, 2005
Diagram of Solaris 2 threads

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.15 Modified for CSCI 399, Royden, 2005
Solaris 2 process

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.16 Modified for CSCI 399, Royden, 2005
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 2002


Operating System Concepts 5.17 Modified for CSCI 399, Royden, 2005
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.

Silberschatz, Galvin and Gagne 2002


Operating System Concepts 5.18 Modified for CSCI 399, Royden, 2005

You might also like