Benefits of threads less time to create(10 times )and terminate than doing the same thing to a process less time to switch between two threads within the same process parallel processing -multiple threads executing simultaneously on different processors communication between different executing modules within the same process In most Os, communication between independent processes requires the intervention of the kernel to provide protection and the mechanism needed for communication Because threads within the same task share memory and files, they can communicate with each other without invoking the kernel
6 Benefits of threads • less time to create (10 times) and terminate than doing the same thing to a process • less time to switch between two threads within the same process • parallel processing -- multiple threads executing simultaneously on different processors. • communication between different executing modules within the same process – In most OS, communication between independent processes requires the intervention of the kernel to provide protection and the mechanism needed for communication. – Because threads within the same task share memory and files, they can communicate with each other without invoking the kernel
RPC RPC Request Process 1 v Server (a) RPC Using Single Thread RPC Thread A (Process 1) Thread B(Process l Server (b)RPC Using One Thread per Server(on a uniprocessor) MZZ Blocked, waiting for response to RPC Blocked, waiting for processor, which is in use by Thread B Figure 4.3 Remote Procedure Call (RPC) Using Threads
7
Request Time quantum request complete expires Thread A(Process 1) Thread b (process 1) Thread C(Process 2) Time quantum exDI Process created 区× Blocked Ready Running Figure 4.4 Multithreading Example on a Uniprocessor
8
Screen-redrawthread Figure 4.5 Thread structure for adobe page maker
9
Example applications using threads Threads in a spreadsheet program One thread displays menus and read user input (foreground work) Another thread executes user commands and updates the spreadsheet (background work Adobe PageMaker Writing, design, and production tool for desktop publishing service thread event-handling thread screen-drawing thread When the event-handling thread(e.g, doing a large computation, etc. or the screen-drawing thread is busy, the service thread(e.g,, printing file importing, etc restricts user activity by disabling menu items and displaying a busy "cursor. The user is free to switch to other applications, or even kill the computation through the service thread
10 Example applications using threads • Threads in a spreadsheet program – One thread displays menus and read user input (foreground work). – Another thread executes user commands and updates the spreadsheet (background work). • Adobe PageMaker – Writing, design, and production tool for desktop publishing – service thread – event-handling thread – screen-drawing thread – When the event-handling thread (e.g., doing a large computation, etc.) or the screen-drawing thread is busy, the service thread (e.g., printing, file importing, etc.) restricts user activity by disabling menu items and displaying a “busy” cursor. The user is free to switch to other applications, or even kill the computation through the service thread