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