0% found this document useful (0 votes)
5 views

OS3

Operating System unit 3(AKTU series)

Uploaded by

ashishtruf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
5 views

OS3

Operating System unit 3(AKTU series)

Uploaded by

ashishtruf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 48
St CPU Scheduling Part-1 : Part-2 : Part-3 Part-4 : Part-5 Part-6 Part-7 Part-8 CONTENTS CPU Scheduling : Scheduling .. Concepts, Performance Criteria Process States, Process Transition Diagram Schedulers........... . 5-10€ to 3-13C Process Control Block (PCB), .. Process Address Space, Process Identification Information Threads and their Management..........3-18C to 3-18C Scheduling Algorithms, ..... 8-19C to 3-82C Multiprocessor Scheduling Deadlock : System Model, .. .. 8-B2C to 3-470 Deadlock Characterization, Prevention, Avoidance and Detection Recovery from Deadlock..... . 8-48C to 3-49 oy 3-1 C(MCA-Sem.2) a g-2 C (MCA-Sem-2) cry Performance Cie CPU Scheduling * Scheduling Concepts, ————————— Que 3.1. What is CPU scheduling ? Give the criteria for scheduling. a Discuss the performance criteria for CPU scheduling Answer CPU scheduling is the management of @PU reseurces. ‘The scheduling mechanism is the part of the process manager handles the removal of the running process from the CPU add selection of another processom the basis of a particular strates 3. CPU scheduling is the basis of multi programmed op 4, The scheduler is responsible Drcisitspning anal 5. CPU schedulingis used toimerease CPU utilization. Scheduling criteria: The scheduling policy each of the criteria. Somecommonly used cries Por 1 CPU utilization: a. oo — processor is busy. b. tuff on the oylln tins elo zation achieved. = *% cn OR ge tiesieonae 0 % to 100%. Om. expensive system ie., time system, CPU: = the primary consideration. 2 Throughput : a Renan refers to the amount of work completed im b, dhe number of processes the system can execute in ‘ le, ‘The higher the number, the more work is done by the Operating Systems 8-3 C (MCA-Sem-2) ———————— ee 3 Waiting time : a, The average period of time a process spends waiting. b. Waiting time may be expressed as turnaround time less the actual execution time. Turnaround time : a. The interval from the time of submission of a process to the time of completion is the turnaround time. b. Turnaround time is the sum of the periods spent waiting to get into memory, waiting into the ready queue, executing on the CPU and doing /0. Response time: a. Response time is the time from the submission ofa request until the first response is produced. Priority : a. Give preferential treatment to processes with higher priorities. Balanced utilization : a. Utilization of memory, I/O devices and other system resources are also considered. b. Notonly CPU utilization considered for performance. Fairness : a. Avoid the process from the starvation. b, All the processes must be given equal opportunity to execute. Que 3.2. | Discuss the objectives of CPU scheduling. Answer Objectives of CPU'scheduling : 1. Efficiency: Keep the CPU busy all the time. 2. Maximize throughput: Service the largest possible number of jobs in a given amount of time; minimize the amount of time users must wait for their results. 3. Minimize response time : Interactive users should see good performance. 4. Minimize overhead : Do not waste too many resources. Keep scheduling time and context switch time at a minimum, 5. Maximize resource use : Favour processes that will use under-utilized resources. 6. Avoid indefinite postponement : Every process should get a chance torun eventually, 9-4 c (MCA-Sem-2) ities : If the scheduler allows a process to be assigned a 1 ques: | What is the difference between pre-emptive and non duling is d seheation of multi-level scheduling. Answer Difference: x 5. No. Proempive | Nompreemptive scheduling re-emptive CPU Scheduling arity, it should be meaningful and enforced. ‘acheduling ? State, why strict nom pre-emptive setae tre ? Explain the Unlikely to be used in computer cen scheduling Processor can be pre-empted to| Once processor starts to execute execute a different process in| a process it must finish it before: the middle of execution of any executing the other. It cannot be current process. pausedin middle. CPU utilization is more compared to non pre-emptive scheduling. Waiting time and response time isless. If a high priority process frequently arrives in the ready |i queue, low priority process may starve. Pre-emptive scheduling is flexible. For example, SRTF, Priority, Round-Robin, etc. gn a N = e oe x ian epee emptive scheduling is unlikely to be used in e a3 Once the CPU has been allocated to a process, the process ‘ CPU until it releases the CPU either by terminati the waiting state. ae og In a general pur CPUs ‘pose computer system, users share about system responsiveness. ee If the system uses non pre-emptive scheduling, some before the monitor for sev i 0 ae eral hours without doing anything ae for the set of processes in front of thous the sys Operating Systems 3-5 C (MCA-Sem.2) 4 So, strictly non pre-emptive scheduling is unlikely to be used in a general purpose computer system. Operation of multilevel scheduling : ts A multilevel queue scheduling algorithm partitions the ready queue into several separate queues (Fig. 3.3.1). ‘The processes are permanently assigned to one queue, generally based ‘on some property of the process, such as memory size, process priority, or process type. Each queue has its own scheduling algorithm. For example, separate queues might be used for foreground and background processes. ‘The foreground queue might be scheduled by an RR algorithm, while the background queue is scheduled by an FCFS algorithm. highest priority lowest priority Fig. 3.3.1. Multilevel queue scheduling. There must be scheduling among the queues, which is commonly implemented as fixed-priority pre-emptive scheduling. For example, the foreground queue may have absolute priority over the background queue. | | | | | ! Que 3.4. | Explain the objectives and implementation of short term scheduling and long term scheduling. Answer Objective and implementation of short term scheduling : ie The main objective is increasing system performance in accordance with the chosen set of criteria. It is the change of ready state to running state of the process. It is also called CPU scheduler, CPU scheduler selects from among the Processes that are ready to execute and allocates the CPU to one of them, | ym-2) 3-6 c (MC ¢ and implementation of ary objective is to provi d processor bound. degree of multi-programming- 3, Ifthe degree of multi-programming is stable, then the average rate of process creation ‘must be equal to the average departure rate of processes Ieaving the system. Long term scheduler determines which programs are admitted to the 2 long term scheduling jectiv' opje de a balanced mix of jobs, such as UO 3, The prim pound an it also controls the 4. gystem for processing. 5. It isalso called job scheduler. § Job scheduler selects processes from the queue and/loads them into Tot ton, Process loads into the memory-for CPU scheduler. memory for €: Process States, Process Transition Diagram. — : Que 35. | List out the various states of process.“ OR = eS Draw the process state diagram and describe the yarious process: states. What are th = a at are the various process states ? Depict process state diagram. What do you understand by context i various processes involved in it. _ 4 me 2 Draw and explain the process Paresh diagram. Answer ee Various process states are : 1. New: The process is being created. 2 Running: Instructions are being executed. > 3 fatae us process is waiting for some event to occur (such or reception of a signal). 4, Ready : The process is waiting to be assigned to a processor. Terminated : The process has finished execution, 3-7 C (MCA-Sem.2) Operating Systems Process state diagram : admitted jnterrupt exit scheduler dispatch 1/0 or event wait 1/0 or event completion Fig. 9.5.1. Context switching : 1. Acontext switching is the me context ofa CPU in process cont : be resumed from the same point at a later time 2. Anyoperating system that allows for multitasking ‘relies heavily on the Use of context switching to allow different processes to run at the same time. 3, Accontext switch occurs when a computer's CPU switches from one process or thread to a different process or thread. 4. Context switching allows for one CPU to handle numerous processes or threads without the need for additional processors. Process involved in context switching are : ‘The steps in a full process switch are : 1. Savethe context of the processor, including program counter and other registers. 2 Update the process control block of the process that is currently in the running state, This includes changing the state of the process to one of the other states (Ready; Blocked; Ready/Suspend or Exit). Other relevant fields must also be updated, including the reason for leaving the running state and accounting information. 3, Move the process control block of this process to the appropriate queue (Ready; Blocked on Event i; Ready/Suspend). Select another process for execution. Update the process control block of the process selected. This includes changing the state of this process to running. 6 Update memory management data structures. This may be required, depending on how address translation is managed. 7. Restore the context of the processor to that which existed at the time oes Process was last switched out of the running state, by loading in the previous values of the program counter and other registers: chanism to store and restore the state or trol block so that a process execution can ey he process ‘The Pr ’for the PUTPOS” rocess of P process Pracess suspension | 1. Parent process request : A process may be suspended * parent process request in order to modify the process- ly and there is no place in the main agnor Pefine the diffe © 1 6) 8 4 9 5 Fig. 3.28.1. Turnaround time = Completion time — Arrival time For P, = 8-0=8ms -, Average turnaround time = Boa SO aii ms Waiting time = Turnaround time — Burst time For P,=8-8=0ms 2 Average waiting time = ee = = =8.75 ms ii, SRTF: Refer Q. 3.26, Page 3-26C, Unit: 3. Que 3.2! Consider the following process : Process] Arrival | Burst | Priority Time Time P, 0 6 3 P, 1 4 1 P, 2 5 2 P, 3 8 4 2 eee $30 ¢ (MCA-Sem-2) ceu Scheduling Draw Gantt chart and find the average wa; DeawGent es # time and average SRIF Scheduling ii, Round robin (Quantum time : 3) Gnswer | i. SRIF: Gantt chart ; LILA Ose ROC Be aera ‘Turnaround time = Completion time — Arrival time For P, = 16-0 = 16 For P, For P,=11-2=9 For P, = 23-3 = 20 Average turnaround time = 18+6+9420 51 ir 22m Waiting time = Turnaround timé= Burst time For P, = 16-6=10 For P, = 6— For P,=9-5=4 For P, = 20-8 = 19 Average waitingtime - W+2+4+12 28 im 4 Round robin (Quantum time = 3) Gantt chart : (PoP, P,|P.[P2 [Ps [PaP, | © 369 12 13.15 18 93 Turnaround time = Completion time ~ Arrival time For P, = 18-0=18 = 18+12+13+20 63 Average tuaround time = 28412413 +20_ = 7 71575 ms Waiting ti For 4 time — Burst time = 16-6=10 3-5=8 For P, = 233-99 8+12 Average waiting time = 10+8+8+12 sa 4 “ae — Operating Systems 3-31C (MC. m-2) Que 3.30. | Explain the various CPU scheduling techniques with Gantt charts clearly as indicated b; 2 y y (process name, arri Process time) for the following (A, 0, 4), (B, 2, 7), (C, 3, 2) mag for FCFS and SRTF. 1 time, D, 2,9) Answer Process Arrival Burst Time Time A 0 4 B 2 7 ic 3 2 D 2 2 1. FCFS: Gantt chart: A B Cc D 0 4 11 14 15 Turnaround time = Completion time —Arrival time For A For B For C=13-3=10 For D= 15-2 =13 Average turnaround time = detaoes z 8 Waiting time ‘Turnaround time — Burst time ForA=4—450 ForB=9=7=2 For C= 10-2 For D =13© =o. 11 Average waiting time = 2 SRIF: Gantt chart : [a|B]c[a[p[B er? ge 5 7. 15 Turnaround time = Completion time — Arrival time ForA=7- 7 For B= 15-2=13 For C=5-— 2 For D=9-2=7 14+13424+7 29 Average turnaround time = ———7——— =" = 7.25 Waiting time = Turnaround time — Burst time ee ee ee average Garner COG aga = & l, Deadlock Characterization. Deadlock : Systern Mog ne and Detection. @ueSSi.| What is deadlock ? What are the necessary for deadlock 7 aOR ¢ the necessary conditions to hold a deadlock in 2 Syste== the necessary conditions. What ar What is a deadlock ? Discus: with examples. Answer 1. A deadlock is a situation where a group of pr s blocked as a’result ef each process having aca resources needed for its completion and waiting for release of remaining resource held by other in the same group. thus 5 impossible for any of the processes to proceed_ Resource mamagers and other operating system, processes cam involved in a deadlock situation: — A deadlock situation can arise if the following four condi simultaneously in a system : 1 Ze ons Mutual exclusion : At least one resource must be held im a nom jrarable mode; that is, only one process at a time can use the resou another process requests that resource, the requesting process be delayed until the resource has been released. sae \d wait : A process must be holding at least one resource Waitine Gaeeee oS : ie 3 Beas additional resources that are currently being held | No pre-emption : Resources cannot be pre-empted; f.¢., a resource: be released onl. r ly voluntarily by the holding it, after that p: has completed sts tacky ‘ily by: process holding it, co. a Operating Systems 3-33 C (MCA-Sem. 2) Operating Syste ‘ 4 Circular tr A set (P, P,, ... P,} of waiting processes mus a et (Py a S must ¢: such that Py is waiting for a resource held by P,, P, is waiting on Fesource held by P,, ...P,_ is waiting fora resource held by P. and P is waiting for a resource held by P,. Da al Que $32. | How deadlock are prevented ? OR Define and explain the deadlock prevention. OR What are the approaches that can be used for i eats lor prevention of Answer Deadlock prevention is an approach which ensures that system will never enter in deadlock state. These are following approaches of deadlock prevention : 1 Mutual exclusion : a Mutual exclusion condition must hold for non-sharable resources, b. If access to a resource requires mutual exclusion, then mutual exclusion must be supported by the operating system. 2 Hold and wait: a. The hold and wait condition can be eliminated by forcing a process to release all resources held by it, whenever it requests a resource that isnot available. b. For example, process copies data from a floppy disk toa hard disk, sort a disk file and then prints the resultsto a printer. ¢. fall the resources must be requested at the beginning of the process, then the process must initially request the floppy disk, hard disk and a printer. ad It will holdthe printer for its entire execution, even though it needs the printer only at the end. 3. Nopre-emption: a Ifa process holding certain resources is denied a further request. That process must release its original resources and if necessary request them again, together with additional resource. b. Ifa process requests a resource that is currently held by another process, the operating system may preempt the second process and require it to release its resources. ¢. Pre-emption is possible for certain types of resources, such as CPU and main memory. 4. Circular wait : a. One way to prevent the circular wait condition is by linear ordering of different types of system resources, mm resources are divided into different classes a, avatar ator eegeeeeee oe ae be Fe alprocens, ba Weet Saly Come renounce teen Ss , get nat ta deadlock avoldance 7 Dan aan sa es 2 Answer madiock ave: DessioGs choices to assure jeadlock avoidance ther dance allows the three necessary conditions = that the deadlock point is never fore allows more co - a : ‘Cvention does- : prev: : : ; 3. Deadlock avoidance =sanizes additional information about . PRSGurces are to be requested. > 4. With deadlock avoidance, = decision is made dynamically With of resource allocation request could, if seamted. to a deadlock. ¢ mwo approaches are used to avoid the deadlock 1. po not start a process if its détiands might lead tode 2 Do not grant an incremental res@urce request to = Allocation might lead to deadisek_ System can be in one of the following states 1. Safe state : Such a state occurs When the system can allocate o cach process (up to its maximum) in some order and avoid = = pe state will be characterized by a safe sequence. Unsafe state = If the system did not follow the safe sequence filecation from the beginning and it is now in a situation, = (fo = deadlock, then it ia in an unsafe state. Retdlock state : Ifthe system has some circular wait me processes, then it is in deadlock. = GRUGSEA > - ang cot ee > <—e Operating Systems 3-850 (MCA-Sem-9) allocation graph is used to describe the deadlock. Tt is al, vem resource allocation graph. 2 2. Graph consists ofa set of vertices (V) and set of edges (E) 3. All the active processes in the system denoted by P= {P\, Po, oP, and sisting of all resource type in the system is denoted by R,) WR, Ry, 4. Request edge is an edge from Process to resource and denoted by PR, S. Anassignment edges an edge from resource to process and denoted by RP. 6 Holding of resource by process is denoted by assignment edge. 7. Requesting of resource by process is denoted by request edge. & For representing process and resource in the resource allocation graph is shown by square and circle. Each process is represented by circle and Tesource by square. Dot within the square represents the number of instance. 9. Consider the following system which consists of three processes i., P, P, and P, and four resources ie., Ry, Ry, R, and R,, Resource R, and R, have one instance, R, has two instance and R, has three instances. i Process P, isholding an instance of resource type Ry and is waiting for an instance of resource type Ry. i. Process P, is holding an instance of R, and R,, and waiting for an instance of resource type R. iii, Process P, is holding an instance of Ry. B= PEP, Ps) R=IR,,Ry,Ry Ry E=1P, > Ry, Py > RyRy, > Pyy Ry > Poy Ryy > Py, Ry > Py) R, Ry = Ry 4 Fig. 3.34.1. Resource-allocation graph. ae a ae uca-Sem?) gage Define deadlock detection. Write an algorithm for ie" . qeeatock detection. e a sk detection js the process of actually determining that a deadiinek 1, Deadlorng identifying the processes and resources involved in the ists ’ Geadlock. ney ’ Wot glock detection algorithms invoked when the allocation cannot 2 reeranted ck detdiately and this condition occurs frequently. ; F vje idea is to check allocation against resoures availability fer all 3. Thtsible allocation sequences ta determine if the systemisin deadlocked | state. > Deadlock detection algorithm : step1: Let work and finish be vector of length m and n respectively- Initialize Work = Available If Allocation, + 0, then Finish{ #] = false Otherwise, Finish{i] =true For#=1, 2,0 step2: Find an index i such that both Finish! i ] = false Request, < Work Step3: Ifno such i exists, go to step + Step4: Work = Work + Allocation, Finish[ i] = true Go to step 2 Step5: = [i] = false, for some i, 1 R, and R, +P; for some resource R,. 5. If WFG contains cycle then the system is deadlocked otherwise isin safe state. ii, Several instances of a resource type: 1 Unmark all active processes from Allocation, Max and Available in accordance with the system state. 2. Find an unmarked process i such that Max < Available If found, mark process i, update Available Available : = Available + Allocation and repeat this step. Ifno process is found, then go to next step. If all processes are marked, the system is not deadlocked, Que 3.37. | “A safe state is not a deadlock state but a deadlock state is an unsafe state”. Explain. Discuss the different methods for handling deadlocks. 3. ee | ate in which there is at least one order in which all the i st ee Asafe Bee @ S\pun to completion without resulting in a deadlock. * gmproce® St GE processes < Poy Pg ge = is a safe sequence for the go ak sede ocation ‘state if, for each P, the resources that P, can stall 2 current &un be satisfied by the currently available resources plus the ea oe held by all the P;, with j aesource type *j p ane a ie a iitis = 20 array of see Il that indicates the remaining resource Need: need of each P Need (i, J1= resource type F, oe Need (é, ) = Max [i, j] — Allocation [%, jl ‘Allocation, specifies the resources currently allocated to process P-and Alles specifies the additional resources that process P, may still to complete its task. request ’ Banker's algorithm is used to avoid deadlock by following algorithms = : rithms: a. Safety algorithm : . 1. Let work and finish be vector of length m and n respectively. Initialize Work = Available and Finish [i] = False fori = 1, 2, 3, 4, ...n. 2, Find ani such that both a a, Finish [i] = False ¥ b, Need [i] < Work If no such i exist, goto step 4. 3, Work = Work + Allocation; Finish [i] = True 5 Goto step 2 4, If Finish [i] = True for all i, then the system is in a safe state. b peemuree request algorithm : Let Request, be the request array for ss P,, Request, {/] = & means process P, wants k instances of resource type R,. When a is : re ain fiction aes ees for resources is made by process P,, the folloy elf Request, < Need, a 2D array of size ‘n*m’ that defines the number of ‘ype currently allocated to each process. rocess. k means process P, currently allocated ’ instances of Got : F Gone i ‘0 step 2; otherwise, raise an error condition, since the proce ‘S exceeded its maximum claim. > Systems 2. if Request, < Available Goto step 3: ot} it Bet otherwise, P,, must wait, since the resources Are nop Have the syste to proces by madiying the ates ote ean Available = Available — Request, Allocation, = Allocation, + Request, Need, = Need, ~ Request, Que 3.41,| Describe Banker's algorithm for deadlock Consider a system with three processes and three res Snapshot of a system at time f, is given below : AVvOidanee, mUrces. The Allocation Max Available ALB | ca te A |B lc 3? {8 )4 | ths |Fer'| 7 [10 2| 0] 38] 4hsls 1 | 4 |ea.| a4 i, Is the current allocation in safe state ? fi, Would the following requests be granted in the current state; a, Process P, requests (1, 0,0) b, Process P, requests (1,0, 0) Answer | Banker's algorithm ; Refer Q. 3.40, Page 3-390, Unit-3 i, First find the available resources in the system Available = Number of instance »sum of allocation Current allocation Available = (7 7 10) —(5 4 10) Available resources = (2 3 0) —_—.. aa __ CPU Scheduling emcs-se = Paged matriiss z o content Process Need 5 Po 1 Py ah Ps 2 2; 0 sequence < Pa, Py, Po>- = m is in safe state. quest (1, 0, 0), this request is less than need. Need for 2, 2, 0), available resource is (2, 3, 0) and request is (1, safe ste! eee process Pz re Hi, Frocess Pz is ( 0, 0). ailable, (1, 0, 0)< (2, 3, 0) t < Av : Reques' 1, 0, 0) to process P, the need becomes as follows : After allocating ( safe sequence . Hence request of P, (1, 0, 0) will be granted. b. Process P, request (1, 0, 0), this request is less)than need, Need for process P, is (2, 3, 0), Available resource is (1,3, 0) and request is (1, 0, 0). Request Que 3.43. | In a system, n processes share m resource units th, at can be reserved and released only one at a time. The maximum ng of each process does not exceed m and sum of all maximum ness! less than m +n. Show that a deadlock cannot occur. 1) Answer n i Max; 1 for alli Proof : Need; = Max, — Allocation, ii, Ifthere exists a deadlock state then : ‘tion of thi, n ¥ Allocation; = m ia Use (i) to get : }; Need;+)) Allocation; = > Max; Available > False So, system will move to next process. Need for P, is (110) and available is (310) Need < Available True Request for P,is granted. Available = Available + Allocation = 310 + 212 =522 Next Process P, needs 501 and available is 522. Available = 522 + 401 = 923 HOonRNe BS a Next process p So, request is granted. Now Available = 923 + 020 = 943 ‘Sts granted 501 < 522, true 3-470 (MCA-Sem, +2) —= 4 needs 733 and available is 923. Next process P; needs (10, 1, 1) and available is 943. Need > Available Hence, no sequence is . aP\<3,3,15 Possible thus the state is unsafe, Need > Available > 331 ¢ 310 Hence, resource wil] not be granted. b. P,<2,1,0> Need < Available > True 210 < 310 Hence, the request will be granted. Que 3.45, Differentiate between deadlock and starvati jon. Answer | Basis for Deadlock Starvation Comparison| Basic Deadlock is where no Starvation is where low protéés proeéeds, and get | priority, processes get blocked, blocked, and high Priority Process proceeds, Arising The occurrence of | Enforcement of priorities, condition mutual uncontrolled resource exclusion, hold and wait, no management, preemption and circular wait simultaneously. Other name | Circular wait, Lifelock. Resources In deadlocked, requested | In starvation, the requested rreources are blocked by | resources are continuously the other processes. used by high priority processes. Prevention Avoiding mutual exclusion, | Ageing. hold and wait, and circular wait and allowing preemption, ll

You might also like