Chapter 5: Threads

cavalcadejewelΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 8 μήνες)

150 εμφανίσεις

Silberschatz and Galvin

1999


4.
1

광운대학교

Network Computing Lab.

Chapter 5: Threads


Overview


Multithreading Models


Threading Issues


Pthreads


Solaris 2 Threads


Windows 2000 Threads


Linux Threads


Java Threads



Silberschatz and Galvin

1999


4.
2

광운대학교

Network Computing Lab.

Threads


Process is an executing program with a single thread of control


Modern OS allows for a process to contain multiple threads of control


A
thread

(or

lightweight process
) is a basic unit of CPU utilization; it
consists of:


Thread ID, program counter


register set


stack space


A thread shares with its peer threads its:


code section


data section


operating
-
system resources such as open files and signals

collectively know as a
task
.


A traditional or
heavyweight

process is equal to a task with one thread

Silberschatz and Galvin

1999


4.
3

광운대학교

Network Computing Lab.

Multiple Threads within a Task

Silberschatz and Galvin

1999


4.
4

광운대학교

Network Computing Lab.

Single and Multithreaded Processes

Silberschatz and Galvin

1999


4.
5

광운대학교

Network Computing Lab.

multiple threaded task


In a multiple threaded task, while one server thread is blocked and
waiting, a second thread in the same task can run.


Cooperation of multiple threads in same job confers higher
throughput and improved performance.


Applications that require sharing a common buffer (i.e.,
producer
-
consumer) benefit from thread utilization.


Threads provide a mechanism that allows sequential processes to
make blocking system calls while also achieving parallelism.



Silberschatz and Galvin

1999


4.
6

광운대학교

Network Computing Lab.

Threads


Many s/w packages that run on modern desktop PCs are multithreaded


An application is implemented as a separate process with several
threads of control


A web browser might have one thread display images or text


While another thread retrieves data from network


Word processor may have a thread for displaying graphics


Another thread for reading keystrokes


Another for performing spelling checking in the background


Web server

Silberschatz and Galvin

1999


4.
7

광운대학교

Network Computing Lab.

Benefits of multithreaded programming


Responsiveness


Allow a program to continue running even if part of it is blocked
or is performing a lengthy operation


Multithreaded web browser allow user interaction in one thread
while an image is being loaded in another thread


Resource Sharing


Threads share the memory and resources of the process


Code sharing allows an application to have several threads
within same address space


Economy


Allocating resources for process creation is costly


More economical to create and context switch threads
(resource sharing)


In solaris 2, creating process is about 30 times slower than is
creating a thread, context switching is about 5 times slower


Silberschatz and Galvin

1999


4.
8

광운대학교

Network Computing Lab.

Benefits of multithreaded programming


Utilization of MP Architectures


Benefits of multiprogramming can be greatly increased in MP
architecture, where each thread may be running in parallel on a
different processor


A single threaded process can only run on one CPU, no matter
how many are available


Multithreading on a multi
-
CPU machine increases concurrency


In a single
-
processor architecture, the CPU moves between
each thread as to create an illusion of parallelism, but in reality
only one thread is running at a time

Silberschatz and Galvin

1999


4.
9

광운대학교

Network Computing Lab.

User and Kernel Threads


User
-
level Threads


supported above the kernel, via a set of thread library calls at
the user level (Project Andrew from CMU).


The library supports for thread creation, scheduling with no
kernel support, so fast


Kernel is unaware of user
-
level threads


Drawbacks


If the kernel is single threaded, then any user
-
level thread
performing a blocking system call will cause the entire
process to block, even if other threads are available to run
within the application


User
-
level thread libraries


POSIX Pthreads, Mach c
-
threads, Solaris 2 UI
-
threds


Silberschatz and Galvin

1999


4.
10

광운대학교

Network Computing Lab.

User and Kernel Threads


Kernel
-
supported Threads Supports for threads is provided by OS


The kernel performs thread creation, scheduling in kernel space


Generally slower to manage threads than user thread


If a thread performs a blocking system call, the kernel can
schedule another thread for execution


In MP environment, the kernel can schedule threads on different
processors


Windows 95/98/NT/2000, Solaris 2, BeOS, Linux, Tru64 UNIX



Hybrid approach implements both user
-
level and kernel
-
supported
threads (Solaris 2).



Silberschatz and Galvin

1999


4.
11

광운대학교

Network Computing Lab.

Multithreading Models


Many
-
to
-
One


Many user
-
level threads mapped to single kernel thread.


Thread management is done in user space


Efficient, but entire process will block if a thread makes a
blocking system call


Because only one thread can access the kernel at a time,
multiple threads are unable to run in parallel on MPs


Allows the developer to create as many user threads, however,
true concurrency is not gained because kernel can schedule
only one thread at a time


Used on systems that do not support kernel threads


One
-
to
-
One


Many
-
to
-
Many


Silberschatz and Galvin

1999


4.
12

광운대학교

Network Computing Lab.

Many
-
to
-
One Model

Silberschatz and Galvin

1999


4.
13

광운대학교

Network Computing Lab.

One
-
to
-
one Model



Each user
-
level thread maps to kernel thread.



provides more concurrency than many
-
to
-
one model by allowing


another thread to run when a thread makes a blocking system call or


by allowing multiple threads to run in parallel (concurrently) on MPs



Disadv.


creating a user thread requires creating the corresponding


kernel thread



Examples


Windows 95/98/NT/2000, OS/2


Silberschatz and Galvin

1999


4.
14

광운대학교

Network Computing Lab.

Many
-
to
-
Many Model


Allows many user level threads to be mapped to a smaller or
equal number of kernel threads.


Allows the operating system to create a sufficient number of
kernel threads.


Solaris 2, IRIX, HP
-
UX, True64 Unix


Silberschatz and Galvin

1999


4.
15

광운대학교

Network Computing Lab.

Threading Issues


Semantics of fork() and exec() system calls.


Thread cancellation.


Signal handling


Thread pools


Thread specific data


Silberschatz and Galvin

1999


4.
16

광운대학교

Network Computing Lab.

fork() and exec() system calls


In a multithreaded program, the semantics of fork, exec change


If one thread in a program calls fork, does the new process
duplicate all threads or is the new process single
-
threaded ?


Unix has 2 versions


One that duplicates all threads


Another that duplicates only the thread that invoked fork



If a thread invokes the exec system call, the program specified in
the parameter to exec will replace the entire process (all threads)


Silberschatz and Galvin

1999


4.
17

광운대학교

Network Computing Lab.

Thread cancellation (1/2)


Task of terminating a thread before it has completed


Ex) multiple threads are concurrently searching through a DB and


one thread returns the result.


A thread that is to be cancelled is referred to as target thread


Asynchronous cancellation


one thread immediately terminates
the target thread


Deferred cancellation


the target thread can periodically check if it
should terminate, allowing the target thread an opportunity to
terminate itself in an orderly fashion

Silberschatz and Galvin

1999


4.
18

광운대학교

Network Computing Lab.

Thread cancellation (1/2)


How about the resources that are allocated to the thread?


If a thread was cancelled while in the middle of updating data it is
sharing with other threads?


With asynchronous cancellation, OS may not free a system
-
wide resource



With synchronous cancellation, cancellation points should be
defined

Silberschatz and Galvin

1999


4.
19

광운대학교

Network Computing Lab.

Signal handling (1/3)


A signal is used to notify a process that a particular event has occurred


A signal is generated by the occurrence of a particular event


A generated signal is delivered to a process


Once delivered, the signal must be handled


Delivery of signal can be done either synchronously or asynchronously


Synch: illegal memory access, division by zero


signal is delivered to the same process that performed the
operation causing the signal


Asynch: generated by an event external to a running process


Terminating a process <control> c


Typically asynchronous signals are sent to another process

Silberschatz and Galvin

1999


4.
20

광운대학교

Network Computing Lab.

Signal handling (2/3)


Every signal may be handled by


A default signal handler


A user
-
defined handler


Every signal has a default signal handler that is run by the kernel


The default signal handler is overridden by a user
-
defined signal
handler


Handling signals in single
-
threaded programs is straightforward


Signals are always delivered to a process

Silberschatz and Galvin

1999


4.
21

광운대학교

Network Computing Lab.

Signal handling (3/3)


Where then should a signal be delivered?


Deliver the signal to the thread to which the signal applies


Synchronous signal


Deliver the signal to every thread in the process


Asynchronous signal such as <control> c


Deliver the signal to certain threads in the process


Some unix allows a thread to specify which signals it will
accept or and which it will block


Delivered only to the first thread found in a process that is
not blocking the signal


Assign a specific thread to receive all signals for the process


Solaris 2


Window 2000 : asynchronous procedure call (APC)


Allows a user thread to specify a function that is to be called
when the user thread receives notification of a particular event


Similar to asynchronous signal


Silberschatz and Galvin

1999


4.
22

광운대학교

Network Computing Lab.

Thread pools


Scenario of multithreading a web server


Creating a separate thread is superior to separate process


But, it also has problems


Time required to create the thread


Unlimited thread creation could exhaust system resources


Create a number of threads at process startup and place them in
pool

Silberschatz and Galvin

1999


4.
23

광운대학교

Network Computing Lab.

Thread
-
specific data


Threads belonging to a process share the data of the process


Benefits of multithreaded programming


However, each thread might need its own copy of certain data


Thread
-
specific data


Most thread libraries provide the thread
-
specific data


Win32, Pthreads, Java


Silberschatz and Galvin

1999


4.
24

광운대학교

Network Computing Lab.

Pthreads


The POSIX standard (IEEE 1003.1c) API for thread creation and
synchronization.


A specification for thread behavior, not an implementation


API specifies behavior of the thread library, implementation is
up to development of the library.


Common in UNIX operating systems.


User
-
level thread library



Silberschatz and Galvin

1999


4.
25

광운대학교

Network Computing Lab.

#include <pthread.h>

int sum; /* shared by the threads */

void *runner ( void *param)

Main( int argc, char *argv[]){


pthread_t tid; /* thread id */


pthread_attr_t attr; /* default thread attributes */


if (argc != 2) { fprintf (stderr, ….); exit(); }


pthread_atr_init(&attr);


pthread_create(&tid, &attr, runner, argv[1]);


/* wait for thread to exit */


pthread_join(tid);

Silberschatz and Galvin

1999


4.
26

광운대학교

Network Computing Lab.

Threads Support in Solaris 2


Solaris 2 is a version of UNIX with support for threads at the
kernel and user levels, symmetric multiprocessing, and

real
-
time scheduling.


LWP


intermediate level between user
-
level threads and kernel
-
level threads.


Resource needs of thread types:


Kernel thread: small data structure and a stack; thread
switching does not require changing memory access
information


relatively fast.


LWP: PCB with register data, accounting and memory
information,; switching between LWPs is relatively slow.


User
-
level thread: only need stack and program counter; no
kernel involvement means fast switching. Kernel only sees
the LWPs that support user
-
level threads.

Silberschatz and Galvin

1999


4.
27

광운대학교

Network Computing Lab.

Solaris 2 Threads

Silberschatz and Galvin

1999


4.
28

광운대학교

Network Computing Lab.

Solaris Process

Silberschatz and Galvin

1999


4.
29

광운대학교

Network Computing Lab.

Windows 2000 Threads


Implements the one
-
to
-
one mapping.


Each thread contains


-

a thread id


-

register set


-

separate user and kernel stacks


-

private data storage area


Silberschatz and Galvin

1999


4.
30

광운대학교

Network Computing Lab.

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)


Silberschatz and Galvin

1999


4.
31

광운대학교

Network Computing Lab.

Java Threads


Java threads may be created by:



Extending Thread class


Implementing the Runnable interface



Java threads are managed by the JVM.


Silberschatz and Galvin

1999


4.
32

광운대학교

Network Computing Lab.

Java Thread States

Blocked : waiting (wait)


notify, notifyall, T/O interrupt



sleeping (sleep)


sleep interval expire interupt


blocked (io, synch)


i/o complete, lock acqusition interrupt

Runnable: ready


running : thread dispatch


running


ready : quantum expire, yield