OS
OS
An Operating System (OS) is an interface between a computer user and computer hardware. An operating
system is a software which performs all the basic tasks like file management, memory management,
process management, handling input and output, and controlling peripheral devices such as disk drives and
printers.
Some popular Operating Systems include Linux Operating System, Windows Operating System, VMS,
OS/400, AIX, z/OS, etc.
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.
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.
Allocates the processor (CPU) to a process.
De-allocates processor when a process is no longer required.
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.
Allocates the memory when a process requests it to do so.
De-allocates the memory when a process no longer needs it or has been terminated.
Device Management (I/O 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.
Allocates the device in the efficient way.
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.
An Operating System does the following activities for file management
Keeps track of information, location, uses, status etc. The collective facilities are often
known as file system.
Decides who gets the resources.
Allocates the resources.
De-allocates the resources.
Control over system performance − Recording delays between request for a service and
response from the 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.
Program execution
Operating systems handle many kinds of activities from user programs to system programs like printer
spooler, name servers, file server, etc. Each of these activities is encapsulated as a process.
A process includes the complete execution context (code to execute, data to manipulate, registers, OS
resources in use). Following are the major activities of an operating system with respect to program
management –
I/O Operation
An I/O subsystem comprises of I/O devices and their corresponding driver software. Drivers hide the
peculiarities of specific hardware devices from the users.
An Operating System manages the communication between user and device drivers.
I/O operation means read or write operation with any file or any specific I/O device.
Operating system provides the access to the required I/O device when required.
Error handling
Errors can occur anytime and anywhere. An error may occur in CPU, in I/O devices or in the memory
hardware. Following are the major activities of an operating system with respect to error handling −
The OS constantly checks for possible errors.
The OS takes an appropriate action to ensure correct and consistent computing.
Resource Management
In case of multi-user or multi-tasking environment, resources such as main memory, CPU cycles and files
storage are to be allocated to each user or job. Following are the major activities of an operating system
with respect to resource management −
The OS manages all kinds of resources using schedulers.
CPU scheduling algorithms are used for better utilization of CPU.
Protection
Considering a computer system having multiple users and concurrent execution of multiple processes, the
various processes must be protected from each other's activities.
Protection refers to a mechanism or a way to control the access of programs, processes, or users to the
resources defined by a computer system. Following are the major activities of an operating system with
respect to protection −
The OS ensures that all access to system resources is controlled.
The OS ensures that external I/O devices are protected from invalid access attempts.
The OS provides authentication features for each user by means of passwords.
Advantages
Batch processing takes much of the work of the operator to the computer.
Increased performance as a new job get started as soon as the previous job is finished,
without any manual intervention.
Disadvantages
Difficult to debug program.
A job could enter an infinite loop.
Due to lack of protection scheme, one batch job can affect pending jobs.
Multiprogramming
Sharing the processor, when two or more programs reside in memory at the same time, is referred as
multiprogramming. Multiprogramming assumes a single shared processor. Multiprogramming increases
CPU utilization by organizing jobs so that the CPU always has one to execute.
The following figure shows the memory layout for a multiprogramming system.
An OS does the following activities related to multiprogramming.
The operating system keeps several jobs in memory at a time.
This set of jobs is a subset of the jobs kept in the job pool.
The operating system picks and begins to execute one of the jobs in the memory.
Multiprogramming operating systems monitor the state of all active programs and system
resources using memory management programs to ensures that the CPU is never idle,
unless there are no jobs to process.
Advantages
High and efficient CPU utilization.
User feels that many programs are allotted CPU almost simultaneously.
Disadvantages
CPU scheduling is required.
To accommodate many jobs in memory, memory management is required.
A time-shared operating system uses the concept of CPU scheduling and multiprogramming to
provide each user with a small portion of a time-shared CPU.
Each user has at least one separate program in memory.
A program that is loaded into memory and is executing is commonly referred to as a process.
When a process executes, it typically executes for only a very short time before it either
finishes or needs to perform I/O.
Since interactive I/O typically runs at slower speeds, it may take a long time to complete.
During this time, a CPU can be utilized by another process.
The operating system allows the users to share the computer simultaneously. Since each action
or command in a time-shared system tends to be short, only a little CPU time is needed for each
user.
As the system switches CPU rapidly from one user/program to the next, each user is given the
impression that he/she has his/her own CPU, whereas actually one CPU is being shared among
many users.
Process
A process is basically a program in execution. The execution of a process must
progress in a sequential fashion.
To put it in simple terms, we write our computer programs in a text file and when we
execute this program, it becomes a process which performs all the tasks mentioned
in the program.
When a program is loaded into the memory and it becomes a process, it can be
divided into four sections ─ stack, heap, text and data. The following image shows a
simplified layout of a process inside main memory –
1 Stack
The process Stack contains the temporary data such as
method/ Function parameters, return address and
local Variables.
2 Heap
When a process executes, it passes through different states. These stages may
differ in different operating systems, and the names of these states are also not
standardized.
In general, a process can have one of the following five states at a time.
1 Start
This is the initial state when a process is first started/created.
2 Ready
The process is waiting to be assigned to a processor. Ready processes are waiting
to have the processor allocated to them by the operating system so that they can
run. Process may come into this state after Start state or while running it by but
interrupted by the scheduler to assign CPU to some other process.
3 Running
Once the process has been assigned to a processor by the OS scheduler, the
process state is set to running and the processor executes its instructions.
4 Waiting
Process moves into the waiting state if it needs to wait for a resource, such as
waiting for user input, or waiting for a file to become available.
5 Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating system, it
is moved to the terminated state where it waits to be removed from main memory.
1 Process State
The current state of the process i.e., whether it is ready,
running, waiting, or whatever.
2 Process privileges
This is required to allow/disallow access to system resources.
3 Process ID
Unique identification for each of the process in the
operating system.
4 Pointer
A pointer to parent process.
5 Program Counter
Program Counter is a pointer to the address of the next
instruction to be executed for this process.
6 CPU registers
Various CPU registers where process need to be stored for
execution for running state.
10 IO status information
Process Scheduling
Definition
The process scheduling is the activity of the process manager that handles the
removal of the running process from the CPU and the selection of another process
on the basis of a particular strategy.
Process scheduling is an essential part of a Multiprogramming operating systems.
Such operating systems allow more than one process to be loaded into the executable
memory at a time and the loaded process shares the CPU using time multiplexing.
Scheduling Queue
The processes that are entering into the system are stored in the Job Queue. Suppose if the processes are in the
Ready state are generally placed in the Ready Queue.
The processes waiting for a device are placed in Device Queues. There are unique device queues which are available
for every I/O device.
First place a new process in the Ready queue and then it waits in the ready queue till it is selected for execution.
Once the process is assigned to the CPU and is executing, any one of the following events occur −
The process issue an I/O request, and then placed in the I/O queue.
The process may create a new sub process and wait for termination.
The process may be removed forcibly from the CPU, which is an interrupt, and it is put back in the ready
queue.
In the first two cases, the process switches from the waiting state to the ready state, and then puts it back
in the ready queue. A process continues this cycle till it terminates, at which time it is removed from all
queues and has its PCB and resources deallocated.
Schedulers
Schedulers are special system software which handle process scheduling in various
ways. Their main task is to select the jobs to be submitted into the system and to
decide which process to run. Schedulers are of three types −
Long-Term Scheduler
Short-Term Scheduler
Medium-Term Scheduler
2 Speed is lesser than short Speed is fastest among Speed is in between both
term scheduler other two short and long term
scheduler.
Cooperting Process
Cooperating processes are those that can affect or are affected by other processes running on the system.
Cooperating processes may share data with each other.
There may be many reasons for the requirement of cooperating processes. Some of these are given as
follows −
Modularity
Modularity involves dividing complicated tasks into smaller subtasks. These subtasks can completed by
different cooperating processes. This leads to faster and more efficient completion of the required tasks.
Information Sharing
Sharing of information between multiple processes can be accomplished using cooperating processes. This
may include access to the same files. A mechanism is required so that the processes can access the files in
parallel to each other.
Convenience
There are many tasks that a user needs to do such as compiling, printing, editing etc. It is convenient if
these tasks can be managed by cooperating processes.
Computation Speedup
Subtasks of a single task can be performed parallely using cooperating processes. This increases the
computation speedup as the task can be executed faster. However, this is only possible if the system has
multiple processing elements.
Methods of Cooperation
Cooperating processes can coordinate with each other using shared data or messages. Details
about these are given as follows −
Cooperation by Sharing
The cooperating processes can cooperate with each other using shared data such as memory, variables,
files, databases etc. Critical section is used to provide data integrity and writing is mutually exclusive to
prevent inconsistent data.
A diagram that demonstrates cooperation by sharing is given as follows −
In the above diagram, Process P1 and P2 can cooperate with each other using shared data such as
memory, variables, files, databases etc.
Cooperation by Communication
The cooperating processes can cooperate with each other using messages. This may lead to deadlock if
each process is waiting for a message from the other to perform a operation. Starvation is also possible if a
process never receives a message.
A diagram that demonstrates cooperation by communication is given as follows −
Interprocess Communication
Inter process communication is the mechanism provided by the operating system that allows processes to
communicate with each other. This communication could involve a process letting another process know
that some event has occurred or the transferring of data from one process to another.
Semaphore
A semaphore is a variable that controls the access to a common resource by multiple processes. The two
types of semaphores are binary semaphores and counting semaphores.
Mutual Exclusion
Mutual exclusion requires that only one process thread can enter the critical section at a time. This is
useful for synchronization and also prevents race conditions.
Barrier
A barrier does not allow individual processes to proceed until all the processes reach it. Many parallel
languages and collective routines impose barriers.
Spinlock
This is a type of lock. The processes trying to acquire this lock wait in a loop while checking if the lock is
available or not. This is known as busy waiting because the process is not doing any useful operation even
though it is active.
Pipe
A pipe is a data channel that is unidirectional. Two pipes can be used to create a two-way data channel
between two processes. This uses standard input and output methods. Pipes are used in all POSIX systems
as well as Windows operating systems.
Socket
The socket is the endpoint for sending or receiving data in a network. This is true for data sent between
processes on the same computer or data sent between different computers on the same network. Most of
the operating systems use sockets for interprocess communication.
File
A file is a data record that may be stored on a disk or acquired on demand by a file server. Multiple
processes can access a file as required. All operating systems use files for data storage.
Signal
Signals are useful in interprocess communication in a limited way. They are system messages that are sent
from one process to another. Normally, signals are not used to transfer data but are used for remote
commands between processes.
Shared Memory
Shared memory is the memory that can be simultaneously accessed by multiple processes. This is done so
that the processes can communicate with each other. All POSIX systems, as well as Windows operating
systems use shared memory.
Message Queue
Multiple processes can read and write data to the message queue without being connected to each other.
Messages are stored in the queue until their recipient retrieves them. Message queues are quite useful for
interprocess communication and are used by most operating systems.
A diagram that demonstrates message queue and shared memory methods of interprocess communication
is as follows −
Scheduling algorithms
SCHEDULING CRITERIA:
Many algorithms exist for CPU scheduling. Various criteria have been suggested for
comparing these CPU scheduling algorithms. Common criteria include:
Consider a set of three processes P1, P2 and P3 arriving at time instant 0 and
having CPU burst times as shown below:
P1 24
P2 3
P3 3
The Gantt chart below shows the result.
P1 P2 P3
0 24 27 30
P2 = 24msec
P3=27 msecs
P1 completes at the end of 24 msecs, P2 at the end of 27 msecs and P3 at the end
of 30 msecs. Average turnaround time = (24 + 27 + 30) / 3 = 81 / 3 = 27 msecs.
If the processes arrive in the order P2, P3 and P3, then the result will be as follows:
P2 P3 P1
0 3 6 30
Thus if processes with smaller CPU burst times arrive earlier, then average waiting
and average turnaround times are less.
The algorithm also suffers from what is known as a convoy effect. Consider the
following scenario. Let there be a mix of one CPU-bound process and many I/O
bound processes in the ready queue.
The CPU-bound process gets the CPU and executes (long I/O burst).
In the meanwhile, I/O bound processes finish I/O and wait for CPU thus leaving
the I/O devices idle.
I/O bound processes have short CPU bursts and they execute and go for I/O quickly.
The CPU is idle till the CPU-bound process finishes the I/O and gets hold of the
CPU.
The above cycle repeats. This is called the convoy effect. Here small processes wait
for one big process to release the CPU.
Since the algorithm is non-preemptive in nature, it is not suited for time- sharing
systems.
As an example, consider the following set of processes P1, P2, P3, P4 and their CPU
burst times:
P1 6
P2 8
P3 7
P4 3
Using SJF algorithm, the processes would be scheduled as shown below.
P4 P1 P3 P2
0 3 9 16 24
The SJF algorithm produces the most optimal scheduling scheme. For a given set of
processes, the algorithm gives the minimum average waiting and turnaround times.
This is because, shorter processes are scheduled earlier than longer ones and hence
waiting time for shorter processes decreases more than it increases the waiting
time of long processes.
The main disadvantage with the SJF algorithm lies in knowing the length of the
next CPU burst. In case of long-term or job scheduling in a batch system, the
time required to complete a job as given by the user can be used to schedule. SJF
algorithm is therefore applicable in long-term scheduling.
But the SJF scheduling algorithm is provably optimal and thus serves as a
benchmark to compare other CPU scheduling algorithms.
Given below are the arrival and burst times of four processes P1, P2, P3 and P4.
P1 0 8 (1+7)
P2 1 4
P3 2 9
P4 3 5
If SJF preemptive scheduling is used, the following Gantt chart shows the
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P3
0 8 12 17 26
P2 P5 P1 P3 P4
0 1 6 16
0 1 6 16 18 19
Average waiting time = (6 + 0 + 16 + 18 + 1) / 5 = 41 / 5 = 8.2 msecs.
Priorities can be defined either internally or externally. Internal definition of
priority is based on some measurable factors like memory requirements, number
of open files, and so on. External priorities are defined by criteria such as
importance of the user depending on the user’s department and other influencing
factors.
Priority-based algorithms can be either preemptive or non-preemptive. In case of
preemptive scheduling, if a new process joins the ready queue with a priority
higher than the process that is executing, then the current process is preempted
and CPU allocated to the new process. But in case of non- preemptive algorithm,
the new process having highest priority from among the ready processes is
allocated the CPU only after the current process gives up the CPU.
P1 24
P2 3
P3 3
0 4 7 10 14 18 22 26 30