5/24/2018 Ch04 - Multithreaded Programming.ppt
1/106
Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition,
Multithreaded Programming
Modified by M.Rebaudengo - 2010
5/24/2018 Ch04 - Multithreaded Programming.ppt
2/106
4.2 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Processes and Threads
! Processabstraction combines two concepts" Concurrency
! Each process is a sequential execution stream of instructions" Protection
! Each process defines an address space! Address space identifies all addresses that can be touched by the program
! Threads" Key idea: separate the concepts of concurrency from protection" A thread is a sequential execution stream of instructions" A process defines the address space that may be shared by multiple
threads" Threads can execute on different cores on a multicore CPU (parallelism for
performance) and can communicate with other threads.
5/24/2018 Ch04 - Multithreaded Programming.ppt
3/106
4.3 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Single Thread vs. Multithreads
!In traditional operating systems, each process has an address space and asingle thread of control
! There are frequently solutions in which it is desirable to have multiplethreads of control in the same address space running concurrently, asthough they are (almost) separate processes (except for the shared address
space).
5/24/2018 Ch04 - Multithreaded Programming.ppt
4/106
4.4 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Thread Model
!(a) Three processes each with one thread
! (b) One process with three threads
5/24/2018 Ch04 - Multithreaded Programming.ppt
5/106
4.5 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Example of multithreads (I)
!Consider how a Web servercan improve performance and interactivity byusing threads." When a Web server receives requests for images and pages from many
clients, it serves each request (from each client) with a different thread" The process that receives all the requests, creates a new separate
thread for each request received" This new thread sends the required information to the remote client." While this thread is doing its task, the original thread is free to accept
more requests" Web servers are multiprocessor systems that allow for concurrent
completion of several requests, thus improving throughput and
response time.
5/24/2018 Ch04 - Multithreaded Programming.ppt
6/106
4.6 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
The Case for Threads
Consider a Web serverget network message (URL) from clientget URL data from diskcompose responsesend response
How well does this web server perform?
5/24/2018 Ch04 - Multithreaded Programming.ppt
7/106
4.7 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
How Can it Help?
!Consider a Web server
! Create a number of threads, and for each threaddo
" get network message from client" get URL data from disk" send data over network
! What did we gain?
5/24/2018 Ch04 - Multithreaded Programming.ppt
8/106
4.8 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Overlapping Requests (Concurrency)
get network message (URL) from clientget URL data from disk
send data over network
get network message (URL) from client
get URL data from disk
send data over network
Request 1Thread 1
Request 2Thread 2
Time
(disk access latency) (disk access latency)
Total time is less than request 1 + request 2
5/24/2018 Ch04 - Multithreaded Programming.ppt
9/106
4.9 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Example of multithreads (II)
!Consider a word processorcomposed of the following threads:
" a thread interacts with the user" a thread handles reformatting the document in the background" a thread performs spell checking" a thread handles the disk backups without interfering with the other two
in order to automatically save the entire file every few minutes to protect
the user against losing the work in the event of program crash.
5/24/2018 Ch04 - Multithreaded Programming.ppt
10/106
4.10 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Process vs. Threads
!A processhas an address space containing program text and data, as wellother resources like, open files, child processes, pending alarms, signal
handlers, accounting information, etc.! A threadof execution has a program counter, registers and a stack.! Processes are used to group resources together! Threads are the entities scheduled for execution on the CPU.
5/24/2018 Ch04 - Multithreaded Programming.ppt
11/106
4.11 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Multithreads
!Different threads in a process are not as independent as differentprocesses:" All threads have exactly the same address space, which means that
they share the same global variables.
5/24/2018 Ch04 - Multithreaded Programming.ppt
12/106
4.12 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Threads
! A thread represents an abstract entity that executes a sequence of instructions" It has its own set of CPU registers" It has its own stack" There is no thread-specific heap or data segment (unlike process)
! Threads are lightweight" Creating a thread is more efficient than creating a process." Communication between threads is easier than btw. processes." Context switching between threads requires fewer CPU cycles and memory
references than switching processes." Threads only track a subset of process state (share list of open files, pid,
).
5/24/2018 Ch04 - Multithreaded Programming.ppt
13/106
4.13 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Single and Multithreaded Processes
5/24/2018 Ch04 - Multithreaded Programming.ppt
14/106
4.14 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Benefits
!Responsiveness
" When one thread is blocked, your browser still responds" E.g. download images while allowing your interaction
! Resource Sharing" Share the same address space" Reduce overhead (e.g. memory)
! Economy" Creating a new process costs memory and resources" E.g. Solaris is 30 times slower in creating process than thread
! Scalablity / Parallelization" Threads can be executed in parallel on multiple processors" Increase concurrency and throughput
5/24/2018 Ch04 - Multithreaded Programming.ppt
15/106
4.15 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Performance Benefits
!It takes far less time to create a new thread in an existing process than tocreate a new process
! It takes less time to terminate a thread than a process! It takes less time to switch between 2 threads within the same process than
to switch between processes! Threads between the same process share memory and files: they can
communicate with each other without invoking the kernel.! If there is an application or function that should be implemented as a set of
related units of execution, it is far more efficient to do so as a collection ofthreads rather than a collection of separate processes.
5/24/2018 Ch04 - Multithreaded Programming.ppt
16/106
4.16 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Parallel Execution on a Multicore System
5/24/2018 Ch04 - Multithreaded Programming.ppt
17/106
4.17 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Multicore Programming
! Multicore systems putting pressure on programmers, challenges include" Dividing activities" Balance" Data splitting" Data dependency" Testing and debugging
5/24/2018 Ch04 - Multithreaded Programming.ppt
18/106
4.18 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
How Can it Help?
! How can this code take advantage of 2 threads?for(k = 0; k < n; k++)
a[k] = b[k] * c[k] + d[k] * e[k];! Rewrite this code fragment as:
do_mult(l, m) {for(k = l; k < m; k++)
a[k] = b[k] * c[k] + d[k] * e[k];}main() {
CreateThread(do_mult, 0, n/2);CreateThread(do_mult, n/2, n);
5/24/2018 Ch04 - Multithreaded Programming.ppt
19/106
4.19 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Latency and Throughput
! Latency: time to complete an operation! Throughput: work completed per unit time.
5/24/2018 Ch04 - Multithreaded Programming.ppt
20/106
4.20 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Relationship between Latency and Throughput
! Latency and bandwidth only loosely coupled" Henry Ford: assembly lines increase bandwidth without reducing
latency! My factory takes 1 day to make a Model-T Ford.
" But I can start building a new car every 10 minutes" At 24 hrs/day, I can make 24 * 6 = 144 cars per day" A special order for 1 green car, still takes 1 day" Throughput is increased, but latency is not.
! Latency reduction is difficult! Often, one can buy bandwidth
" E.g., more memory chips, more disks, more computers" Big server farms (e.g., google) are high bandwidth
5/24/2018 Ch04 - Multithreaded Programming.ppt
21/106
4.21 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Latency and bandwith
! Multiplying vector example: reduced latency! Web server example: increased throughput! What is High speed Internet?
" Low latency: needed to interactive gaming" High bandwidth: needed for downloading large files.
5/24/2018 Ch04 - Multithreaded Programming.ppt
22/106
4.22 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Thread Programming models
! Boss/worker! Peer model! Pipeline! Thread pool
5/24/2018 Ch04 - Multithreaded Programming.ppt
23/106
4.23 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
taskX
taskY
taskZ
main ( )
WorkersProgram
Files
Resources
Databases
Disks
Special
Devices
Boss
Input (Stream)
The boss/worker model
5/24/2018 Ch04 - Multithreaded Programming.ppt
24/106
4.24 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Example
main() /* the boss */
{
forever {
get a request;
switch( request )
case X: thread_create(....,taskX);
case Y: thread_create(....,taskY);
....
}
}
taskX() /* worker */
{
perform the task, sync if accessing shared resources
}
taskY() /* worker */
{
perform the task, sync if accessing shared resources
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
25/106
4.25 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
The peer model
taskX
taskY
WorkersProgram
Files
Resources
Databases
Disks
Special
Devices
taskZ
Input
(static)
5/24/2018 Ch04 - Multithreaded Programming.ppt
26/106
4.26 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Example
main()
{
thread_create(....,task1);
thread_create(....,task2);
....
signal all workers to start
wait for all workers to finish
do any cleanup
}
}
task1() /* worker */
{
wait for start
perform the task, sync if accessing shared resources
}
task2() /* worker */
{
wait for start
perform the task, sync if accessing shared resources
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
27/106
4.27 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Resources Files
Databases
Disks
Special Devices
Files
Databases
Disks
Special Devices
Files
Databases
Disks
Special Devices
Stage 1 Stage 2 Stage 3
Program Filter Threads
Input (Stream)
A thread pipeline
5/24/2018 Ch04 - Multithreaded Programming.ppt
28/106
4.28 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Examplemain()
{
thread_create(....,stage1);thread_create(....,stage2);
....
wait for all pipeline threads to finish
do any cleanup
}
stage1() {
get next input for the program
do stage 1 processing of the input
pass result to next thread in pipeline
}
stage2(){
get input from previous thread in pipeline
do stage 2 processing of the input
pass result to next thread in pipeline
}
stageN()
{
get input from previous thread in pipeline
do stage N processing of the input
pass result to program output.
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
29/106
4.29 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Thread pool
! Runtime overhead of creating thread can be solved by thread poolthemain thread creates all worker threads at program initialization and each
worker thread suspends itself immediately for a wakeup call from boss.! Typically there are more tasks than threads. Tasks are organized in queue.
As soon as a thread completes its task, it will request the next task from the
queue.! The number of threads is a parameter that can be tuned to provide the best
performance.! The number of threads could be dynamic based on the number of waiting
tasks.
5/24/2018 Ch04 - Multithreaded Programming.ppt
30/106
4.30 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Thread Model
! Items shared by all threads in a process! Items private to each thread
5/24/2018 Ch04 - Multithreaded Programming.ppt
31/106
4.31 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Thread Control Block (TCB)
! Each thread has:" an identifier," a set of registers (including the program counter)" a set of attributes including the state, the stack size, scheduling
parameters, etc.
5/24/2018 Ch04 - Multithreaded Programming.ppt
32/106
4.32 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Stacks
! Each thread has its own stack:" Each stack contains one frame for each procedure called but not yet
returned from" This frame contains the procedures local variables and the return
address to use when the procedure call has finished" Each thread will call different procedures and thus have a different
execution history.
5/24/2018 Ch04 - Multithreaded Programming.ppt
33/106
4.33 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Threads vs. Processes
Threads! A thread has no data segment or
heap! A thread cannot live on its own, it
must live within a process! There can be more than one thread
in a process, the first thread callsmain and has the processs stack! If a thread dies, its stack is
reclaimed! Inter-thread communication via
memory.! Each thread can run on a different
physical processor! Inexpensive creation and context
switch
Processes
A process has code/data/heap &
other segments
There must be at least one thread in
a process
Threads within a process share
code/data/heap, share I/O, but eachhas its own stack and registers
If a process dies, its resources are
reclaimed and all threads die
Inter-process communication via OS
and data copying.
Each process can run on a different
physical processor
Expensive creation and context
switch
5/24/2018 Ch04 - Multithreaded Programming.ppt
34/106
4.34 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Implementing Threads
!Processes define an addressspace; threads share the addressspace
! Process Control Block (PCB)contains process-specific
information
"Owner, PID, heap pointer,priority, active thread, andpointers to thread information
! Thread Control Block (TCB)contains thread-specific information" Stack pointer, PC, thread state
(running, ), register values, apointer to PCB, Code
Initialized data
Heap
DLLs
mapped segments
Processsaddress space
Stack thread1
PCSP
StateRegisters
TCB for
Thread1
Stack thread2
PCSP
StateRegisters
TCB forThread2
5/24/2018 Ch04 - Multithreaded Programming.ppt
35/106
4.35 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Threads Life Cycle
! Threads (just like processes) go through a sequence of start, ready, running,waiting, and donestates
RunningReady
Waiting
Start Done
5/24/2018 Ch04 - Multithreaded Programming.ppt
36/106
4.36 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Exercise
Context switch time for which entity is greater?1. Process2. Thread
5/24/2018 Ch04 - Multithreaded Programming.ppt
37/106
4.37 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Exercise
Threads have their own?1. CPU2. Address space3. PCB4. Stack5. Registers
5/24/2018 Ch04 - Multithreaded Programming.ppt
38/106
4.38 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Exercise
Threads have the same scheduling states as processes1. True2. False
5/24/2018 Ch04 - Multithreaded Programming.ppt
39/106
4.39 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Exercise
#include #include #include int value = 0; /* this data is shared by the thread(s) */void *runner(void *param); /* the thread */
}void *runner(void *param)
{value = 5;pthread_exit(0);
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
40/106
4.40 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Exercise (cont.)
int main(int argc, char *argv[]){pid_t pid;pthread_t tid; /* the thread identifier */pthread_attr_t attr; /* set of attributes for the thread */pid = fork()if (pid == 0) { /* child process */
pthread_attr_init(&attr);pthread_create(&tid,&attr,runner,NULL);
/* now wait for the thread to exit */pthread_join(tid,NULL);printf("CHILD: value = %d\n",value);
}else if (pid > 0) { /* parent process */
wait(NULL);printf("PARENT: value = %d\n",value);
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
41/106
4.41 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Thread Libraries
! Thread libraries provide programmer with APIs for creating and managingthreads
! Two primary ways of implementing" User-Level Threads (ULT): library entirely in user space (invoking a
function in the library results in a local function call in user space and
not a system call)" Kernel-Level Threads (KLT): Kernel-level library supported by the OS
(system call).
5/24/2018 Ch04 - Multithreaded Programming.ppt
42/106
4.42 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
User-Level Thread
! All of the work of thread management is done by the application and thekernel is not aware of the existence of threads
! An application can be programmed to be multithreading by using a threadslibrary, which is a package of routines for thread management
! Threads library contains code for:" creatingand destroyngthreads" passing databetween threads" schedulingthread execution" saving and restoring thread context.
5/24/2018 Ch04 - Multithreaded Programming.ppt
43/106
4.43 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
User-Level Thread
5/24/2018 Ch04 - Multithreaded Programming.ppt
44/106
4.44 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
ULT: behavior (I)
! By default, an application begins with a single thread and begins running the thread! This application and its thread are allocated to a single process managed by the
kernel! At any time that the application is running (i.e., the process is in the Running state),
the application may spawn a new thread to run within the same process. Spawning isdone by invoking the spawn utility in the threads library. Control is passed to thatutility by a procedure call
! The threads library creates a data structure for the new thread and then passescontrol to one of the threads, within this process, in the Ready state using somescheduling algorithm
! When control is passed to the library, the context of the current thread is saved, andwhen the control is passed from the library to a thread the context of that thread isrestored. The context consists of the contents of user registers, the program counterand the stack pointers.
! All the previous activities takes place in the user space and within a single process.The kernel is anaware of this activity.
5/24/2018 Ch04 - Multithreaded Programming.ppt
45/106
4.45 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
ULT: behavior (II)
! The kernel continues to schedule the process as a unit and assigns a singleexecution state (Running, Blocked, Ready, etc.) to that process
! When a thread does something that may cause it to become blocked locally(e.g., waiting for another thread in its process to complete some work), itcalls a procedure in the threads library
! This procedure checks if the thread must be put into blocked state. If so, itsaves its context, calls the thread scheduler to pick another thread to run
! The thread scheduler looks in the thread table for a ready thread to run andrestores its context
5/24/2018 Ch04 - Multithreaded Programming.ppt
46/106
4.46 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
User-level Threads Scheduling
5/24/2018 Ch04 - Multithreaded Programming.ppt
47/106
4.47 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
User-level threads: comments
! + Fast to create and switch" procedures that saves the thread's state and the scheduler are user
procedures" no system call is needed" no context switch is needed" the memory cache does need to be flushed
! - When a ULT executes a system call, all the threads within the process areblocked" E.g., read from file can block all threads
! - User-level scheduler can fight with kernel-level scheduler! - A multithread application cannot take advantage of multiprocessing. A
kernel assigns one process to only one processor at a time. There areapplcations that would benefit the ability to execute portions of code
simultaneously.
5/24/2018 Ch04 - Multithreaded Programming.ppt
48/106
4.48 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Exercise
When a user level thread does I/O it blocks the entire process.1. True2. False
5/24/2018 Ch04 - Multithreaded Programming.ppt
49/106
4.49 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Kernel-Level Threads
! The kernel knows the threads and manges them! There is no thread table in each process. Instead, the kernel has a thread
table that keeps track of all the threads in the system! When a process wants to create a new thread or destroy an existing thread,
it makes a kernel call, which then does the creation or destruction by
updating the kernel thread table.! The thread table containing TCBs holds the same information as with the
ULT, but now kept in the kernel instead of in user space.
5/24/2018 Ch04 - Multithreaded Programming.ppt
50/106
4.50 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Kernel-Level Threads
5/24/2018 Ch04 - Multithreaded Programming.ppt
51/106
4.51 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Kernel-level Threads Scheduling
5/24/2018 Ch04 - Multithreaded Programming.ppt
52/106
4.52 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
! Kernel-level threads" + Kernel-level threads do not block process for system call
!if one thread in a process is blocked by a system call (e.g., for apage fault), the kernel can easily check if the process has one
thread ready to run" + Only one scheduler (and kernel has global view)" - Can be difficult to make efficient (create & switch)
Kernel-level threads: comments
5/24/2018 Ch04 - Multithreaded Programming.ppt
53/106
4.53 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Multithreading Models
! Many-to-One#
! One-to-One#! Many-to-Many
5/24/2018 Ch04 - Multithreaded Programming.ppt
54/106
4.54 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Many-to-One (ULT)
! Many user-level threads mapped to single kernel thread" the kernel has no knowledge of the application threads
! Examples:" Solaris Green Threads" GNU Portable Threads
5/24/2018 Ch04 - Multithreaded Programming.ppt
55/106
4.55 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Many-to-One Model
5/24/2018 Ch04 - Multithreaded Programming.ppt
56/106
4.56 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
One-to-One (KLT)
! Each user-level thread maps to kernel thread! Examples
" Windows NT/XP/2000" Linux" Solaris 9 and later
5/24/2018 Ch04 - Multithreaded Programming.ppt
57/106
4.57 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
One-to-one Model
5/24/2018 Ch04 - Multithreaded Programming.ppt
58/106
4.58 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Many-to-Many Model
! Allows many user level threads to be mapped to many kernelthreads
! Allows the operating system to create a sufficient number ofkernel threads
! The threads library is responsible for scheduling user threadson the available schedulable entities
! When a thread performs a blocking system call, the kernel canschedule another thread for execution
! Examples:" Solaris prior to version 9" Windows NT/2000 with the ThreadFiberpackage
5/24/2018 Ch04 - Multithreaded Programming.ppt
59/106
4.59 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Many-to-Many Model
5/24/2018 Ch04 - Multithreaded Programming.ppt
60/106
4.60 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Two-level Model
! Similar to M:M, except that it allows a user thread to bebound to kernel thread
! Examples" IRIX" HP-UX" Tru64 UNIX" Solaris 8 and earlier
5/24/2018 Ch04 - Multithreaded Programming.ppt
61/106
4.61 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Two-level Model
5/24/2018 Ch04 - Multithreaded Programming.ppt
62/106
4.62 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Windows XP Threads
! Implements the one-to-one mapping! Each thread contains
" A thread id" Register set" Separate user and kernel stacks" Private data storage area
! The register set, stacks, and private storage area are knownas the context of the threads
! The primary data structures of a thread include:" ETHREAD (executive thread block)" KTHREAD (kernel thread block)" TEB (thread environment block)
5/24/2018 Ch04 - Multithreaded Programming.ppt
63/106
4.63 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Linux Threads
! Linux refers to them as tasksrather 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)
5/24/2018 Ch04 - Multithreaded Programming.ppt
64/106
4.64 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Java Threads
! Java threads are managed by the JVM! Java threads may be created by:
" Extending Thread class" Implementing the Runnable interface#
5/24/2018 Ch04 - Multithreaded Programming.ppt
65/106
4.65 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Pthreads
! May be provided either as user-level or kernel-level! A POSIX standard (IEEE 1003.1c) API to:
" create and destroy threads" synchronize threads and lock program resourses" manage thread scheduling
! API specifies behavior of the thread library, implementation is up todevelopment of the library
! Common in UNIX operating systems (Solaris, Linux, Mac OS X).
5/24/2018 Ch04 - Multithreaded Programming.ppt
66/106
4.66 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
pthread_create()
! Synthax:" pthread_create(thread,attr,start_routine,arg)
! Arguments:" thread: A unique identifier for the new thread returned by the
subroutine.
" attr: it specifies a thread attributes object, or NULL for the defaultvalues.
" start_routine: the C routine that the thread will execute once it iscreated.
" arg: A single argument that may be passed to start_routine. It must bepassed by reference as a pointer cast of type void. NULL may be used ifno argument is to be passed.
5/24/2018 Ch04 - Multithreaded Programming.ppt
67/106
4.67 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
#include
#include
main() {
pthread_t f2_thread, f1_thread, f3_thread; int i1=1,i2=2;
void *f2(), *f1(),*f3();
pthread_create(&f1_thread,NULL,f1,&i1);
pthread_create(&f2_thread,NULL,f2,&i2);
pthread_create(&f3_thread,NULL,f3,NULL);
}
void *f1(int *i){
}
void *f2(int *i){
}
void *f3() {
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
68/106
4.68 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
pthread_exit()
! When a thread has finished its work, it can exit by calling thepthread_exit()library procedure
! The thread then vanishes and is no longer schedulable and the stack isreleased.
5/24/2018 Ch04 - Multithreaded Programming.ppt
69/106
4.69 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Examplevoid *PrintHello(void *threadid)
{
long tid;tid = (long) threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0;t
5/24/2018 Ch04 - Multithreaded Programming.ppt
70/106
4.70 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Examplevoid *PrintHello(void *threadid)
{
long tid;
tid = (long) threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0;t
5/24/2018 Ch04 - Multithreaded Programming.ppt
71/106
4.71 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Multiple arguments via a structure: example
include #include #include #define NUM_THREADS 5char *messages[NUM_THREADS];struct thread_data
{int thread_id;int sum;char *message;
};
struct thread_data thread_data_array[NUM_THREADS];
5/24/2018 Ch04 - Multithreaded Programming.ppt
72/106
4.72 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
void *PrintHello(void *threadarg){
int taskid, sum;char *hello_msg;struct thread_data *my_data;sleep(1);my_data = (struct thread_data *) threadarg;taskid = my_data->thread_id;sum = my_data->sum;hello_msg = my_data->message;printf("Thread %d: %s Sum=%d\n", taskid, hello_msg, sum);pthread_exit(NULL);
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
73/106
4.73 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
int main(int argc, char *argv[]){pthread_t threads[NUM_THREADS];int *taskids[NUM_THREADS];int rc, t, sum =0;messages[0] = "English: Hello World!";messages[1] = "French: Bonjour, le monde!";messages[2] = "Spanish: Hola al mundo";messages[3] = "German: Guten Tag, Welt!";
messages[4] = "Russian: Zdravstvytye, mir!";for(t=0;t
5/24/2018 Ch04 - Multithreaded Programming.ppt
74/106
4.74 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
pthread_join()
! In some thread systems, one thread can wait for a (specific) thread to exitby calling the pthread_join()procedure
! This procedure blocks the calling thread until (a specific) thread has exited! The thread identifier of the thread to wait for is given as a parameter.
5/24/2018 Ch04 - Multithreaded Programming.ppt
75/106
4.75 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Joinable or not?
! When a thread is created, one of its attributes defines whether it is joinable ordetached." Only threads that are created as joinable can be joined." If a thread is created as detached, it can never be joined.
! Define and initialize attribute object:pthread_attr_t attr;
! To explicitly create a thread as joinable, the attrargument in thepthread_create()routine is used:
" Initialize the attribute variable withpthread_attr_init()! To set the detach state:
" pthread_attr_setdetachstate(&attr, THREAD_CREATE_DETACHED );
5/24/2018 Ch04 - Multithreaded Programming.ppt
76/106
4.76 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Example
void *howdy(void *vargp);
int main() {pthread_t tid;
pthread_create(&tid, NULL, howdy, NULL);pthread_join(tid, NULL);exit(0);
}
/* thread routine */void *howdy(void *vargp) {printf("Hello, world!\n");return NULL;
}
Thread attributes(usually NULL)
Thread arguments(void *p)
return value(void **p)
5/24/2018 Ch04 - Multithreaded Programming.ppt
77/106
4.77 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Execution
main thread
peer thread
return NULL;main thread waits forpeer thread to terminate
exit()
terminatesmain thread andany peer threads
call Pthread_create()call Pthread_join()
Pthread_join() returns
printf()(peer threadterminates)
Pthread_create() returns
5/24/2018 Ch04 - Multithreaded Programming.ppt
78/106
4.78 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
pthread_yield()
! pthread_yield()is another library call that allows a thread to voluntarilygive up the CPU to let another thread run
! There is no such call for processes because the assumption is thatprocesses are competitive and each one wants all the CPU time it can get.However, since the threads of a process are working together and their
code is written by the same programmer, it is possible that a thread
activates another thread.
5/24/2018 Ch04 - Multithreaded Programming.ppt
79/106
4.79 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Example
! A pthread program illustrating how to create a simple thread and some ofthe pthreadAPI
! The program implements the summation function where the summationoperation is run as a separate thread.
5/24/2018 Ch04 - Multithreaded Programming.ppt
80/106
4.80 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
#include #include int sum; /* this data is shared by the thread(s) */void *runner(void *param); /* the thread */int main(int argc, char *argv[]){pthread_t tid; /* the thread identifier */pthread_attr_t attr; /* set of attributes for the thread */if (argc != 2) { fprintf(stderr,"usage: a.out \n");
return -1; }if (atoi(argv[1]) < 0) { fprintf(stderr,"Argument %d must be non-negative\n",atoi(argv[1]));
return -1;}/* get the default attributes */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);printf("sum = %d\n",sum);}
5/24/2018 Ch04 - Multithreaded Programming.ppt
81/106
4.81 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
/*** The thread will begin control in this function*/
void *runner(void *param)
{int i, upper = atoi(param);sum = 0;
if (upper > 0) {for (i = 1; i
5/24/2018 Ch04 - Multithreaded Programming.ppt
82/106
4.82 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
p gAnother Threads Stack
char **ptr; /* global */
int main(){
int i;pthread_t tid;char *msgs[N] = {
"Messagge 1","Messagge 2"};
ptr = msgs;for (i = 0; i < 2; i++)
Pthread_create(&tid,NULL,thread,
(void *)i);Pthread_exit(NULL);
}
/* thread routine */void *thread(void *vargp){
int myid = (int)vargp;static int svar = 0;
printf("[%d]: %s (svar=%d)\n",
myid, ptr[myid], ++svar);}
Peer threads access main threads stackindirectly through global ptr variable
5/24/2018 Ch04 - Multithreaded Programming.ppt
83/106
4.83 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Mapping Vars to Mem. Instances
char **ptr; /* global */
int main(){
int i;pthread_t tid;
char *msgs[N] = {"Messagge 1","Messagge 2"
};ptr = msgs;for (i = 0; i < 2; i++)
Pthread_create(&tid,NULL,
thread,(void *)i);
Pthread_exit(NULL);}
/* thread routine */void *thread(void *vargp){
int myid = (int)vargp;static int svar = 0;
printf("[%d]: %s (svar=%d)\n",myid, ptr[myid], ++svar);
}
Global var: 1 instance (ptr [data])
Local static var: 1 instance (svar [data])
Local automatic vars: 1 instance (i.m, msgs.m )
Local automatic var: 2 instances (myid.p0[peer thread 0s stack],myid.p1[peer thread 1s stack]
)
5/24/2018 Ch04 - Multithreaded Programming.ppt
84/106
4.84 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Shared Variable Analysis
! Which variables are shared?Variable Referenced by Referenced by Referenced byinstance main thread? peer thread 0? peer thread 1?ptr yes yes yessvar no yes yesi.m yes no no
msgs.m yes yes yesmyid.p0 no yes nomyid.p1 no no yes
! Answer: A variable x is shared if multiple threads reference at least oneinstance of x. Thus:" ptr, svar, and msgsare shared." iand myidare NOTshared.
5/24/2018 Ch04 - Multithreaded Programming.ppt
85/106
4.85 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Semantics of fork() and exec()
! Does fork()duplicate only the calling thread or all threads?" some UNIX systems have chosen to have two versions of fork():
!one that duplicates all threads (forkall())!one that duplicates only the thread invoked by the fork() system call
(fork1())" If exec() is called immediately after forking, then duplicating all threads
is unnecessary. In this istance, duplicating only the calling thread is
appropriate.
Th d C ll i
5/24/2018 Ch04 - Multithreaded Programming.ppt
86/106
4.86 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Thread Cancellation
! Terminating a thread before it has completed. This case is called target thread.The pthread_cancel(thread)function requests that threadbe canceled
! Cancelability statedetermines whether a thread can receive a cancellationrequest. If the cancelability state is disabled, the thread does not receive anycancellation requests. The current thread's cancelability state can be changedby calling pthread_setcancelstate()
! Two approaches (types) (the current thread's cancelability type can be changedby calling pthread_setcanceltype()) :" Asynchronous cancellationterminates the target thread immediately.
If you set a thread's cancelability type to asynchronous, the thread canreceive a cancellation request at any time.
" Deferred cancellationallows the target thread to periodically check if itshould be cancelledIf you set a thread's cancelability type to deferred, cancellation requests areacted on as soon as the thread reaches a cancellation point
pthread_testcancel() creates a cancellation point in the callingthread.
Si l
5/24/2018 Ch04 - Multithreaded Programming.ppt
87/106
4.87 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Signal
! A signalis a software notification to a process of an event" Signal is generated when the event that causes the signal occurs" Signal is delivered when the process takes action based on the signal" The lifetime of a signal is the interval between its generation and
delivery" Signal that has been generated but not yet delivered is pending" Process catches signal if it executes signal handler when the signal is
delivered" Alternatively, a process can ignore a signal when it is delivered, that is
to take no action
" Process can temporarily prevent signal from being delivered by blockingit
" Signal Mask contains a set of signals currently blocked.
Si l
5/24/2018 Ch04 - Multithreaded Programming.ppt
88/106
4.88 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Signal
! Signals are used in UNIX systems to notify a process that aparticular event has occurred
! Examples:" Typing certain key combinations at the terminal of a running
process causes the system to send it certain signals:
" CTRL-C sends an INT signal (SIGINT); by default, this causesthe process to terminate.
" CTRL-Z sends a TSTP signal (SIGTSTP); by default, thiscauses the process to suspend execution.
" CTRL-\ sends a QUIT signal (SIGQUIT); by default, this causesthe process to terminate and store the content of the memory(core dump).
A ti P f d R i i Si l
5/24/2018 Ch04 - Multithreaded Programming.ppt
89/106
4.89 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Actions Performed upon Receiving a Signal
! There are three ways in which a process can respond to a signal:" Explicitly ignore the signal." Execute the default action associated with the signal." Catch the signal by invoking a corresponding signal-handler function.
! OS signal system call" To ignore: signal(SIG#, SIG_IGN)" To reinstate default: signal(SIG#, SIG_DFL)" To catch: signal(SIG#, myHandler)
! OS provides a facility for writing your own event handlers in the style ofinterrupt handlers.
Si l H dl
5/24/2018 Ch04 - Multithreaded Programming.ppt
90/106
4.90 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Signal Handler
! A signal handleris used to process signals1. Signal is generated by particular event2. Signal is delivered to a process3. Signal is handled
! Corresponding to each signal there is a signal handler! Called when a process receives a signal! The function is called asynchronously! When the signal handler returns the process continues, as if it was never
interrupted! Signal are different from interrupts as:
" Interrupts are sent to OS by H/W" Signals are sent to a process by the OS, or by other processes" Note that signals have nothing to do with software interrupts, which are still
sent by the hardware (the CPU itself, in this case).
5/24/2018 Ch04 - Multithreaded Programming.ppt
91/106
4.91 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Signal Generated
Process
Signal Handler
Signal delivered
Signal not blocked
Signal Caught by handler
Return from Signal Handler
Process Resumed
SignalMask
SignalMask
SignalMask
E l
5/24/2018 Ch04 - Multithreaded Programming.ppt
92/106
4.92 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Example
#include
#include
void sigproc()
{
signal(SIGINT, sigproc); /* NOTE some versions of UNIX will reset
* signal to default after each call. So for
* portability reset signal each time */
printf(you have pressed ctrl-c - disabled \n);
}
void quitproc()
{
printf(ctrl-\\ pressed to quit\n); /* this is ctrl& \*/
exit(0); /* normal exit status */
}
main()
{
signal(SIGINT, sigproc); /* ctrl-c : DEFAULT ACTION: term */signal(SIGQUIT, quitproc); /* ctrl-\ : DEFAULT ACTION: term */
printf(ctrl-c disabled use ctrl-\\ to quit\n);
for(;;);
}
Examples of POSIX Required Signals
5/24/2018 Ch04 - Multithreaded Programming.ppt
93/106
4.93 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Examples of POSIX Required Signals
Signal Description default action
SIGABRT process abort implementation dependent
SIGALRM alarm clock abnormal termination
SIGBUS access undefined part of memory object implementation dependent
SIGCHLD child terminated, stopped or continued ignore
SIGILL invalid hardware instruction implementation dependent
SIGINT interactive attention signal (usually ctrl-C) abnormal termination
SIGKILL terminated (cannot be caught or ignored) abnormal termination
Signal Description default action
5/24/2018 Ch04 - Multithreaded Programming.ppt
94/106
4.94 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Signal Description default action
SIGSEGV Invalid memory reference implementation dependent
SIGSTOP Execution stopped stopSIGTERM termination Abnormal termination
SIGTSTP Terminal stop stopSIGTTIN Background process attempting read stop
SIGTTOU Background process attempting write stop
SIGURG High bandwidth data available on socket ignore
SIGUSR1 User-defined signal 1 abnormal termination
Send a signal
5/24/2018 Ch04 - Multithreaded Programming.ppt
95/106
4.95 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Send a signal
! Raise a signal with kill(pid, signal)! Posix function: int pthread_kill(pthread_t thread, int sig);
" pid: input parameter, id of the thread to terminate" sig:signal number" returns 0 to indicate success, error code otherwise
Signal Handling
5/24/2018 Ch04 - Multithreaded Programming.ppt
96/106
4.96 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
/* code for process p */. . .
signal(SIG#, myHndlr);. . .
/* ARBITRARY CODE */
void myHndlr(...) {/* ARBITRARY CODE */
}
Signal Handling
Signal Handling
5/24/2018 Ch04 - Multithreaded Programming.ppt
97/106
4.97 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
g g
/* code for process p */. . .
signal(SIG#, sig_hndlr);. . .
/* ARBITRARY CODE */
void sig_hndlr(...) {/* ARBITRARY CODE */
}
An executing process, q
Raise SIG#for p
sig_hndlrruns in
ps address spaceq is blocked
q resumes execution
Toy Signal Handler
5/24/2018 Ch04 - Multithreaded Programming.ppt
98/106
4.98 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Toy Signal Handler
#include static void sig_handler(int);
int main () {int i, parent_pid, child_pid, status;
if(signal(SIGUSR1, sig_handler) == SIG_ERR)printf(Parent: Unable to create handler for SIGUSR1\n);
if(signal(SIGUSR2, sig_handler) == SIG_ERR)
printf(Parent: Unable to create handler for SIGUSR2\n);parent_pid = getpid();
if((child_pid = fork()) == 0) {kill(parent_pid, SIGUSR1);for (;;) pause();
} else {kill(child_pid, SIGUSR2);
printf(Parent: Terminating child \n);
kill(child_pid), SIGTERM);
wait(&status);printf(done\n);
}
}
T Si l H dl (2)
5/24/2018 Ch04 - Multithreaded Programming.ppt
99/106
4.99 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Toy Signal Handler (2)
static void sig_handler(int signo) {
switch(signo) {case SIGUSR1: /* Incoming SIGUSR1 */printf(Parent: Received SIGUSER1\n);
break;case SIGUSR2: /* Incoming SIGUSR2 */
printf(Child: Received SIGUSER2\n);
break;
default: break;}
return}
#include
#include
5/24/2018 Ch04 - Multithreaded Programming.ppt
100/106
4.100 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
void sighup()
{
signal(SIGHUP,sighup); /* reset signal */
printf("CHILD: I received a SIGHUP\n");
}
void sigint()
{
signal(SIGINT,sigint); /* reset signal */
printf("CHILD: I received a SIGINT\n");
}
void sigquit()
{
printf("My DADDY has Killed me!!!\n");
exit(0);}
void sighup();
void sigint();
void sigquit();
main(){
int pid;
/* get child process */
if ((pid=fork()) < 0)
{ perror("fork"); exit(1); }
if (pid == 0) { /* child */
signal(SIGHUP, sighup);signal(SIGINT, sigint);
signal(SIGQUIT, sigquit);
for(;;);
} else { /* parent */
printf("\nPARENT: sending SIGHUP\n\n");
kill(pid,SIGHUP);
sleep(3);
printf("\nPARENT: sending SIGINT\n\n");
kill(pid,SIGINT);sleep(3);
printf("\nPARENT: sending SIGQUIT\n\n");
kill(pid,SIGQUIT);
sleep(3);
}
Command Line Generates Signals
5/24/2018 Ch04 - Multithreaded Programming.ppt
101/106
4.101 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Command Line Generates Signals
! You can send a signal to a process from the command lineusing kill
! kill l" will list the signals the system understands
! kill [-signal] pid" will send a signal to a process.
!The optional argument may be a name or a number.
!The default is SIGTERM.! To unconditionally kill a process, use:
" kill -9 pidwhich is
!kill -SIGKILL pid.
Homework
5/24/2018 Ch04 - Multithreaded Programming.ppt
102/106
4.102 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
Homework
X
A
=
B C
C[1,1] = A[1,1]*B[1,1]+A[1,2]*B[2,1]..
.
C[m,n]=sum of product of corresponding elements in row of A
and column of B.
Each resultant element can be computed independently.
Let write a program that execute a matrix multiplication
exploiting multithread programming.
5/24/2018 Ch04 - Multithreaded Programming.ppt
103/106
4.103 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
#include
#include
#include
#define M 3
#define K 2
#define N 3
#define NUM_THREADS 10
int A [M][K] = { {1,4}, {2,5}, {3,6} };
int B [K][N] = { {8,7,6}, {5,4,3} };
int C [M][N];
struct v {
int i; /* row */
int j; /* column */
};
void *runner(void *param); /* the thread */
5/24/2018 Ch04 - Multithreaded Programming.ppt
104/106
4.104 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
int main(int argc, char *argv[]) {
int i,j, count = 0;
for(i = 0; i < M; i++) {
for(j = 0; j < N; j++) {
//Assign a row and column for each thread
struct v *data = (struct v *) malloc(sizeof(struct v));
data->i = i;
data->j = j;
/* Now create the thread passing it data as a parameter */
pthread_t tid; //Thread IDpthread_attr_t attr; //Set of thread attributes
//Get the default attributes
pthread_attr_init(&attr);
//Create the thread
pthread_create(&tid,&attr,runner,data);
//Make sure the parent waits for all thread to complete
pthread_join(tid, NULL);
count++;
}
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
105/106
4.105 Silberschatz, Galvin and Gagne 2009Operating System Concepts 8thEdition
//Print out the resulting matrix
for(i = 0; i < M; i++) {for(j = 0; j < N; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
}
5/24/2018 Ch04 - Multithreaded Programming.ppt
106/106
//The thread will begin control in this function
void *runner(void *param) {
struct v *data = param; // the structure that holds our data
int n, sum = 0; //the counter and sum
//Row multiplied by column
for(n = 0; n< K; n++){
sum += A[data->i][n] * B[n][data->j];
}
//assign the sum to its coordinate
C[data->i][data->j] = sum;
//Exit the thread
pthread_exit(0);
}