Unit 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

Madan Mohan Malaviya Univ.

of Technology, Gorakhpur

Operating System Concepts (MCA-204)


Unit 1

Neha Singh
Department of Information Technology &
Computer Application, Madan Mohan Malaviya
University of Technology
Gorakhpur-273010,India
21-05-2023 Side 1
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Content
Introduction: Basic architectural concepts, Operating System Services,
interrupt handling, concepts of batch processing, multiprogramming,
time-sharing, real-time operations; Resource Manager view, process view
and hierarchical view of an OS.
Memory management: Partitioning, paging, concepts of virtual memory
demand paging, page replacement algorithms, segmentation,
Segmentation and demand-paging, Cache memory management.

21-05-2023 Side 2
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Operating System
Operating system is a collection of set of programs, which manages all the resources
of computer system. It is an intermediate between the user and computer hardware.
The Operating System performs basic tasks such as-
 Controlling and allocating memory.
 Controlling Input and Output device and facilitating networking and managing
files.
Examples of Operating System- Windows, Unix, Linux, Mac OS etc.
 Characteristics
1. Scheduling
2. Memory Management
3. Security
4. Interact with different software
5. Device Management
6. File Management
21-05-2023 Side 3
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Types of Operating System

1. Batch Operating System


2. Time Sharing OS
3. Distributed OS
4. Network OS
5. Real Time OS
6. Multi-Processing OS
7. Multi-Tasking OS
8. Multi Programming OS

21-05-2023 Side 4
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Advantages
1. OS provides a GUI interface for the users in the form of menu, icons
and buttons.
2. OS also allow us to sharing the resources with other users.
3. It helps users to understand the function of a computer.
4. It is very easy to use.
5. It can be easily updated.
 Disadvantages
1. If the OS is corrupted than it will affect entire system and computer
system will not work.
2. Only same task run at a time.

21-05-2023 Side 5
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Basic Structure of Operating System

User User User

Applications
AAAAAPPPPPPPPPPPPPPPPAAAAAaksxk
jxskkfjhfjhjhvfvfbmvbfmdnnvbncm
Operating System

Hardware
CPU I/O Secondary RAM
Devices Memory

21-05-2023 Side 6
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

1. Users- Users are layman person or IT professionals who is using the


computer to fulfill it’s requirement.
2. Application- Applications are the software that users wants to use.
3. Operating System- Act as a bridge/interface between the computer
user and it’s hardware.
4. Hardware- It is the physical part of the computer system, it include
CPU, I/O devices, Ram etc.

21-05-2023 Side 7
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Operating System Services


An Operating System provides services to both the users and to the
programs. It provides programs an environment to execute. It provides
users the services to execute the programs in a convenient manner.
Following are a few common services provided by an operating system −
1. Program execution
2. I/O operations
3. File System manipulation
4. Communication
5. Error Detection
6. Resource Allocation
7. Protection

21-05-2023 Side 8
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 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.
Following are the major activities of an operating system with respect to
program management −
 Loads a program into memory.
 Executes the program.
 Handles program's execution.
 Provides a mechanism for process synchronization.
 Provides a mechanism for process communication.
 Provides a mechanism for deadlock handling

21-05-2023 Side 9
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 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.

21-05-2023 Side 10
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 File System Manipulation


A file represents a collection of related information. Computers can store files on the
disk (secondary storage), for long-term storage purpose.
Following are the major activities of an operating system with respect to file
management −
 Program needs to read a file or write a file.
 The operating system gives the permission to the program for operation on file.
 Permission varies from read-only, read-write, denied and so on.
 Operating System provides an interface to the user to create/delete files.
 Operating System provides an interface to the user to create/delete directories.
 Operating System provides an interface to create the backup of file system.

21-05-2023 Side 11
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Communication
In case of distributed systems which are a collection of processors that do not
share memory, peripheral devices, or a clock, the operating system manages
communications between all the processes.
Following are the major activities of an operating system with respect to
communication −
 Two processes often require data to be transferred between them both the
processes can be on one computer or on different computers, but are
connected through a computer network.
 Communication may be implemented by two methods, either by Shared
Memory or by Message Passing.

21-05-2023 Side 12
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 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
21-05-2023 Side 13
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Protection
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 external I/O devices are protected from invalid
access attempts.
 The OS provides authentication features for each user by means of
passwords.

21-05-2023 Side 14
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Interrupt Handling

 Interrupt handling is a key function in real-time software. Only those physical


interrupts which of high enough priority can be entered into system interrupt
table. The software assigns each interrupt to a handler in the interrupt table.
An interrupt handler is just a routine containing a sequence of operations.
 The routine for handling a specific interrupt is known as the interrupt service
routine for the specific interrupt. The RTOS layer often stores a list of the
pairs of interrupts and their handlers known as the interrupt table. All interrupt
handlers run constant within the background process. Thus, an interrupt can be
handled either as a thread or as a sub-process within a task or process.

21-05-2023 Side 15
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Concept of Batch Processing-

 Batch processing system will work to submit similar kind of job


together. In this operating system user do not interact directly with our
computer system.
 OS defines a job which has predefined sequence of commands,
programs and data as a single unit. OS keeps a number a jobs in
memory and executes them without any manual information.

21-05-2023 Side 16
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Jobs are processed in the order of submission i.e. first come first served
fashion. When job completes its execution, its memory is released and the
output for the job gets copied into an output spool for later printing or
processing.

Job 1 Batch

Operating CPU
Job 2 Batch
System

Job n Batch

21-05-2023 Side 17
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Advantages
1. Batch processing takes much of the work of the operator to the
computer.
2. It is useful when we, working with large files which can take
more time to execute.
 Disadvantages
1. Once the job is submitted, the user did not have any interaction
with it.

21-05-2023 Side 18
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Multiprogramming

When two or more programs are residing in memory at the same time, then sharing
the processor is referred to the multiprogramming. Multiprogramming assumes a
single shared processor. Multiprogramming increases CPU utilization by
organizing jobs.
Operating system 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 job in the memory.

21-05-2023 Side 19
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Following figure shows the memory layout for a multiprogramming system.

Operating System

Job 1
Job 2
:
:
:
Job n

Empty Space

21-05-2023 Side 20
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Advantages
1. High and efficient CPU utilization.
2. User feels that many programs are allotted CPU almost
simultaneously.
 Disadvantages
1. CPU scheduling is required.
2. To accommodate many jobs in memory, memory
management is required.

21-05-2023 Side 21
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Time –Sharing
 The time sharing operating system works with time sharing concept.
 Here, the CPU will provide a same time period to each an every
process to complete its task as soon as possible weather it is a long
process or short process.

Process 1 Process 4

CPU
Process 2 Process 5

Process 3 Process 6

21-05-2023 Side 22
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Advantages of Time sharing


1. Provide advantage of quick response.
2. Avoids duplication of software.
3. If the process of a task is completed, then the time between the
other tasks increases.
 Disadvantages of Time sharing
1. Sometimes in this operating system there may be a problem of
data communication.

21-05-2023 Side 23
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Real-Time Operations-
Real time operating system is very useful where we required a quick
response for example – missile systems, where the missile has to be
launched at certain time.
In this operating system CPU provides maximum effort's to its task with
quick response. There are two types of real-time operating systems-
Hard Real-Time Systems- Hard real-time systems guarantee that
critical tasks complete on time is called hard real-time systems.
Soft Real-Time Systems- Soft real time systems are less restrictive.
Soft real time systems does not guarantee that a job will be complete
within a specified time period.

21-05-2023 Side 24
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Advantages of Real Time System


1. Real time operating system provide a quick response hence,
generally used in scientific engineering NASA and many more
organization.
 Disadvantages of Real Time System
1. These operating system is very costly.

21-05-2023 Side 25
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Resource Manager View

 The operating system is the resource manager view i.e. it can manage
the resource of a computer system internally. The resources are
processor, memory, files, and I/O devices. In this role, the operating
system keeps track of the status of each resource, and decides who gets
a resource, for how long and when.
 The operating system can be viewed as a resource allocator. A
computer system consists of many resources like - hardware and
software - that must be managed efficiently. The operating system acts
as the manager of the resources, and controls the execution of
programs, etc.

21-05-2023 Side 26
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Process View

 Process is the execution of a program that performs the actions


specified in that program. It can be defined as an execution unit where
a program runs. The OS helps you to create, schedule, and terminates
the processes which is used by CPU. A process created by the main
process is called a child process.
 Process operations can be easily controlled with the help of
PCB(Process Control Block). You can consider it as the brain of the
process, which contains all the crucial information related to
processing like process id, priority, state, CPU registers, etc.

21-05-2023 Side 27
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Process Life Cycle

21-05-2023 Side 28
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Processes in the operating system can be in any of the following


states:
 NEW- The process is being created.
 READY- The process is waiting to be assigned to a processor.
 RUNNING- Instructions are being executed.
 WAITING- The process is waiting for some event to occur(such
as an I/O completion or reception of a signal).
 TERMINATED- The process has finished execution

21-05-2023 Side 29
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Hierarchical View of OS

21-05-2023 Side 30
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Level Functions
3 File Management
2 Memory Management
1 I/O Supervision
0 Resource Management
 Each layer or level of the structure can use the functions provided by lower
levels just as if they were part of the real machine.
 Level 0, often called the kernel of the operating system, deals directly with
the underlying hardware.
 User program deals with the highest-level interface.
 Operating system routines at a given level can use the relatively simple
functions and interfaces provided by lower levels.

21-05-2023 Side 31
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 The operating system can be implemented and tested one level at a


time, this reduce the complexity of each part of the operating system
and makes the tasks of implementation and debugging much simpler.
 In a strict hierarchy each level may refer only to the level immediately
beneath it.
 This approach has advantage of simplicity of use, however such a
restriction can lead to inefficiency because it increases the number of
calls that must be performed to reach the inner level.

21-05-2023 Side 32
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Memory Management

 Memory Management is the process of controlling and


coordinating computer memory, assigning portions known as
blocks to various running programs to optimize the overall
performance of the system.
 It is the most important function of an operating system that
manages primary memory. It helps processes to move back and
forward between the main memory and execution disk. It helps OS
to keep track of every memory location, irrespective of whether it
is allocated to some process or it remains free.

21-05-2023 Side 33
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Partitioning
 Memory is divided into different blocks or partitions. Each process is
allocated according to the requirement. Partition allocation is an ideal
method to avoid internal fragmentation.
 Below are the various partition allocation schemes :
 First Fit: In this type fit, the partition is allocated, which is the first
sufficient block from the beginning of the main memory.
 Best Fit: It allocates the process to the partition that is the first smallest
partition among the free partitions.
 Worst Fit: It allocates the process to the partition, which is the largest
sufficient freely available partition in the main memory.
 Next Fit: It is mostly similar to the first Fit, but this Fit, searches for the
first sufficient partition from the last allocation point

21-05-2023 Side 34
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

21-05-2023 Side 35
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Paging
 Paging is a technique in which physical memory is broken into blocks
of the same size called pages. When a process is to be executed, it's
corresponding pages are loaded into any available memory frames.
 Logical address space of a process can be non-contiguous and a process
is allocated physical memory whenever the free memory frame is
available. Operating system keeps track of all free frames. Operating
system needs n free frames to run a program of size n pages. Address
generated by CPU is divided into tow parts-
 Page number (p) - Page number is used as an index into a page table
which contains base address of each page in physical memory.
 Page offset (d) - Page offset is combined with base address to define
the physical memory address.

21-05-2023 Side 36
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

21-05-2023 Side 37
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Following figure show the paging table architecture-

21-05-2023 Side 38
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Concept of Virtual Memory

 Virtual memory is a technique that allows the execution


of processes which are not completely available in memory.
The main visible advantage of this scheme is that programs can
be larger than physical memory. Virtual memory is the
separation of user logical memory from physical memory.
 This separation allows an extremely large virtual memory to be
provided for programmers when only a smaller physical
memory is available. Following are the situations, when entire
program is not required to be loaded fully in main memory.

21-05-2023 Side 39
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

21-05-2023 Side 40
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Demand Paging
 A demand paging system is quite similar to a paging system with swapping. When
we want to execute a process, we swap it into memory. Rather than swapping the
entire process into memory, however, we use a lazy swapper called pager.
 When a process is to be swapped in, the pager guesses which pages will be used
before the process is swapped out again. Instead of swapping in a whole process,
the pager brings only those necessary pages into memory. Thus, it avoids reading
into memory pages that will not be used in anyway, decreasing the swap time and
the amount of physical memory needed.
 Hardware support is required to distinguish between those pages that are in
memory and those pages that are on the disk using the valid-invalid bit scheme.
Where valid and invalid pages can be checked by checking the bit. Marking a page
will have no effect if the process never attempts to access the page. While the
process executes and accesses pages that are memory resident, execution proceeds
normally.
21-05-2023 Side 41
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

21-05-2023 Side 42
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Access to a page marked invalid causes a page-fault trap. This trap is the result of the
operating system's failure to bring the desired page into memory. But page fault can
be handled as following-

21-05-2023 Side 43
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Step 1: Check an internal table for this process, to determine whether the reference was a
valid or it was an invalid memory access.

Step 2: If the reference was invalid, terminate the process. If it was valid, but page have not
yet brought in, page in the latter.

Step 3: Find a free frame.

Step 4: Schedule a disk operation to read the desired page into the newly allocated frame.

Step 5: When the disk read is complete, modify the internal table kept with the process and
the page table to indicate that the page is now in memory.

Step 6: Restart the instruction that was interrupted by the illegal address trap. The process
can now access the page as though it had always been in memory. Therefore, the operating
system reads the desired page into memory and restarts the process as though the page had
always been in memory.
21-05-2023 Side 44
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Page Replacement Algorithm


 The page replacement algorithm decides which memory page is to be replaced. The
process of replacement is sometimes called swap out. Page replacement is done when the
requested page is not found in the main memory (page fault).
 A page replacement algorithm tries to select which pages should be replaced so as to
minimize the total number of page misses. There are many different page replacement
algorithms. These algorithms are evaluated by running them on a particular string of
memory reference and computing the number of page faults.
 If a process requests for page and that page is found in the main memory then it is
called page hit, otherwise page miss or page fault.
 Page replacement algorithm is mainly three types-
1. First In First Out (FIFO)
2. Least Recently Used (LRU)
3. Optimal Page Algorithm
21-05-2023 Side 45
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 First In First Out (FIFO) Algorithm


 Oldest page in main memory is the one which will be selected for replacement.
 Easy to implement, keep a list, replace pages from the tail and add new pages at
the head.
 Example1: Consider page reference string 1, 3, 0, 3, 5, 6 with 3 page frames.
Find number of page faults.

21-05-2023 Side 46
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Total Hit Pages = 1


 Hit Ratio = No of Hit / Total no of References* 100
= 1/ 7 * 100 = 14.2857
 Page Fault Ratio = No of Page Fault / Total no of References * 100
= 6/7*100 = 85.7142
Example 2: Consider the page reference string of size 12: 1, 2, 3, 4, 5, 1, 3, 1, 6,
3, 2, 3 with frame size 4(i.e. maximum 4 pages in a frame)
Example 3: Consider the page reference string of size 15: 7, 0, 1, 2, 0, 3, 0, 4, 2,
3, 0, 3, 1, 2, 0 with frame size 3(i.e. maximum 3 pages in a frame)

21-05-2023 Side 47
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
 Least Recently Used (LRU) Algorithm
 Page which has not been used for the longest time in main memory is the one which
will be selected for replacement.
 In this algorithm page will be replaced which is least recently used.

21-05-2023 Side 48
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Example 2: Consider the page reference string of size 12: 1, 2, 3, 4, 5, 1, 3, 1, 6, 3,


2, 3 with frame size 4(i.e. maximum 4 pages in a frame)
Example 3: Consider the page reference string of size 15: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3,
0, 3, 1, 2, 0 with frame size 3(i.e. maximum 3 pages in a frame)

 Optimal Page algorithm


 An optimal page-replacement algorithm has the lowest page-fault rate of all
algorithms. An optimal page-replacement algorithm exists, and has been called
OPT or MIN.
 Replace the page that will not be used for the longest period of time .
Example 1: Consider the page reference string of size 12: 1, 2, 3, 4, 5, 1, 3, 1, 6, 3,
2, 3 with frame size 4(i.e. maximum 4 pages in a frame)
Example 2: Consider the page reference string of size 18: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3,
0, 3, 2, 1, 2, 0, 1, 7 with frame size 3(i.e. maximum 3 pages in a frame)

21-05-2023 Side 49
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

21-05-2023 Side 50
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Segmentation

 Segmentation is a technique to break memory into logical pieces where


each piece represents a group of related information. For example, data
segments or code segment for each process, data segment for operating
system and so on. Segmentation can be implemented using or without
using paging.
 Address generated by CPU is divided into tow parts-
1. Segment number (s) - Segment number is used as an index into a
segment table which contains base address of each segment in physical
memory and a limit of segment.
2. Segment offset (o) - Segment offset is first checked against limit and
then is combined with base address to define the physical memory
address.

21-05-2023 Side 51
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

21-05-2023 Side 52
Madan Mohan Malaviya Univ. of Technology, Gorakhpur
Segmentation and Demand Paging
S.No. Demand Paging Segmentation
In demand paging, the pages are of While in segmentation, segments can be of
1.
equal size. different size.

Page size is fixed in the demand Segment size may vary in segmentation as it
2.
paging. grants dynamic increase of segments.

3. It does not allows sharing of the pages. While segments can be shared in segmentation.

In demand paging, on demand pages In segmentation, during compilation segments are


4.
are loaded in the memory. allocated to the program.

Page map table in demand paging Segment map table in segmentation demonstrates
5.
manages record of pages in memory. every segment address in the memory.

It provides large virtual memory and It provides virtual memory and maximum size of
6.
have more efficient use of memory. segment is defined by the size of memory.
21-05-2023 Side 53
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

Cache Memory Management


 Cache is a type of memory that is used to increase the speed of data
access. Normally, the data required for any process resides in the main
memory. However, it is transferred to the cache memory temporarily if it
is used frequently enough.
 A diagram to better understand the data transfer in cache management is
as follows −

21-05-2023 Side 54
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Cache Performance
The cache performance can be explained using the following steps −
 If a process needs some data, it first searches in the cache memory. If the
data is available in the cache, this is termed as a cache hit.
 If the data is not in the cache then it is termed as a cache miss. Then the data
is obtained from the main memory. After that the data is transferred to the
cache memory.
 The performance of the cache is measured using the hit ratio. It is the
number of cache hits divided by the total cache accesses. The formula for
this is:

21-05-2023 Side 55
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Types of Cache Memory


There are mainly two types of cache memory i.e. primary cache and secondary
cache.
 Primary Cache
Primary cache is very fast and its access time is similar to the processor
registers. This is because it is built onto the processor chip. It is also known as
a level 1 cache and is build using static RAM (SRAM).
 Secondary Cache
The secondary cache or external cache is cache memory that is external to the
primary cache. It is located between the primary cache and the main memory.
It is also known as a level 2 cache.

21-05-2023 Side 56
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

 Advantages of Cache Memory


1. Cache memory is faster than main memory as it is located on the processor
chip itself.
2. The memory access time is considerably less for cache memory as it is quite
fast. This leads to faster execution of any process.
3. The cache memory can store data temporarily as long as it is frequently
required.
 Disadvantages of Cache Memory
1. it is quite expensive.

21-05-2023 Side 57
Madan Mohan Malaviya Univ. of Technology, Gorakhpur

THE END

21-05-2023 Side 58

You might also like