Download thread

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Control chart wikipedia , lookup

Transcript
Lecture 03
Processes II
Threads
Fall 2000
M.B.Ibáñez
Process Concept Revisited
• Unit of resource
ownership
– Address space for the
process image
– Resources such as main
memory, I/O devices, files
• Unit of dispatching
– Execution state (running,
ready, …)
– Entity that is scheduled and
dispatched by the OS.
Fall 2000
M.B.Ibáñez
Process
Control
Block
User
Address
Space
User
Stack
Kernel
Stack
Threads
• The unit of
dispatching is usually
referred to as a thread
or lightweight process
• The unit of resource
ownership is still
referred to as a process
or a task
Fall 2000
M.B.Ibáñez
Thread
Thread
Thread
Thread
Control
Block
Thread
Control
Block
Thread
Control
Block
Process
Control
Block
User
Stack
User
Stack
User
Stack
User
Address
Space
Kernel
Stack
Kernel
Stack
Kernel
Stack
Threads and Processes
Java
MS-DOS
run-time engine
one process
one thread
one process
multiple threads
Windows NT,
Solaris
UNIX
multiple processes
one thread per process
Fall 2000
multiple processes
multiple threads per process
M.B.Ibáñez
Operating Systems
Internals and Design Priciples
W. Stalling. Prentice Hall
Processes and Threads
• Processes
• Threads
– A virtual address space
that holds the process
image
– Protected access to
•
•
•
•
Fall 2000
Processors
Other processes
Files
I/O resources
M.B.Ibáñez
– A thread execution
state
– A saved thread context
when not running
– An execution task
– Storage for local
variables
– Access to the memory
and resources of its
process
Benefits of threads
• It takes less time to create a new thread in
an existing process than to create a new
process
• It takes less time to terminate a thread
• It takes less time to switch between to
threads within the same process
Fall 2000
M.B.Ibáñez
Examples of the uses of threads in a
single-user multiprocessing system
• Foreground and background work
– Example: spreadsheet program. One thread could display menus and read
user input, while another thread executes user commands and updates the
spreadsheet.
• Asynchronous processing
– As a protection against power failure, one can design a word processor to
write its RAM buffer to disk once every minute
• Speed execution
– A multithreaded process can compute one batch of data while reading the
next batch from a device
Fall 2000
M.B.Ibáñez
Thread States
• Spawn
– When a new process is spawned, a thread for that process is also spawned
– A thread within a process may spawn another thread within the same process
• Block
– The processor may now turn to the execution of another ready thread
• Unblock
– When the event for which a thread is blocked occurs, the thread is moved to the
ready state
• Finish
– When a thread completes, its register context and stacks are deallocated
• Running / Ready
Fall 2000
M.B.Ibáñez
Thread synchronization
• All of the threads of a process share the same
address space and other resources, such as open
files
• Any alteration of a resource by one thread affects
the environment of the other threads in the same
process
• It is therefore necessary to synchronize the
activities of the various threads so that they do not
interfere with each other or corrupt data structures
Fall 2000
M.B.Ibáñez
User-level Threads
• All the work of thread management is done by the
application and the kernel is not aware of the
existence of threads
• Any application can be programmed to be
multithreaded by using a threads library
• The thread library contains code for creating and
destroying threads, for passing messages and data
between threads, for scheduling thread execution,
and for saving and restoring thread contexts
Fall 2000
M.B.Ibáñez
Relationship between thread
scheduling and process scheduling I
Fall 2000
thread 3 state
running
ask for I/O
running
process state
running
blocked
M.B.Ibáñez
Kernel-level Threads
• All the work of thread management is done
by the kernel
• The kernel maintains context information
for the process as a whole and for
individuals thread within the process
• Scheduling by the kernel is done in a thread
basis
Fall 2000
M.B.Ibáñez