ch4 Thread
ch4 Thread
ch4 Thread
Definition
Multithreading Models
Threading Issues
Pthreads (Unix)
Solaris 2 Threads
Windows 2000 Threads
Linux Threads
Java Threads
1
Thread
A Unix process (heavy-weight process HWP) can
create a number of threads (light-weight processes
LWP)
Program Counter
Register set
Stack
1 2 3
4
Single and Multithreaded Processes
Program
Counter
5
Thread Model
6
Thread Model
Resource Sharing
Several threads work on the same application
Economy
It is much more time consuming to create and manage a
HWP compare to a LWP; PCB vs. the thread context.
Creating a process is 30 time slower than creating a thread
Utilization of MP Architectures
Each thread can run in parallel on a different processor.
8
Thread Usage
Spell-check &
Formatting
Chapter04-OSedition7Final.pptxReading &
writing
files
Scans key-stroke
9
User Threads
10
Relationships Between ULT
States and Process States
Figure 4.6 Examples of the Relationships between User-Level Thread States and
Process States
All of the activity described in the preceding paragraph takes place in user space and within a single
process. The kernel is unaware of this activity. The kernel continues to schedule the process as a
unit and assigns a single execution state (Ready, Running, Blocked, etc.) to that process. The
following examples should clarify the relationship between thread scheduling and process
scheduling. Suppose that process B is executing in its thread 2; the states of the process and two
ULTs that are part of the process are shown in Figure 4.6a . Each of the following is a possible
occurrence:
1. The application executing in thread 2 makes a system call that blocks B. For
example, an I/O call is made. This causes control to transfer to the kernel. The kernel invokes the I/
O action, places process B in the Blocked state, and switches to another process. Meanwhile,
according to the data structure maintained by the threads library, thread 2 of process B is still in the
Running state. It is important to note that thread 2 is not actually running in the sense of being
executed on a processor; but it is perceived as being in the Running state by the threads library.
The corresponding state diagrams are shown in Figure 4.6b .
2. A clock interrupt passes control to the kernel and the kernel determines
that the currently running process (B) has exhausted its time slice. The kernel places process B in
the Ready state and switches to another process. Meanwhile, according to the data structure
maintained by the threads library, thread 2 of process B is still in the Running state. The
corresponding state diagrams are shown in Figure 4.6c .
3. Thread 2 has reached a point where it needs some action performed by thread
1 of process B. Thread 2 enters a Blocked state and thread 1 transitions from Ready to Running.
The process itself remains in the Running state. The corresponding state diagrams are shown in
Figure 4.6d .
In cases 1 and 2 ( Figures 4.6b and 4.6c ), when the kernel switches control back to process B,
execution resumes in thread 2. Also note that a process can be interrupted, either by exhausting its
time slice or by being preempted by a higher priority process, while it is executing code in the
threads library. Thus, a process may be in the midst of a thread switch from one thread to another
when interrupted. When that process is resumed, execution continues within the threads library, 12
which completes the thread switch and transfers control to another thread within that process.
Implementing Threads in User Space
- load programs,
- handle exceptions.
Examples
- Windows 95/98/NT/2000
- Solaris
- Tru64 UNIX
- BeOS
Prof. Peter Buhr
- Linux
University of Waterloo
MicroC++
The starting point for the project was the development of high-level concurrency in an object-oriented
programming language. This approach is in contrast to programming with threads and locks, e.g., POSIX
threads, which deemed unreasonable because it is complicated and error-prone (like assembler
programming). An attempt was made to add concurrency without extending C++, but it turns out to be
impossible to build either an efficient or sound concurrency library due to fundamental language issues (not
problems with C++); therefore, a concurrent dialect of C++ was created, called MiroC++.
14
Implementing Threads in the Kernel
Multithreaded
Web server
Dispatcher thread
Worker thread
16
Multithreading Models
Many-to-One
One-to-One
Many-to-Many
17
Many-to-One
Many user-level threads mapped to
single kernel thread.
18
Many-to-One Model
19
One-to-One
Each user-level thread maps to kernel thread.
Examples
- Windows 95/98/NT/2000
- OS/2
20
One-to-one Model
21
Many-to-Many Model
Allows many user level threads to be mapped
(multiplexed) to many (not equal numbers) kernel
threads.
Solaris 2
22
Many-to-Many Model
23
Hybrid Implementations
Thread cancellation
Asynchronous: may damage incomplete task
Thread pools
25
Pthreads
A POSIX standard (IEEE 1003.1c) API for
thread creation and synchronization.
26
Multi-threaded C program using Pthread API
#include <pthread.h>
#include <stdio.h>
main (int argc, char *argv[]) /* The thread will begin control in this function */
Attribute Value
scope PTHREAD_SCOPE_PROCESS
New thread is unbound not permanently attached to LWP.
Detachstate PTHREAD_CREATE_JOINABLE
Exit status of thread is served after the thread terminates.
stackaddr NULL
New thread has system-allocated stack address.
stacksize 1 megabyte
New thread has system-defined stack size.
inheritsched PTHREAD_INHERIT_SCHED
New thread inherits parent thread scheduling priority.
schedpolicy SCHED_OTHER
New thread uses Solaris-defined fixed priority scheduling; threads run until preempted by a higher-priority
thread or until they block or yield.
28
Solaris 2 Threads
29