Operating Systems: Internals and Design Principles Chapter 3 Process Description and Control Seventh Edition By William Stallings
Process Control Block
Structure of Process Images in Virtual Memory
How to do SWITCH??? 1.Mode Switch 2.Context Switch 3.Process Switch
Context Switch save the context of the processor update the process control block of the process currently in the Running state move the process control block of this process to the appropriate queue If the currently running process is to be moved to another state (Ready, Blocked, etc.), then the OS must make substantial changes in its environment select another process for execution restore the context of the processor to that which existed at the time the selected process was last switched out update memory management data structures update the process control block of the process selected
Mode Switch If no interrupts are pending the processor: If an interrupt is pending the processor: proceeds to the fetch stage and fetches the next instruction of the current program in the current process Saves the context of the current program being executed. sets the program counter to the starting address of an interrupt handler program switches from user mode to kernel mode so that the interrupt processing code may include privileged instructions
Process Switch A process switch may occur any time that the OS has gained control from the currently running process. Possible events giving OS control are: Due to some sort of event that is external to and independent of the currently running process clock interrupt I/O interrupt memory fault Time slice the maximum amount of time that a process can execute before being interrupted An error or exception condition generated within the currently running process OS determines if the condition is fatal moved to the Exit state and a process switch occurs action will depend on the nature of the error
Process Switch A process switch may occur any time that the OS has gained control from the currently running process. Steps: 1. Save the Current state 2. Give control the new process 3. Restore the previous state. What is the difference between Mode switch and Process switch?
Non-Process Kernel Types of Exe within Userprocess Kernel Process based Kernel
Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing Computation speed-up via parallel sub-tasks Modularity by dividing system functions into separate processes Convenience - even an individual may want to edit, print and compile in parallel
Operating Systems: Internals and Design Principles Chapter 4 Process and Threads Seventh Edition By William Stallings
Process and Threads Process The unit or resource allocation and a unit of protection The key states for a Process are: Running Ready Blocked Suspended Exit Process operations are: Create Terminate Spawn. Thread The unit of dispatching is referred to as a thread or lightweight process The key states for a thread are: Running Ready Blocked Thread operations associated with a change in thread state are: Spawn Block Unblock Finish
P R O C E S S & T H R E A D S
Process has PCB and Address space Thread has TCB and Stacks
Benefits of THREADS Takes less time to create a new thread than a process Less time to terminate a thread than a process Switching between two threads takes less time than switching between processes Threads enhance efficiency in communication between programs
Types of Threads User Level Thread (ULT) Kernel level Thread (KLT)
Comparison KLTs 1. Kernel is aware of the presence of KLT 2. Thread management is done by the kernel 3. If one thread in a process is blocked, the kernel can schedule another thread of the same process ULTs 1. The kernel is not aware of the existence of threads 2. All thread management is done by the application (Thread Library; A package of routines for ULT) 3. In a typical OS many system calls are blocking as a result, when a ULT executes a system call, not only is that thread blocked, but all of the threads within the process are blocked
Difference and Similarities KLTs 4. Can run on specific OS 5. The kernel can simultaneously schedule multiple threads from the same process on multiple processors 6. The transfer of control from one thread to another within the same process requires a mode switch to the kernel 7. Examples are: Windows NT ULTs 4. ULTs can run on any OS 5. In a pure ULT strategy, a multithreaded application cannot take advantage of multiprocessing (multi-processor or multi-core) 6. Thread switching does not require kernel mode privileges 7. Examples are: DCE Thread library
Relationships Between ULT States and Process States
Pictorial view
Combined Approaches Thread creation is done in the user space Bulk of scheduling and synchronization of threads is by the application Solaris is an example
Solaris Process Process User-level Threads Lightweight Processes (LWP) Kernel Threads includes the user s address space, stack, and process control block a user-created unit of execution within a process a mapping between ULTs and kernel threads fundamental entities that can be scheduled and dispatched to run on one of the system processors
Processes and Threads in Solaris
A Lightweight Process (LWP) Data Structure Includes: 1. An LWP identifier 2. The priority of this LWP 3. A signal mask 4. Saved values of user-level registers 5. The kernel stack for this LWP 6. Resource usage and profiling data 7. Pointer to the corresponding kernel thread 8. Pointer to the process structure
User-Level Threads Thread management is done by user-level threads library without the intervention of the kernel Fast to create and manage If the kernel is single threaded, any user-level thread performing a blocking system call will cause the entire process to be blocked User thread libraries POSIX Pthreads Mach C-threads Solaris 2 UI-threads
Kernel-Level Threads Supported by the Kernel Slower to create and manage than user threads If thread performs a blocking system call, the kernel can schedule another thread in the application for execution In multi-processor environments, the kernel can schedule threads on multiple processors Examples - Windows 95/98/NT/2000 - Solaris - Tru64 UNIX - BeOS - Linux
Relationship Between Threads and Processes
Implementation of Threads in Java public class Mythread { public static void main(string args[]) { SomeThread p1=new SomeThread(1); p1.start(); SomeThread p2=new SomeThread(2); p2.start(); SomeThread p3=new SomeThread(3); p3.start(); } } class SomeThread extends Thread { int myid; SomeThread(int id) { this.myid=id; } public void run(){ int i; for(i=1;i<10;i++) System.out.println ("Thread" + myid + ":" + i); } }
Java Threads Java threads may be created by extending the Thread class or implementing the Runnable interface Java threads are managed by the JVM
Thread Run Trace Thread2:1 Thread2:2 Thread3:1 Thread3:2 Thread3:3 Thread3:4 Thread3:5 Thread3:6 Thread3:7 Thread3:8 Thread3:9 Thread1:1 Thread2:3 Thread2:4 Thread2:5 Thread1:2 Thread1:3 Thread2:6 Thread1:4 Thread1:5 Thread2:7 Thread1:6 Thread1:7 Thread2:8 Thread1:8 Thread1:9 Thread2:9
Summary User-level threads created and managed by a threads library that runs in the user space of a process a mode switch is not required to switch from one thread to another only a single user-level thread within a process can execute at a time if one thread blocks, the entire process is blocked Kernel-level threads threads within a process that are maintained by the kernel a mode switch is required to switch from one thread to another multiple threads within the same process can execute in parallel on a multiprocessor blocking of a thread does not block the entire process Process/related to resource ownership Thread/related to program execution