20
Operating Systems Operating Systems COMP 4850/CISG 5550 COMP 4850/CISG 5550 Threads, Part II Threads, Part II Dr. James Money Dr. James Money

Operating Systems COMP 4850/CISG 5550

  • Upload
    glora

  • View
    34

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Operating Systems COMP 4850/CISG 5550

Operating SystemsOperating SystemsCOMP 4850/CISG 5550COMP 4850/CISG 5550

Threads, Part IIThreads, Part II

Dr. James MoneyDr. James Money

Page 2: Operating Systems COMP 4850/CISG 5550

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

Page 3: Operating Systems COMP 4850/CISG 5550

Thread UsageThread Usage

Page 4: Operating Systems COMP 4850/CISG 5550

Thread UsageThread Usage

Page 5: Operating Systems COMP 4850/CISG 5550

Thread UsageThread Usage

(a)– Dispatcher Thread(b)- Worker Thread

Page 6: Operating Systems COMP 4850/CISG 5550

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

Page 7: Operating Systems COMP 4850/CISG 5550

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

Page 8: Operating Systems COMP 4850/CISG 5550

User Space ThreadsUser Space Threads

Page 9: Operating Systems COMP 4850/CISG 5550

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

Page 10: Operating Systems COMP 4850/CISG 5550

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

Page 11: Operating Systems COMP 4850/CISG 5550

Kernel Space ThreadsKernel Space Threads

Page 12: Operating Systems COMP 4850/CISG 5550

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

Page 13: Operating Systems COMP 4850/CISG 5550

Hybrid ImplementationsHybrid Implementations

Page 14: Operating Systems COMP 4850/CISG 5550

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.

Page 15: Operating Systems COMP 4850/CISG 5550

Multithreaded systemsMultithreaded systems

• Possible overwriting of global Possible overwriting of global variables:variables:

Page 16: Operating Systems COMP 4850/CISG 5550

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

Page 17: Operating Systems COMP 4850/CISG 5550

Multithreaded SystemsMultithreaded Systems

Page 18: Operating Systems COMP 4850/CISG 5550

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”);

Page 19: Operating Systems COMP 4850/CISG 5550

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.

Page 20: Operating Systems COMP 4850/CISG 5550

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