760 likes | 1k Views
UNIT II Process Management, Thread Scheduling Chapter 3: Process Concept Process Concepts Chapter 4: Multithread Programming Threads Chapter 5: Process Scheduling Scheduling Criteria Algorithms – their evaluation Thread Scheduling Case Studies UNIX / Linux / Windows
E N D
UNIT II Process Management, Thread Scheduling Chapter 3: Process Concept Process Concepts Chapter 4: Multithread Programming Threads Chapter 5: Process Scheduling Scheduling Criteria Algorithms – their evaluation Thread Scheduling Case Studies UNIX / Linux / Windows University Exam Questions Objective Type Questions Process Management, Thread Scheduling
Process Concepts • Process • Process States • Process Control Block (PCB) • Switch from Process to Process • Process Scheduling Queues • Schedulers • Cooperating Processes • Inter Process Communication (IPC) |<< Process Management, Thread Scheduling
Process • A program in execution. • Process execution must progress in Sequential fashion. • includes: • Program Counter • Stack • Data Section Process Management, Thread Scheduling |<<
Process States As a process executes, it changes state • new: The process is being created • running: Instructions are being executed • waiting: The process is waiting for some event to occur • ready: The process is waiting to be assigned to a processor • terminated: The process has finished execution Process Management, Thread Scheduling |<<
Process Control Block (PCB) Information associated with each process • Process State • Program Counter • CPU Registers • CPU Scheduling information • Memory Management information • Accounting information • I/O Status information Process Management, Thread Scheduling |<<
CPU Switch From Process to Process Process Management, Thread Scheduling |<<
Process Scheduling Queues • Job queue • Set of all processes in the system • Ready queue • Set of all processes residing in main memory, ready and waiting to execute • Device queues • Set of processes waiting for an I/O device • Processes migrate among the various queues Process Management, Thread Scheduling
Ready Queue and Various I/O Device Queues Process Management, Thread Scheduling
Representation of Process Scheduling Process Management, Thread Scheduling |<<
Schedulers • Long-term scheduler (or job scheduler) • Selects which processes should be brought into the ready queue • Short-term scheduler (or CPU scheduler) • Selects which process should be executed next and allocates CPU Process Management, Thread Scheduling
Addition of Medium Term Scheduling Process Management, Thread Scheduling |<<
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 • Modularity • Convenience Process Management, Thread Scheduling |<<
Interprocess Communication (IPC) • Mechanism for processes to communicate and to synchronize their actions • Message system • Processes communicate with each other without resorting to shared variables • Provides two operations: • send(message) – message size fixed or variable • receive(message) • If P and Q wish to communicate, they need to: • establish a communicationlink between them • exchange messages via send/receive • Implementation of communication link • Physical (e.g., shared memory, hardware bus) • Logical (e.g., logical properties) Process Management, Thread Scheduling |<<
Threads • Threads • Benefits • User Threads • Kernel Threads • Multithreading Models • Threading Issues • Application Threads |<< Process Management, Thread Scheduling
Threads • Light Weight Process (LWP). • Basic unit of CPU utilisation. • Comprises of • Thread ID • Program Counter • Register Set • Stack • Types • User Threads • Kernel Threads OS-5 Threads
Single and Multithreaded Processes Process Management, Thread Scheduling |<<
Benefits • Responsiveness • Resource Sharing • Economy • Utilization of Multi Processor Architectures Process Management, Thread Scheduling |<<
User Threads • Thread management done by user-level threads library • Three primary thread libraries: • POSIX Pthreads • Win32 threads • Java threads Process Management, Thread Scheduling |<<
Kernel Threads • Supported by the Kernel • Examples • Windows XP/2000 • Solaris • Linux • Tru64 UNIX • Mac OS X Process Management, Thread Scheduling |<<
Multithreading Models • Many-to-One Model • One-to-One Model • Many-to-Many Model Process Management, Thread Scheduling |<<
Many-to-One Model • Many user-level threads mapped to single kernel thread • Examples: • Solaris Green Threads • GNU Portable Threads Process Management, Thread Scheduling |<<
One-to-One Model • Each user-level thread maps to kernel thread • Examples • Windows NT/XP/2000 • Linux • Solaris 9 and later Process Management, Thread Scheduling |<<
Many-to-Many Model • Allows many user level threads to be mapped to many kernel threads • Allows the operating system to create a sufficient number of kernel threads • Solaris prior to version 9 • Windows NT/2000 with the ThreadFiber package Process Management, Thread Scheduling
Two-Level Model • Similar to Many-to-Many, except that it allows a user thread to be bound to kernel thread. • Examples • IRIX • HP-UX • Tru64 UNIX • Solaris 8 and earlier Process Management, Thread Scheduling |<<
Threading Issues • Semantics of fork() and exec() system calls • Thread Cancellation • Signal Handling • Thread Pools • Thread Specific Data • Scheduler Activations Process Management, Thread Scheduling |<<
Thread Cancellation • Terminating a thread before it has finished. • General approaches: • Asynchronous cancellation • Terminates the target thread immediately • Deferred cancellation • Allows the target thread to periodically check, if it should be cancelled Process Management, Thread Scheduling |<<
Signal Handling • Signals are used in UNIX systems to notify a process that a particular event has occurred • A signal handler is used to process signals • Signal is generated by particular event • Signal is delivered to a process • Signal is handled • Options: • Deliver the signal to the thread to which the signal applies • Deliver the signal to every thread in the process • Deliver the signal to certain threads in the process • Assign a specific thread to receive all signals for the process Process Management, Thread Scheduling |<<
Thread Pools • Create a no. of threads in a pool where they await work • Advantages: • Usually slightly faster to service a request with an existing thread than create a new thread • Allows the no. of threads in the application(s) to be bound to the size of the pool Process Management, Thread Scheduling |<<
Thread Specific Data • Allows each thread to have its own copy of data • Useful when you do not have control over the thread creation process (i.e., when using a thread pool) Process Management, Thread Scheduling |<<
Scheduler Activations • Both Many-to-Many and Two-level models require communication to maintain the appropriate number of kernel threads allocated to the application • Scheduler activations provide upcalls • A communication mechanism from the kernel to the thread library • This communication allows an application to maintain the correct number kernel threads Process Management, Thread Scheduling |<<
Application Threads • Pthreads • Windows XP Threads • Linux Threads • Java Threads Process Management, Thread Scheduling |<<
Pthreads • A POSIX standard (IEEE 1003.1c) API for thread creation and synchronization • API specifies behavior of the thread library, implementation is up to development of the library • Common in UNIX operating systems • Solaris • Linux • Mac OS X Process Management, Thread Scheduling |<<
Windows XP Threads • Implements the one-to-one mapping • Each thread contains • A thread id • Register set • Separate user and kernel stacks • Private data storage area • The register set, stacks, and private storage area are known as the context of the threads • The primary data structures of a thread include: • ETHREAD (executive thread block) • KTHREAD (kernel thread block) • TEB (thread environment block) Process Management, Thread Scheduling |<<
Linux Threads • Linux refers to them as tasks rather than threads • Thread creation is done through clone() system call • clone() • allows a child task to share the address space of the parent task Process Management, Thread Scheduling |<<
Java Threads • Java threads are managed by the JVM • Java threads may be created by: • Extending Thread class • Implementing the Runnable interface Java Thread States Process Management, Thread Scheduling |<<
CPU Scheduler • Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them • CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state (Non-Preemptive) 2. Switches from running to ready state (Preemptive) 3. Switches from waiting to ready (Preemptive) 4. Terminates (Non-Preemptive) Process Management, Thread Scheduling
Scheduling Criteria • CPU utilization • Keep the CPU as busy as possible • Throughput • No. of processes that complete their execution per time unit • Turnaround time • Amount of time to execute a particular process • Waiting time • Amount of time a process has been waiting in the ready queue • Response time • Amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment) Process Management, Thread Scheduling
Optimization Criteria • Max CPU utilization • Max throughput • Min turnaround time • Min waiting time • Min response time |<< Process Management, Thread Scheduling
Scheduling Algorithms • FCFS Scheduling • SJF Scheduling • Non-Preemptive • Preemptive • Priority Scheduling • Round Robin • Multilevel Queue Scheduling • Multilevel Feedback Scheduling |<< Process Management, Thread Scheduling
P1 P2 P3 0 24 27 30 FCFS Scheduling ProcessBurst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P1 , P2 , P3 Gantt Chart: Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27) / 3 = 17 Process Management, Thread Scheduling
P2 P3 P1 0 3 6 30 FCFS Scheduling (Cont.) ProcessBurst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P2 , P3 , P1 Gantt Chart: Waiting time for P1 = 6;P2 = 0;P3 = 3 Average waiting time: (6 + 0 + 3) / 3 = 3 Process Management, Thread Scheduling |<<
Shortest-Job-First (SJF) Scheduling • Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time • Schemes: • Non-Preemptive • Once CPU given to the process it cannot be preempted until completes its CPU burst • Preemptive / Shortest-Remaining-Time-First (SRTF) • If a new process arrives with CPU burst length less than remaining time of current executing process, preempt. • Optimal • Gives minimum average waiting time for a given set of processes Process Management, Thread Scheduling |<<
P1 P3 P2 P4 0 3 7 8 12 16 Example of Non-Preemptive SJF ProcessArrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 Gantt Chart: Average waiting time = (0 + 6 + 3 + 7) / 4 = 4 Process Management, Thread Scheduling |<<
P1 P2 P3 P2 P4 P1 11 16 0 2 4 5 7 Example of Preemptive SJF ProcessArrival TimeBurst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 Gantt Chart: Average waiting time = (9 + 1 + 0 +2) / 4 = 3 Process Management, Thread Scheduling |<<
Priority Scheduling • A priority No. (Integer) is associated with each process • The CPU is allocated to the process with the highest priority • Smallest integer highest priority • Preemptive • Non-preemptive • SJF is a priority scheduling where priority is the predicted next CPU burst time • Problem Starvation • Low priority processes may never execute • Solution Aging • As time progresses increase the priority of the process Process Management, Thread Scheduling |<<
Round Robin (RR) • Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. • After this time has elapsed, the process is preempted and added to the end of the ready queue. • If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. • No process waits more than (n-1)q time units. Process Management, Thread Scheduling
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162 Example of RR with Time Quantum = 20 ProcessBurst Time P1 53 P2 17 P3 68 P4 24 Gantt chart: Higher average turnaround than SJF, but better response Process Management, Thread Scheduling |<<
Multilevel Queue • Ready queue is partitioned into separate queues: • Foreground (Interactive) • Background (Batch) • Each queue has its own scheduling algorithm • Foreground – RR • Background – FCFS • Scheduling must be done between the queues • Fixed priority scheduling; • (i.e., serve all from foreground then from background). • Possibility of starvation. • Time slice • Each queue gets a certain amount of CPU time which it can schedule amongst its processes; • i.e., 80% to foreground in RR • 20% to background in FCFS Process Management, Thread Scheduling
Multilevel Queue Scheduling Process Management, Thread Scheduling |<<
Multilevel Feedback Queue • A process can move between the various queues; • aging can be implemented this way • Multilevel-feedback-queue scheduler defined by the following parameters: • No. of queues • Scheduling algorithms for each queue • Method used to determine when to upgrade a process • Method used to determine when to demote a process • Method used to determine which queue a process will enter when that process needs service Process Management, Thread Scheduling