Process Management Process Management Operating System Services 1. Program execution 2. I/O operations 3. File system manipulation 4. Communication 5. Error detection 6. Resource allocation 7. Protection Process Management Operating System Services Program execution • The operating system Loads program into memory and then: 1. It Handles the program’s execution 2. It Provides a mechanism for process synchronization 3. It Provides a mechanism for process communication Process Management Operating System Services Program execution • The operating system Loads program into memory and then: 1. It Handles the program’s execution 2. It Provides a mechanism for process synchronization 3. It Provides a mechanism for process communication Process Management Operating System Services Program execution • The operating system Loads program into memory. It also Executes the program 1. It Handles the program’s execution 2. It Provides a mechanism for process synchronization 3. It Provides a mechanism for process communication Process Management Process Text section program code value of the program counter Stack contains temporary data (such as function parameters, return addresses, and local variables) data section contains global variables Heap dynamically allocated during process run time Process Management Process A program becomes a process when an executable file is loaded into memory. Process: A process is a program at the time of execution. Process Program Process is a dynamic object Program is a static object Process is sequence of instruction execution Program is a sequence of instructions Process loaded into main memory Program loaded into secondary storage Devices Time span of process is limited Time span of program is unlimited Process is a active entity Program is a passive entity Process Management Process State: 1. The state of a process is defined in part by the current activity of that process. Process Management Process State: 1. The state of a process is defined in part by the current activity of that process. 2. A process can be in one of the following 5 states 1. New. The process is being created. 2. Running. Instructions are being executed. 3. Waiting. The process is waiting for some event to occur (such as an I/0 completion or reception of a signal). 4. Ready. The process is waiting to be assigned to a processor. 5. Terminated. The process has finished execution. Process Management Threads A process is divided into number of light weight process, each lightweight process is said to be a Thread. Components: 1. program counter (Keeps track of which instruction to execute next) 2. registers (holds its current working variables), 3. stack (execution History) Thread States 1. born State : A thread is just created. 2. ready state : The thread is waiting for CPU. 3. running : System assigns the processor to the thread. 4. sleep : A sleeping thread becomes ready after the designated sleep time expires. 5. dead : The Execution of the thread finished. Thread Examples of Thread: Word processor 1. Typing, 2. Formatting, 3. Spell check, 4. Saving are threads. Differences between Process and Thread Process Thread Process takes more time to create. Thread takes less time to create. it takes more time to complete execution & terminate. Less time to terminate. Execution is very slow. Execution is very fast. It takes more time to switch b/w two processes. It takes less time to switch b/w two threads. Communication b/w two processes is difficult Communication b/w two threads is easy. Process can’t share the same memory area. Threads can share same memory area. System calls are requested to communicate each other. System calls are requested to communicate each other. Process is loosely coupled. Threads are tightly coupled. It requires more resources to execute. Requires few resources to execute. Multithreading The number of Threads within a Process execute at a time is called Multithreading. Why Multithreading can give best performance? Multithreading The number of Threads within a Process execute at a time is called Multithreading. Why Multithreading can give best performance? If a program, is multithreaded, even when some portion of it is blocked, the whole program is not blocked. The rest of the program continues working If multiple CPU’s are available Multithreading Multithreading The number of Threads within a Process execute at a time is called Multithreading. Why Multithreading can give best performance? If a program, is multithreaded, even when some portion of it is blocked, the whole program is not blocked. The rest of the program continues working If multiple CPU’s are available If we have only a single thread, number of CPU’s available, No performance benefits achieved. PROCESS SCHEDULING Scheduling Queues 1. job queue: consists of all processes in the system 2. ready queue: processes that are residing in main memory and are ready and waiting to execute are kept on a list called the ready queue. 3. device queue: if a process is present in waiting state (or) waiting for an i/o event to complete is said to be in device queue.(or) The processes waiting for a particular I/O device is called device queue PROCESS SCHEDULING Schedulers There are 3 schedulers: 1. Long term scheduler. 2. Medium term scheduler 3. Short term scheduler PROCESS SCHEDULING Schedulers There are 3 schedulers: 1. Long term scheduler. 2. Medium term scheduler 3. Short term scheduler Scheduler duties: 1. Maintains the queue. 2. Select the process from queues assign to CPU. PROCESS SCHEDULING Context Switch: Assume, main memory contains more than one process. If cpu is executing a process, if time expires or if a high priority process another main memory, then the scheduler saves information about current process in the PCB and switches to execute another process. The concept of moving CPU by scheduler from one process to other process is known as context switch. PROCESS SCHEDULING Non-Preemptive Scheduling: CPU is assigned to one process, CPU do not release until the competition of that process. The CPU will assigned to some other process only after the previous process has finished. PROCESS SCHEDULING Preemptive scheduling: Here CPU can release the processes even in the middle of the execution. CPU received a signal from process p2. OS compares the priorities of p1 ,p2. If p1>p2, CPU continues the execution of p1. If p1<p2 CPU preempt p1 and assigned to p2. PROCESS SCHEDULING Dispatcher: The main job of dispatcher is switching the cpu from one process to another process. Dispatcher connects the cpu to the process selected by the short term scheduler. PROCESS SCHEDULING Dispatcher latency: The time it takes by the dispatcher to stop one process and start another process is known as dispatcher latency. If the dispatcher latency is increasing, then the degree of multiprogramming decreases. SCHEDULING CRITERIA 1. Throughput: how many jobs are completed by the cpu with in a time period. 2. Turn around time : The time interval between the submission of the process and time of the completion is turn around time. TAT = Waiting time in ready queue + executing time + waiting time in waiting queue for I/O. PROCESS SCHEDULING Waiting time: The time spent by the process to wait for cpu to be allocated. Response time: Time duration between the submission and first response. CPU Utilization: CPU is costly device, it must be kept as busy as possible. Eg: CPU efficiency is 90% means CPU Scheduler 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. 2. Switches from running to ready state. 3. Switches from waiting to ready. 4. Terminates. Scheduling under 1 and 4 is nonpreemptive. Operating System Concepts Alternating Sequence of CPU And I/O Bursts PROCESS SCHEDULING ALGORITHMS First-Come, First-Served Scheduling: • the simplest CPU-scheduling algorithm • Data structure: FIFO queue • PCB Position: Tail of the queue Allocated Resource : ?? PROCESS SCHEDULING ALGORITHMS First-Come, First-Served Scheduling: • the simplest CPU-scheduling algorithm • Data structure: FIFO queue • PCB Position: Tail of the queue Allocated Resource : CPU A period during which a process or program actively uses the CPU to execute instructions:-CPU burst PROCESS SCHEDULING ALGORITHMS Consider the following set of processes that arrive at time 0, with the length of the CPU burst given in milliseconds: Process Burst Time ð1 24 ð2 3 ð3 3 Assumption: the processes arrive in the order ð1 , ð2 , ð3 PROCESS SCHEDULING ALGORITHMS Assumption: the processes arrive in the order ð1 , ð2 , ð3 Process Burst Time ð1 24 ð2 3 ð3 3 ð·ð 0 Average Waiting Time: 17ms ð·ð 24 ð·ð 27 30 PROCESS SCHEDULING ALGORITHMS Assumption: the processes arrive in the order ð1 , ð2 , ð3 Process Burst Time ð1 24 ð2 3 ð3 3 ð·ð 0 ð·ð 24 ð·ð 27 Average Waiting Time: 17ms ->quite long 30 First-Come, First-Served (FCFS) Scheduling Process Burst Time P1 24 P2 3 P3 3 P1 0 P2 24 P3 27 30 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: • Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 Operating System Concepts FCFS Scheduling (Cont.) Suppose that the processes arrive in the order P2 , P3 , P1 . The Gantt chart for the schedule is: P2 0 P3 3 P1 6 • Waiting time for P1 = 6; P2 = 0; P3 = 3 30 • Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case. Convoy effect short process behind long process Operating System Concepts PROCESS SCHEDULING ALGORITHMS Assumption: the processes arrive in the order ð2 , ð3 , ð1 Process Burst Time ð1 24 ð2 3 ð3 3 ð·ð 0 ð·ð 3 ð·ð 6 Average Waiting Time: 3ms ->much better 30 PROCESS SCHEDULING ALGORITHMS Assumption: the processes arrive in the order ð2 , ð3 , ð1 Process Burst Time ð1 24 ð2 3 ð3 3 ð·ð 0 ð·ð 3 ð·ð 6 Average Waiting Time: 3ms ->much better 30 PROCESS SCHEDULING ALGORITHMS FCFS scheduling: The average waiting time under an FCFS policy is generally not minimal and may vary substantially if the processes CPU burst times vary greatly. Problem: CPU-bound process and many I/O-bound process PROCESS SCHEDULING ALGORITHMS FCFS scheduling: Problem: CPU-bound process and many I/O-bound process. 1. The CPU-bound process will get and hold the CPU. 2. During this time, all the other processes will finish their I/0 and will move into the ready queue, waiting for the CPU. 3. While the processes wait in the ready queue, the I/0 devices are idle. PROCESS SCHEDULING ALGORITHMS FCFS scheduling: Problem: CPU-bound process and many I/O-bound process. 3. Eventually, the CPU-bound process finishes its CPU burst and moves to an I/0 device. 4. All the I/O-bound processes, which have short CPU bursts, execute quickly and move back to the I/0 queues. 5. At this point, the CPU sits idle. 6. The CPU-bound process will then move back to the ready queue and be allocated the CPU. PROCESS SCHEDULING ALGORITHMS FCFS scheduling: Problem: CPU-bound process and many I/O-bound process. 7. Again, all the I/0 processes end up waiting in the ready queue until the CPU-bound process is done. 8. There is a convoy effect as all the other processes wait for the one big process to get off the CPU. 9. This effect results in lower CPU and device utilization than might be possible if the shorter processes were allowed to go first PROCESS SCHEDULING ALGORITHMS FCFS scheduling algorithm: • is nonpreemptive. • Once the CPU has been allocated to a process, that process keeps the CPU until it releases the CPU, either by terminating or by requesting I/0. • particularly troublesome for time-sharing systems, where it is important that each user get a share of the CPU at regular intervals. • It would be disastrous to allow one process to keep the CPU for an extended period. 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. Two schemes: • nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst. • preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF). SJF is optimal – gives minimum average waiting time for a given set of processes. Operating System Concepts PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: Assigned available CPU to a process with the smallest CPU burst time. PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: Assigned available CPU to a process with the smallest CPU burst time. What happen when two process have the same CPU burst time. FCFS scheduling is used to break the tie. PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: Note: a more appropriate term for this scheduling method would be the shortest-next-CPU-burst algorithm, because scheduling depends on the length of the next CPU burst of a process, rather than its total length. The term SJF is use because most people and textbooks use this term to refer to this type of scheduling. PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: Assigned available CPU to a process with the smallest CPU burst time. Process ð·ð ð·ð ð·ð ð·ð Burst Time 6 8 7 3 PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: Assigned available CPU to a process with the smallest CPU burst time. Process ð·ð ð·ð Burst Time 6 8 ð·ð ð·ð 7 3 ð·ð 0 ð·ð 3 ð·ð 9 ð·ð 16 24 PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: Assigned available CPU to a process with the smallest CPU burst time. Process Burst Time ð·ð 6 ð·ð 8 ð·ð 7 ð·ð 3 ð·ð 0 ð·ð 3 ð·ð 9 ð·ð 16 average waiting time :>7ms 24 PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: Assigned available CPU to a process with the smallest CPU burst time. Process Burst Time ð1 6 ð2 8 ð3 7 ð4 3 ð·ð 0 ð·ð 3 ð·ð 9 ð·ð 16 average waiting time :>7ms What is the average waiting time for FCFS? 24 PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: Assigned available CPU to a process with the smallest CPU burst time. Process Burst Time ð·ð 6 ð·ð 8 ð·ð 7 ð·ð 3 ð·ð 0 ð·ð 3 ð·ð 9 ð·ð 16 average waiting time :>7ms What is the average waiting time for FCFS? 10.25ms 24 PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: . Moving a short process before a long one decreases the waiting time of the short process more than it increases the waiting time of the long process. Consequently, the average waiting time decreases PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: The SJF scheduling algorithm is provably optimal, in that it gives the minimum average waiting time for a given set of processes. Moving a short process before a long one decreases the waiting time of the short process more than it increases the waiting time of the long process. Consequently, the average waiting time decreases PROCESS SCHEDULING ALGORITHMS Shortest-Job-First Scheduling: The SJF scheduling algorithm is provably optimal, in that it gives the minimum average waiting time for a given set of processes. Moving a short process before a long one decreases the waiting time of the short process more than it increases the waiting time of the long process. Consequently, the average waiting time decreases Challenge:The real difficulty with the SJF algorithm is knowing the length of the next CPU request. Example of Non-Preemptive SJF 1. At time 0ms, only P1 is there and so it is assigned to CPU. 2. P1 completes execution at 6ms and at that time P2 and P3 have arrived, but not P4. 3. P3 is assigned to CPU since it has the shortest burst time among current processes. 4. P3 completes execution at time 10ms. 5. By that time P4 has arrived and so SJF algorithm is run on the processes P2 and P4. Hence, we find that the order of execution is P1, P3, P4, P2 Operating System Concepts Example of Non-Preemptive SJF 1. At time 0ms, only P1 is there and so it is assigned to CPU. 2. P1 completes execution at 6ms and at that time P2 and P3 have arrived, but not P4. 3. P3 is assigned to CPU since it has the shortest burst time among current processes. 4. P3 completes execution at time 10ms. 5. By that time P4 has arrived and so SJF algorithm is run on the processes P2 and P4. Hence, we find that the order of execution is P1, P3, P4, P2 Operating System Concepts Example of Non-Preemptive SJF Operating System Concepts Example of Non-Preemptive SJF Operating System Concepts Example of Non-Preemptive SJF Operating System Concepts Example of Preemptive SJF (SRTF) algorithm Example of Preemptive SJF (SRTF) algorithm Determining Length of Next CPU Burst Can only estimate the length. Can be done by using the length of previous CPU bursts, using exponential averaging. 1. tn = actual lenght of n CPU burst th 2. ïī n +1 = predicted value for the next CPU burst 3. ïĄ , 0 ïĢ ïĄ ïĢ 1 4. Define : ïī n=1 = ïĄ tn + (1 − ïĄ )ïī n . Prediction of the Length of the Next CPU Burst Operating System Concepts Examples of Exponential Averaging • ïĄ =0 • ïīn+1 = ïīn • Recent history does not count. • ïĄ =1 • ïīn+1 = tn • Only the actual last CPU burst counts. • If we expand the formula, we get: ïīn+1 = ïĄ tn+(1 - ïĄ) ïĄ tn -1 + … +(1 - ïĄ )j ïĄ tn -1 + … +(1 - ïĄ )n=1 tn ïī0 Since both ïĄ and (1 - ïĄ) are less than or equal to 1, each successive term has less Operating System weight Concepts than its predecessor. Priority Scheduling • A priority number (integer) is associated with each process • The CPU is allocated to the process with the highest priority (smallest integer ïš highest priority). 1. Preemptive 2. nonpreemptive • 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. Operating System Concepts 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. • Performance 1. q large ï FIFO 2. q small ï q must be large with respect to context switch, otherwise overhead is too high. Operating System Concepts Example of RR with Time Quantum = 20 Process Burst Time P1 53 P2 17 P3 68 P4 24 The Gantt chart is: P1 0 P2 20 P3 37 P4 57 P1 77 P3 97 P4 117 P1 P3 121 134 P3 154 162 Typically, higher average turnaround than SJF, but better response. Operating System Concepts Time Quantum and Context Switch Time Turnaround Time Varies With The Time Quantum Multilevel Queue 1. Ready queue is partitioned into separate queues: • foreground (interactive) • background (batch) 2. Each queue has its own scheduling algorithm, • • foreground – RR background – FCFS 3. 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 Operating System Concepts Multilevel Queue 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: • • • • • number 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 Operating System Concepts Example of Multilevel Feedback Queue • Three queues: o Q0 – time quantum 8 milliseconds o Q1 – time quantum 16 milliseconds o Q2 – FCFS • Scheduling o A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1. o At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2. Multilevel Feedback Queues Multiple-Processor Scheduling • CPU scheduling more complex when multiple CPUs are available. • Homogeneous processors within a multiprocessor. • Load sharing • Asymmetric multiprocessing – only one processor accesses the system data structures, alleviating the need for data sharing. Real-Time Scheduling • Hard real-time systems – required to complete a critical task within a guaranteed amount of time. • Soft real-time computing – requires that critical processes receive priority over less fortunate ones. Dispatch Latency Algorithm Evaluation • Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload. • Queueing models • Implementation Operating System Concepts Evaluation of CPU Schedulers by Simulation Solaris 2 Scheduling Windows 2000 Priorities
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )