Threads. SMP and microkernels Processes and threads The two characteristics of a process unit of resource ownership virtual address space for the process image I/O channels. devices files unit of dispatching/scheduling/execution This is the execution path through one or more modules It is also the entity that is being scheduled and dispatched by the os a process may have many dispatching units. a unit of dispatching is commonly called a thread or lightweight process New notion of process: unit of resource ownershI
1 Threads, SMP, and MicroKernels • Processes and threads – The two characteristics of a process • unit of resource ownership – virtual address space for the process image – I/O channels, devices, files • unit of dispatching/scheduling/execution – This is the execution path through one or more modules. – It is also the entity that is being scheduled and dispatched by the OS. – A process may have many dispatching units. A unit of dispatching is commonly called a thread or lightweight process. – New notion of Process : unit of resource ownership
Threads Single process, single thread DOS Multiple processes, single thread per process UNIX One process, multiple threads Java run-time(actually not an Os) Multiple processes multiple threads per process Solaris. Windows 2000/XP Windows XP. mach. oS/2. Linux Multithreading a process unit of protection and unit of resource allocation )virtual address space, process image >)protected access to processors, interprocess communication (IPC), files. lo resources 2
2 Threads – Single process, single thread • DOS – Multiple processes, single thread per process • UNIX – One process, multiple threads • Java run-time (actually not an OS) – Multiple processes, multiple threads per process • Solaris, Windows 2000/XP, Windows XP, Mach, OS/2, Linux – Multithreading • a process – unit of protection and unit of resource allocation » virtual address space, process image » protected access to processors, interprocess communication (IPC), files, I/O resources
one process one thread multiple thread multiple processes multiple processes thread ocess multiple threads per process instruction trace Figure 4.1 Threads and Processes [andE971
3
Single-Threaded Multithreaded Process model Process model Thread Thread Thread Thread Thread Thread Pr User ocess Control Control Control Stack Block block Block Block Process User User sel sel Kernel Control: Stack Stack I Stack Address Stack Block S ace User Kernel Kernel i Kernel Address Stack Stack! Stack Space Figure 4.2 Single threaded and Multithreaded Process mode
4
Threads(cont Multithreading(cont Each thread has a thread execution state(running, ready, etc. a separate control block, with priority, some thread related state information, and saved processor context when not running (with program counter an execution stack some per-thread static storage for local variables access to the memory and resources of its process, shared with all other threads in the process A single application logically doing several functions especially in GUI systems Example application-a file server entertaining requests to create, open, read and write on files >>One thread per request Two threads cannot write on the same file at the same time
5 Threads (cont.) – Multithreading (cont.) • Each thread has – a thread execution state (running, ready, etc.) – a separate control block, with priority, some thread related state information, and saved processor context when not running (with program counter) – an execution stack – some per-thread static storage for local variables – access to the memory and resources of its process, shared with all other threads in the process, • A single application logically doing several functions, especially in GUI systems. – Example application -- a file server entertaining requests to create, open, read, and write on files. »One thread per request »Two threads cannot write on the same file at the same time
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
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