Chapter 4: Threads
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)
Thread: a basic unit of CPU utilization, consisting of:
Thread ID
Program Counter
Register set
Stack
A thread shares with other threads in the same
process:
Code section
Data section
Other operating system resources, e.g., open files
2
Thread Model
1 2 3
(a) Three processes each with one thread:
traditional Unix type system
(b) One process with three threads
3
The Thread Model (3)
Each thread has its own stack
4
Single and Multithreaded Processes
Program
Counter
5
Thread Model
6
Thread Model
Items shared by all threads in a process
Items private to each thread
7
Benefits
Responsiveness:
System continues working if a thread blocks: web browser
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
A word processor with three threads
9
User Threads
Thread creation, scheduling, and management
done by user-level threads library:
Fast to create; a blocked thread blocks all threads
Chapter04-OSedition7Final.pptx
Examples
- POSIX Pthreads: provides recommendations
- Mach C-threads
- Solaris 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
Runtime system includes
all the code necessary to:
- load programs,
- dynamically link libraries,
- manage memory, and
- handle exceptions.
A user-level threads package 13
Kernel Threads
Supported by the Kernel as kernel constructs:
Slow to create and manage; kernel reschedules blocked
thread.
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
A threads package managed by the kernel
15
Thread Usage
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.
Used on systems that do not support
kernel threads.
Threads can not run on multi-processor
system.
18
Many-to-One Model
19
One-to-One
Each user-level thread maps to kernel thread.
Threads can run on a multi-processor system.
Restriction on the number of the created threads.
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.
Allows the operating system to create a sufficient
number of kernel threads (not equal number as one-
to-one); it can use multi-processor system.
Solaris 2
Windows NT/2000 with the ThreadFiber package
22
Many-to-Many Model
23
Hybrid Implementations
Multiplexing user-level threads onto kernel- level
threads
24
Threading Issues
Semantics of fork() and exec() system calls.
Create all threads or just one thread
Exec() is executed immediately or not
Thread cancellation
Asynchronous: may damage incomplete task
Synchronous: thread periodically scans for canceling: divide-by-zero / page
fault
Signal handling (synchronous, async.)
Signal: notifying the process that particular event happened
Signal delivered to process and must be handled
Default signal handler (can override by user handler)
Thread pools
Thread specific data
25
Pthreads
A POSIX standard (IEEE 1003.1c) API for
thread creation and synchronization.
API specifies behavior of the thread library,
implementation is up to development of the
library.
Common in UNIX operating systems.
26
Multi-threaded C program using Pthread API
#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[]) /* The thread will begin control in this function */
pthread-t tid; /* the thread identifier */ void *runner(void *Param)
pthread-attr-t attr; /* set of thread attributes */
if (argc != 2) { {
fprintf(stderr,"usage: a.out <integer value>\n”) int upper = atoi(param);
exit(); int i;
} sum = 0;
if (atoi(argv[1]) < 0) { if (upper > 0) {
fprintf(stderr,"%d must be >= 0\n", atoi(argv[l])); for (i = 1; i <= upper; i++)
exit(); sum += i;
} }
/* get the default attributes */ pthread-exit(O);
pthread-attr-init (&attr); }
/* create the thread */
pthread-create (&tid, &attr, runner, argv [1]);
/* now wait for the thread to exit */
pthread-join (tid, NULL) ; 27
printf(“sum = %d\n",sum);
}
Pthread attributes
http://www.cs.cf.ac.uk/Dave/C/node30.html
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