OPERATIONSYSTEMS
THREADS
Prof. Luz Carbonero
AGENDA
Threads
2. Benefits
3. Classic Model
4. Multihilamiento
5. Planning and Activation
6. Programming Patterns
7. Examples
Processes Threads
Processes Two characteristics:
Property of Resources
Address space image management (program, data,
stack and attributes PCB SO provides protection avoid
unwanted interferences between processes due to resources
2. Planning and Execution
Execution route (trace) Interspersed with other processes
Execution status(running, ready, blocked) + priority of
activation Planned by SO EXECUTION THREAD
Threads
Execution trace route CPU bursts and I/O bursts
Interspersed with other processes
Execution statusrunning, ready, blocked) + priority of
activation Planned by SO EXECUTION THREAD
Processes and Threads
Traditional SO each process has a
address space and a single thread
execution MONOFILO
Convenient various execution threads in the
same address space running
in almost parallel as separate processes
sharing the address space
SO support for multiple threads of execution in
a single process MULTITHREAD
Processes and Threads
For belonging to different
address spaces the
inter-process communication
it is only possible using
calls to the system
Threads within a process
are part of the same one
address space the
communication between threads is
much faster and simpler
Processes and Threads
Changes in the context of the processes are more
expensive They imply saving the context of
process Change of mode (trap to OS, etc.) and
another mode change to restore the context of the
new process
Threads share the same address space
change of context between one thread and another
(belonging to the same process), could
become completely independent from
SO (user-level threads)
Procesos e Hilos
A process/A thread MS-
DOS
Multiple processes/One thread
Varieties of Unix
•One process/Multiple threads
Execution environment
(MVJ) of Java
Multiple Procesos/
Multiple Threads
Windows, Solaris, OS/2
Benefits of Threads
Applications various activities at the same time
Sequential threads running in pseudo-
parallel, sharing the same space of
directions They simplify programming.
more efficient
Easier and faster to crear,finalizar,
communicate and switch between each othermore
And the processes are light very useful property
when the number of threads changes in a manner
dynamic and fast
Benefits of Threads
When there is a considerable amount of
I/O operations and calculations seal it
application speed
Very useful and natural Systems with several
CPUs (Multicores) and Multithreading
True parallelism
Classic Thread Model
Independent concepts Grouping of
resources and execution Sometimes it is useful
separate them
Process group resources in a space of
directions Easier management
Thread execution thread He has a PC, records
(work variables) and a stack (history of
execution–calls to procedures) Process
Light
Classic Thread Model
2 3
Threads add to the Process Model allowing to
they carry out various executions in the same environment of the
process
Multithreading
SO Multiple Threads in the Same Process
Single CPU system threads take turns to execute
switch quickly between one thread and another pseudo
parallelism
The different threads of a process are not so independent.
like the processes they have the same space of
directions they share the same global variables
Each thread can access each memory address
within the process address space
There is no protection between threads the application must
consider it user creates threads to cooperate
Multihulling
Processes are associated:
Address space that supports the
image of the process
Global Variables
Protected access to processors, others
processes (IPC), open files and
I/O resources (devices and channels)
Accounting information
Multi-threading
It is associated with Threads:
Thread execution state (ready, etc.)
Thread Context PC
A stack of execution
Storage space for variables
locales
Access to the memory and resources of your
process shared with the other threads
Processes - Threads
Planning and Activation of Threads
The OS stores process information. PCB
organized in a table or list of processes.
Thread Information data structure
thread control block(TCB)
Each PCB points to a list of TCBs
Thread Planning and Activation
SO with thread support Planning and
Activation is done at the thread level.
most of the info about execution states
it is in data structures at the thread level
Wire can be running, blocked
ready or finished Transitions are
the same as between the states of the
processes
Planning and Activation of Threads
Each thread has its own
how much local variables and
return address for
each procedure
call that has not yet
has returned
Each Thread calls
different procedures
Has a history of
different execution each
thread needs its own
how much
Thread State Changes
Creation: a process is created, it is also created
a thread Then this can create another thread of the
same process with a pointer to the text,
variables, PC, stack space Ready
Blocking: Waiting for an event Store
records, PC and SP Processor can pass
Execute another thread in the Ready state del
same process or in a different one
Thread State Changes
Unlocking: The event occurs for which
The thread is blocked pass to state
Ready
Terminado: cuando se completa un Hilo
they release their context register and stack
Thread programming patterns
Single task thread
Process creates a thread for a specific task
to be executed. When the function is completed
the thread ends
Worker threads
Process can have a number of tasks
distinct could be executed
concurrently, for each of them it
create a Thread. e.g., Word processors (i.
Input and other commands–ii. Output on screen)
Thread Programming Patterns
Thread pools The process
create a number of threads at startup, the
which will carry out a task taken from a
work queue or assigned by a thread
dispatcher. Protection is needed so that
two threads do not do the same work. e.g.
Multithreaded network services where each service
is done by a thread (WebPages in a
WebServer
Examples of Threads
File Server every time it arrives
new file request file management
can run a new thread They are created and
threads finish in a short time They must
share data files faster to use
threads and shared memory
Work in the foreground and background Ex. Sheet
of calculation a thread shows the menu and reads
user inputs another executes commands and
update tables backup another time
newspapers
Examples of Threads
Management of I/O Devices A thread
can compute a series of data while another
read the following data from a device The
what may be blocked, while the other
no
Modular programs various are carried out
tasks have multiple sources and destinations of I/O
more can be designed and implemented
easily using threads
Web Server Example
Page requests most visited
frequently (in cached memory
main, instead of disk)
• Dispatcher Wire Read requests from
entry and send request to Thread
Pool worker (blocked ready)
• Worker thread(running)
check cache hit
If cache fails start operationRead
and is blocked waiting for operation in
disco
Plan another thread torunning (perhaps the
dispatcher
The dispatcher is in an infinite loop of
read requests and activate worker threads
Web Server Example
Dispatcher Thread Worker Thread