Sei sulla pagina 1di 29

OPERATING SYSTEMS

THREADS
WHAT IS A THREAD?

Giorgio Giacinto 2019 Operating Systems 2


– Execution of multiple tasks for the same goal
– interactive applications for text, audio, video, etc.
– servers, etc.
– Two main implementations
– child processes
Motivations – threads - the context switch between threads of the
same process requires less resources than the context
switch between processes
– Implementation of threads
– Libraries
– Hardware support
– Multicore architectures

Giorgio Giacinto 2019 Operating Systems 3


– An application may require the concurrent execution
of multiple functions each devoted to a single task
– interactive graphical (or audio) interface in multimedia
production and editing apps, CAD, etc.
– Input validation
Multithreading – Server
– one thread associated to each client process
– Operating Systems
– some functionalities are implemented as a group of
threads within the same process

Giorgio Giacinto 2019 Operating Systems 4


one process one process
one thread multiple threads

Processes and
Threads

multiple processes multiple processes


one thread per process multiple threads per process

= instruction trace

Figure 4.1 Threads and Processes


Giorgio Giacinto 2019 Operating Systems 5
– The CPU executes threads
– The process holds the resources
– All threads of the same process share the main memory,
open files, stack, etc.
– Each thread has a program counter, a set of registers, the
stack
Single-Threaded Multithreaded
Process Model Process Model
Threads User
Thread
Thread
Thread
Thread
Thread
Thread
Process Control Control Control
Control Stack Block Block Block
Block

Process User User User


User Kernel Stack Stack Stack
Control
Address Stack
Block
Space

User Kernel Kernel Kernel


Address Stack Stack Stack
Space

Giorgio Giacinto 2019 Operating Systems 6


Figure 4.2 Single Threaded and Multithreaded Process Models
(2) create new
(1) request thread to service
the request
client server thread
Multithreaded
server
architectures
(3) resume listening
for additional
client requests

Giorgio Giacinto 2019 Operating Systems 7


Time

RPC RPC
Time Request Request

Process 1
RPC RPC
Request Request
Server Server

Process 1
(a) RPC Using Single Thread

Server Server

Server
RPC and (a) RPC Using Single Thread RPC
Request

threads RPC Server


Thread A (Process 1)

Thread B (Process 1)
Request
RPC
Thread A (Process 1) Request Server

Thread B (Process 1)
(b) RPC Using One Thread per Server (on a uniprocessor)
RPC
Request Server
Blocked, waiting for response to RPC
(b) RPC Using One Thread per Server (on a uniprocessor) Blocked, waiting for processor, which is in use by Thread B
Running
Blocked, waiting for response to RPC

Giorgio Giacinto 2019 Blocked, waiting for processor, which is in use


Operating by Thread B
Systems 8
Running
Figure 4.3 Remote Procedure Call (RPC) Using Thre
– Response time
– A blocked thread does not necessarily block the
process
– Resource sharing
– Threads within the same process can easily cooperate
Advantages of without the help of the OS
Multithreading – Lightweight management
– The management of concurrent threads requires less
computational resources than concurrent processes
– Scalability
– Independent threads can exploit the availability of
multiple cores or multiple processors

Giorgio Giacinto 2019 Operating Systems 9


Time

I/O Request Time quantum


request complete expires

Thread A (Process 1)

Thread B (Process 1)
Multithreading Thread C (Process 2) Time quantum
expires
Process
created

Blocked Ready Running

Giorgio Giacinto 2019


Figure 4.4 Multithreading
Operating Systems
Example on a Uniprocessor 10
Pthreads

Giorgio Giacinto 2019 Operating Systems 11


Pthreads
(cont.)

Giorgio Giacinto 2019 Operating Systems 12


Pthreads

Giorgio Giacinto 2019 Operating Systems 13


– The design of a multithreaded application that exploits
multicore architectures requires
– Isolating all independent tasks
Threads and – Subdividing the load among tasks
multicore – Data separation to avoid conflicts in data access
architectures – Data integrity for tasks operating on a common set of data
– A long test e debugging phase for identifying inaccuracies
due to the uncertainties in the order of execution of
different tasks.

Giorgio Giacinto 2019 Operating Systems 14


Multithread
Programming

Giorgio Giacinto 2019 Operating Systems 15


– User-Level Threads (ULT)
– Do not require a multithreaded OS
User-Level – Implemented through libraries of the programmng
Threads and language
Kernel-Level- – Kernel-Level Threads (KLT)
– The OS is in charge of managing threads
Threads – All major OS are multithreaded: Windows, Linux,
macOS, Solaris, z/OS, INTEGRITY RTOS, etc.

Giorgio Giacinto 2019 Operating Systems 16


– Thread scheduling is not
managed by the OS, that
can only schedule
Threads Threads
User User processes
User Threads User
User
Library Space Space Library Space
Space Space
ULT Library
Kernel
– Kernel
If a thread makes a Kernel
Space
Kernel blocking
Space system call, the
Space
Kernel
Many-to-one P
Space kernel blocks the process
Space

model P
– Multiple threads
P cannot
P
P make concurrent systems
(a) Pure user-level (b) Pure kernel-level calls (c) Combined

P
– No possibility of exploiting
User-level thread Kernel-level thread P Process
multicore architectures
(a) Pure user-level (b) Pure kernel-level

Figure 4.5 User-Level and Kernel-Level Threads


17
Giorgio Giacinto 2019
User-level thread Operating Systems
Kernel-level thread P Process
(a) (b)
Thread 1 Thread 2 Thread 1 Thread 2
Ready Running Ready Running Ready Running Ready Running

Blocked Blocked Blocked Blocked

Process B Process B
Ready Running Ready Running

ULT Blocked Blocked

Process and (c)


Thread 1 Thread 2
(d)
Thread 1 Thread 2

Thread states Ready Running Ready Running Ready Running Ready Running

Blocked Blocked Blocked Blocked

Process B Process B
Ready Running Ready Running

Blocked Blocked

Colored state
is current state
Figure 4.6 Examples of the Relationships Between User-Level Thread States and Process States

Giorgio Giacinto 2019 Operating Systems 18


– The OS schedule threads and assigns resources to
processes
– Risk: creating too many threads
– This is the model followed Threads
da Windows and
User Linux User
Library Space Space

KLT Kernel
Space
Kernel
Space

One-to-one Threads User User P


Threads User
model Library Space Space Library Space
P
Kernel Kernel Kernel
Space Space Space
(a) Pure user-level (b) Pure kernel-level

P
User-level thread Kernel-level thread P Process
P P P

(a) Pure user-level (b) Pure kernel-level (c) Combined

Giorgio Giacinto 2019 Operating Systems Figure 4.5 User-Level and Kernel-Level
19 T
user thread

KLT
Many-to-
many model
k k k kernel thread

The number of kernel threads can be less than or equal to the


number of user threads

Giorgio Giacinto 2019 Operating Systems 20


Threads User User
Library Space Space

hreads User User Kernel


Threads User Kernel
ibrary Space Space Library
Space Space Space

Combined Kernel
Space
Kernel
Space P
Kernel
Space

model
P
ULTP and KLT
P (a) Pure user-level
P P (b) Pure kernel-level

(a) Pure user-level (b) Pure kernel-level (c) Combined


User-level thread Kernel-level thread P Process

er-level thread Kernel-level thread P Process

Giorgio Giacinto 2019 Operating Systems 21


Figure 4.5 User-Level and Kernel-Level Thread
Thread Libraries

Giorgio Giacinto 2019 Operating Systems 22


– Multithread programming requires the use of
specific APIs
– User libraries (they do not require any action by the
kernel)
Thread – Kernel libraries
Libraries – Example of thread libraries
– Pthreads - POSIX (user and kernel threads)
– Win32 (kernel threads)
– Java (user level) JRE can use the kernel libraries if the
host OS supports multithreading

Giorgio Giacinto 2019 Operating Systems 23


Multithread Issues

Giorgio Giacinto 2019 Operating Systems 24


– What happens to a multithread process whena a
thread makes a call to either fork() or exec() ?
– Which tasks has to be performed when a thread is
cancelled?
– Signals are sent to processes and not to individual
threads.
Main issues – Thread pools to implement the same service for
multiple clients
– Permanent association between threads and a
subset of data managed by the process
– Communication between kernel and user libraries
(LWP and scheduler activation)

Giorgio Giacinto 2019 Operating Systems 25


– Many-to-many model with
a third element between
user and kernel threads
– LWP - LightWeight Process
(introduced in Solaris)
Thread, – This is a data structure
Processes and managed by the kernel
– Typically one LWP for each
scheduling blocking system call
– max number of LWPs set a system
parameter
– The user library schedule the
user threads on the available
LWPs

Giorgio Giacinto 2019 Operating Systems 26


Examples of Thread
state diagrams

Giorgio Giacinto 2019 Operating Systems 27


Stopped

signal signal

Running
Linux creation
State
termination
Ready scheduling Executing Zombie
Process/Thread
model signal
event
or
event
Uninterruptible

Interruptible

Figure 4.15 Linux Process/Thread Model


Giorgio Giacinto 2019 Operating Systems 28
Runnable
Pick to Standby
Run Switch

Preempted
Ready Running
Windows
Thread States
Resource Unblock/Resume Terminate
Resource Available Block/
Available
Suspend

Transition Unblock
Waiting Terminated
Resource Not Available
Not Runnable

Giorgio Giacinto 2019 Operating Systems 29


Figure 4.11 Windows Thread States

Potrebbero piacerti anche