Memory Management Processor Management Device Management File Management Security
Memory Management Processor Management Device Management File Management Security
Memory Management Processor Management Device Management File Management Security
Definition
An operating system is a program that acts as an interface between the
user and the computer hardware and controls the execution of all kinds of
programs.
Memory Management
Processor Management
Device Management
File Management
Security
Control over system performance
Job accounting
Memory Management
Memory management refers to management of Primary Memory or Main
Memory. Main memory is a large array of words or bytes where each word
or byte has its own address.
Main memory provides a fast storage that can be accessed directly by the
CPU. For a program to be executed, it must in the main memory. An
Operating System does the following activities for memory management −
Keeps tracks of primary memory, i.e., what part of it are in use by whom, what
part are not in use.
In multiprogramming, the OS decides which process will get memory when and
how much.
Processor Management
In multiprogramming environment, the OS decides which process gets the
processor when and for how much time. This function is called process
scheduling. An Operating System does the following activities for
processor management −
Keeps tracks of processor and status of process. The program responsible for
this task is known as traffic controller.
Device Management
An Operating System manages device communication via their respective
drivers. It does the following activities for device management −
Keeps tracks of all devices. Program responsible for this task is known as
the I/O controller.
Decides which process gets the device when and for how much time.
De-allocates devices.
File Management
A file system is normally organized into directories for easy navigation and
usage. These directories may contain files and other directions.
Keeps track of information, location, uses, status etc. The collective facilities are
often known as file system.
Job accounting − Keeping track of time and resources used by various jobs
and users.
Error detecting aids − Production of dumps, traces, error messages, and other
debugging and error detecting aids.
TYPES OF OS :
Operating systems are there from the very first computer generation and
they keep evolving with time. In this chapter, we will discuss some of the
important types of operating systems which are most commonly used.
CPU is often idle, because the speed of the mechanical I/O devices is slower than
the CPU.
Multiple jobs are executed by the CPU by switching between them, but the
switches occur so frequently. Thus, the user can receive an immediate
response. For example, in a transaction processing, the processor executes
each user program in a short burst or quantum of computation. That is,
if nusers are present, then each user can get a time quantum. When the
user submits the command, the response time is in few seconds at most.
Problem of reliability.
Batch processing suffers from starvation. If there are five jobs J1, J2, J3, J4, J4 and J5
present in the batch. If the execution time of J1 is very high then other four jobs will never
be going to get executed or they will have to wait for a very high time. Hence the other
processes get starved.
2. Not Interactive
Batch Processing is not suitable for the jobs which are dependent on the user's input. If a
job requires the input of two numbers from the console then it will never be going to get it
in the batch processing scenario since the user is not present at the time of execution.
In multiprogramming environment, for the time a process does its I/O, The CPU can start
the execution of other processes. Therefore, multiprogramming improves the efficiency of
the system.
next →← prev
Process States
State Diagram
The process, from its creation to completion, passes through various states. The
minimum number of states is five.
The names of the states are not standardized although the process may be in one of the
following states during execution.
1. New
A program which is going to be picked up by the OS into the main memory is called a
new process.
2. Ready
Whenever a process is created, it directly enters in the ready state, in which, it waits for
the CPU to be assigned. The OS picks the new processes from the secondary memory
and put all of them in the main memory.
The processes which are ready for the execution and reside in the main memory are
called ready state processes. There can be many processes present in the ready state.
3. Running
One of the processes from the ready state will be chosen by the OS depending upon the
scheduling algorithm. Hence, if we have only one CPU in our system, the number of
running processes for a particular time will always be one. If we have n processors in the
system then we can have n processes running simultaneously.
4. Block or wait
From the Running state, a process can make the transition to the block or wait state
depending upon the scheduling algorithm or the intrinsic behavior of the process.
When a process waits for a certain resource to be assigned or for the input from the user
then the OS move this process to the block or wait state and assigns the CPU to the
other processes.
5. Completion or termination
When a process finishes its execution, it comes in the termination state. All the context
of the process (Process Control Block) will also be deleted the process will be terminated
by the Operating system.
6. Suspend ready
A process in the ready state, which is moved to secondary memory from the main
memory due to lack of the resources (mainly primary memory) is called in the suspend
ready state.
If the main memory is full and a higher priority process comes for the execution then the
OS have to make the room for the process in the main memory by throwing the lower
priority process out into the secondary memory. The suspend ready processes remain in
the secondary memory until the main memory gets available.
7. Suspend wait
Instead of removing the process from the ready queue, it's better to remove the blocked
process which is waiting for some resources in the main memory. Since it is already
waiting for some resource to get available hence it is better if it waits in the secondary
memory and make room for the higher priority process. These processes complete their
execution once the main memory gets available and their wait is finished.
Once the process is created, it will be ready and come into the ready queue (main
memory) and will be ready for the execution.
2. Scheduling
Out of the many processes present in the ready queue, the Operating system chooses
one process and start executing it. Selecting the process which is to be executed next, is
known as scheduling.
3. Execution
Once the process is scheduled for the execution, the processor starts executing it.
Process may come to the blocked or wait state during the execution then in that case the
processor starts executing the other processes.
4. Deletion/killing
Once the purpose of the process gets over then the OS will kill the process. The Context of
In the uniprogrammming systems like MS DOS, when a process waits for any I/O
operation to be done, the CPU remains idol. This is an overhead since it wastes the time and
causes the problem of starvation. However, In Multiprogramming systems, the CPU doesn't
remain idle during the waiting time of the Process and it starts executing other processes.
Operating System has to define which process the CPU will be given.
This is a task of the short term scheduler to schedule the CPU for the number of processes
present in the Job Pool. Whenever the running process requests some IO operation then the
short term scheduler saves the current context of the process (also called PCB) and changes
its state from running to waiting. During the time, process is in waiting state; the Short
term scheduler picks another process from the ready queue and assigns the CPU to this
process. This procedure is called context switching.
If most of the running processes change their state from running to waiting then there may
always be a possibility of deadlock in the system. Hence to reduce this overhead, the OS
needs to schedule the jobs to get the optimal utilization of CPU and to avoid the possibility
to deadlock
Another component involved in the CPU scheduling function is the Dispatcher. The
dispatcher is the module that gives control of the CPU to the process selected by the short-
term scheduler. This function involves:
Switching context
Switching to user mode
Jumping to the proper location in the user program to restart that program from where it
left last time.
The dispatcher should be as fast as possible, given that it is invoked during every process
switch. The time taken by the dispatcher to stop one process and start another process is
known as the Dispatch Latency. Dispatch Latency can be explained using the below
figure:
Non-Preemptive Scheduling
Under non-preemptive scheduling, once the CPU has been allocated to a process, the
process keeps the CPU until it releases the CPU either by terminating or by switching to the
waiting state.
This scheduling method is used by the Microsoft Windows 3.1 and by the Apple Macintosh
operating systems.
It is the only method that can be used on certain hardware platforms, because It does not
require the special hardware(for example: a timer) needed for preemptive scheduling.
Preemptive Scheduling
In this type of Scheduling, the tasks are usually assigned with priorities. At times it is
necessary to run a certain task that has a higher priority before another task although it is
running. Therefore, the running task is interrupted for some time and resumed later when
the priority task has finished its execution.
Various Times related to the Process
1. Arrival Time
The time at which the process enters into the ready queue is called the arrival time.
2. Burst Time
The total amount of time required by the CPU to execute the whole process is called the
Burst Time. This does not include the waiting time. It is confusing to calculate the execution
time for a process even before executing it hence the scheduling problems based on the
burst time cannot be implemented in reality.
3. Completion Time
The Time at which the process enters into the completion state or the time at which the
process completes its execution, is called completion time.
4. Turnaround time
The total amount of time spent by the process from its arrival to its completion, is called
Turnaround time.
5. Waiting Time
The Total amount of time for which the process waits for the CPU to be assigned is called
waiting time.
6. Response Time
The difference between the arrival time and the time at which the process first gets the CPU
is called Response Time.
Scheduling Algorithms
To decide which process to execute first and which process to execute last to achieve
maximum CPU utilisation, computer scientists have defined some algorithms, they are:
First Come First Serve, is just like FIFO(First in First out) Queue data structure, where
the data element which is added to the queue first, is the one who leaves the queue
first.
This is used in Batch Systems.
It's easy to understand and implement programmatically, using a Queue data
structure, where a new process enters through the tail of the queue, and the scheduler
selects process from the head of the queue.
A perfect real life example of FCFS scheduling is buying tickets at ticket counter.
irst come first serve (FCFS) scheduling algorithm simply schedules the jobs according to
their arrival time. The job which comes first in the ready queue will get the CPU first. The
lesser the arrival time of the job, the sooner will the job get the CPU. FCFS scheduling may
cause the problem of starvation if the burst time of the first process is the longest among all
the jobs.
Advantages of FCFS
o Simple
o Easy
Disadvantages of FCFS
1. The scheduling method is non preemptive, the process will run to the completion.
2. Due to the non-preemptive nature of the algorithm, the problem of starvation may
occur.
Example
Let's take an example of The FCFS scheduling algorithm. In the Following schedule, there
are 5 processes with process ID P0, P1, P2, P3 and P4. P0 arrives at time 0, P1 at time 1,
P2 at time 2, P3 arrives at time 3 and Process P4 arrives at time 4 in the ready queue. The
processes and their respective Arrival and Burst time are given in the following table.
The Turnaround time and the waiting time are calculated by using the following formula.
The average waiting Time is determined by summing the respective waiting time of all the
processes and divided the sum by the total number of processes.
Process ID Arrival Time Burst Time Completion Time Turn Around Time
0 0 2 2 2
1 1 6 8 7
2 2 4 12 8
3 3 9 21 18
4 4 12 33 29
(Gantt chart)
If a process with very least priority is being executed, more like daily routine
backup process, which takes more time, and all of a sudden some other high priority
process arrives, like interrupt to avoid system crash, the high priority process will
have to wait, and hence in this case, the system will crash, just because of improper
process scheduling.
If the CPU gets the processes of the higher burst time at the front end of the ready queue
then the processes of lower burst time may get blocked which means they may never get
the CPU if the job in the execution has a very high burst time. This is called convoy
effect or starvation.
As you can see in the GANTT chart above, as P1 arrives first, hence it's execution starts
immediately, but just after 1 ms, process P2 arrives with a burst time of 3 ms which is less
than the burst time of P1, hence the process P1(1 ms done, 20 ms left) is preemptied and
process P2 is executed.
As P2 is getting executed, after 1 ms, P3 arrives, but it has a burst time greater than that
of P2, hence execution of P2 continues. But after another millisecond, P4 arrives with a
burst time of 2 ms, as a result P2(2 ms done, 1 ms left) is preemptied and P4 is executed.
After the completion of P4, process P2 is picked up and finishes, then P2 will get executed
and at last P1.
The Pre-emptive SJF is also known as Shortest Remaining Time First, because at any
given point of time, the job with the shortest remaining time is executed first.
Priority Scheduling
In Priority scheduling, there is a priority number assigned to each process. In some
systems, the lower the number, the higher the priority. While, in the others, the higher the
number, the higher will be the priority. The Process with the higher priority among the
available processes is given the CPU. There are two types of priority scheduling algorithm
exists. One is Preemptive priority scheduling while the other is Non Preemptive Priority
scheduling.
The priority number assigned to each of the process may or may not vary. If the priority
number doesn't change itself throughout the process, it is called static priority, while if it
keeps changing itself at the regular intervals, it is called dynamic priority.
Next
Example
In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their priorities, Arrival
Time and burst time are given in the table.
1 2 0 3
2 6 2 5
3 3 1 4
4 5 4 2
5 7 6 9
6 4 5 4
7 10 7 10
We can prepare the Gantt chart according to the Non Preemptive priority scheduling.
The Process P1 arrives at time 0 with the burst time of 3 units and the priority number 2.
Since No other process has arrived till now hence the OS will schedule it immediately.
Meanwhile the execution of P1, two more Processes P2 and P3 are arrived. Since the priority
of P3 is 3 hence the CPU will execute P3 over P2.
Meanwhile the execution of P3, All the processes get available in the ready queue. The
Process with the lowest priority number will be given the priority. Since P6 has priority
number assigned as 4 hence it will be executed just after P3.
After P6, P4 has the least priority number among the available processes; it will get
executed for the whole burst time.
Since all the jobs are available in the ready queue hence All the Jobs will get executed
according to their priorities. If two jobs have similar priority number assigned to them, the
one with the least arrival time will be executed.
From the GANTT Chart prepared, we can determine the completion time of every process.
The turnaround time, waiting time and response time will be determined.
1 2 0 3 3 3 0
2 6 2 5 18 16 11
3 3 1 4 7 6 2
4 5 4 2 13 9 7
5 7 6 9 27 21 12
6 4 5 4 11 6 2
7 10 7 10 37 30 18
The difference between preemptive priority scheduling and non preemptive priority
scheduling is that, in the preemptive priority scheduling, the job which is being executed
can be stopped at the arrival of a higher priority job.
Once all the jobs get available in the ready queue, the algorithm will behave as non-
preemptive priority scheduling, which means the job scheduled will run till the completion
and no preemption will be done.
Example
There are 7 processes P1, P2, P3, P4, P5, P6 and P7 given. Their respective priorities,
Arrival Times and Burst times are given in the table below.
1 2(L) 0 1
2 6 1 7
3 3 2 3
4 5 3 6
5 4 4 5
6 10(H) 5 15
7 9 15 8
The Next process P3 arrives at time unit 2, the priority of P3 is higher to P2. Hence the
execution of P2 will be stopped and P3 will be scheduled on the CPU.
During the execution of P3, three more processes P4, P5 and P6 becomes available. Since,
all these three have the priority lower to the process in execution so PS can't preempt the
process. P3 will complete its execution and then P5 will be scheduled with the priority
highest among the available processes.
Meanwhile the execution of P5, all the processes got available in the ready queue. At this
point, the algorithm will start behaving as Non Preemptive Priority Scheduling. Hence now,
once all the processes get available in the ready queue, the OS just took the process with
the highest priority and execute that process till completion. In this case, P4 will be
scheduled and will be executed till the completion.
Since P4 is completed, the other process with the highest priority available in the ready
queue is P2. Hence P2 will be scheduled next.
P2 is given the CPU till the completion. Since its remaining burst time is 6 units hence P7
will be scheduled after this.
The only remaining process is P6 with the least priority, the Operating System has no choice
unless of executing it. This will be executed at the last.
Multilevel Queue Scheduling
Another class of scheduling algorithms has been created for situations in which processes are
easily classified into different groups.
For example: A common division is made between foreground(or interactive) processes and
background (or batch) processes. These two types of processes have different response-time
requirements, and so might have different scheduling needs. In addition, foreground processes
may have priority over background processes.
A multi-level queue scheduling algorithm partitions the ready queue into several separate
queues. 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 Round Robin algorithm, while the background queue is
scheduled by an FCFS algorithm.
In addition, there must be scheduling among the queues, which is commonly implemented as
fixed-priority preemptive scheduling. For example: The foreground queue may have absolute
priority over the background queue.
1. System Processes
2. Interactive Processes
4. Batch Processes
5. Student Processes
Each queue has absolute priority over lower-priority queues. No process in the batch queue, for
example, could run unless the queues for system processes, interactive processes, and interactive
editing processes were all empty. If an interactive editing process entered the ready queue while
a batch process was running, the batch process will be preempted.
Multilevel Feedback Queue
Scheduling
In a multilevel queue-scheduling algorithm, processes are permanently assigned to a queue on
entry to the system. Processes do not move between queues. This setup has the advantage of low
scheduling overhead, but the disadvantage of being inflexible.
Multilevel feedback queue scheduling, however, allows a process to move between queues. The
idea is to separate processes with different CPU-burst characteristics. If a process uses too much
CPU time, it will be moved to a lower-priority queue. Similarly, a process that waits too long in
a lower-priority queue may be moved to a higher-priority queue. This form of aging prevents
starvation.
An example of a multilevel feedback queue can be seen in the below figure.
The method used to determine which queue a process will enter when that process needs service.
The definition of a multilevel feedback queue scheduler makes it the most general CPU-
scheduling algorithm. It can be configured to match a specific system under design.
Unfortunately, it also requires some means of selecting values for all the parameters to define the
best scheduler. Although a multilevel feedback queue is the most general scheme, it is also
the most complex.
Round Robin Scheduling Algorithm
Round Robin scheduling algorithm is one of the most popular scheduling algorithm which
can actually be implemented in most of the operating systems. This is the preemptive
version of first come first serve scheduling. The Algorithm focuses on Time Sharing. In this
algorithm, every process gets executed in a cyclic way. A certain time slice is defined in
the system which is called time quantum. Each process present in the ready queue is
assigned the CPU for that time quantum, if the execution of the process is completed during
that time then the process will terminate else the process will go back to the ready
queue and waits for the next turn to complete the execution.
Advantages
1. It can be actually implementable in the system because it is not depending on the
burst time.
Disadvantages
1. The higher the time quantum, the higher the response time in the system.
2. The lower the time quantum, the higher the context switching overhead in the
system.
3. Deciding a perfect time quantum is really a very difficult task in the system.
RR Scheduling Example
In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6. Their
arrival time and burst time are given below in the table. The time quantum of the system is
4 units.
1 0 5
2 1 6
3 2 3
4 3 1
5 4 5
6 6 4
According to the algorithm, we have to maintain the ready queue and the Gantt chart. The
structure of both the data structures will be changed after every scheduling.
Ready Queue:
Initially, at time 0, process P1 arrives which will be scheduled for the time slice 4 units.
Hence in the ready queue, there will be only one process P1 at starting with CPU burst time
5 units.
P1
GANTT chart
The P1 will be executed for 4 units first.
Ready Queue
Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in the ready
queue. P1 has not completed yet, it needs another 1 unit of time hence it will also be added
back to the ready queue.
P2 P3 P4 P5 P
6 3 1 5 1
GANTT chart
After P1, P2 will be executed for 4 units of time which is shown in the Gantt chart.
Ready Queue
During the execution of P2, one more process P6 is arrived in the ready queue. Since P2 has
not completed yet hence, P2 will also be added back to the ready queue with the remaining
burst time 2 units.
P3 P4 P5 P1 P6
3 1 5 1 4
GANTT chart
After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time is only 3
seconds.
Ready Queue
Since P3 has been completed, hence it will be terminated and not be added to the ready
queue. The next process will be executed is P4.
P4 P5 P1 P6 P
1 5 1 4 2
GANTT chart
After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is lesser then
the time quantum hence it will be completed.
Ready Queue
The next process in the ready queue is P5 with 5 units of burst time. Since P4 is completed
hence it will not be added back to the queue.
P5 P1 P6 P2
5 1 4 2
GANTT chart
P5 will be executed for the whole time slice because it requires 5 units of burst time which is
higher than the time slice.
Ready Queue
P5 has not been completed yet; it will be added back to the queue with the remaining burst
time of 1 unit.
P1 P6 P2 P5
1 4 2 1
GANTT Chart
The process P1 will be given the next turn to complete its execution. Since it only requires 1
unit of burst time hence it will be completed.
Ready Queue
P1 is completed and will not be added back to the ready queue. The next process P6
requires only 4 units of burst time and it will be executed next.
P6 P2 P5
4 2 1
GANTT chart
P6 will be executed for 4 units of time till completion.
Ready Queue
Since P6 is completed, hence it will not be added again to the queue. There are only two
processes present in the ready queue. The Next process P2 requires only 2 units of time.
P2 P5
2 1
GANTT Chart
P2 will get executed again, since it only requires only 2 units of time hence this will be
completed.
Ready Queue
Now, the only available process in the queue is P5 which requires 1 unit of burst time. Since
the time slice is of 4 units hence it will be completed in the next burst.
P5
GANTT chart
P5 will get executed till completion.
The completion time, Turnaround time and waiting time will be calculated as shown in the
table below.
As, we know,
Process ID Arrival Time Burst Time Completion Time Turn Around Time
1 0 5 17 17
2 1 6 23 22
3 2 3 11 9
4 3 1 12 9
5 4 5 24 20
6 6 4 21 15
A semaphore S is an integer variable that can be accessed only through two standard
operations : wait() and signal().
The wait() operation reduces the value of semaphore by 1 and the signal() operation increases
its value by 1.
wait(S){
while(S<=0); // busy waiting
S--;
}
signal(S){
S++;
}
Problem Statement – We have a buffer of fixed size. A producer can produce an item and can
place in the buffer. A consumer can pick items and can consume them. We need to ensure that
when a producer is placing an item in the buffer, then at the same time consumer should not
consume any item. In this problem, buffer is the critical section.
To solve this problem, we need two counting semaphores – Full and Empty. “Full” keeps track
of number of items in the buffer at any given time and “Empty” keeps track of number of
unoccupied slots.
Initialization of semaphores –
mutex = 1
Full = 0 // Initially, all slots are empty. Thus full slots are 0
Empty = n // All slots are empty initially
Solution for Producer –
do{
//produce an item
wait(empty);
wait(mutex);
//place in buffer
signal(mutex);
signal(full);
}while(true)
When producer produces an item then the value of “empty” is reduced by 1 because one slot
will be filled now. The value of mutex is also reduced to prevent consumer to access the buffer.
Now, the producer has placed the item and thus the value of “full” is increased by 1. The value
of mutex is also increased by 1 beacuse the task of producer has been completed and
consumer can access the buffer.
wait(full);
wait(mutex);
signal(mutex);
signal(empty);
// consumes item
}while(true)
As the consumer is removing an item from buffer, therefore the value of “full” is reduced by 1
and the value is mutex is also reduced so that the producer cannot access the buffer at this
moment. Now, the consumer has consumed the item, thus increasing the value of “empty” by 1.
The value of mutex is also increased so that producer can access the buffer now.
Reader writer