Operating-System Compressed
Operating-System Compressed
For
COMPUTER SCIENCE
.
OPERATING SYSTEM
SYLLABUS
Processes, threads, interprocess communication, concurrency and synchronization.
Deadlock. CPU scheduling. Memory management and virtual memory. File systems.
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
CONTENTS
Topics Page No
1. PROCESS AND DEADLOCK
2. MEMORY MANAGEMENT
2.1 Introduction 53
2.2 Single Partition Allocation 55
2.3 External & Internal Fragmentation 56
2.4 Segmentation 58
2.5 Fragmentation 59
2.6 Virtual Memory 59
2.7 Page Replacement 60
2.8 Demand Paging 61
2.9 Page-Replacement 61
2.10 Counting Algorithms 63
2.11 Allocation of Frames 63
2.12 Thrashing 64
Gate Questions 65
3. INTERRUPTS
3.1 Introduction 78
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
3.2 Interrupts 78
3.3 Applications I/O Interface 79
3.4 Block and Character Devices 79
3.5 Kernel I/O subsystems 79
3.6 Performance 81
3.7 Stable-Storage Implementation 83
Gate Questions 85
4. FILE-MANAGEMENT
4.1 Introduction 88
4.2 File Concept 88
4.3 Access Methods 89
4.4 Single-Level Directory 91
4.5 Types of Access 91
4.6 Allocation Methods 92
4.7 Free-Space Management 94
4.8 Directory Implementation 94
Gate Questions 96
5. ASSIGNMENT QUESTIONS 99
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
1 PROCESS & DEADLOCK
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
The set of these attributes is called context. Program Counter. Thread share data, code,
Context is stored in PCB. memory, files, address space, File
descriptor table, so on.
1.5 THREADS
Threads are light weight processes. Each
thread has its own Stack, Registers and
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
around Time, and minimize Waiting This reduction is substantial. Thus, the
Time of processes. average waiting time under a FCFS policy
is generally not minimal.
1.7 SCHEDULING ALGORITHMS The FCFS scheduling algorithm is non-
preemptive. Once, the CPU has been
1.7.1 First-Come, First-Served allocated to a process, that process keeps
Scheduling (FCFS) the CPU until it releases the CPU, either by
Criteria: Arrival Time terminating or by requesting I/O.
Mode : Non Preemptive
Data Structure: Queue 1.7.2 Shortest –Job-First Scheduling
Tie Breaker : Process Number (SJF)
The average waiting time under the FCFS Criteria: Burst Time
policy, however, is often quite long. Mode : Non Preemptive
Consider the following set of processes Data Structure: Min Heap
that arrive at time 0, with the length of the Tie Breaker : Arrival Time
CPU- burst time given in milliseconds: Shortest Job First has minimum average
waiting time among all CPU scheduling
PROC ARRIVA BURS CT TA W algorithms.
ESS L TIME T T T Consider the following set of process, with
TIME the length of the CPU burst time given in
P1 0 24 24 24 0 milliseconds:
P2 0 3 27 27 24 PROCESS BURST TIME
P3 0 3 30 30 27 P1 6
P2 8
P3 7
GANTT CHART:
P4 3
Using SJF scheduling, we would schedule
P1 P2 P3
these processes according the following
0 24 25 27 28 30 Gantt chart:
Now, the waiting time is 0 milliseconds for
P4 P1 P3 P2
process P1, 24 milliseconds for process P2,
0 3 9 16 24
and 27 milliseconds for process P3. Thus,
0+24+27 The waiting time is 3 milliseconds for
the average waiting time = = process P1, 16 milliseconds for process P2,
3
17milliseconds. 9 milliseconds for process P3, and 0
This example also shows Convoy effect. milliseconds for process P4.
FCFS suffers from convoy effect. Thus, the average waiting time =
If the processes arrive in the order P2, P3, 3 16 9 0
P1, however, the results will be as shown in 4 = 7 milliseconds.
the following Gantt chart: If we were using the FCFS scheduling
P2 P3 P1 scheme, then the average waiting time
0 3 6 30 would be 10.25 milliseconds.
6+0+3
The SJF scheduling algorithm is provably
Now, the average waiting time = = optimal, in that it gives the minimum
3
3milliseconds. average waiting time for a given set of
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
processes. If two processes have the same controls the relative weight of
length next CPU burst, FCFS scheduling is recent and past history in our
used to break the tie. production.
If =0, then n 1 = tn, and recent history
SJF ADVANTAGES
has no effect (current conditions are
(a) Maximum Throughput.
assumed to be transient);
(b) Minimum Average WT and
If =1, then n 1 = tn and only the most
Minimum Average TAT.
SJF DISADVANTAGES recent CPU burst matters (history is
(a) Starvation to longer jobs. assumed to be old and irrelevant). More
(b) It is not implementable because BT commonly, = 1/2.
of process cannot be known ahead. To understand the behavior of the
Solution: exponential average, we can expand the
SJF with predictive Burst Time.
formula for n 1 by substituting for n, to
find
Prediction Techniques:
n 1 t n (1 )n1 ... (1 ) j t n j (1 )n 1 0
I. Static :
a. Process Size Since both and (1 - ) are less than or
b. Process Type (OS equal to 1, each successive term has less
,Interactive, Foreground, weight than its predecessor. The SJF
Background) algorithm may be either preemptive or
II. Dynamic: non-pre-emptive. The choice arises when a
a. Simple Averaging new process arrives at the ready queue
b. Exponential Averaging while a previous process is of the currently
executing process. A preemptive SJF
a. Simple Averaging: algorithm will preempt the currently
Number of process: n (P1, P2, executing process; whereas a non-pre-
P3, . . . . . . . , Pn) emptive SJF algorithm will allow the
ti: Actual BT ,where i= 1 to n currently running process to finish its CPU
i : Predicted BT burst Preemptive SJF scheduling is
n+1 = (1/n) * ∑ ti , where i=1 to sometimes called shortest-remaining-
n. lime-first scheduling.
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
Preemptive SJF schedule is as depicted in (Practically implementable since it
the following Gantt chart: doesn’t depend on Burst Time).
Mode : Preemptive
P1 P2 P4 P1 P3 Data Structure: Queue
0 1 5 10 17 26 Tie Breaker : Process Number
Process P1 is started at time 0, since it is No Starvation.
the only process in the queue. Process P2 Minimum Response Time.
arrives at time 1. The remaining time for
process P1 (7 milliseconds) is larger than What would happen if Time Quantum is
the time required by process P2 (4 very small and TQ is very large?
milliseconds), so process P1 is preempted, Small TQ: More Context Switching.
and process P2 is scheduled. Therefore, reduced throughput.
The average waiting time Large TQ: Starvation for some process.
= 9+0+15+2/4
=26/4 Example:
1. TQ=4 units
=6.5milliseconds. Pno AT BT CT TAT WT
A non-pre-emptive SJF scheduling would 1 0 4 4 4 0
result in an average waiting time of 7.75 2 1 5 19 18 13
milliseconds. 3 2 2 10 8 6
1.7.3 Shortest Remaining Time First 4 3 1 11 8 7
Scheduling (SRTF) 5 4 6 21 17 13
Criteria: Burst Time 6 5 3 18 12 9
Mode : Preemptive
Data Structure: Min Heap P1 P2 P3 P4 P5 P6 P2 P5
Tie Breaker : LEVEL 0: Arrival Time 0 4 8 10 11 15 18 19 21
LEVEL 1: Process No.
A preemptive version of shortest job first As you increase TQ, Context Switching
is shortest remaining time next. With decreases, Response Time increases.
this algorithm, the scheduler always As you decrease TQ, Context Switching
chooses the process whose remaining run increases, Response Time decreases.
time is the shortest. Again here, the run If TQ = infinity, Round Robin reduces to
time has to be known in advance. When a FCFS.
new job arrives, its total time is compared
to the current process' remaining time. If Example 2:
the new job needs less time to finish than Let Time Quantum = ‘q’ units.
the current process, the current process is Context Switching Time =‘s’ units.
suspended and the new job started. This Each process is guaranteed to get turn
scheme allows new short jobs to get good at CPU for‘t’ seconds.
service. Number of processes = ‘n’
What must be the value of ‘q’ so that ‘s’
1.7.4 Round Robin Scheduling is reduced?
Criteria: Time Quantum and Arrival
Time Solution:
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
3 6 5 20 14 9 15-
6=9
4 8 2 15 7 5 13-
8=5
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
1.7.7 Pre-emptive Priority Scheduling foreground processes may have priority
Criteria: Priority (externally defined) over back ground
Mode : Preemptive processes.
Tie Breaker: Level – 0 Arrival Time A multilevel queue-scheduling algorithm
Level—1 Process Number. partitions the ready queue into several
First prefer lower arrival time, separate queues (Figure below).
then prefer lower process number.
PRI AT BT CT TA W RT
Pno ORI T T
TY
1 2 0 4 25 25 21 0
2 4 1 2 22 21 19 0
3 6 2 3 21 19 16 0
4 10 3 5 12 9 4 0
5 8 4 1 19 15 14 14
6 12 5 4 9 4 0 0
7 9 6 6 18 12 6 6
Advantages: Different Scheduling
algorithms can be applied for different
Gantt chart: queues.
P P P P P P P P P P P P Disadvantages: As long as there is a single
1 2 3 4 4 6 4 7 5 3 2 1 process in highest queue, no process from
0 1 2 3 4 5 9 12 18 19 21 22 25 lower queues will get a chance.
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
a) The number of queues (b) All process are CPU Bound: CPU
b) The scheduling algorithm for each Utilization increases. Throughput
queue decreases. Here, throughput
c) The method used to determine when to decreases because number of
demote a process to a lower-priority process executed per second
queue decreases.
d) The method used to determine which
queue a process will enter when that Throughput = Number of process
process needs service. The definition of completed (n)/Time to complete
a multilevel feedback queue scheduler (t)
makes it the most general CPU STARVATION: (IN ALL
scheduling algorithm. ALGORITHMS)
FCFS NP- SRT RR PRIORI PRIOR LJF- LRTF HRR MULTILEV MULTILEVEL
SJF F TY-NP ITY-P NP N EL QUEUE FEEDBACK
QUEUE
NO YES YES NO YES YES YES YES NO YES NO
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
drives. Suppose that there are three after that process has completed its
processes, each holding one of these task.
tape drives. If each process now 4) Circular waits
requests another tape drive, the three There must exist a set {P0, P1, ….Pn} of
processes will be in a deadlock state. waiting such that P0 is waiting for a
Each is waiting for the event "tape drive resource that is held by P1, P1 is waiting
is released," which can be caused only by for a resource that is held by P2, ……,Pn-
one of the other waiting processes. This 1 is waiting for a resource that is held
example illustrates a deadlock by Pn, and Pn is waiting for a resource
involving processes competing for the that is held by P0.
same resource type. Deadlocks may All these four conditions must hold for
also involve different resource types. deadlock to occur.
Consider a system with one printer and
one tape drive. Suppose that process Pi 1.8.3 Methods for Handling Deadlocks
is holding the tape drive and process Pj There are three different methods for
is holding the printer. If Pi requests the dealing with the deadlock problem.
printer and Pj requests the tape drive, a a) We can use a protocol to ensure that
deadlock occurs. the system will never enter a deadlock
state.
1.8.2 DEADLOCK CHARACTERIZATION b) We can allow the system to enter a
Necessary Conditions deadlock state and then recover.
A deadlock situation can arise if the c) We can ignore the problem all together,
following four conditions hold and pretend that deadlocks never
simultaneously in a system: occur in the system. This solution is the
one used by most operating systems,
a) Mutual exclusion including UNIX.
At least one resource must be held in a To ensure that deadlocks never occur,
non-sharable mode; i.e., only one the system can use either deadlock
process at a time can use the resource. prevention or a deadlock-avoidance
If another process requests that scheme.
resource, the requesting process must
be delayed until the resource has been 1.8.4 Deadlock Prevention
released. For a deadlock to occur, each of the four
necessary conditions must hold. By
b) Hold and wait ensuring that at least of these conditions
There must exist a process that is cannot hold, we can prevent the
holding at least one resource and is occurrence of a deadlock. Now consider
waiting to acquire additional resources each of the four necessary conditions
that are currently being held by other separately.
processes.
1.8.4.1 Mutual Exclusion
c) No preemption The mutual –exclusion must hold for non-
Resources cannot be preempted; i.e., a sharable resources. For example, a printer
resource can be released only cannot be simultaneously shared by
voluntarily by the process holding it, several processes. Sharable resources, on
the other hand, do not require mutually
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
exclusive access, and thus cannot be Consider a system Allocat Requ Availa
involved in a deadlock. Read-only files are with five ion est ble
a good example of a sharable resource. If processes P0
several Processes attempt to open a read- through P4 and
only file at the same time, they can be three resource
granted simultaneous access to the file. type A,B,C.
Resource type A 7
1.8.4.2 Hold and Wait instance, A B C A B C A B C
To ensure that the hold-and –wait resources. 0 1 0 0 0 0 0 0
condition occurs in the system, we must Suppose that, at 2 0 0 2 0
guarantee that, whether a process time T0, we have 3 0 3 0 0
requests a resource, it does not hold any the following 2 1 1 1 0
other resources. One protocol that can be resource 0 0 2 0 0
used requires each process to request and allocation stare:
be allocated all its resources before it P∩
being execution. We can implement this P1
provision by requiring that system calls P2
requesting resources for a process precede P3
all other system calls. P4
1.8.4.3 No Preemption
To ensure that this condition does hold, we We claim that the system is not in a
can use following protocol. If a process deadlocked state. Indeed, if we execute our
that is hold some resources requests algorithm, we will find that the sequence <
another resource that cannot be P0, P2 P3, P1 P4 > will result in Finish[i] = true
immediately allocated to it (that is, the for all i. Now suppose that process P2
process must wait), then all resources makes one additional request for an
currently being held are preempted. That instance of type C. The Request matrix is
is, these resources are implicitly released. modified as follows:
The preempted resources are added to the Request
list of resources for which the process is
P0 0 0 2
waiting. The process will be restarted only
P1 2 0 2
when it can regain its old resources, as well
P2 0 0 1
as the new ones that it is requesting.
P3 1 0 0
P4 0 0 2
An alternative protocol allows a process to
request resources only when the process
has none. A process may request some We can say that the system is now
resources and use them. Before it can deadlocked. Although we can reclaim the
request any additional consider a system resources held by process P0 the number of
with five processes Po through P4 and three available resources is not sufficient to fulfill
resources that it is currently allocated. the requests of the other processes. Thus, a
deadlock exists, consisting of processes P1,
P2, P3, and P4.
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
1.8.4.4 Detection-Algorithm Usage a) Selecting a victim
When should we invoke the detection Which resources and which processes
algorithm? The answer depends on two are to be preempted? As is process
factors: termination, we must determine the
order of preemption to minimize cost.
i) How often is a deadlock likely to occur? Cost factors may include such
ii) How many processes will be affected by parameters as the number of resources
deadlock when it happens? a deadlock process is holding, and the
amount of time a deadlock process has
1.8.5 Recovery from Deadlock thus far consumed during its
execution.
1.8.5.1 Process Termination
To eliminate deadlocks by aborting a b) Rollback
process, we use one of two methods. In both If we preempt a resource from a
methods, the system reclaims all resources process, what should be done with that
allocated to the terminated processes. process? Clearly, it cannot continue
with its normal execution; it is missing
some needed resource. We must roll
i) Abort all deadlocked processes back the process to some safe state,
This method clearly will break the and restart it from that state.
deadlock cycle, but at a great expense, Since, in general, it is difficult to
since these processes may have determine what a safe state is, the
computed for a long time, and the simplest solution is a total rollback:
results of these partial computations Abort the process and then restart it.
must be discarded, and probably However, it is more effective to roll
must be recomputed later. back the process only as far as
necessary to break the deadlock.
ii) Abort one process at a time until
the deadlock cycle is eliminated c) Starvation
This method incurs considerable How do we ensure that starvation will
overhead, since, after each process is not occur? That is, how can we
aborted, a deadlock-detection guarantee that resources will not
algorithm must be invoked to always be preempted from the same
determine whether any processes are process? In a system where victim
still deadlocked. selection is based primarily on cost
factors, it may happen that its
1.8.5.2 Resource Preemptions designated task, a starvation situation
To eliminate deadlock using resource that needs to be dealt with in any
preemption, we successively preempt practical system. Clearly, we must
some resources and give these resources ensure that a process can be picked as
to other processes until the deadlock cycle a victim only a (small) finite number of
is broken. If preemption is required to deal times. The most common solution is to
with deadlocks, then there issues need to include the number of rollbacks in the
be addressed: cost factor.
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
1.9 THERE ARE THREE TYPES OF PROBLEMS WITH IPC:
Inconsistency.
Loss of data.
Deadlock.
STEPS: STEPS:
Produce Item. 1. Check condition to
Check condition to wait (sleep) or polling.
Wait (sleep) or polling. 2. Increment OUT.
BUFFER [IN] = Item. 3. Decrement COUNT.
Increment IN. 4. Consume Item.
Increment COUNT.
Produce_item(temp);Step 1 While(COUNT==0);Step 1
While(COUNT==N);Step 2 Itemc = BUFFER[OUT];
BUFFER[IN] = Item;Step 3 OUT = (OUT+1) %N; Step 2
IN = (IN+1) %N; Step 4 COUNT = COUNT – 1; Step 3
COUNT = COUNT + 1; Step 5 Consume_item(itemc); Step 4
} }
} }
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
Problems: Load Ri, M [IN] //Read IN.
Store SD [Ri],”Filename” //Make an
Step 5 or producer () and Step 3 of entry.
consumer() are points of focus here. Increment Ri.
Suppose COUNT=4. Store M[IN],Ri //Update IN
Producer puts an item on buffer, but Printer & Spooler suffers from Loss of
before incrementing COUNT, control goes data problem.
to consumer.
Consumer consumes one item, but before Deadlock: Possibility for processes
decrementing COUNT, control goes to entering deadlock is an IPC problem.
producer. Race Condition: When the final value of a
Now, one time increment and one time shared variable depends on the order of
decrement is to be executed. execution of two processes, it is said to be
COUNT++ : 4+1 =5 a race condition.
COUNT--:4-1=3 Example: COUNT variable in Producer
COUNT-- : 5-1 =4 Consumer IN variable in Printer & Spooler
COUNT++: 3+1=4
But increment & decrements are not There are three conditions to achieve
executed in one machine instruction, they Synchronization:
require three machine instructions which Mutual Exclusion: No two processes
are interleaved. are present in the critical section at
the same time.
COUNT++ Progress: No process running outside
R1 = COUNT Critical Section should restrict
R1 = R1 +1 another process to go inside, if
COUNT = R1 Critical section is free.
COUNT— Bounded/Busy waiting: No process has
R2=COUNT to wait forever to enter critical
R2 = R2 – 1 section. If this condition isn’t
COUNT = R2 satisfied, there is a possibility of
This injects inconsistency in the variable starvation.
states.
1.12 SOLUTIONS TO
Producer Consumer suffers with IPC/SYNCHRONIZATION
Inconsistency. This is not compulsory that
it must suffer from all problems. If anyone Some solutions comply with all three
is present, it is an IPC problem conditions, some fail at one or two
conditions.
1.11 PRINTER AND SPOOLER PROBLEM
1.12.1 Software Solutions:
Shared Resource are IN variable and 1.12.1.1 Lock Variable
Spooler Directory. Mutual Exclusion fails.
IN: It is used by processes. Progress is achieved.
Bounded/Busy wait fails here.
Algorithmic steps to enter a file in Spooler Entry Section:
Directory: Load Ri,M[lock]
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
Cmp Ri,#0 Mutual Exclusion is
Jnz Step 1 satisfied.
Store M[lock],#1 Progress is achieved.
Critical Section Bounded/Busy wait is
Store M[lock],#0 satisfied.
Lock: 0 Critical Section is free.
Lock: 1 Critical Section is busy. 1.12.1.4 Semaphores
Semaphore is a system variable used to
1.12.1.2 Strict Alteration & Decker’s achieve mutual exclusion.
Algorithm The following operations can be
Only alternate sequence is allowed. performed on semaphores:
This approach is restricted to two Down() or P() or Wait()
processes only. Up() or V() or Release() or Signal()
P0 P1
While (true) { While (true) { Semaphores are of two types:
Non_CS () Non_CS () Counting Semaphore:
While (turn! = 0); While (turn! = 1);
CS (); CS (); Down () operations is performed
Turn=1; turn=0; as follows:
} } Value_of_semaphore--;
If(value_of_semaphore<0)
Mutual Exclusion is satisfied. Block process,put it in suspended list.
Progress is not achieved.
Bounded/Busy wait is satisfied. Up () operations is performed as follows:
Value = Value +1
1.12.1.3 Peterson’s Solution If (value <= 0 )
#define N 2 Select a process from suspended list &
#define TRUE 1 wake up.
#define FALSE 0
Int turn; Down () is successful if process is not
//Shared Variable blocked, else unsuccessful.
Int interested [N]; Up () is always successful.
//Shared Variable
II. Binary Semaphore:
Void enter_region(int process){
Int other = 1- process; Down()
Interested[process] = TRUE; {
Turn = process; If(value == 1)
While(turn ==process && S=0
interested[other]==TRUE); Else if(value == 0)
} Block process, put
it in suspended list.
Void leave_region(int process){ }
Interested [process] = FALSE; Up()
} {
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
If list is non empty, select a COUNT in previous approach is
process & wake up. replaced here by Mutex, Empty,
Else if S==0 or S==1, make Full.
it 1.
} 1.14 Solution to READER & WRITER
Down() is successful when PROBLEM
S=1,unsuccessful when S=0.
Up() is always successful. Multiple readers are allowed to read
database.
1.13 Solution to PRODUCER Writer needs exclusive access i.e. no
CONSUMER PROBLEM using reader, no writer.
SEMAPHORES For writer who is waiting outside, we
have to maintain rc(reader count).
Producer As one reader does rc++,other
reader reads old value, so
While(true){ inconsistency may occur.
Produce(itemp); Therefore, rc requires a semaphore
Down(empty); (mutex).
Down(mutex); Semaphore db: For access to database.
Consumer Down(mutex)
//increment rc & down db
While(true){ Rc = rc+1
// if 1st reader
Down(full); If(rc==1)
Down(mutex); Down(db);
Up(mutex);
Itemc = BUFFER[OUT]; Read();
OUT = (OUT+1) %N; Down(mutex);
Up(mutex); Rc = rc -1;
Up(empty); If(rc==0)
} Up(db);
Up(mutex);
Note: }
Empty + Full = N (Always)
Mutex: Binary Semaphore to access Void writer(){
buffer in mutually exclusive //decrement rc & up(db)
manner. While(TRUE){
Empty: Counting Semaphore //if last reader
Full : Counting Semaphore Down(db)
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
Write();
Up(db);
}
}
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
GATE QUESTIONS
Q.2 A CPU has two modes- privileged Q.6 Consider Peterson's algorithm for
and non- privileged. In order to mutual exclusion between two
change the mode from privileged concurrent processes I and j. The
to non- privileged program executed by process is
a) a hardware interrupt is needed shown below.
b) a software interrupt is needed repeat flag [i] = true;
c) a privileged instruction (which turn = j ;
does not generate an interrupt) while (P) do no-op;
is needed. Enter critical section, perform
d) a non-privileged instruction actions, then exit critical section
(which does not generate an Flag [i] = false;
interrupt) is needed. Perform other non-critical section
[GATE -2001] actions. Until false;
For the program to guarantee
Q.3 Consider a set of n tasks with mutual exclusion, the predicate P
known runtimes r1 , r2 ,, rn to be in the while loop should be
run on a uni-processor machine. a) flag [j]= true and turn = i
Which of the following processor b) flag [j] = true and turn = j
scheduling algorithms will result c) flag [i]= true and turn = j
in the maximum throughput? d) flag [i] = true and turn = i
a) Round Robin [GATE-2001]
b) Shortest Job First
c) Highest Response Ratio Next Q.7 Which combination of the
d) First Come First Served following features will suffice to
[GATE-2001] characterize an operating system
as a multi-programmed operating
Q.4 Which of the following scheduling system?
algorithms is non-pre-emptive? A) More than one program may be
a) Round Robin loaded into main memory at
b) First-in-First out the same time for execution.
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
B) If a program waits for certain Common Data for Questions 10 and 11:
events such as IN/OUT, Suppose we want to synchronize two
another program is concurrent processes P and Q using
immediately scheduled for binary semaphores S and T. The code for
execution. the processes P and Q is shown below.
C) If the execution of a program Process P
terminates, another program is While (1)
immediately scheduled for {
execution. W:
a) (A) only b) (A) and (B) Print ‘0’;
c)(A) and (C) d) (A)(B)and(C) Print ‘0’;
[GATE-2002] X:
}
Q.8 Draw the process state transition Process Q
diagram of an OS in which (i) each while (1)
process is in one of the five states: {
created, ready, running, blocked Y:
(i.e. sleep or wait). Or terminated, print ‘1’
and (ii) only non- preemptive print ‘1’
scheduling is used by the OS Label Z:
the transitions appropriately. Synchronization statements can be
[GATE -2002] inserted only at points W, X, Y and Z.
Q.9 A uni-processor computer system Q.10 Which of the following will always
only has two processes, both of lead to an output starting with?
which alternate 10 ms CPU bursts 01100110011'?
with 90 ms IN/OUT bursts. Both a) P(S) at W, V(S) at X, P(T) at Y,
the processes were created at V(T) at Z, S and T initially 1
nearly the same time. The I N/OUT b) P(S) at W, V(T) at X, P(T) at Y,
of both processes can proceed in V(S) at Z, S initially 1 , and T
parallel. Which of the following initially 0
scheduling strategies will result in c) P(S) at W, V(T) at X, P(T) at Y,
the least CPU utilization (over a V(S) at Z, S and T initially 1
long period of time) for this d) P(S) at W, V(T) at X, P(T) at Y,
system? V(T) at Z, S initially 1, and T
a) First come first served initially 0
scheduling [GATE-2003]
b) Shortest remaining time first
scheduling Q.11 Which of the following will ensure
c) Static priority scheduling with that the output string never
different priorities for the two contains a substring of the form
processes 01𝑛 0 or 10𝑛 0 where n is odd?
d) Round robin scheduling with a a) P(S) at W, V(S) at X, P(T) at Y,
time quantum of 5 ms V(T) at Z, S and T initially 1
[GATE-2003] b) P(S) at W, V(T) at X, P(T) at Y,
V(S) at Z, S and T initially 1,
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
c) P(S) at W, V(S) at X, P(S) at Y, and the CPU Burst times given in
V(S) at Z, S initially 1 millisecond
d) P(S) at W, V(T) at X, P(S) at Y, Arrival Burst
Process
P(T) at Z, S and initially 1 Time Time
[GATE-2003] P1 0 5
P2 1 3
Q.12 Consider the following statements P3 2 3
with respect to user-level threads P4 4 1
and kernel-supported threads: What is the average turn-around
1. Context switch is faster with time for these processes with the
kernel-supported threads. pre-emptive shortest Remaining
2. for user-level threads, a system Processing Time First (SRPTF)
call can block the entire algorithm?
process. a) 5.50 b) 5.75
3. Kernel supported threads can c) 6.00 d) 6.25
be scheduled independently. [GATE-2004]
4. User level threads are
transparent to the kernel. Q.16 Consider two processes P1 and P2
Which of the above statements are accessing the shared variables X
true? and Y protected two binary
a) 2, 3 and 4 b) 2 and 3 semaphores Sx and Sy respectively,
c) 1 and 3 d) 1 and 2 both initialized to 1. P and V
[GATE-2004] denote the usual semaphore
operators, where P decrements the
Q.13 Which one of the following is NOT semaphore value, and V
shared by the threads of the same increments the semaphore value.
process? The pseudo-code of P1 and P2 is as
a) Stack follows:
b) Address Space
c) File Descriptor Table P1 P2
d) Message Queue While true While true
[GATE -2004] do{ do{
L1: ……… L3: ………
Q.14 A process executes the following
L2: ……… L4: ………
code
X=X+1; Y=Y+1;
for (i=0 ; i < n ; i + +)
Y=Y-1; X=Y-1;
fork ( );
The number of new processes V(Sx); V(Sy);
created is V(Sy); V(Sx);
a) n b) 2n - 1 In order to avoid deadlock, the
c) 2 n d) 2n+1 – 1 correct operators at L1, L2, L3 and
[GATE-2004] L4 are respectively
a) P(Sy ),P(Sx ),P(Sx ),P(Sy )
Q.15 Consider the following set of b) P(Sx ),P(Sy ),P(Sy ),P(Sx )
processes with the arrival times c) P(Sx ),P(Sx ),P(Sy ),P(Sy )
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
CPU Arrival
d) P(Sx ),P(Sy ),P(Sx ),P(Sy )
Proces time time
Priority
s require (hh:mm:ss
[GATE-2004] d )
10
Q.17 Suppose n processes, P1 , P2 ...., Pn P1 (highest 20 sec 0:00:05
share m identical resource units, )
which can be reserved and P2 9 10 sec 0:00:03
released one at a time. The 8
maximum resource requirement of P3 15 sec 0:00:00
(lowest)
process Pi is Si where Si>0. Which
one of the following is a sufficient
condition for ensuring that We have a choice of preemptive or
deadlock does not occur? non-preemptive scheduling. In
a) i,si m
preemptive scheduling, a late
b) i,si n
n
arriving higher priority process
c) si (m n) can preempt a currently running
i 1 process with lower priority. In
n
d) si (m*n) non-preemptive scheduling, a late–
i 1 arriving higher priority process
[GATE-2005] must wait for the currently
executing process to complete
Q.18 A user level process in Unix traps
the signal sent on a Ctrl- C input, before it can be scheduled on the
and has a signal handling routine processor. What are the
that saves appropriate files before turnaround times (time from
terminating the process. When a arrival till completion) of P2 using
Ctrl – C input is given to this preemptive and no preemptive
process, what is the mode in which scheduling respectively?
the signal handling routine
executes? a) 30 sec, 30 sec
a) kernel mode b) 30 sec 10sec
b) super user mode c) 42 sec 42 sec
c) privileged mode d) 30sec 42 sec
d) user mode [GATE -2005]
[GATE -2005]
Q.20 Two shared resources R 1 and R 2
Q.19 We wish to schedule three
are used by processes P1 and P2 .
processes P1, P2 and P3 on a
uniprocessor system. The Each process has a certain priority
priorities, CPU time requirements for accessing each resource Let Tij
and arrival times of the processes denote the priority of Pi for
are as shown below. accessing Rj A process Pi can
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
snatch a resource R k from process Q.23 The process state transition
Pj if Tik is greater than Tjk diagram of an operating system is
as give below.
Given the following:
I. T11 T21 II. T12 T22
III. T11 T21 IV. T12 T22
Which of the following conditions
ensures that P1 and P2 can never
deadlock?
a) (I) and (IV) b) (II) and (III)
c) (I) and (II) d)None
[GATE -2005]
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
a) 0% b) 10.6% access to the memory location x.
c) 30.0% d) 89.4% Consider the following
[GATE-2006] implementation of P and V
functions on a binary semaphore S.
Q.25 Consider three processes (process void P ( binary_semaphore *s) {
id 0, 1, 2 respectively) with unsigned y;
compute time bursts 2, 4 and 8 unsigned *x = & (S -> value);
time units. All processes arrive at do {
time zero. Consider the Longest -- fetch-and-set x , y;
Remaining Time First (LRTF) } while (y);
scheduling algorithm. In LRTF ties void V (binary_semaphore *S) {
are broken by giving priority to the s -> value = 0 ;
process with the lowest process id. }
The average turnaround time is Which one of the following is true?
a) 13 unit b) 14 unit a) The implementation may not
c) 15 unit d) 16 unit work, if context switching is
[GATE-2006] disabled in P
b) Instead of using fetch-and-set,
Q.26 Consider the following, snapshot a pair of normal load/store can
of a system running n processes. be used
Process i is holding Xi instances of c) The implementation of V is
a resource R, l≤i≤n. Currently, all wrong
instances of R are occupied. d) The code does not implement a
Further, for all i process i has binary semaphore
placed a request for an additional [GATE-2006]
Yi instances while holding
the Xi instances it already has. Statements for Linked Answer
There are exactly two processes p Questions 28 and 29
and q such that Yp=Yq=0. Which Barrier, is a synchronization construct
one of the following can serve as a where a set of processes synchronizes
necessary condition to guarantee globally, i.e., each process in the set
that the system is not approaching arrives at the barrier and waits for all
a deadlock? others to arrive and then all processes
a) min (Xp , Xq)<MAX(Yk) where leave the Barrier. Let the number of
k<> p and k<> q. processes in the set be 3 and S be a binary
b) Xp+Xq≥Min(Yk) where k<>p & semaphore with the usual P and V
k<>q. functions.
c) max(Xp , Xq) > 1 Consider the following C implementation
d) min (Xp , Xq) > 1 of a barrier with line numbers shown on
[GATE-2006] left :
void Barrier (void) {
Q.27 The atomic fetch-and-set x, y 1 : p(s) ;
instruction unconditionally sets 2 : process_arrived ++,
the memory location x to 1 and 3 : V(S) ;
fetches the old value of x in y 4 : while (process_arrived != 3);
without allowing any intervening 5 : P(S)
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
6 : Process_left++ ; [GATE-2006]
7 : if (process_left ==3) ; {
8 : process_arrived = 0; Q.30 Two processes P1 and P2, need to
9 : process_left = 0; access a critical section of code.
10 : } Consider the following
11 : V(S) ; synchronization construct used by
} the processes:
The variables process arrived and process /*P1 */
left are shared among all processes and While (true) {
are initialized to zero. In a concurrent Wants 1 = true ;
program all the three processes call the While
barrier function when they need to (wants 2 == true) ;
synchronize globally. /* Critical
Section */
Q.28 The above implementation of Wants1 = false ;
barrier is incorrect. Which one of } (/* Remainder section)
the following is true? /* P2 */
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
List I List II
P. Gang 1. Guaranteed a) 5 b) 15
Scheduling scheduling c) 40 d) 55
Q. Rate 2. Real-time [GATE-2007]
Monotonic Scheduling
Scheduling Q.34 A single processor system has
R. Fair share 3. Thread three resources types X, Y and Z,
Scheduling Scheduling which are shared by three
processes. There are .5 units of
Codes each resources type. Consider the
a) P-3, Q-2, R-1 following scenario, where the
b) P-1, Q-2, R-3 column alloc denotes the number
c) P-2, Q-3, R-1 of units of each resource type
d) P-1, Q-3, R-2 allocated to each process, and the
[GATE-2007] column request denotes the
number of units of each resource
Q.32 Consider the following statements type requested by a process in
about user level threads and order to complete execution.
kernel level threads: Which one of Which of these processes will
the following statements is false? finish last?
a) Context switch time is longer for Alloc request
kernel level threads than for X Y Z X Y Z
user level threads P0 1 2 1 1 0 3
b) User level threads do not need P1 2 0 1 0 1 2
any hardware support P2 2 2 1 1 2 0
c) Related kernel level threads can a) P0
be scheduled on different b) P1
processors in a multi-processor c) P2
system d) None of the above, since is in a
d) Blocking one kernel level thread deadlock
Blacks all related threads [GATE-2007]
[GATE-2007]
Q.35 Which of the following is not true
Q.33 An operating system uses Shortest of dead lock prevention and
Remaining Time First (SRTF) deadlock avoidance schemes?
process scheduling algorithm. a) In deadlock prevention, the
Consider the arrival times and request for resources is always
execution times for the following granted, if the resulting state is
safe
Execution Arrival b) In deadlock avoidance, the
Process
Time Time request for resources is always
P1 20 0 granted, if the resulting state is
P2 25 15 safe
P3 10 30 c) Deadlock avoidance is less
P4 15 45 restrictive than deadlock
Which is the total waiting time for prevention
process P2?
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission
d) Deadlock avoidance requires The total number of child
knowledge of resource processes created is
requirements a priori a) n b) 2n 1
[GATE-2008] c) 2n d) 2n 1 1
[GATE -2008]
Q.36 If the time–slice used in the
round– robin scheduling policy is Q.39 The P and V operations on
more that the maximum time counting semaphores, where s is a
required to executed any process, counting semaphore, are defined
then the policy will as follows
a) degenerate to shortest job first P(s) : s = s - 1;
b)degenerate to priority If s < 0 then wait;
scheduling V(s) : s = s + 1;
c) degenerate to first come first if s <= 0 then wake up a process
serve waiting on s;
d) None of the above Assume that Pb and Vb the wait and
[GATE -2008] signal operations on binary
semaphores are provided. Two
Q.37 Which of the following statements binary semaphores Xb and Yb are
about synchronous and used to implement the semaphore
asynchronous IN/OUT is not true? operations P(s) and V(s) as follows
a) An ISR is invoked on P(S) : Pb, (Xb);
completion of IN/OUT in S = s-1 ;
synchronous IN/OUT but not in If (s < 0) (
asynchronous IN/OUT Vb (Xb) ;
b) In both synchronous and Vb (Yb) ;
asynchronous IN/OUT, an ISR. }
(Interrupt Service Routine) is Else Vb (Xb) ;
invoked after completion of the V(S) : Pb (Yb) ;
IN/OUT S=s+1;
c) A process making a synchronous If (s <= 0) Vb (Yb) ;
IN/OUT call waits until IN/OUT Vb (Xb) ;
is complete, but a process The initial values of Xb, and Yb are
making an asynchronous respectively
IN/OUT call does not wait for a) 0 and 0 b) 0 and 1
completion of the INI/OLT c) 1 and 0 d) 1 and 1
d) in the case of synchronous [GATE-2008]
IN/OUT, the process waiting
for the completion of IN/OUT is Q.40 In the following process state
woken up by the ISR that is transition diagram for a
invoked after the completion of uniprocessor system, assume that
I IN/OUT there are always some processes
[GATE-2008] in the ready state.
© Copyright Reserved by Gateflix.in No part of this material should be copied or reproduced without permission