Module 3
–
Threads
Reading: Chapter 4 ( Goal:
Understanding the concept of threads and its relationship to the process
Understand how the operating systems manage and use the threads.
Silberschatz
)
▪
▪
1
Topics
▪ The execution thread in the processes
▪ Multi-thread versus single thread (the
thread)
▪ User level threads and kernel level threads
▪ The challenges of “Threading”
▪ Examples of threads
2
Characteristics
▪ Resource ownership unit – a process has: ▪ an addressable virtual space containing the
process image
▪ other resources (files, I / O units …)
▪ Execution unit (dispatching) – a process is executed along a path among several programs
▪ execution nested among the execution of several processes
▪ the process has an execution state and a priority for scheduling
of
processes
3
Process
▪ Owns its memory, files, resources, etc.
▪ Protected access to memory, files, resources of other processes
4
Process characteristics
▪ These 2 characteristics are perceived as independent by some OS
▪ The execution unit is usually denoted by execution thread
▪ The unit of resource ownership is usually referred to as process
5
Resource ownership unit
▪ Related to the following components of the process image
▪ The part of the PCB which contains identification and resource structures
▪ Memory containing the execution code
▪ Memory containing global data
Process image
BCP
Identification Structures data
State of processor
Ordonnan.
Memory user
Battery
Battery user
Coded execution
Data global
Battery core
6
Execution unit
▪ Related to the following components of the process image
▪ PCB
▪ Processor state
▪ Scheduling structure
▪ Stack
Process image
BCP
Identification Structures data
State of processor
Ordonnan.
Memory user
Battery
Battery user
Coded execution
Data global
Battery core
7
Topics
▪ The execution thread in the processes
▪ Multi-thread versus single thread (the
thread)
▪ User level threads and kernel level threads
▪ The challenges of “Threading”
▪ Examples of threads
8
Threads =
▪ A thread is a subdivision of a process ▪ A control thread in a process
▪ The different threads of a process share addressable space and resources of a process
▪ when a thread modifies a variable (not local), all other threads see the modification
▪ a file opened by one thread is accessible to other threads (of the same process)
lightweight
processes
9
Example
▪ The MS-Word process involves several threads:
▪ Interacting with the keyboard
▪ Arrangement of characters on the page ▪ Regular backup of work done
▪ Spell check
▪ Etc.
▪ These threads share all the same document
10
Single and multi
–
threaded processes
11
Threads and processes
[Stallings]
12
Thread
▪ Has an execution status (ready, blocked, etc.)
▪ Has its stack and a private space for local variables
▪ Has access to the addressable space, files and resources of the process to which it belongs
▪ In common with the other threads of the same process
13
Why threads
▪ Responsiveness: a process can be subdivided into several threads, e.g. one dedicated to interacting with users, the other dedicated to processing data
▪ One can run as long as the other is blocked
▪ Use of multiprocessors: threads can run in parallel on different CPUs
14
Switching between threads is less expensive than switching between processes
▪ A process has memory, files, other resources
▪ Changing from one process to another involves saving and restoring the state of it all.
▪ Switch from one thread to another in the same process is much simpler, involves saving CPU registers, stack, and little else
15
Communication is also less expensive between threads than between processes.
▪ Since threads share their memory,
▪ communication between threads in the same process is more efficient than communication between processes
16
Creation is less expensive
▪ Creating and terminating new threads in an existing proc is also less expensive than creating a proc.
17
Kernel and user threads
▪ Where to implement the threads:
▪ In user libraries
▪ user-controlled
▪ POSIX Pthreads, Java threads, Win32 threads
▪ In the kernel of the OS:
▪ kernel controlled
▪ Windows XP / 2000, Solaris, Linux, True64 UNIX, Mac OS X
▪ Mixed solutions
▪ Solaris 2, Windows 2000 / NT
18
User and kernel threads
▪ user threads: supported by user libraries or prog language
▪ efficient because ops on threads do not request system calls
▪ disadvantage: the kernel is not able to distinguish between state of process and state of threads in the process
▪ blocking a thread implies blocking the process
▪ kernel threads: directly supported by the OS kernel
(WIN NT, Solaris)
▪ the kernel is able to directly manage the states of the threads
▪ It can assign different threads to different CPUs
(kernel)
19
Mixed solutions: user and kernel threads
▪ Relationship between user threads and kernel threads ▪ many to one
▪ one by one
▪ many to many (2 models)
▪ We must take into consideration several levels: ▪ Process
▪ User thread
▪ Kernel thread
▪ Processor (CPU)
20
Multiple user threads for a kernel thread: the user controls the threads
21
▪ OS does not know user threads
▪ v. advantages and disadvantages mentioned before
▪ Examples
▪ Solaris Green Threads ▪ GNU Portable Threads
One to one:
the OS controls the threads
Thread utilisateur
22
▪ Ops on threads are system calls
▪ Allows another thread to run when a thread
executes a blocking system call ▪ Win NT, XP, OS / 2
▪ Linux, Solaris 9
Thread noyau
Many to many: mixed solution (M: M
–
many to
23
many)
▪ Uses both user threads and kernel threads
▪ Flexibility for the user to use the technique he prefers
▪ If a user thread blocks, its kernel thread can be assigned to another
▪ If more. UCT are available, plus. kernel threads can run at the same time
▪ Some versions of Unix, including Solaris before version 9
▪ Windows NT / 2000 with the ThreadFiber package
Multithreads and monothreads
24
▪ MS-DOS supports a single-threaded user process
▪ UNIX SVR4 supports multiple single- threaded processes
▪ Solaris, Widows NT, XP and OS2 support multiple multithreaded processes
Topics
▪ The execution thread in the processes ▪ Multi-thread versus single thread (the
thread)
▪ User level threads and kernel level threads ▪ The challenges of “Threading”
▪ Examples of threads
25
Threading challenges
How beautiful it is to have children, but what are the practical consequences?
Challenges:
▪ Semantics of system calls fork () and exec() ▪ Canceling threads
▪ A group of threads (pools)
▪ Thread-specific data
▪ Scheduling
26
Fork () and exec () semantics
▪ Does fork () copy only the calling thread or all the threads?
▪ Often two versions available ▪ What does exec() do?
▪ It replaces the address space, so all threads are replaced
27
Cancellation
▪ The termination of the thread before it is finished. ▪ Two general approaches:
▪ Asynchronous cancellation which ends the child immediately
▪ May leave shared data in a bad state ▪ Some resources are not released.
▪ Deferred cancellation
▪ Use a flag that the child checks to see if it should
cancel its execution
▪ Gives a smooth ending
of
thread
28
(Thread Pools)
Wire groupings
▪ A server process can service its requests by creating a thread for each request
▪ Thread creation takes time
▪ No control over the number of threads, which can
increase the load on the system.
▪ Solution
▪ Let’s create a number of threads waiting for work
▪ Advantages:
▪ The creation time only takes place at the beginning of
the creation of the group of children
▪ The number of running threads is limited by the size of the group
29
–
specific
data
thread
▪ Allows each thread to have a private copy of data
▪ Useful when the control of the creation of the thread is limited (ie in a group of threads).
30
Topics
▪ The execution thread in the processes ▪ Multi-thread versus single thread (the
thread)
▪ User level threads and kernel level threads ▪ The challenges of “Threading”
▪ Examples of threads
31
Examples
▪ Pthreads
▪ Win32
▪ Java threads
of thread
libraries
32
Pthreads
▪ A POSIX standard (IEEE 1003.1c) of an API for the creation and synchronization of threads
▪ The API specifies the behavior of the thread library (its realization depends on the developer)
▪ Common in UNIX operating systems (Solaris, Linux, Mac OS X)
▪ Typical functions:
▪ pthread_create (& threadid, & attr, start_routine, arg) ▪ pthread_exit (status)
▪ pthread_join (threadid, status)
▪ pthread_attr_init (& attr)
33
34
Programming
Goal: Write a matrix multiplication program with several threads, to take advantage of several CPUs.
Program for multiplication with single thread of matrix A and B of order nxn
for (i = 0; i