THREADS
OS creating a new process involves a lot of work
- find empty PCB
- read a.out or .exe file to find out
how much RAM is needed (code plus data plus compiler guess about
heap and stack size)
- find enough free RAM
- initialize all PCB fields, e.g., base and limit
- read a.out or .exe file and copy code and data
into RAM
- initialize process address space stack and heap in RAM to empty
- link PCB into end of ready queue
example:
Web server processing CGI scripts each in a new process
example:
suppose you have multiple CPUs in your PC and want to use them to multiply
matrices with a parallel algorithm
- each CPU has a process and each process must figure out
how to share data with the other processes
example:
a Web server with a RAM cache of recently accessed Web pages
example:
word processor
- handle user keyboard and mouse input
- reformat document from current position to end
- periodically autosave to disk
example:
virus scanner
- load files (or big hunks of files) into RAM
- scan RAM copies of files for viruses
- build report for user
example:
browser
- download an HTML file from a Web server
- when download completes, download each embedded image file,
one image at a time
using threads
- makes programming easier than one process doing everything
(perhaps with non-blocking IO system calls)
- has less overhead than creating multiple processes
- facilitates sharing of data
example:
browser
- download an HTML file from a Web server
- when download completes, create threads to download all the embedded
image files concurrently
analogies:
- multiple little cars or bugs moving around on a map following roads
- multiple mice in a maze
- multiple cooks working out of one recipe book
- following are three ways to prepare a Boston Cream Pie (really a cake):
- a single cook doing the recipe from beginning to end
entirely sequentailly one step at a time;
corresponds to using a blocking file IO library
and a single thread;
first do the cake part;
then when that is completely done, do the cream filling part;
then when that is completely done, do the chocolate glaze part
- using the everyday meaning of multi-tasking, one cook does
all three parts simultaneously, mixing something while something
else is heating or baking or cooling;
corresponds to using a non-blocking file IO library
and a single thread
- several cooks working in parallel,
one cook doing the cake part
entirely sequentailly one step at a time,
one cook doing the cream filling part
entirely sequentailly one step at a time,
one cook doing the chocolate glaze part
entirely sequentailly one step at a time;
corresponds to a multi-threaded approach
with a blocking file IO library
all processes start out with a single thread and that thread
may create other threads in the process
to implement multiple threads in a process
- modify the PCB so each thread has its own CPU register save area,
its own clock ticks used field, and its own state field
- modify the process address space so each thread has its own stack
for called procedure return address, passed parameters, and local
variables since threads call procedures independently
- code, data, and heap are shared by all threads
- open files are shared by all threads
- but each thread in a process has its own stack, SP register value,
PC register value, and other CPU register values
all threads in all processes are scheduled round-robin on the CPU(s)
using multiple threads makes a program easier to write and
execute more quickly, particularly if there are multiple
CPUs available to dedicate to threads
- threads share data, making it easier to take advantage
of multiple CPUs, for example matrix multiplication
- one thread can wait for an IO operation to complete while another
thread computes, for example a word processor
(user input thread, reformat document thread, write file to disk thread)
- threads can form a pipeline: input thread, processing thread,
output thread, for example a virus scanning program (one thread
loads files into RAM, a second thread scans the RAM-resident files,
and a third thread builds a report for the user)
example:
a Web server with a RAM cache of recently accessed Web pages
what remains is
the issue of safely sharing data to avoid or prevent
race conditions and deadlock,
in other words,
synchronization of multiple threads sharing data
home page:
http://elvis.rowan.edu/~hartley/index.html
e-mail:
hartley@elvis.rowan.edu