1
School of Computing Science
Simon Fraser University
CMPT 300: Operating Systems I
Ch 4: Threads
Dr. Mohamed Hefeeda
2
Objectives
Thread definitions and relationship to process
Multithreading Models
Threading Issues
Example thread libraries
Pthreads, Win32, Java
3
Thread Definitions
Thread is a basic unit of CPU utilization
A sequence of instructions enclosed in a function
which CPU can execute as a unit
Process is a program in execution
A process is composed of one or more threads
Thread is comprised of (from OS perspective)
Program counter
Register set, and
Stack
Threads belonging to same process share
Code section
Data section
OS resources such as open files and signals
4
Single and Multithreaded Processes
Shared among threads
5
Why Multithreading?
Responsiveness:
one thread for GUI and another for
processing
Resource Sharing:
similar requests handled by the same
code and use same files/resources
Economy:
threads are much cheaper to create/delete
than processes
Utilization of multiprocessors:
single threaded
-
process
can NOT make use of multiple processors
Examples of multithreaded applications?
Web browsers: parallel downloads
Web servers: handle multiple concurrent clients
Word processors: spell check in the background
…. Many others …
6
Multithreading Models
Threads can be created/managed at two levels:
User level threads
•
All data structures are maintained in the user level
•
All thread operations are performed in user mode
•
Kernel knows nothing about user
-
level threads
•
Provided by user
-
level libraries
Kernel level threads
•
All data structures are maintained in the kernel space
•
All thread operations have to be in kernel mode (need
system calls to perform them)
•
Provided by the OS (kernel)
–
Most modern OSs (e.g., XP, Linux, MaC OS X, Solaris)
are multithreaded
7
Multithreading Models (cont’d)
In multithreaded kernels (i.e., most current OSs),
mapping from user
-
level threads to kernel
-
level
threads can be:
Many
-
to
-
One
One
-
to
-
One
Many
-
to
-
Many
8
Many
-
to
-
One
Many user
-
level threads
mapped to single kernel thread
Thread management (create,
delete, schedule, …) is done in
user level
Pros
Managing user threads are
faster (no system calls, no
need to switch to kernel
mode)
Cons
One thread blocks, the
entire process blocks
Cannot use
multiprocessors
Examples (Libraries)
Solaris Green Threads
GNU Portable Threads
9
One
-
to
-
One
Each user
-
level thread maps to a kernel thread
Pros
Increased concurrency (can use multiprocessors)
A thread blocks, others can run
Cons
Creating too many kernel threads may degrade
performance because they consume OS resources
may put limit on number of threads a user can create
Examples: Windows NT/XP/2000, Linux, Solaris 9 and later
10
Many
-
to
-
Many Model
Multiple user threads are mapped to
multiple kernel threads
Mapping is NOT fixed, need
thread scheduler
(in user level)
OS support: a user thread
blocks, kernel notifies thread
scheduler (
upcall
) to select
another to use the free kernel
thread
Pros
Increased Concurrency
Flexible: user may create as
many user threads as she
wants, and kernel creates
only
a sufficient number of kernel
threads
Cons
Complex to implement
Examples:
Solaris prior to version 9
Windows NT/2000 with the
ThreadFiber
package
11
Two
-
level Model
Similar to Many
-
to
-
Many,
except that it allows a user
thread to be
bound
to
kernel thread
Bound thread can be used
for critical operations or
could be the
thread
scheduler
Examples
IRIX
HP
-
UX
Tru64 UNIX
Solaris 8 and earlier
12
Some Threading Issues
Semantics of
fork()
and
exec()
system calls
Signal handling
Thread pools
13
Semantics of fork() within threads
A process has multiple threads. One of them
calls
fork()
Should
fork()
duplicate only the calling thread
or all threads (entire process)?
It depends:
If
exec()
is called immediately after
fork()
, no need to
duplicate all threads (they will be overwritten
anyway)
Else, all threads should be duplicated
14
Signals in Unix
Signals are used in Unix to notify a process
that an event has occurred
Two Types:
Synchronous
: delivered to the same process that
performed the operation caused the signal (e.g.,
illegal memory access)
Asynchronous
: Signal generated by an external
event, usually delivered to a different process (e.g.,
ctrl
-
C)
Sequence:
1.
Signal is generated by an event access, Control
-
C,
Packet arrived, …)
2.
Signal is delivered to a process
3.
Signal handler
processes the signal (default by OS,
or user
-
defined)
15
Signals: Example
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#define BUFFER_SIZE 50
static char
b
uffer[BUFFER_SIZE];
/* the signal handler function */
void handle_SIGINT()
{
write(STDOUT_FILENO,
buffer,
strlen(buffer));
exit(0);
}
int main(int argc, char *argv[])
{
/* set up the signal handler */
struct sigaction handler;
handler.sa_handler = handle_SIGINT;
sigaction(SIGINT, &handler, NULL);
strcpy(buffer,"Caught <ctrl><c>
\
n");
/* wait for <control> <C> */
while (1)
;
return 0;
}
16
Signal Handling (cont’d)
Should OS deliver a signal to: all threads, one
thread, or specific thread of a process?
It depends on the signal type
Synchronous
: a thread performed an operation that
caused a signal to be generated (division by 0, illegal
memory access)
•
Deliver signal to the thread
Asynchronous
: external event generated the signal
(
ctrl
–
C
⤠
•
Deliver signal to all threads belonging to the
process
17
Thread Pools
Create a number of threads in a pool where
they await work
E.g., web server serving many requests of the same
page
Advantages
Usually faster to service a request with an existing
thread than create a new one
Limit number of threads in an application to pool size
•
Further requests are queued till a thread is free
important to maintain performance
18
Example Thread Libraries
POSIX Threads (pthreads library)
API
specifications;
implementation is up to the OS
Common in UNIX systems: Solaris, Linux, Mac OS X
Win 32 Threads
Implementation
of the one
-
to
-
one model in kernel
Java
Java threads are managed by JVM, which is run on top
of an OS
JVM
specifies
the interface, does NOT dictate the
implementation
JVM uses thread services provided by the host OS
19
A Note on Linux Threads
Linux refers to threads and processes as
tasks
Thread creation is done through
clone()
system
call
clone()
can be parameterized to allow a range
of resource sharing possibilities:
No sharing of resources between parent and child,
the entire process is duplicated (same as
fork()
)
Sharing of all resources between parent and child
•
Only
pointers
to shared resources are created for child
Linux does not differentiate between process
and thread (both are tasks). Is this good or bad?
Good: simplify scheduling
Bad: complex to correlate threads with their
processes
20
Summary
A thread is a basic unit of CPU utilization, a
process is composed of one or more threads
Thread has: Program counter, stack, registers
Threads share: code, data, OS resources (open
files and signals)
User level threads vs. kernel threads
Several mapping models from user threads to
kernel threads
M
-
to
-
1, 1
-
to
-
1, M
-
to
-
M
Thread issues: fork(), signals, thread pools,
Thread libraries: pthreads, Win32, Java
Enter the password to open this PDF file:
File name:
-
File size:
-
Title:
-
Author:
-
Subject:
-
Keywords:
-
Creation Date:
-
Modification Date:
-
Creator:
-
PDF Producer:
-
PDF Version:
-
Page Count:
-
Preparing document for printing…
0%
Σχόλια 0
Συνδεθείτε για να κοινοποιήσετε σχόλιο