Operating Systems COMP 4850/CISG 5550
Download
Report
Transcript Operating Systems COMP 4850/CISG 5550
Operating Systems
COMP 4850/CISG 5550
Threads, Part II
Dr. James Money
Thread Usage
• Why are they used?
– Programs block, but might have other tasks to
perform
– Easier to create/destroy than processes
– Increase in performance
– Multiple CPU utilization
Thread Usage
Thread Usage
Thread Usage
(a)– Dispatcher Thread
(b)- Worker Thread
Implementing Threads
• There are two basic ways to implement
threads:
– In User Space
– In Kernel Space
• Each has it’s own advantages and
disadvantages
User Space Threads
• The OS knows nothing about threads.
• Each process has its own user space
thread table similar to the process table
User Space Threads
User Space Threads
• Advantages:
– No traps to kernel mode – better performance
– Custom scheduling algorithms
– Scales well
• Disadvantages
– Dealing with blocking system calls
– Page faults block all threads
– Requires voluntary yielding of CPU
Kernel Space Threads
• Kernel maintains thread table
• When a system call blocks, the kernel can
run another thread automatically
• Threads tend to get recycled – not
actually destroyed since they require a
trap
Kernel Space Threads
Hybrid Implementations
• Research has been done on implementing
kernel and user space threads at the same
time.
• The kernel knows about it’s threads, but
the process may have multiple user space
threads for one kernel thread
Hybrid Implementations
Making Single Threaded Code
Multithreaded
• Most programs are written for one thread
• We must convert them to multithread
form to use them in a threaded system
• This is not easy to perform in general.
Multithreaded systems
• Possible overwriting of global variables:
Multitheaded Systems
• Solutions:
– Multiple global variables similar to separate
stacks
– Thread wide global variable library
Multithreaded Systems
Private Library
• create_global(“bufptr”);
• set_global(“bufptr”,&buf);
• bufptr=read_global(“bufptr”);
Multithreaded Systems
• Library procedures are not re-entrant
usually!
• Buffered message passing may get
overwritten.
• Must rewrite these functions
• Or completely block re-use of the library
functions when one is in use.
Multithreaded Systems
• Thread management issues
• Many times the kernel will just extend the
stack when it overflows
• This can cause two separate thread stacks
to collide now