0% found this document useful (0 votes)
38 views63 pages

Os Unit 1

Uploaded by

seilmonbhaii1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views63 pages

Os Unit 1

Uploaded by

seilmonbhaii1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

SYLLABUS

What is an operating system?

An operating system (OS) is the program that, after being initially loaded into the computer
by a boot program, manages all of the other application programs in a computer. The
application programs make use of the operating system by making requests for services
through a defined application program interface (API)

Objectives of Operating Systems


Let us now see some of the objectives of the operating system, which are mentioned below.

● Convenient to use: One of the objectives is to make the computer system more
convenient to use in an efficient manner.
● User Friendly: To make the computer system more interactive with a more
convenient interface for the users.
● Easy Access: To provide easy access to users for using resources by acting as an
intermediary between the hardware and its users.
● Management of Resources: For managing the resources of a computer in a better
and faster way.
● Controls and Monitoring: By keeping track of who is using which resource,
granting resource requests, and mediating conflicting requests from different
programs and users.
● Fair Sharing of Resources: Providing efficient and fair sharing of resources
between the users and programs.

Functions of the Operating System


● Resource Management: The operating system manages and allocates memory,
CPU time, and other hardware resources among the various programs and
processes running on the computer.
● Process Management: The operating system is responsible for starting, stopping,
and managing processes and programs. It also controls the scheduling of processes
and allocates resources to them.
● Memory Management: The operating system manages the computer’s primary
memory and provides mechanisms for optimizing memory usage.
● Security: The operating system provides a secure environment for the user,
applications, and data by implementing security policies and mechanisms such as
access controls and encryption.
● Job Accounting: It keeps track of time and resources used by various jobs or
users.
● File Management: The operating system is responsible for organizing and
managing the file system, including the creation, deletion, and manipulation of
files and directories.
● Device Management: The operating system manages input/output devices such
as printers, keyboards, mice, and displays. It provides the necessary drivers and
interfaces to enable communication between the devices and the computer.
● Networking: The operating system provides networking capabilities such as
establishing and managing network connections, handling network protocols, and
sharing resources such as printers and files over a network.
● User Interface: The operating system provides a user interface that enables users
to interact with the computer system. This can be a Graphical User Interface
(GUI), a Command-Line Interface (CLI), or a combination of both.
● Backup and Recovery: The operating system provides mechanisms for backing
up data and recovering it in case of system failures, errors, or disasters.
● Virtualization: The operating system provides virtualization capabilities that
allow multiple operating systems or applications to run on a single physical
machine. This can enable efficient use of resources and flexibility in managing
workloads.
● Performance Monitoring: The operating system provides tools for monitoring
and optimizing system performance, including identifying bottlenecks, optimizing
resource usage, and analyzing system logs and metrics.
● Time-Sharing: The operating system enables multiple users to share a computer
system and its resources simultaneously by providing time-sharing mechanisms
that allocate resources fairly and efficiently.
● System Calls: The operating system provides a set of system calls that enable
applications to interact with the operating system and access its resources. System
calls provide a standardized interface between applications and the operating
system, enabling portability and compatibility across different hardware and
software platforms.
● Error-detecting Aids: These contain methods that include the production of
dumps, traces, error messages, and other debugging and error-detecting methods.

Types of Operating Systems (OS)

An operating system is a well-organized collection of programs that manages the computer


hardware. It is a type of system software that is responsible for the smooth functioning of the
computer system.

Batch Operating System


In the 1970s, Batch processing was very popular. In this technique, similar types of jobs were
batched together and executed in time. People were used to having a single computer which
was called a mainframe.

In Batch operating system, access is given to more than one person; they submit their
respective jobs to the system for the execution.

The system put all of the jobs in a queue on the basis of first come first serve and then
executes the jobs one by one. The users collect their respective output when all the jobs get
executed.

The purpose of this operating system was mainly to transfer control from one job to another
as soon as the job was completed. It contained a small set of programs called the resident
monitor that always resided in one part of the main memory. The remaining part is used for
servicing jobs.
Advantages of Batch OS

○ The use of a resident monitor improves computer efficiency as it eliminates CPU time
between two jobs.

Disadvantages of Batch OS

1. Starvation

Batch processing suffers from starvation.

For Example:
There are five jobs J1, J2, J3, J4, and J5, present in the batch. If the execution time of J1 is
very high, then the other four jobs will never be executed, or they will have to wait for a very
long time. Hence the other processes get starved.

2. Not Interactive

Batch Processing is not suitable for jobs that are dependent on the user's input. If a job
requires the input of two numbers from the console, then it will never get it in the batch
processing scenario since the user is not present at the time of execution.

Multiprogramming Operating System

Multiprogramming is an extension to batch processing where the CPU is always kept busy.
Each process needs two types of system time: CPU time and IO time.

In a multiprogramming environment, when a process does its I/O, The CPU can start the
execution of other processes. Therefore, multiprogramming improves the efficiency of the
system.

Advantages of Multiprogramming OS

○ Throughout the system, it increased as the CPU always had one program to execute.
○ Response time can also be reduced.
Disadvantages of Multiprogramming OS

○ Multiprogramming systems provide an environment in which various systems


resources are used efficiently, but they do not provide any user interaction with the
computer system.

Multiprocessing Operating System

In Multiprocessing, Parallel computing is achieved. There are more than one processors
present in the system which can execute more than one process at the same time. This will
increase the throughput of the system.

In Multiprocessing, Parallel computing is achieved. More than one processor present in the
system can execute more than one process simultaneously, which will increase the
throughput of the system.
Advantages of Multiprocessing operating system:

○ Increased reliability: Due to the multiprocessing system, processing tasks can be


distributed among several processors. This increases reliability as if one processor
fails, the task can be given to another processor for completion.
○ Increased throughout: As several processors increase, more work can be done in
less.

Disadvantages of Multiprocessing operating System

○ Multiprocessing operating system is more complex and sophisticated as it takes care


of multiple CPUs simultaneously.

Multitasking Operating System


The multitasking operating system is a logical extension of a multiprogramming system that
enables multiple programs simultaneously. It allows a user to perform more than one
computer task at the same time.

Advantages of Multitasking operating system

○ This operating system is more suited to supporting multiple users simultaneously.


○ The multitasking operating systems have well-defined memory management.

Disadvantages of Multitasking operating system

○ The multiple processors are busier at the same time to complete any task in a
multitasking environment, so the CPU generates more heat.

Network Operating System


An Operating system, which includes software and associated protocols to communicate with
other computers via a network conveniently and cost-effectively, is called Network Operating
System.

Advantages of Network Operating System

○ In this type of operating system, network traffic reduces due to the division between
clients and the server.
○ This type of system is less expensive to set up and maintain.

Disadvantages of Network Operating System


○ In this type of operating system, the failure of any node in a system affects the whole
system.
○ Security and performance are important issues. So trained network administrators are
required for network administration.

Real Time Operating System

In Real-Time Systems, each job carries a certain deadline within which the job is supposed to
be completed, otherwise, the huge loss will be there, or even if the result is produced, it will
be completely useless.

The Application of a Real-Time system exists in the case of military applications, if you want
to drop a missile, then the missile is supposed to be dropped with a certain precision.

Advantages of Real-time operating system:

○ Easy to layout, develop and execute real-time applications under the real-time
operating system.
○ In a Real-time operating system, the maximum utilization of devices and systems.

Disadvantages of Real-time operating system:

○ Real-time operating systems are very costly to develop.


○ Real-time operating systems are very complex and can consume critical CPU cycles.

Time-Sharing Operating System

In the Time Sharing operating system, computer resources are allocated in a time-dependent
fashion to several programs simultaneously. Thus it helps to provide a large number of user's
direct access to the main computer. It is a logical extension of multiprogramming. In time-
sharing, the CPU is switched among multiple programs given by different users on a
scheduled basis.

A time-sharing operating system allows many users to be served simultaneously, so


sophisticated CPU scheduling schemes and Input/output management are required.

Time-sharing operating systems are very difficult and expensive to build.

Advantages of Time Sharing Operating System

○ The time-sharing operating system provides effective utilization and sharing of


resources.
○ This system reduces CPU idle and response time.

Disadvantages of Time Sharing Operating System

○ Data transmission rates are very high in comparison to other methods.


○ Security and integrity of user programs loaded in memory and data need to be
maintained as many users access the system at the same time.

Distributed Operating System

The Distributed Operating system is not installed on a single machine, it is divided into parts,
and these parts are loaded on different machines. A part of the distributed Operating system is
installed on each machine to make their communication possible. Distributed Operating
systems are much more complex, large, and sophisticated than Network operating systems
because they also have to take care of varying networking protocols.

Advantages of Distributed Operating System

○ The distributed operating system provides sharing of resources.


○ This type of system is fault-tolerant.

Disadvantages of Distributed Operating System


○ Protocol overhead can dominate computation cost.

Generations of Operating System

The First Generation (1940 to early 1950s)

When the first electronic computer was developed in 1940, it was created without any
operating system. In early times, users have full access to the computer machine and write a
program for each task in absolute machine language. The programmer can perform and solve
only simple mathematical calculations during the computer generation, and this calculation
does not require an operating system.

The Second Generation (1955 - 1965)

The first operating system (OS) was created in the early 1950s and was known as GMOS.
General Motors has developed OS for the IBM computer. The second-generation operating
system was based on a single stream batch processing system because it collects all similar
jobs in groups or batches and then submits the jobs to the operating system using a punch
card to complete all jobs in a machine. At each completion of jobs (either normally or
abnormally), control transfer to the operating system that is cleaned after completing one job
and then continues to read and initiates the next job in a punch card. After that, new machines
were called mainframes, which were very big and used by professional operators.

The Third Generation (1965 - 1980)

During the late 1960s, operating system designers were very capable of developing a new
operating system that could simultaneously perform multiple tasks in a single computer
program called multiprogramming. The introduction of multiprogramming plays a very
important role in developing operating systems that allow a CPU to be busy every time by
performing different tasks on a computer at the same time. During the third generation, there
was a new development of minicomputer's phenomenal growth starting in 1961 with the DEC
PDP-1. These PDP's leads to the creation of personal computers in the fourth generation.

The Fourth Generation (1980 - Present Day)


The fourth generation of operating systems is related to the development of the personal
computer. However, the personal computer is very similar to the minicomputers that were
developed in the third generation. The cost of a personal computer was very high at that time;
there were small fractions of minicomputers costs. A major factor related to creating personal
computers was the birth of Microsoft and the Windows operating system. Microsoft created
the first window operating system in 1975. After introducing the Microsoft Windows OS,
Bill Gates and Paul Allen had the vision to take personal computers to the next level.
Therefore, they introduced the MS-DOS in 1981; however, it was very difficult for the
person to understand its cryptic commands. Today, Windows has become the most popular
and most commonly used operating system technology. And then, Windows released various
operating systems such as Windows 95, Windows 98, Windows XP and the latest operating
system, Windows 7. Currently, most Windows users use the Windows 10 operating system.
Besides the Windows operating system, Apple is another popular operating system built in
the 1980s, and this operating system was developed by Steve Jobs, a co-founder of Apple.
They named the operating system Macintosh OS or Mac OS.

Operating System Structure


We want a clear structure to let us apply an operating system to our particular needs because
operating systems have complex structures. It is easier to create an operating system in
pieces, much as we break down larger issues into smaller, more manageable subproblems.
Every segment is also a part of the operating system. Operating system structure can be
thought of as the strategy for connecting and incorporating various operating system
components within the kernel. Operating systems are implemented using many types of
structures, as will be discussed below:

SIMPLE STRUCTURE

It is the most straightforward operating system structure, but it lacks definition and is only
appropriate for usage with tiny and restricted systems. Since the interfaces and degrees of
functionality in this structure are clearly defined, programs are able to access I/O routines,
which may result in unauthorized access to I/O procedures.

This organizational structure is used by the MS-DOS operating system:

○ There are four layers that make up the MS-DOS operating system, and each has its
own set of features.
○ These layers include ROM BIOS device drivers, MS-DOS device drivers, application
programs, and system programs.
○ The MS-DOS operating system benefits from layering because each level can be
defined independently and, when necessary, can interact with one another.
○ If the system is built in layers, it will be simpler to design, manage, and update.
Because of this, simple structures can be used to build constrained systems that are
less complex.
○ When a user program fails, the operating system as whole crashes.
○ Because MS-DOS systems have a low level of abstraction, programs and I/O
procedures are visible to end users, giving them the potential for unwanted access.

The following figure illustrates layering in simple structure:

Advantages of Simple Structure:

○ Because there are only a few interfaces and levels, it is simple to develop.
○ Because there are fewer layers between the hardware and the applications, it offers
superior performance.

Disadvantages of Simple Structure:

○ The entire operating system breaks if just one user program malfunctions.
○ Since the layers are interconnected, and in communication with one another, there is
no abstraction or data hiding.
○ The operating system's operations are accessible to layers, which can result in data
tampering and system failure.

MONOLITHIC STRUCTURE

The monolithic operating system controls all aspects of the operating system's operation,
including file management, memory management, device management, and operational
operations.

The core of an operating system for computers is called the kernel (OS). All other System
components are provided with fundamental services by the kernel. The operating system and
the hardware use it as their main interface. When an operating system is built into a single
piece of hardware, such as a keyboard or mouse, the kernel can directly access all of its
resources.

The monolithic operating system is often referred to as the monolithic kernel. Multiple
programming techniques such as batch processing and time-sharing increase a processor's
usability. Working on top of the operating system and under complete command of all
hardware, the monolithic kernel performs the role of a virtual computer. This is an old
operating system that was used in banks to carry out simple tasks like batch processing and
time-sharing, which allows numerous users at different terminals to access the Operating
System.

The following diagram represents the monolithic structure:


Advantages of Monolithic Structure:

○ Because layering is unnecessary and the kernel alone is responsible for managing all
operations, it is easy to design and execute.
○ Due to the fact that functions like memory management, file management, process
scheduling, etc., are implemented in the same address area, the monolithic kernel runs
rather quickly when compared to other systems. Utilizing the same address speeds up
and reduces the time required for address allocation for new processes.

Disadvantages of Monolithic Structure:

○ The monolithic kernel's services are interconnected in address space and have an
impact on one another, so if any of them malfunctions, the entire system does as well.
○ It is not adaptable. Therefore, launching a new service is difficult.

LAYERED STRUCTURE

The OS is separated into layers or levels in this kind of arrangement. Layer 0 (the lowest
layer) contains the hardware, and layer 1 (the highest layer) contains the user interface
(layer N). These layers are organized hierarchically, with the top-level layers making use of
the capabilities of the lower-level ones.

The functionalities of each layer are separated in this method, and abstraction is also an
option. Because layered structures are hierarchical, debugging is simpler, therefore all lower-
level layers are debugged before the upper layer is examined. As a result, the present layer
alone has to be reviewed since all the lower layers have already been examined.

The image below shows how OS is organized into layers:

Advantages of Layered Structure:

○ Work duties are separated since each layer has its own functionality, and there is
some amount of abstraction.
○ Debugging is simpler because the lower layers are examined first, followed by the top
layers.

Disadvantages of Layered Structure:

○ Performance is compromised in layered structures due to layering.


○ Construction of the layers requires careful design because upper layers only make use
of lower layers' capabilities.

MICRO-KERNEL STRUCTURE

The operating system is created using a micro-kernel framework that strips the kernel of any
unnecessary parts. Systems and user applications are used to implement these optional kernel
components. So, Micro-Kernels is the name given to these systems that have been developed.

Each Micro-Kernel is created separately and is kept apart from the others. As a result, the
system is now more trustworthy and secure. If one Micro-Kernel malfunctions, the remaining
operating system is unaffected and continues to function normally.

The image below shows Micro-Kernel Operating System Structure:


Advantages of Micro-Kernel Structure:

○ It enables portability of the operating system across platforms.


○ Due to the isolation of each Micro-Kernel, it is reliable and secure.
○ The reduced size of Micro-Kernels allows for successful testing.
○ The remaining operating system remains unaffected and keeps running properly even
if a component or Micro-Kernel fails.

Disadvantages of Micro-Kernel Structure:

○ The performance of the system is decreased by increased inter-module


communication.
○ The construction of a system is complicated

Modular structure or approach


It is considered as the best approach for an OS. It involves designing of a modular kernel.
The kernel has only a set of core components and other services are added as dynamically
loadable modules to the kernel either during runtime or boot time. It resembles layered
structure due to the fact that each kernel has defined and protected interfaces, but it is more
flexible than a layered structure as a module can call any other module. For example Solaris
OS is organized as shown in the figure.
OPERATING SYSTEM OPERATION
User application programs can only interact with the system hardware with the help of an
operating system. Operating system provides an environment to different types of
application and programs in which they can perform their useful task , it itself doesnot
provide any functionality.

Operations of operating system are :

1. process management,

2. memory management,

3. device management and

4. file management

Process Management
The Operating system manages the processes, it assigns the processor to process the task ata
time,which is termed as process scheduling. To do this it uses different types of algorithms
like

● FCFS (first come first served),

● SJF (shortest job first),

● priority scheduling,

● round robin scheduling etc.

To handle processes in process management many scheduling queues are used.

1. As the processes enter the system, they are queued under the job queues.

2. When the processes are ready to execute in the main memory, they are sent to ready

queue.
3. Processes that needs I/O device are sent to device queue.

Memory Management
The role of Memory Management is very important in operating system. It allocates memory
and switches the processes between disk and primary memory for execution whenever
required.

Following activities are performed by operating system for memory management.

● it allocates memory to the processes by using some algorithms like best fit, first fit,
and worst fit

● All memory allocations are being tracked by the operating system, means what part of
memory are in use and what parts are empty.

● Operating System also deallocate memory from processes when process gets
terminated or if process does not require it.

Device Management
operating system handles many I/O devices such as mouse, keyboard, disk drive etc. To
handle a specific device, operating system can be connected to different device drivers.
Interface of the device and the device driver is device controller. All the I/O devices such as
mouse, keyboard, disk drive etc. can be accessed by user applications by using device
drivers.

File Management
To provide a uniform and systematic view of data storage, Files are used by the operating
system.

All the files are mapped to non volatile physical devices, in order to protect data loss in case
of system failure.

There are two ways to access Files

1. sequential access and

2. direct access
Sequential Access
In this Data in files are accessed or processed in serialised/sequential order.

Data is accessed one by one ie. one after the other.

some examples of file systems that uses sequential access are editors, compilers etc.

Direct Access
It is a Relative access ie. in this files are accessed in random order for read and write
operations.

Modern operating systems are interrupt driven.

● If there are no processes to execute,

● no I/O devices to service, and no users to whom to respond,

an operating system will sit quietly, waiting for something to happen. Events are almost
always signalled by the occurrence of an interrupt or a trap.

Wondering!! what is trap ?


A trap (or an exception) is a software-generated interrupt caused either by an error (for
example, division by zero or invalid memory access) or by a specific request from a user
program that an operating-system service be performed.

● The interrupt-driven nature of an operating system defines that system's general


structure. For each type of interrupt, separate segments of code in the operating
system determine what action should be taken.

● An interrupt service routine is provided that is responsible for dealing with the
interrupt. Since the operating system and the users share the hardware and software
resources of the computer system, we need to make sure that an error in a user
program could cause problems only for the one program that was running. With
sharing, many processes could be adversely affected by a bug in one program. For
example, if a process gets stuck in an infinite loop, this loop could prevent the correct
operation of many other processes.
● More subtle errors can occur in a multiprogramming system, where one erroneous
program might modify another program, the data of another program, or even the
operating system itself. Without protection against these sorts of errors, either the
computer must execute only one process at a time or all output must be suspect. A
properly designed operating system must ensure that an incorrect (or malicious)
program cannot cause other programs to execute incorrectly.

Dual-Mode Operation
In order to ensure the proper execution of the operating system, we must be able to
distinguish between the execution of operating-system code and user defined code. The
approach taken by most computer systems is to provide hardware support that allows us to
differentiate among various modes of execution. At the very least, we need two separate
modes of operation: user mode and kernel mode (also called supervisor mode, system
mode, or privileged mode). A bit, called the mode bit, is added to the hardware of the
computer to indicate the current mode: kernel (0) or user (1). With the mode bit, we are able
to distinguish between a task that is executed on behalf of the operating system and one that
is executed on behalf of the user.k

● When the computer system is executing on behalf of a user application, the system is
in user mode.
● However, when a user application requests a service from the operating system (via a
system call), it must transition from user to kernel mode to fulfill the request. As
we shall see, this architectural enhancement is useful for many other aspects of system
operation as well.

● At system boot time, the hardware starts in kernel mode.

● The operating system is then loaded and starts user applications in user mode.

● Whenever a trap or interrupt occurs, the hardware switches from user mode to kernel
mode (that is, changes the state of the mode bit to 0).

● Thus, whenever the operating system gains control of the computer, it is in kernel
mode.

● The system always switches to user mode (by setting the mode bit to 1) before
passing control to a user program.

The dual mode of operation provides us with the means for protecting the operating system
from errant users—and errant users from one another. We accomplish this protection by
designating some of the machine instructions that may cause harm as privileged instructions.
The hardware allows privileged instructions to be executed only in kernel mode. If an
attempt is made to execute a privileged instruction in user mode, the hardware does not
execute the instruction but rather treats it as illegal and traps it to the operating system. The
instruction to switch to user mode is an example of a privileged instruction. Some other
examples include I/O control, timer management, and interrupt management. As we shall
see throughout the text, there are many additional privileged instructions.

We can now see the life cycle of instruction execution in a computer system. Initial control
is within the operating system, where instructions are executed in kernel mode. When
control is given to a user application, the mode is set to user mode. Eventually, control is
switched back to the operating system via an interrupt, a trap, or a system call. System calls
provide the means for a user program to ask the operating system to perform tasks reserved
for the operating system on the user program's behalf. A system call is invoked in a variety
of ways, depending on the functionality provided by the underlying processor. In all forms,
it is the method used by a process to request action by the operating system. A system call
usually takes the form of a trap to a specific location in the interrupt vector. This trap can be
executed by a generic trap instruction, although some systems (such as the MIPS R2000
family) have a specific syscall instruction.

When a system call is executed, it is treated by the hardware as a software interrupt. Control
passes through the interrupt vector to a service routine in the operating system, and the mode
bit is set to kernel mode. The system call service routine is a part of the operating system.
The kernel examines the interrupting instruction to determine what system call has occurred;
a parameter indicates what type of service the user program is requesting. Additional
information needed for the request may be passed in registers, on the stack, or in memory
(with pointers to the memory locations passed in registers). The kernel verifies that the
parameters are correct and legal, executes the request, and returns control to the instruction
following the system call. The lack of a hardware-supported dual mode can cause serious
shortcomings in an operating system.

For instance, MS-DOS was written for the Intel 8088 architecture, which has no mode bit
and therefore no dual mode. A user program running awry can wipe out the operating system
by writing over it with data; and multiple programs are able to write to a device at the same
time, with possibly disastrous results. Recent versions of the Intel CPU, such is the Pentium,
do provide dual-mode operation. Accordingly, most contemporary operating systems, such
as Microsoft Windows 2000 and Windows XP, and Linux and Solaris for x86 systems, take
advantage of this feature and provide greater protection for the operating system. Once
hardware protection is in place, errors violating modes are detected by the hardware. These
errors are normally handled by the operating system. If a user program fails in some
way—such as by making an attempt either to execute an illegal instruction or to access
memory that is not in the user's address space—then the hardware will trap to the operating
system. The trap transfers control through the interrupt vector to the operating system, just as
an interrupt does.

When a program error occurs, the operating system must terminate the program abnormally.
This situation is handled by the same code as is a user-requested abnormal termination. An
appropriate error message is given, and the memory of the program may be dumped. The
memory dump is usually written to a file so that the user or programmer can examine it and
perhaps correct it and restart the program.

Timer
We must ensure that the operating system maintains control over the CPU. We must
prevent a user program from getting stuck in an infinite loop or not calling system
services and never returning control to the operating system. To accomplish this goal, we
can use a timer. A timer can be set to interrupt the computer after a specified period. The
period may be fixed (for example, 1/60 second) or variable (for example, from 1 millisecond
to 1 second). A variable timer is generally implemented by a fixed-rate clock and a counter.

The operating system sets the counter. Every time the clock ticks, the counter is
decremented. When the counter reaches 0, an interrupt occurs. For instance, a 10-bit counter
with a 1-millisecond clock allows interrupts at intervals from 1 millisecond to 1,024
milliseconds, in steps of 1 millisecond. Before turning over control to the user, the operating
system ensures that the timer is set to interrupt. If the timer interrupts, control transfers
automatically to the operating system, which may treat the interrupt as a fatal error or may
give the program more time. Clearly, instructions that modify the content of the timer are
privileged. Thus, we can use the timer to prevent a user program from running too long. A
simple technique is to initialize a counter with the amount of time that a program is allowed
to run. A program with a 7-minute time limit, for example, would have its counter initialized
to 420.
Every second, the timer interrupts and the counter is decremented by 1. As long as the
counter is positive, control is returned to the user program. When the counter becomes
negative, the operating system terminates the program for exceeding the assigned time limit.

Difference between user mode and kernel mode

terms User Mode Kernel Mode

Definition User Mode is a restricted mode, Kernel Mode is the privileged


which the application programs mode, which the computer enters
are executing and starts. when accessing hardware
resources.

Modes User Mode is considered as the Kernel mode is the system mode,
slave mode or the restricted master mode or the privileged
mode. mode.

Address In User mode, a process gets its In Kernel Mode, processes get a
Space own address space. single address space.

Interruptions In User Mode, if an interrupt In Kernel Mode, if an interrupt


occurs, only one process fails. occurs, the whole operating
system might fail.

Restrictions In user mode, there are In kernel mode, both user


restrictions to access kernel programs and kernel programs can
programs. Cannot access them access.
directly.
Process
A process is basically a program in execution. The execution of a process must progress in a
sequential fashion.

A process is defined as an entity which represents the basic unit of work to be implemented in
the system.

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 −
S.N. Component & Description

1 Stack
The process Stack contains the temporary data such as method/function parameters,
return address and local variables.

2 Heap
This is dynamically allocated memory to a process during its run time.

3 Text
This includes the current activity represented by the value of Program Counter and
the contents of the processor's registers.

4 Data
This section contains the global and static variables.

Program

A program is a piece of code which may be a single line or millions


of lines. A computer program is usually written by a computer
programmer in a programming language. For example, here is a
simple program written in C programming language −

#include <stdio.h>

int main() {

printf("Hello, World! \n");

return 0;

}
A computer program is a collection of instructions that performs a specific task when
executed by a computer. When we compare a program with a process, we can conclude that a
process is a dynamic instance of a computer program.

A part of a computer program that performs a well-defined task is known as an algorithm. A


collection of computer programs, libraries and related data are referred to as a software.

Process Life Cycle

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.

S.N. State & Description

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.

Process Control Block (PCB)

A Process Control Block is a data structure maintained by the


Operating System for every process. The PCB is identified by an
integer process ID (PID). A PCB keeps all the information needed to
keep track of a process as listed below in the table −

S.N. Information & Description

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.

7 CPU Scheduling Information


Process priority and other scheduling information which is required to schedule the
process.

8 Memory management information


This includes the information of page table, memory limits, Segment table
depending on memory used by the operating system.

9 Accounting information
This includes the amount of CPU used for process execution, time limits, execution
ID etc.

10 IO status information
This includes a list of I/O devices allocated to the process.

File System
A file is a collection of correlated information which is recorded on secondary or non-volatile
storage like magnetic disks, optical disks, and tapes. It is a method of data collection that is
used as a medium for giving input and receiving output from that program.
In general, a file is a sequence of bits, bytes, or records whose meaning is defined by the file
creator and user. Every File has a logical location where they are located for storage and
retrieval.

File structure
A File Structure needs to be predefined format in such a way that an operating system
understands. It has an exclusively defined structure, which is based on its type.
Three types of files structure in OS:
● A text file: It is a series of characters that is organized in lines.

● An object file: It is a series of bytes that is organized into blocks.

● A source file: It is a series of functions and processes.

File Attributes

A file has a name and data. Moreover, it also stores meta information like file creation date
and time, current size, last modified date, etc. All this information is called the attributes of a
file system.
Here, are some important File attributes used in OS:

● Name: It is the only information stored in a human-readable form.

● Identifier: Every file is identified by a unique tag number within a file system known
as an identifier.

● Location: Points to file location on device.

● Type: This attribute is required for systems that support various types of files.

● Size. Attribute used to display the current file size.

● Protection. This attribute assigns and controls the access rights of reading, writing,
and executing the file.

● Time, date and security: It is used for protection, security, and also used for
monitoring
File Type

It refers to the ability of the operating system to differentiate various types of files like text
files, binary, and source files. However, Operating systems like MS_DOS and UNIX has the
following type of files:
Character Special File
It is a hardware file that reads or writes data character by character, like mouse, printer, and
more.
Ordinary files

● These types of files stores user information.

● It may be text, executable programs, and databases.


● It allows the user to perform operations like add, delete, and modify.

Directory Files

● Directory contains files and other related information about those files. Its basically a
folder to hold and organize multiple files.
Special Files

● These files are also called device files. It represents physical devices like printers,
disks, networks, flash drive, etc.
Functions of File

● Create file, find space on disk, and make an entry in the directory.

● Write to file, requires positioning within the file

● Read from file involves positioning within the file

● Delete directory entry, regain disk space.

● Reposition: move read/write position.

Commonly used terms in File systems

Field
This element stores a single value, which can be static or variable length.
DATABASE
Collection of related data is called a database. Relationships among elements of data are
explicit.
FILES
Files is the collection of similar record which is treated as a single entity.
RECORD
A Record type is a complex data type that allows the programmer to create a new data type
with the desired column structure. Its groups one or more columns to form a new data type.
These columns will have their own names and data type.
File Access Methods
File access is a process that determines the way that files are accessed and read into memory.
Generally, a single access method is always supported by operating systems. Though there
are some operating system which also supports multiple access methods.
Three file access methods are:

● Sequential access

● Direct random access

● Index sequential access

Sequential Access
In this type of file access method, records are accessed in a certain pre-defined sequence. In
the sequential access method, information stored in the file is also processed one by one.
Most compilers access files using this access method.
Random Access
The random access method is also called direct random access. This method allow accessing
the record directly. Each record has its own address on which can be directly accessed for
reading and writing.
Sequential Access
This type of accessing method is based on simple sequential access. In this access method, an
index is built for every file, with a direct pointer to different memory blocks. In this method,
the Index is searched sequentially, and its pointer can access the file directly. Multiple levels
of indexing can be used to offer greater efficiency in access. It also reduces the time needed
to access a single record.
Space Allocation

In the Operating system, files are always allocated disk spaces.


Three types of space allocation methods are:

● Linked Allocation

● Indexed Allocation

● Contiguous Allocation

Contiguous Allocation
In this method,
● Every file users a contiguous address space on memory.

● Here, the OS assigns disk address is in linear order.

● In the contiguous allocation method, external fragmentation is the biggest issue.

Linked Allocation
In this method,

● Every file includes a list of links.

● The directory contains a link or pointer in the first block of a file.

● With this method, there is no external fragmentation

● This File allocation method is used for sequential access files.

● This method is not ideal for a direct access file.

Indexed Allocation
In this method,

● Directory comprises the addresses of index blocks of the specific files.

● An index block is created, having all the pointers for specific files.

● All files should have individual index blocks to store the addresses for disk space.
INTERPROCESS COMMUNICATION

Processes executing concurrently in the operating system may be either


1. Independent processes : A process is independent if it cannot affect or be affected by the
other processes executing in the system.

2. Cooperating processes: Any process that does not share data with any other process is
independent. A process is cooperating if it can affect or be affected by the other processes
executing in the system. Clearly, any process that shares data with other processes is a cooperating
process.

There are several reasons for providing an environment that allows process cooperation:

Information sharing. Since several users may be interested in the same piece of information (for
instance, a shared file), we must provide an environment to allow concurrent access to such
information.

Computation speedup. If we want a particular task to run faster, we must break it into subtasks,
each of which will be executing in parallel with the others. Notice that such a speedup can be
achieved only if the computer has multiple processing elements (such as CPUs or I/O channels).

Modularity. We may want to construct the system in a modular fashion, dividing the system
functions into separate processes or threads

Convenience. Even an individual user may work on many tasks at the same time. For instance, a
user may be editing, printing, and compiling in parallel.

Cooperating processes require an interprocess communication (IPC) mechanism that will allow
them to exchange data and information. There are two fundamental models of interprocess
communication:
(1) shared memory
(2) message passing.

Shared-Memory Systems: In the shared-memory model, a region of memory that


is shared by cooperating processes. Processes can then exchange information by reading and
writing data to the shared region.Interprocess communication using shared memory requires
communicating processes to establish a region of shared memory. Typically, a shared-memory
region resides in the address space of the process creating the shared-memory segment. Other
processes that wish to communicate using this shared-memory segment must attach it to their
address space. Recall that, normally, the
operating system tries to prevent one process from accessing another process's memory. Shared
memory requires that two or more processes agree to remove this restriction. They can then
excbange information by reading and writing data in the shared areas. The form of the data and the
location are determined by these processes and are not under the operating system's control. The
processes are also responsible for ensuring that they are not writing to the same location
simultaneously.A producer process produces information that is consumed by a consumer process.

One solution to the producer-consumer problem uses shared memory. To allow producer and
consumer processes to run concurrently, we must have available a buffer of items that can be
filled by the producer and emptied by the consumer. This buffer will reside in a region of memory
that is shared by the producer and consumer processes. A producer can produce one item while the
consumer is consuming another item. The producer and consumer must be synchronized, so that
the consumer does not try to consume an item
that has not yet been produced.

Two types of buffers can be used. The UNBOUNDED BUFFER places no practical limit on the
size of the buffer. The consumer may have to wait for new items, but the producer can always
produce new items. The BOUNDED BUFFER assumes a fixed buffer size. In this case, the
consumer must wait if the buffer is empty, and the producer must wait if the buffer is full.

Message-Passing Systems: Message passing provides a mechanism to allow processes to


communicate and to synchronize their actions without sharing the same address space and is
particularly useful in a distributed environment, where the communicating processes may reside
on different computers connected by a network.A message-passing facility provides at least two
operations: send(message) and receive(message). Messages sent by a process can be of either
fixed or variable size. If only fixed-sized messages can be sent, the system-level implementation is
straightforward.

If processes P and Q want to communicate, they must send messages to and


receive messages from each other; a communication link must exist between
them. This link can be implemented in a variety of ways. We are concerned here
not with the link's physical implementation (such as shared memory, hardware
bus, or network, which are covered in Chapter 16) but rather with its logical
implementation. Here are several methods for logically implementing a link
and the send 0 I receive() operations:

● Direct or indirect communication


● Synchronous or asynchronous communication
● Automatic or explicit buffering

MUTUAL EXCLUSION:

we assume that processes are numbered uniquely from 1 to n and that a one-to-one mapping exists
between processes and processors (that is, each process has its own processor).

1. Centralized Approach: In a centralized approach to providing mutual exclusion, one of


the processes in the system is chosen to coordinate the entry to the critical section. Each
process that wants to invoke mutual exclusion sends a request message to the coordinator.
When the process receives a reply message from the coordinator, it can enter its critical
section. After exiting its critical section, the process sends a release message to the
coordinator and proceeds with its execution. On receiving a request message, the
coordinator checks to see whether some other process is in its critical section. If no process
is in its critical section, the coordinator immediately sends back a reply message.
Otherwise, the request is queued. When the coordinator receives a release message, it
removes one of the request messages from the queue (in accordance with some scheduling
algorithm) and sends a reply message to the requesting process. This scheme requires three
messages per critical-section entry: a requesT, a reply, and a release.

2. Fully Distributed Approach: When a process g wants to enter its critical section, it
generates a new timestamp, TS, and sends the message request(P;, TS) to all processes in
the system (including itself). On receiving a request message, a process may reply
immediately (that is, send a reply message back to P; ), or it may defer sending a reply
back (because it is already in its critical section, for example). A process that has received
a reply message from all other processes in the system can enter its critical section,
queueing incmning requests and deferring them. After exiting its critical section, the
process sends reply messages to all its deferred requests.

The decision whether process Pi replies immediately to a request(Pj, TS) message or


defers its reply is based on three factors:

● If process Pi is in its critical section, then it defers its reply to Pj. If process Pi does
not want to enter its critical section, then it sends a reply immediately to P j.
● If process P; wants to enter its critical section but has not yet entered it, then it
compares its own request timestamp with the timestamp of the incoming request
made by process Pj.
● If its own request timestamp is greater than that of the incoming request, then it
sends a reply immediately to Pj (Pj asked first). Otherwise, the reply is deferred.

3. Token-Passing Approach: A TOKEN is a special type of message that is passed from


process to process. Possession of the token entitles the holder to enter the critical section.
Since there is only a single token, only one process can be in its critical section at a time.

We assume that the processes in the system are logically organized in a ring structure. The
physical communication network need not be a ring. As long as the processes are
connected to one another, it is possible to implement a logical ring. To implement mutual
exclusion, we pass the token around the ring. When a process receives the token, it may
enter its critical section, keeping the token. After the process exits its critical section, the
token is passed around again. If the process receiving the token does not want to enter its
critical section, it passes the token to its neighbor., but a token is substituted for a shared
variable. If the ring is unidirectional, freedom from starvation.

Two types of failure must be considered. First, if the token is lost an election must be
called to generate a new token. Second, if a process fails, a new logical ring must be
established.

SEMAPHORES:
The hardware-based solutions to the critical-section problem presented are complicated for
application programmers to use. To overcome this difficulty, we can use a synchronisation tool
called a SEMAPHORE.

A semaphore S is an integer variable that, apart from initialization, is accessed only through two
standard atomic operations: wait () and signal (). The wait () operation was originally termed P
(from the Dutch proberen, "to test"); signal() was originally called V (from verhogen, "to
increment"). The definition of wait () is as follows:

wait(S)
{
while S <= 0 II no-op:
s--·
}'

The definition of signal() is as follows:

signal(S)
{
S++;
}

All modifications to the integer value of the semaphore in the wait () and signal() operations must
be executed indivisibly. That is, when one process modifies the semaphore value, no other process
can simultaneously modify that same semaphore value. In addition, in the case of wait (S), the
testing of the integer value of S (S :S 0), as well as its possible modification (S--), must be
executed without interruption

Operating systems often distinguish between counting and binary semaphores. The value of a
counting semaphore can range over an unrestricted domain. The value of a binary semaphore can
range only between 0 and 1.

We can use binary semaphores to deal with the critical-section problem £or multiple processes.
Then processes share a semaphore, mutex, initialised to 1. Each process Pi is organised. Counting
semaphores can be used to control access to a given resource consisting of a finite number o£
instances. The semaphore is initialised to the number of resources available. Each process that
wishes to use a resource performs a wait() operation on the semaphore (thereby decrementing the
count). When a process releases a resource, it performs a signal() operation (incrementing the
count). When the count for the semaphore goes to 0, all resources are being used. After that,
processes that wish to use a resource will block until the count becomes greater than 0.

The main disadvantage of the semaphore definition given here is thatit requires While a process
is in its critical section, any other process that tries to enter its critical section must loop
continuously in the entry code. This continual looping is clearly a problem in a real
multiprogramming system,where a single CPU is shared among ncany processes. Busy waiting
wastes CPU cycles that some other process might be able to use productively. This type of
semaphore is also called a SPINLOCK.because the process "spins" while waiting for the lock.
(Spinlocks do have an advantage in that no context switch is required when a process must wait on
a lock, and a context switch may take considerable time. Thus, when locks are expected to be held
for short times, spinlocks are useful; they are often employed on multiprocessor systems where
one thread can "spin" on one processor while another thread performs its critical section on
another processor.)

WAIT() AND SIGNAL() OPERATION :


we can modify the definition of the wait() and signal() semaphore operations. When a process
executes the wait () operation and finds that the semaphore value is not positive, it must wait.
However, rather than engaging in busy waiting, the process can block itself.

The block operation places a process into a waiting queue associated with the semaphore, and the
state of the process is switched to the waiting state. Then control is transferred to the CPU
scheduler, which selects another process to execute. A process that is blocked, waiting on a
semaphore S, should be restarted when some other process executes a signal() operation. The
process is restarted by a wakeup () operation, which changes the process from the waiting state to
the ready state. The process is then placed in the ready queue. (The CPU may or may not be
switched from the running process to the newly ready process, depending on the CPU-scheduling
algorithm.)

Each semaphore has an integer value and a list of processes list. When a process must wait on a
semaphore, it is added to the list of processes. A signal() operation removes one process from the
list of waiting processes and awakens that process. The wait() semaphore operation can now be
defined as
The block() operation suspends the process that invokes it. The wakeup(P) operation resumes the
execution of a blocked process P. These two operations are provided by the operating system as
basic system calls. It is critical that semaphores be executed atomically. We must guarantee that
no two processes can execute wait() and signal() operations on the same semaphore at the same
time. This is a critical-section problem; and in a single-processor environment (that is, where only
one CPU exists), we can solve it by simply inhibiting interrupts during the time the wait() and
signal() operations are executing. This scheme works in a single-processor environment because,
once interrupts are inhibited, instructions from different processes cannot be interleaved. Only the
currently running process executes until interrupts are reenabled and the scheduler can regain
control.

CRITICAL SECTION PROBLEM:


Consider a system consisting of n processes {Po, P1 , ... , P11 _ I}. Each process has a segment of
code, called a CRITICAL SECTION.in which the process may be changing common variables,
updating a table, writing a file, and so on. The important feature of the system is that, when one
process is executing in its critical section, no other process is to be allowed to execute in its
critical section. That is, no two processes are executing in their critical sections at the same time.
The critical-section problem is to design a protocol that the processes can use to cooperate. Each
process must request permission to enter its critical section. The section of code implementing this
request is the ENTRY SECTION.The critical section may be followed by an exit The remaining
code is the REMINDER SECTION.

The entry section and exit section are enclosed in boxes to highlight these important segments of
code. A solution to the critical-section problem must satisfy the following three requirements:

1. Mutual exclusion. If process Pi is executing in its critical section, then no other processes can be
executing in their critical sections.

2. Progress. If no process is executing in its critical section and some processes wish to enter their
critical sections, then only those processes that are not executing in their remainder sections can
participate in deciding which will enter its critical section next, and this selection carmot be
postponed indefinitely.

3.Bounded waiting. There exists a bound, or limit, on the number of times that other processes are
allowed to enter their critical sections after a process has made a request to enter its critical section
and before that request is granted.

Two general approaches are used to handle critical sections in operating systems:
(1) preemptive kernels
(2) nonpreemptive kernels.
A preemptive kernel allows a process to be preempted while it is running in kernel mode. A
nonpreemptive kernel does not allow a process running in kernel mode to be preempted; a kernel-
mode process will run until it exits kernel mode, blocks, or voluntarily yields control of the CPU.
Obviously, a nonpreemptive kernel is essentially free from race conditions on kernel data
structures, as only one process is active in the kernel at a time. We cannot say the same about
preemptive kernels, so they must be carefully designed to ensure that shared kernel data are free
from race conditions. Preemptive kernels are especially difficult to design for SMP architectures,
since in these environments it is possible for two kernel-mode processes to run simultaneously on
different processors.

PROCESS SCHEDULING:
The process scheduler selects an available process (possibly from a set of several available
processes) for program execution on the CPU. For a single-processor system, there will never
be more than one running process. If there are more processes, the rest will have to wait until
the CPU is free and can be rescheduled.

Scheduling Queues:
● As processes enter the system, they are put into a job queue, which consists of all
processes in the system.
● The processes that are residing in main memory and are ready and waiting to execute
are kept on a list called the ready queue.This queue is generally stored as a linked
list. A ready-queue header contains pointers to the first and final PCBs in the list.
Each PCB includes a pointer field that points to the next PCB in the ready queue.
● When a process is allocated the CPU, it executes for a while and eventually quits, is
interrupted, or waits for the occurrence of a particular event, such as the completion of
an I/0 request. Suppose the process makes an I/O request to a shared device, such as a
disk. Since there are many processes in the system, the disk may be busy with the I/0
request of some other process. The process therefore may have to wait for the disk.
The list of processes waiting for a particular I/0 device is called a device queue. Each
device has its own device queue .

A new process is initially put in the ready queue. It waits there until it is selected for
execution, or is dispatched. Once the process is allocated the CPU and is executing, one of
several events could occur:
● The process could issue an I/0 request and then be placed in an I/0 queue.
● The process could create a new subprocess and wait for the subprocess's termination.
● The process could be removed forcibly from the CPU, as a result of an interrupt, and
be put back in the ready queue.
SCHEDULER:
A process migrates among the various scheduling queues throughout its lifetime. The
operating system must select, for scheduling purposes, processes from these queues in some
fashion. The selection process is carried out by the appropriate scheduler.

● The long-term scheduler, or job scheduler, selects processes from this pool and loads
them into memory for execution.The long-term scheduler executes much less
freqvently; minutes may separate the creation of one new process and the next. The
long-term scheduler
controls the degree of multiprogramming (the number of processes in memory). If the
degree of multiprogramming is stable, then the average rate of process creation must
be equal to the average departure rate of processes leaving the system. Thus, the long-
term scheduler may need to be invoked only when a process leaves the system.
Because of the longer interval between executions, the long-term scheduler can afford
to take more tin<e to decide which process should be selected for execution.

● The short-term scheduler, or CPU scheduler, selects from among the processes that
are ready to execute and allocates the CPU to one of them.The short-term scheduler
must select a new process for the CPU frequently. A process may execute for only a
few milliseconds before waiting for an I/0 request. Often, the short-term scheduler
executes at least once every 100 milliseconds. Because of the short time between
executions, the short-term scheduler must be fast. If it takes 10 milliseconds to decide
to execute a process for 100 milliseconds, then 10 I (100 + 10) = 9 percent of the CPU
is being used (wasted) simply for scheduling the work.
● The key idea behind a medium-term scheduler is that sometimes it can be advantageous to
remove processes from memory (and from active contention for the CPU) and thus reduce
the degree of multiprogramrning. Later, the process can be reintroduced into memory, and
its execution can be continued where it left off. This scheme is called swapping. The
process is swapped out, and is later swapped in, by the medium-term scheduler. Swapping
may be necessary to improve the process mix or because a change in memory requirements
has overcommitted available memory, requiring memory to be freed up.

Context Switch:
When an interrupt occurs, the system needs to save the current CONTEXT of the process
running on the CPU so that it can restore that context when its processing is done,
essentially suspending the process and then resuming it. The context is represented in the
PCB of the process; it includes the value of the CPU registers, the process state and
memory-management information.

Switching the CPU to another process requires performing a state save of the current
process and a state restore of a different process. This task is known as a CONTEXT
SWITCH.When a context switch occurs, the kernel saves the context of the old process in
its PCB and loads the saved context of the new process scheduled to run. Context-switch
time is pure overhead, because the system does no useful work while switching. Its speed
varies from machine to machine, depending on the memory speed, the number of registers
that must be copied, and the existence of special instructions (such as a single instruction
to load or store all registers). Typical speeds are a few milliseconds.

Scheduling criteria:

CPU utilization.
We want to keep the CPU as busy as possible. Conceptually, CPU utilization can range
from 0 to 100 percent. In a real system, it should range from 40 percent (for a lightly
loaded system) to 90 percent (for a heavily used system).
Throughput.
If the CPU is busy executing processes, then work is being done. One measure of work is
the number of processes that are completed per time unit, called throughput. For long
processes, this rate may be one process per hour; for short transactions, it may be ten
processes per second.

Turnaround time. From the point of view of a particular process, the an important
criterion is how long it takes to execute that process. The interval from the time of
submission of a process to the time of completion is the turnaround time. Turnaround time
is the sum of the periods spent waiting to get into memory, waiting in the ready queue,
executing on the CPU, and doing I/0.

Waiting time. The CPU-scheduling algorithm does not affect the amount of time during
which a process executes or does I/0; it affects only the an1.ount of time that a process
spends waiting in the ready queue. Waiting time is the sum of the periods spent waiting in
the ready queue.

Response time. In an interactive system, turnaround time may not be


the best criterion. Often, a process can produce some output fairly early
and can continue computing new results while previous results are being
output to the user. Thus, another measure is the time from the submission
of a request until the first response is produced. This measure, called
response time, is the time it takes to start responding, not the time it takes
to output the response. The turnaround time is generally limited by the
speed of the output device.

SCHEDULING ALGORITHM:
CPU scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated the CPU.

First-Come, First-Served Scheduling:


By far the simplest CPU-scheduling algorithm is the first-come, first-served (FCFS) scheduling
algorithm. With this scheme, the process that requests the CPU first is allocated the CPU first. The
implementation of the FCFS policy is easily managed with a FIFO queue. When a process enters
the ready queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated
to the process at the head of the queue. The running process is then removed from the queue. The
code for FCFS scheduling is simple to write and understand.

On the negative side, the average waiting time under the FCFS policy is often quite long. Consider
the following set of processes that arrive at time 0, with the length of the CPU burst given in
milliseconds:
Process Burst Time
P1 24
P2 3
P3 3

the FCFS scheduling algorithm is nonpreemptive. Once the CPU has been allocated to a process,
that process keeps the CPU until it releases the CPU, either by terminating or by requesting I/0.
The FCFS algorithm is thus particularly troublesome for time-sharing systems, where it is
important that each user get a share of the CPU at regular intervals. It would be disastrous to allow
one process to keep the CPU for an extended period.

Shortest-Job-First Scheduling:
A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling algorithm. This
algorithm associates with each process the length of the process's next CPU burst. When the CPU
is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU
bursts of two processes are the same, FCFS scheduling is used to break the tie. Note that a more
appropriate term for this scheduling method would be the shortest-next-CPU-burst algorithm,
because scheduling depends on the length of the next CPU burst of a process, rather than its total
length. We use the term SJF because most people and textbooks use this term to refer to this type
of scheduling.
The real difficulty with the SJF algorithm is knowing the length of the next CPU request. For
long-term (job) scheduling in a batch system, we can use as the length the process time limit that a
user specifies when he submits the job.

Priority Scheduling:

The SJF algorithm is a special case of the general priority scheduling algorithm. A priority is
associated with each process, and the CPU is allocated to the process with the highest priority.
Equal-priority processes are scheduled in FCFS order. An SJF algorithm is simply a priority
algorithm where the priority (p) is the inverse of the (predicted) next CPU burst. The larger the
CPU burst, the lower the priority, and vice versa.

In terms of high priority and low priority. Priorities are generally indicated by some fixed range
of numbers, such as 0 to 7 or 0 to 4,095. However, there is no general agreement on whether 0 is
the highest or lowest priority. Some systems use low numbers to represent low priority; others use
low numbers for high priority. This difference can lead to confusion. In this text, we assume that
low numbers represent high priority. As an example, consider the following set of processes,
assumed to have arrived at time 0 in the order P1, P2, · · ·, Ps, with the length of the CPU burst
given in milliseconds:
The average waiting time is 8.2 milliseconds.

Priorities can be defined either internally or externally. Internally defined priorities use some
measurable quantity or quantities to compute the priority of a process. For example, time limits,
memory requirements, the number of open files, and the ratio of average I/0 burst to average CPU
burst have been used in computing priorities. External priorities are set by criteria outside the
operating system, such as the importance of the process, the type and amount of funds being paid
for computer use, the department sponsoring the work, and other, often political factors

A major problem with priority scheduling algorithms is indefinite blocking, or starvation. A


process that is ready to run but waiting for the CPU can be considered blocked. A priority
scheduling algorithm can leave some low priority processes waiting indefinitely. In a heavily
loaded computer system, a steady stream of higher-priority processes can prevent a low-priority
process from ever getting the CPU.A solution to the problem of indefinite blockage of low-
priority processes is aging. Aging is a technique of gradually increasing the priority of processes
that wait in the system for a long time.

Round-Robin Scheduling:

The round-robin (RR) scheduling algorithm is designed especially for timesharing systems. It is
similar to FCFS scheduling, but preemption is added to enable the system to switch between
processes. A small unit of time, called a time quantum or time slice, is defined. A time quantum is
generally fronc 10 to 100 milliseconds in length. The ready queue is treated as a circular queue.
The CPU scheduler goes around the ready queue, allocating the CPU to each process for a time
interval of up to 1 time quantum.

To implement RR scheduling, we keep the ready queue as a FIFO queue o£ processes. New
processes are added to the tail of the ready queue. The CPU scheduler picks the first process from
the ready queue, sets a timer to interrupt after 1 time quantum, and dispatches the process. One of
two things will then happen. The process may have a CPU burst of less than 1 time quantum. In
this case, the process itself will release the CPU voluntarily. The scheduler will then proceed to
the next process in the ready queue. Otherwise, if the CPU burst of the currently running process
is longer than 1 time quantum, the timer will go off and will cause an interrupt to the operating
system. A context switch will be executed, and the process will be put at the tail o£ the ready
queue. The CPU scheduler will then select the next process in the ready queue.

The average waiting time under the RR policy is often long. Consider the following set of
processes that arrive at time 0, with the length of the CPU burst given in milliseconds:

of the process accordingly (Figure 5.4). Thus, we want the time quantum to be large with respect
to the context switch time. If the context-switch time is approximately 10 percent of the time
quantum, then about 10 percent of the CPU time will be spent in context switching. In practice,
most modern systems have time quanta ranging from 10 to 100 milliseconds. The time required
for a context switch is typically less than 10 microseconds; thus, the context-switch time is a small
fraction of the time quantum.
Multilevel Queue Scheduling:
A multilevel 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 an RR algorithm, while the background
queue is scheduled by an FCFS algorithm.

Let's look at an example of a multilevel queue scheduling algorithm with five queues, listed below
in order of priority:

System processes
Interactive processes
Interactive editing processes
Batch processes
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 would be preempted.

Multilevel Feedback Queue Scheduling:


The multilevel feedback queue scheduling algorithm, in contrast, allows a process to move
between queues. The idea is to separate processes according to the characteristics of their CPU
bursts. If a process uses too much CPU time, it will be moved to a lower-priority queue. This
scheme leaves I/O-bound and interactive processes in the higher-priority queues. In addition, 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.

A process entering the ready queue is put in queue 0. A process in queue 0 is given a time
quantum of 8 milliseconds. If it does not finish within this time, it is moved to the tail of queue 1.
If queue 0 is empty, the process at the head of queue 1 is given a quantum of 16 milliseconds. If it
does not complete, it is preempted and is put into queue 2. Processes in queue 2 are run on an
FCFS basis but are run only when queues 0 and 1 are empty.

This scheduling algorithm gives highest priority to any process with a CPU burst of 8 milliseconds
or less. Such a process will quickly get the CPU, finish its CPU burst, and went off to its next I/0
burst. Processes that need more than 8 but less than 24 milliseconds are also served quickly,
although with lower priority than shorter processes. Long processes automatically sync to queue
2 and are served in FCFS order with any CPU cycles left over from queues 0 and 1.
In general, a multilevel feedback queue scheduler is defined by the
following parameters:

● The number of queues


● The scheduling algorithm for each queue

● The method used to determine when to upgrade a process to a higher-priority queue

● The method used to determine when to demote a process to a lower-priority queue

● The method used to determine which queue a process will enter when that process needs
service

Threads
The process model discussed so far has implied that a process is a program that performs a
single thread of execution. For example, when a process is running a word-processor
program, a single thread of instructions is being executed. This single thread of control allows
the process to perform only one task at one time. The user cannot simultaneously type in
characters and run the spell checker within the same process, for example. Many modern
operating systems have extended the process concept to allow a process to have multiple
threads of execution and thus to perform more than one task at a time. On a system that
supports threads, the PCB is expanded to include information for each thread. Other changes
throughout the system are also needed to support threads.
Multithreading
The term multithreading refers to an operating system's capacity to support execution among
fellow threads within a single process. The advantages of implementation of threads are
resource ownership (Address space and utilized file I/O) and execution (TCB and
scheduling). All threads inside a process will have to share compute resources such as code,
data, files, and memory space with its peer thread, but stacks and registers will not be shared,
and each new thread will have its own stacks and registers.

What is important to note here is that requests from one thread do not block requests from
other separate threads, which improves application responsiveness. The term 'multithreading'
also reduces the number of computing resources used and makes them more efficient. The
concept of multithreading is the event of a system executing many threads, with the execution
of these threads being of two types: Concurrent and Parallel multithread executions.
The concurrent process of threads is defined as the ability of a processor to move execution
resources between threads in a multithreaded process on a single processor. When each
thread in a multithreaded program executes on a separate processor at the same time, it is
referred to as parallel execution.

The threads of the same process share the following items during multithreading:

● Address space
● Global variables
● Accounting information
● Opened files, used memory and I/O
● Child processes
● Pending alarms
● Signal and signal handlers

The following things are private (not shared) to the individual thread of a process in
multithreading.

1. Stack (parameters, temporary, variables return address, etc)


2. TCB (Thread Control Block): It contains 'thread ids', 'CPU state information' (user-
visible, control and status registers, stack pointers), and 'scheduling information' (state
of thread priority, etc.)
Advantages of Multithreading

● Improves execution speed (by combining CPU machine and I/O wait times).
● Multithreading can be used to achieve concurrency.
● Reduces the amount of time it takes for context switching.
● Improves responsiveness.
● Make synchronous processing possible (separates the execution of independent
threads).
● Increases throughput.
● Performs foreground and background work in parallel.

Disadvantages of Multithreading

● Because the threads share the same address space and may access resources such as
open files, difficulties might arise in managing threads if they utilize incompatible
data structures.
● If we don't optimize the number of threads, instead of gaining performance, we may
lose performance.
● If a parent process requires several threads to function properly, the child processes
should be multithreaded as well, as they may all be required.

You might also like