0% found this document useful (0 votes)
3 views

Os by ArifurRefat

Uploaded by

refatari
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)
3 views

Os by ArifurRefat

Uploaded by

refatari
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/ 85

1

Chapter 1

Q. What is Operating System? What are the main purposes of an Operating


System? (18-19) 3
Q. What are the main purposes of an Operating System? (17-18) 2
Operating System: An operating system is a program that manages the computer hardware.
It also provides a basis for application programs and acts as an intermediary between the
computer user and the computer hardware.
The main purposes of an operating system: An operating system (OS) serves three primary
functions:

1. Resource Management: The OS efficiently manages the computer’s resources,


including memory, CPU, and input/output devices. It ensures that each running
process or application has access to the necessary resources without interfering with
other processes.
2. Control and Coordination: Acting as a traffic controller, the OS coordinates the
execution of programs and services on the computer. It provides a uniform interface
for all applications and services, making it easy for users to interact with the system.
3. User Interface (UI): The OS offers a user-friendly interface that enables users to
interact with the computer and its applications. Whether graphical or text-based, the
UI provides access to system utilities, file management, customization of settings, and
network resources.

In summary, operating systems act as the bridge between hardware and software, allowing us
to navigate our devices and access essential programs
Types of Operating Systems: There are several types of Operating Systems which are —
⸰ Batch Operating System ⸰ Time-Sharing Operating System
⸰ Multi-Programming System ⸰ Distributed Operating System
⸰ Multi-Processing System ⸰ Network Operating System
⸰ Multi-Tasking Operating System ⸰ Real-Time Operating System
Q. Write the advantages and disadvantages of Time Sharing Systems and Distributed

Arifur Refat
2

systems? (18-19) 4
Q. Write the advantages and disadvantages of Multiprocessor Systems and Distributed
systems? (17-18) 3
1. Batch Operating System: This type of operating system does not interact with the
computer directly. There is an operator which takes similar jobs having the same requirement
and groups them into batches. It is the responsibility of the operator to sort jobs with similar
needs.

Advantages Disadvantages
Multiple users can share the batch systems. Batch systems are hard to debug.
The idle time for the batch system is very The other jobs will have to wait for an
less. unknown time if any job fails.
It is easy to manage large work repeatedly It is sometimes costly.
in batch systems.

2. Multi-Programming Operating System: Multiprogramming Operating Systems can


be simply illustrated as more than one program is present in the main memory and any one of
them can be kept in execution. This is basically used for better execution of resources.

Advantages Disadvantages
Multi Programming increases the There is not any facility for user interaction
Throughput of the System. of system resources with the system.
It helps in reducing the response time.

Arifur Refat
3

3. Multi-Processing Operating System: Multi-Processing Operating System is a type


of Operating System in which more than one CPU is used for the execution of resources. It
betters the throughput of the System.

Advantages Disadvantages
It increases the throughput of the system. Due to the multiple CPU, it can be more
complex and somehow difficult to
As it has several processors, so, if one processor
understand.
fails, we can proceed with another processor.

4. Multi-Tasking Operating System: Multitasking Operating System is simply a


multiprogramming Operating System with having facility of a Round-Robin Scheduling
Algorithm. It can run multiple programs simultaneously. There are two types of Multi-
Tasking Systems which are Preemptive Multi-Tasking and Cooperative Multi-Tasking.

Advantages Disadvantages
Multiple Programs can be executed simultaneously The system gets heated in case of
in Multi-Tasking Operating System. heavy programs multiple times.
It comes with proper memory management.

Arifur Refat
4

5. Time-Sharing Operating Systems: Each task is given some time to execute so that
all the tasks work smoothly. Each user gets the time of the CPU as they use a single system.
These systems are also known as Multitasking Systems. The task can be from a single user or
different users also. The time that each task gets to execute is called quantum. After this time
interval is over OS switches over to the next task.

Advantages Disadvantages
Each task gets an equal opportunity. High Overhead: Time-sharing systems have
a higher overhead than other operating
CPU idle time can be reduced.
systems due to the need for scheduling,
context switching.
Resource Sharing: Time-sharing systems Complexity: Time-sharing systems are
allow multiple users to share hardware complex and require advanced software to
resources such as the CPU, memory, and manage multiple users simultaneously. This
peripherals, reducing the cost of hardware complexity increases the chance of bugs and
and increasing efficiency. errors.
Improved Productivity: Time-sharing allows Security Risks: With multiple users sharing
users to work concurrently, thereby resources, the risk of security breaches
reducing the waiting time for their turn to increases. Time-sharing systems require
use the computer. This increased careful management of user access,
productivity translates to more work getting authentication, and authorization to ensure
done in less time. the security of data and software.

Arifur Refat
5

6. Distributed Operating System: Various autonomous interconnected computers


communicate with each other using a shared communication network. Independent systems
possess their own memory unit and CPU.

Advantages Disadvantages
Failure of one will not affect the other Failure of the main network will stop the
network communication, as all systems are entire communication.
independent of each other.
Since resources are being shared, To establish distributed systems the
computation is highly fast and durable. language is used not well-defined yet.
Load on host computer reduces. These types of systems are not readily
These systems are easily scalable as many available as they are very expensive. Not
systems can be easily added to the network. only that the underlying software is highly
complex and not understood well yet.
Delay in data processing reduces.

Arifur Refat
6

7. Network Operating System: These systems run on a server and provide the
capability to manage data, users, groups, security, applications, and other networking
functions. These types of operating systems allow shared access to files, printers, security,
applications, and other networking functions over a small private network.

Advantages Disadvantages
Highly stable centralized servers. Servers are costly.
Security concerns are handled through servers. Maintenance and updates are required
New technologies and hardware up-gradation regularly.
are easily integrated into the system.
Server access is possible remotely from User has to depend on a central location
different locations and types of systems. for most operations.

Arifur Refat
7

8. Real-Time Operating System: These types of OSs serve real-time systems. The time
interval required to process and respond to inputs is very small. This time interval is called
response time.
Real-time systems are used when there are time requirements that are very strict like missile
systems, air traffic control systems, robots, etc. Types of Real-Time Operating Systems
• Hard Real-Time Systems: Hard Real-Time OSs are meant for applications where
time constraints are very strict and even the shortest possible delay is not acceptable.
These systems are built for saving life like automatic parachutes or airbags which are
required to be readily available in case of an accident. Virtual memory is rarely found
in these systems.
• Soft Real-Time Systems: These OSs are for applications where time-constraint is
less strict.

Advantages Disadvantages
Maximum utilization of devices and systems, Very few tasks run at the same time
thus more output from all the resources.
These types of systems are error-free. The system resources are expensive.
Focus on running applications and less The algorithms are very complex and
importance on applications that are in the difficult for the designer to write on.
queue.
The time assigned for shifting tasks in these Sometimes the system resources are not so
systems is very less. good
Memory allocation is best managed in these It is not good to set thread priority as these
types of systems. systems are very less prone to switching
tasks.

Q. Show the memory layout of a multi-programmed system with an appropriate


diagram. (17-18) 2

Arifur Refat
8

Q. What is Overlays? (18-19) 1


Overlays in Memory Management operate on the premise that when a process runs, it does
not consume the complete program at the same time but rather a subset of it.

Storage Divice Hierarchy:

Q. Write the major activities of Operating System.


1. Process Management: Handling processes efficiently.

Arifur Refat
9

2. Memory Management: Managing system memory allocation.


3. File System Management: Organizing and accessing files.
4. Device Management: Controlling hardware devices.
5. Security: Ensuring system integrity and user safety.
6. User Interface: Providing interaction with the system.
7. Networking: Enabling communication between devices.
8. Error Handling: Detecting and recovering from errors.
9. Resource Allocation: Optimizing resource usage.
10. Performance Optimization: Monitoring and improving system performance.

Chapter 2

Q. What is System calls? (18-19) 2


System Call: System call is the programmatic way in which a computer program requests a
service from the kernel of the operating system. These calls are generally available as routines
written in C and C++. System calls provide an interface to the services made available by an
operating system.

Arifur Refat
10

Chapter 3

Q. What is process? Explain process state with proper diagram. (17-18 & 18-19) 5
Process: A program loaded into memory and executing is called a process. A process is more
than the program code, which is sometimes known as the text section. It also includes the
current activity, as represented by the value of the program counter and the contents of the
processor's registers. A process generally also includes the process stack, which contains
temporary data (such as function parameters, return addresses, and local variables), and a data
section, which contains global variables. A process may also include a heap, which is
memory that is dynamically allocated during process run time. The structure of a process in
memory is shown in the following Figure.

Arifur Refat
11

Figure: Process in Memory

Thread: A thread is a small unit of execution within a process that enables concurrent tasks.
Threads share resources with other threads in the same process and can execute tasks
simultaneously. They're lightweight and efficient but require careful management to prevent
issues like race conditions.
Q. Show the process state with a diagram. (17-18) 4
Process State: As a process executes, it changes state. The state of a process is defined in
part by the current activity of that process.

Arifur Refat
12

Figure: Diagram of Process State

Each process may be in one of the following states:


New: The process is being created.
Running: Instructions are being executed.
Waiting: The process is waiting for some event to occur (such as an I/0 completion).
Ready: The process is waiting to be assigned to a processor.
Terminated: The process has finished execution.

Process Control Block: Each process is represented in the operating system by a process
control block (PCB)—also called a task control block. A PCB is shown in the following
Figure.

Arifur Refat
13

Figure: Process Control Block (PCB)


It contains many pieces of information associated with a specific process, including these:
Process state: The state may be new, ready, running, waiting, halted, and so on.
Program counter: The counter indicates the address of the next instruction to be executed
for this process.
CPU registers: The registers vary in number and type, depending on the computer
architecture. CPU registers are being used by a particular process. For example —
accumulators, index registers, stack pointers, and general-purpose registers.
CPU-scheduling information: This information includes a process priority, pointers to
scheduling queues, and any other scheduling parameters.
Memory-management information: This information may include such information as the
value of the base and limit registers and the page tables, or the segment tables, depending on
the memory system used by the operating system.
Accounting information: This information includes the amount of CPU and real time used,
time limits, account numbers, job or process numbers, and so on.
I/O status information: This information includes the list of I/O devices allocated to the
process, a list of open files, and so on.
In brief, the PCB simply serves as the repository for any information that may vary from
process to process.

Process Scheduling: The objective of multiprogramming is to have some process running at


all times, to maximize CPU utilization. The objective of time sharing is to switch the CPU
among processes so frequently that users can interact with each program while it is running.
To meet these objectives, the process scheduler selects an available process for program

Arifur Refat
14

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.
Q. Draw the diagram of a context switch and describe how it works. (17-18) 5
Context switching in OS is the process of storing the state of a running process or thread, so
that it can be restored and resume execution at a later point, and then loading the context or
state of another process or thread and run it.
Steps Of Context Switching in OS
The steps involved in context switching in OS are as follows:
1. Save the state of the current process: This includes saving the contents of the CPU
registers, the memory map, and the stack.
2. Load the state of the new process: This includes loading the contents of the CPU
registers, the memory map, and the stack.
3. Update the process scheduler: The process scheduler needs to be updated to reflect
the new state of the system.
4. Switch to the new process. This involves transferring control to the new process's
instruction pointer.
State Diagram of Context Switching:

Figure: Diagram showing CPU switch from process to process.


Q. Discuss the important process scheduling queues of an operating system. (17-18) 3
Process Scheduling Queues

Arifur Refat
15

The OS maintains all Process Control Blocks (PCBs) in Process Scheduling Queues. The OS
maintains a separate queue for each of the process states and PCBs of all processes in the
same execution state are placed in the same queue. When the state of a process is changed, its
PCB is unlinked from its current queue and moved to its new state queue.

The Operating System maintains the following important process scheduling queues −
Job Queue: The job queue stores all processes in the system. It represents processes that are
waiting to be admitted into the system. New processes enter the job queue when they are
created. The long-term scheduler (also known as the job scheduler) manages this queue.
Ready Queue: The ready queue contains processes residing in main memory, ready and
waiting for execution. Processes in the ready state compete for CPU time. When a process is
ready to run, it enters the ready queue. The short-term scheduler (also called the CPU
scheduler) selects processes from this queue for execution on the CPU.
Device Queues or Waiting Queues: Device queues are also known as I/O queue, device
queue contains the processes which are waiting for the completion of I/O request and the
main is each device has its own device queue.

Chapter 5
5.1.1 CPU –I/O Burst Cycle:

Arifur Refat
16

The success of CPU scheduling depends on an observed property of processes: process


execution consists of a cycle of CPU execution and I/O wait. Processes alternate between
these two states. Process execution begins with a CPU burst. That is followed by an I/O burst,
which is followed by another CPU burst, then another I/O burst, and so on. Eventually, the
final CPU burst ends with a system request to terminate execution.

5.1.2 CPU Scheduler (the short-term scheduler):


Whenever the CPU becomes idle, the operating system must select one of the processes in the
ready queue to be executed. The selection process is carried out by the CPU scheduler, which
selects a process from the processes in memory that are ready to execute and allocates the
CPU to that process.

Q. Describe different types of schedulers. (17-18) 4


The different types of process schedulers in an operating system:

Arifur Refat
17

Long-Term Scheduler (Job Scheduler): The long-term scheduler brings new processes into
the ‘Ready State’. It controls the degree of multi-programming, which refers to the number of
processes present in the ready state at any given time.
Short-Term Scheduler (CPU Scheduler): The short-term scheduler selects one process
from the ready state for execution on the CPU. It ensures that no process suffers from
starvation due to long burst times. It only selects the process; the actual loading onto the CPU
is handled by the dispatcher.
Medium-Term Scheduler: The medium-term scheduler is less common and operates
between the long-term and short-term schedulers. It is responsible for swapping processes in
and out of main memory.

In summary, these three types of schedulers work together to manage processes efficiently in
an operating system, ensuring optimal resource utilization and responsiveness.

Q. What is the difference between long-term scheduler and short-term


scheduler? (17-18) 2
Long-Term Short-Term Medium-Term
It is a job scheduler It is a CPU scheduler It is a process-swapping
scheduler.
Speed is lesser than short Speed is the fastest among It offers medium speed.
term scheduler. all of them.
It brings the new process to It selects those processes It is responsible for
the ‘Ready State’. which are ready to execute. suspending and resuming
the process. It mainly does
swapping.
It controls the degree of It gives less control over It reduces the degree of
multiprogramming. how much multiprogramming.
multiprogramming is done.

5.1.3 Preemptive and Non-preemptive Scheduling:


CPU-scheduling decisions may take place under the following four circumstances:

Arifur Refat
18

1. When a process switches from the running state to the waiting state (as the result of an I/O
request)
2. When a process switches from the running state to the ready state (when an interrupt
occurs)
3. When a process switches from the waiting state to the ready state (at completion of I/O)
4. When a process terminates.
Nonpreemptive or cooperative: Under nonpreemptive scheduling, once the CPU has been
allocated to a process, the process keeps the CPU until it releases the CPU either by
terminating or switching to the waiting state.
Preemptive: Preemptive scheduling is used when a process switches from the running state
to the ready state or from the waiting state to the ready state.
The resources (mainly CPU cycles) are allocated to the process for a limited amount of time
and then taken away, and the process is again placed back in the ready queue if that process
still has CPU burst time remaining. That process stays in the ready queue till it gets its next
chance to execute.
Q. Distinguish between preemptive and Non-preemptive scheduling. (18-19) 2
Preemptive Scheduling Non-Preemptive Scheduling
The CPU is allocated to the processes for a The CPU is allocated to the process till it
certain amount of time. ends its the fewer execution or switches to
waiting state.
The executing process here is interrupted in The executing process here is not
the middle of execution. interrupted in the middle of execution.
This is cost associative as it has to maintain This is not cost associative.
the integrity of shared data.
This scheduling leads to more context This scheduling leads to less context
switches. switches compared to preemptive
scheduling.
It also affects the design of the operating It does not affect the design of the OS
system Kernel. kernel.
Preemptive scheduling is more complex. Simple, but very inefficient.
Example: Round robin method. Example: First come first serve method.

5.1.4 Dispatcher:
Q. Write a short note on dispatcher. (17-18) 2

Arifur Refat
19

The dispatcher is the module that gives control of the CPU to the process selected by the CPU
scheduler. The dispatcher performs context switching, switches to user mode, and jumps to
the proper location in the newly loaded program.
The dispatcher should be as fast as possible, since it is invoked during every process switch.
The time it takes for the dispatcher to stop one process and start another running is known as
the dispatch latency and is illustrated in Figure 5.3

5.2 Scheduling Criteria:


Q. Define Turnaround Time, Waiting Time and Response Time. (17-18) 3
Q. What criteria we should follow for CPU scheduling? Briefly describe. (18-19) 5

Arifur Refat
20

Many criteria have been suggested for comparing CPU-scheduling algorithms. The criteria
include the following:
(i) 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 loaded system).
(ii) Throughput: Throughput is the total amount of work done in a given time.
(iii) Turnaround time: The interval from the time of submission of a process to the time of
completion is the turnaround time.
(iv) Waiting time: Waiting time is the sum of the periods spent waiting in the ready queue.
(v) Response time: Response time is the amount of time it takes for the CPU to respond to a
request made by a process. It is the duration between the arrival of a process and the first
time it runs.
It is desirable to maximize CPU utilization and throughput and to minimize turnaround time,
waiting time, and response time.
5.3 Scheduling Algorithms:
CPU scheduling deals with the problem of deciding which of the processes in the ready queue
is to be allocated the CPU. There are many different CPU-scheduling algorithms.
5.3.1 FCFS Scheduling Algorithm:
The CPU scheduling algorithm First Come, First Served (FCFS), also known as First In, First
Out (FIFO), allocates the CPU to the processes in the order they are queued in the ready
queue.
FCFS uses non-preemptive scheduling, which means that once a CPU has been assigned to a
process, it stays assigned to that process until it is either not terminated or may be interrupted
by an I/O interrupt.
Example:
Process Arrival Time Burst Time
P1 0 8
P2 1 7
P3 2 10

Gantt Chart:
P1 P2 P3
0 8 15 25
Waiting time = Completion time – Burst Time – Arrival Time

P1 waiting time: 0
P2 waiting time: 8-1=7
P3 waiting time: 15-2=13

Arifur Refat
21

Average Waiting Time = = = 6.6667

Response time = Time at which the process gets the CPU for the first time - Arrival time
P1 response time: 0
P2 response time: 8-1=7
P3 response time: 15-2=13

Average Response Time = = = 6.6667

Turnaround Time = Completion Time – Arrival Time


P1 turnaround time: 8-0=8
P2 turnaround time: 15-1=14
P3 turnaround time: 25-2=23

Average Turnaround Time = = = 15

5.3.2 Shortest-Job-First Scheduling:


Shortest Job First (SJF) is an algorithm in which the process having the smallest execution
time is chosen for the next execution. This scheduling method can be preemptive or
nonpreemptive.
Non-Preemptive SJF:
In non-preemptive scheduling, once the CPU cycle is allocated to process, the process holds it
till it reaches a waiting state or terminated.
Consider the following five processes each having its own unique burst time and arrival time.
Process Burst time Arrival time
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step-0: At time=0, P4 arrives and starts execution.

Arifur Refat
22

Step-1: At time= 1, Process P3 arrives. But, P4 still needs 2 execution units to complete. It
will continue execution.

Step-2: At time =2, process P1 arrives and is added to the waiting queue. P4 will continue
execution.

Step-3: At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is
compared. Process P1 is executed because its burst time is less compared to P3.

Step-4: At time = 4, process P5 arrives and is added to the waiting queue. P1 will continue
execution.

Step-5: At time = 5, process P2 arrives and is added to the waiting queue. P1 will continue
execution.

Arifur Refat
23

Step-6: At time = 9, process P1 will finish its execution. The burst time of P3, P5, and P2 is
compared. Process P2 is executed because its burst time is the lowest.

Step-7: At time=10, P2 is executing and P3 and P5 are in the waiting queue.

Step-8: At time = 11, process P2 will finish its execution. The burst time of P3 and P5 is
compared. Process P5 is executed because its burst time is lower.

Step-9: At time = 15, process P5 will finish its execution.

Step-10: At time = 23, process P3 will finish its execution.

Arifur Refat
24

Step-11: Let’s calculate the average waiting time for above example.
Waiting time = Completion time – Burst Time – Arrival Time
Turnaround Time = Completion Time – Arrival Time
Process Waiting Time Turnaround Time
P1 9-6-2=1 9-2=7
P2 11-2-5=4 11-5=6
P3 23-8-1=14 23-1=22
P4 3-3-0=0 3-0=3
P5 15-4-4=7 15-4=11
Average Waiting Time = 1+4+14+0+7 Average Turnaround Time = 7+6+22+3+11

= =
= 5.2 = 9.8

Preemptive SJF:
In Preemptive SJF Scheduling, jobs are put into the ready queue as they come. A process with
shortest burst time begins execution. If a process with even a shorter burst time arrives, the
current process is removed or preempted from execution, and the shorter job is allocated CPU
cycle. Consider the following five process:
Process Burst time Arrival time
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step-0: At time = 0, P4 arrives and starts execution.

Arifur Refat
25

Step-1: At time = 1, Process P3 arrives. But, P4 has a shorter burst time. It will continue
execution.

Step-2: At time = 2, process P1 arrives with burst time = 6. The burst time is more than that
of P4. Hence, P4 will continue execution.

Step-3: At time = 3, process P4 will finish its execution. The burst time of P3 and P1 is
compared. Process P1 is executed because its burst time is lower.

Step-4: At time = 4, process P5 will arrive. The burst time of P3, P5, and P1 is compared.
Process P5 is executed because its burst time is lowest. Process P1 is preempted.

Step-5: At time = 5, process P2 will arrive. The burst time of P1, P2, P3, and P5 is compared.
Process P2 is executed because its burst time is least. Process P5 is preempted.

Arifur Refat
26

Step-6: At time = 6, P2 is executing.

Step-7: At time =7, P2 finishes its execution. The burst time of P1, P3, and P5 is compared.
Process P5 is executed because its burst time is lesser.

Step-8: At time =10, P5 will finish its execution. The burst time of P1 and P3 is compared.
Process P1 is executed because its burst time is less.

Step-9: At time =15, P1 finishes its execution. P3 is the only process left. It will start
execution.

Step-10: At time = 23, P3 finishes its execution.

Arifur Refat
27

Step-11: Let’s calculate the average waiting time for above example.
Waiting time = Completion time – Burst Time – Arrival Time
Turnaround Time = Completion Time – Arrival Time
Process Waiting Time Turnaround Time
P1 15-6-2=7 15-2=13
P2 7-2-5=0 7-5=2
P3 23-8-1=14 23-1=22
P4 3-3-0=0 3-0=3
P5 10-4-4=2 10-4=6
Average Waiting Time = 7+0+14+0+2 Average Turnaround Time = 13+2+22+3+6

= =
= 4.6 = 9.2

5.3.3 Round-Robin Scheduling:

Arifur Refat
28

• Round Robin is the preemptive process scheduling algorithm.


• Each process is provided a fix time to execute, it is called a quantum.
• Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.
• Context switching is used to save states of preempted processes.
Examples:
Process Arrival Time Burst Time
P1 0 7
P2 1 4
P3 2 15
P4 3 11
P5 4 20
P6 4 9
Assume Time Quantum TQ = 5 Ready
Queue:
P1 P2 P3 P4 P5 P6 P1 P3 P4 P5 P6 P3 P4 P5

Gantt chart:

P1 P2 P3 P4 P5 P6 P1 P3 P4 P5 P6 P3 P4 P5
0 5 9 14 19 24 29 31 36 41 46 50 55 56 66
Waiting time = Completion time – Burst Time – Arrival Time
Turnaround Time = Completion Time – Arrival Time
Process Arrival Burst Time Completion Turnaround Waiting Time
Time Time Time
P1 0 7 31 31 24
P2 1 4 9 8 4
P3 2 15 55 53 38
P4 3 11 56 53 42
P5 4 20 66 62 42
P6 4 9 50 46 37
Average Completion Time = 𝟑𝟏+𝟗+𝟓𝟓+𝟓𝟔+𝟔𝟔+𝟓𝟎 = 𝟐𝟔𝟕 = 44.5
𝟔 𝟔

Average Waiting Time = 𝟐𝟒+𝟒+𝟑𝟖+𝟒𝟐+𝟒𝟐+𝟑𝟕 = 𝟏𝟖𝟕 = 31.1667


𝟔 𝟔

Average Turn Around Time = 𝟑𝟏+𝟖+𝟓𝟑+𝟓𝟑+𝟔𝟐+𝟒𝟔 = 𝟐𝟓𝟑 = 42.1667


𝟔 𝟔

Arifur Refat
29

5.3.4 Priority Scheduling: Priority Scheduling is a method of scheduling processes that is


based on priority.
Characteristics of Priority Scheduling Algorithm

• A CPU algorithm that schedules processes based on priority.


• Operating systems use it for performing batch processes
• If there exist two jobs/processes in the ready state (ready for execution) that have the
same priority, then it works on a FIRST COME, FIRST SERVED basis.
• In priority scheduling, a number is assigned to each process that indicates its priority
level.
• Lower the number, higher is the priority.

There are two types of priority scheduling algorithms in OS:


Preemptive Scheduling: In Preemptive Scheduling, the tasks are mostly assigned with their
priorities. Sometimes it is important to run a task with a higher priority before another lower
priority task, even if the lower priority task is still running. The lower priority task holds for
some time and resumes when the higher priority task finishes its execution.
Non-Preemptive Scheduling: In this type of scheduling method, the CPU has been allocated
to a specific process. The process that keeps the CPU busy, will release the CPU either by
switching context or terminating. It is the only method that can be used for various hardware
platforms. That’s because it doesn’t need special hardware (for example, a timer) like
preemptive scheduling.
Non-Preemptive Priority Scheduling Example:
Process Priority Arrival Time Burst Time
P1 2 0 3
P2 6 2 5
P3 3 1 4
P4 5 4 2
P5 7 6 9
P6 4 5 4
P7 10 7 10

We can prepare the Gantt chart according to the Non-Preemptive priority scheduling.
The Process P1 arrives at time 0 with the burst time of 3 units and the priority number 2.
Since No other process has arrived till now hence the OS will schedule it immediately.

Meanwhile the execution of P1, two more Processes P2 and P3 are arrived. Since the priority
of P3 is 3 hence the CPU will execute P3 over P2.

Arifur Refat
30

Meanwhile the execution of P3, All the processes get available in the ready queue. The
Process with the lowest priority number will be given the priority. Since P6 has priority
number assigned as 4 hence it will be executed just after P3.

After P6, P4 has the least priority number among the available processes; it will get executed
for the whole burst time.
Since all the jobs are available in the ready queue hence All the Jobs will get executed
according to their priorities. If two jobs have similar priority number assigned to them, the
one with the least arrival time will be executed.

P1 P3 P6 P4 P2 P5 P7
0 3 7 11 13 18 27 37

From the GANTT Chart prepared, we can determine the completion time of every process.
The turnaround time, waiting time and response time will be determined.

Waiting time = Completion time – Burst Time – Arrival Time


Response time = Time at which the process gets the CPU for the first time - Arrival time
Turnaround Time = Completion Time – Arrival Time

Process Priority Arrival Burst Completion Turnaround Waiting Response


Time Time Time Time Time Time
P1 2 0 3 3 3-0=3 3-3-0=0 0-0=0
P2 6 2 5 18 18-2=16 18-5-2=11 13-2=11
P3 3 1 4 7 7-1=6 7-4-1=2 3-1=2
P4 5 4 2 13 13-4=9 13-2-4=7 11-4=7
P5 7 6 9 27 27-6=21 27-9-6=12 18-6=12
P6 4 5 4 11 11-5=6 11-4-5=2 7-5=2
P7 10 7 10 37 37-7=30 37-10-7=20 27-7=20

Average Response Time = 𝟎+𝟏𝟏+𝟐+𝟕+𝟏𝟐+𝟐+𝟐𝟎 = 𝟓𝟒 = 7.71 units


𝟕 𝟕

Average Waiting Time = 𝟎+𝟏𝟏+𝟐+𝟕+𝟏𝟐+𝟐+𝟐𝟎 = 𝟓𝟒 = 7.71 units


𝟕 𝟕

Average Turn Around Time = 𝟑+𝟏𝟔+𝟔+𝟗+𝟐𝟏+𝟔+𝟑𝟎 = 𝟗𝟏 = 13 units


𝟕 𝟕

Arifur Refat
31

Preemptive Priority Scheduling Example:


Process Priority Arrival Time Burst Time
P1 2 0 1
P2 6 1 7
P3 3 2 3
P4 5 3 6
P5 4 4 5
P6 10 5 15
P7 9 6 8

Gantt Chart Preparation:


At time 0, P1 arrives with the burst time of 1 units and priority 2. Since no other process is
available hence this will be scheduled till next job arrives or its completion (whichever is
lesser).

P1
0 1

At time 1, P2 arrives. P1 has completed its execution and no other process is available at this
time hence the Operating system has to schedule it regardless of the priority assigned to it.
P1 P2
0 1 2
The Next process P3 arrives at time unit 2, the priority of P3 is higher to P2. Hence the
execution of P2 will be stopped and P3 will be scheduled on the CPU.
P1 P2 P3
0 1 2 5

During the execution of P3, three more processes P4, P5 and P6 becomes available. Since, all
these three have the priority lower to the process in execution. So P3 will complete its
execution and then P5 will be scheduled with the priority highest among the available
processes.
P1 P2 P3 P5
0 1 2 5 10
Meanwhile the execution of P5, all the processes got available in the ready queue. At this
point, the algorithm will start behaving as Non-Preemptive Priority Scheduling. Hence now,
once all the processes get available in the ready queue, the OS just took the process with the
highest priority and execute that process till completion. In this case, P4 will be scheduled and
will be executed till the completion.

Arifur Refat
32

P1 P2 P3 P5 P4
0 1 2 5 10 16
Since P4 is completed, the other process with the highest priority available in the ready queue
is P2. Hence P2 will be scheduled next.
P1 P2 P3 P5 P4 P2
0 1 2 5 10 16 22

P2 is given the CPU till the completion. Since its remaining burst time is 6 units hence P7 will
be scheduled after this.
P1 P2 P3 P5 P4 P2 P7
0 1 2 5 10 16 22 30
The only remaining process is P6 with the least priority, the Operating System has no choice
unless of executing it. This will be executed at the last.
P1 P2 P3 P5 P4 P2 P7 P6
0 1 2 5 10 16 22 30 45

The Completion Time of each process is determined with the help of Gantt Chart.
The turnaround time, the waiting time and the response time can be calculated by the
following formula.
Waiting Time = Completion Time – Burst Time – Arrival Time
Response time = Time at which the process gets the CPU for the first time - Arrival time
Turnaround Time = Completion Time – Arrival Time
Process Priority Arrival Burst Completion Turnaround Waiting Response
Time Time Time Time Time Time
P1 2 0 1 1 1-0=1 1-1-0=0 0-0=0
P2 6 1 7 22 22-1=21 22-7-1=14 1-1=0
P3 3 2 3 5 5-2=3 5-3-2=0 2-2=0
P4 5 3 6 16 16-3=13 16-6-3=7 10-3=7
P5 4 4 5 10 10-4=6 10-5-4=1 5-4=1
P6 10 5 15 45 45-5=40 45-15-5=25 30-5=25
P7 9 6 8 30 30-6=24 30-8-6=16 22-6=16

Average Completion Time = 𝟏+𝟐𝟐+𝟓+𝟏𝟔+𝟏𝟎+𝟒𝟓+𝟑𝟎 = 𝟏𝟐𝟗 = 18.43 units


𝟕 𝟕

Average Turn Around Time = 𝟏+𝟐𝟏+𝟑+𝟏𝟑+𝟔+𝟒𝟎+𝟐𝟒 = 𝟏𝟎𝟖 = 15.43 units


𝟕 𝟕

Arifur Refat
33

Average Waiting Time = 𝟎+𝟏𝟏+𝟐+𝟕+𝟏𝟐+𝟐+𝟐𝟎 = 𝟔𝟑 = 9 units


𝟕 𝟕

Average Response Time = 𝟎+𝟎+𝟎+𝟕+𝟏+𝟐𝟓+𝟏𝟔 = 𝟒𝟗 = 7 units


𝟕 𝟕

Chapter 6

Process Synchronization is the coordination of execution of multiple processes in a


multiprocess system to ensure that they access shared resources in a controlled and
predictable manner. It aims to resolve the problem of race conditions and other
synchronization issues in a concurrent system.
The main objective of process synchronization is to ensure that multiple processes access
shared resources without interfering with each other and to prevent the possibility of
inconsistent data due to concurrent access. To achieve this, various synchronization
techniques such as semaphores, monitors, and critical sections are used.
In a multi-process system, synchronization is necessary to ensure data consistency and
integrity, and to avoid the risk of deadlocks and other synchronization problems.

On the basis of synchronization, processes are categorized as one of the following two types:
• Independent Process: The execution of one process does not affect the execution of
other processes.
• Cooperative Process: A process that can affect or be affected by other processes
executing in the system.
Process synchronization problem arises in the case of Cooperative processes also because
resources are shared in Cooperative processes.

When more than one processes try to access the same code segment that segment is known as
the critical section. The critical section contains shared variables or resources that need to be
synchronized to maintain the consistency of data variables.

Arifur Refat
34

Figure: General structure of a typical process 𝑃𝑖


In the entry section, the process requests for entry in the Critical Section.
When more than one process is executing the same code or accessing the same memory or
any shared variable in that condition there is a possibility that the output or the value of the
shared variable is wrong so for that all the processes doing the race to say that my output is
correct this condition known as a race condition. A race condition is a situation that may
occur inside a critical section.

Q. What is Critical Section problem? What criteria have to satisfy to solve the Critical
Section problem? (18-19) 5
The Critical-Section Problem: The critical section problem refers to the problem of how to
ensure that at most one process is executing its critical section at a given time.
A solution to the critical-section problem must satisfy the following three requirements:
1. Mutual exclusion: When one process is executing in its critical section, no other
process is allowed to execute in its critical section.
2. Progress: When no process is executing in its critical section, and there exists a
process that wishes to enter its critical section, it should not have to wait indefinitely to enter
it.
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.

Arifur Refat
35

Chapter 7

Deadlock: In a multiprogramming environment, several processes may compete for a finite


number of resources. A process requests resources; if the resources are not available at that
time, the process enters a waiting state. Sometimes, a waiting process is never again able to
change state, because the resources it has requested are held by other waiting processes. This
situation is called a deadlock.

Q. Describe the necessary conditions of Deadlock. (17-18) 2


A deadlock situation can arise if the following four conditions hold simultaneously in a
system:
1. Mutual exclusion: At least one resource must be held in a non-sharable mode;
that is, only one process at a time can use the resource. If another process requests
that resource, the requesting process must be delayed until the resource has been
released.
2. Hold and wait: A process must be holding at least one resource and waiting to
acquire additional resources that are currently being held by other processes.
3. No preemption: Resources cannot be preempted; that is, a resource can be
released only voluntarily by the process holding it, after that process has
completed its task.
4. Circular wait: A set {P0, P1, ..., Pn} of waiting processes must exist such that P0
is waiting for a resource held by P1, P1 is waiting for a resource held by P2, ...,
Pn−1 is waiting for a resource held by Pn, and Pn is waiting for a resource held by
P0.
We emphasize that all four conditions must hold for a deadlock to occur.

Deadlocks can be described more precisely in terms of a directed graph called a system
resource-allocation graph. This graph consists of a set of vertices V and a set of edges E.
A directed edge from process Pi to resource type Rj is denoted by Pi → Rj and is called a
request edge.

Arifur Refat
36

A directed edge from resource type Rj to process Pi is denoted by Rj → Pi and is called an


assignment edge.
Pictorially, we represent each process Pi as a circle and each resource type Rj as a rectangle.
Since resource type Rj may have more than one instance, we represent each such instance as a
dot within the rectangle

The resource-allocation graph:

Figure: Resource-allocation graph.


The sets P, R, and E:

• P = {P1, P2, P3}

• R = {R1, R2, R3, R4}

• E = {P1 → R1, P2 → R3, R1 → P2, R2 → P2, R2 → P1, R3 → P3} Resource instances:

• One instance of resource type R1

• Two instances of resource type R2

• One instance of resource type R3

• Three instances of resource type R4 Process states:

• Process P1 is holding an instance of resource type R2 and is waiting for an instance

of resource type R1.

Arifur Refat
37

• Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an

instance of R3.

• Process P3 is holding an instance of R3.


Given the definition of a resource-allocation graph, it can be shown that, if the graph contains
no cycles, then no process in the system is deadlocked. If the graph does contain a cycle, then
a deadlock may exist.
If each resource type has exactly one instance, then a cycle implies that a deadlock has
occurred. If the cycle involves only a set of resource types, each of which has only a single
instance, then a deadlock has occurred. Each process involved in the cycle is deadlocked. In
this case, a cycle in the graph is both a necessary and a sufficient condition for the existence
of deadlock.
If each resource type has several instances, then a cycle does not necessarily imply that a
deadlock has occurred. In this case, a cycle in the graph is a necessary but not a sufficient
condition for the existence of deadlock.

Q. Show resource allocation graph with deadlock and no deadlock. (18-19) 4


Resource-allocation graph with a deadlock:

Figure: Resource-allocation graph with a deadlock Here,


two minimal cycles exist in the system:

P1 → R1 → P2 → R3 → P3 → R2 → P1

Arifur Refat
38

P2 → R3 → P3 → R2 → P2

Processes P1, P2, and P3 are deadlocked. Process P2 is waiting for the resource R3, which is
held by process P3. Process P3 is waiting for either process P1 or process P2 to release
resource R2. In addition, process P1 is waiting for process P2 to release resource R1.
Resource-allocation graph with a cycle but no deadlock:

Figure: Resource-allocation graph with a cycle but no deadlock Here,


also have a cycle:

P1 → R1 → P3 → R2 → P1

There is no deadlock. Because process P4 may release its instance of resource type R2. That
resource can then be allocated to P3, breaking the cycle. Or, process P2 may also release its
instance of resource type R1. That resource can be allocated to P1 for breaking the cycle.

Methods for Handling Deadlocks: We can deal with the deadlock problem in one of three
ways—

• We can use a protocol to prevent or avoid deadlocks, ensuring that the system will
never enter a deadlocked state.

• We can allow the system to enter a deadlocked state, detect it, and recover.

Arifur Refat
39

• We can ignore the problem altogether and pretend that deadlocks never occur in the
system.
The third solution is the one used by most operating systems, including Linux and Windows.
It is then up to the application developer to write programs that handle deadlocks.

To ensure that deadlocks never occur, the system can use either a deadlock-prevention or a
deadlock-avoidance scheme.
Q. What are the ways for deadlock prevention. (18-19) 4
Deadlock prevention provides a set of methods to ensure that at least one of the necessary
conditions (Mutual exclusion, Hold and wait, No preemption and Circular wait) cannot hold.
These methods prevent deadlocks by constraining how requests for resources can be made.
Q. How deadlock can be avoided in a system? (18-19) 3
Deadlock avoidance requires that the operating system be given additional information in
advance concerning which resources a process will request and use during its lifetime. With
this additional knowledge, the operating system can decide for each request whether or not
the process should wait. To decide whether the current request can be satisfied or must be
delayed, the system must consider the resources currently available, the resources currently
allocated to each process, and the future requests and releases of each process.
For example, in a system with one tape drive and one printer, the system might need to know
that process P will request first the tape drive and then the printer before releasing both
resources, whereas process Q will request first the printer and then the tape drive. With this
knowledge of the complete sequence of requests and releases for each process, the system can
decide for each request whether or not the process should wait in order to avoid a possible
future deadlock.

Q. How a system can be in a safe state? (18-19) 3


Safe State: A system is considered to be in a safe state if it can allocate resources to each
process (up to its maximum requirement) without causing a deadlock. Achieving this ensures
system stability and prevents resource conflicts. More formally, a system is in a safe state
only if there exists a safe sequence. The Banker’s Algorithm is commonly used for deadlock
avoidance. Remember, safe states are deadlock-free, while unsafe states don’t always lead to
deadlocks.

Arifur Refat
40

Figure: Safe, unsafe, and deadlocked state spaces.


Banker’s Algorithm: The name was chosen because the algorithm could be used in a
banking system. The Banker’s Algorithm is a deadlock avoidance algorithm used in
operating systems to manage multiple resources across multiple processes. Its primary goal is
to ensure that the system remains in a safe state, meaning it can allocate resources to each
process without risking deadlock.

Q. Write down the data structure for the banker’s algorithm. (18-19) 3

The following Data structures are used to implement the Banker’s Algorithm where 𝑛 is the
number of processes in the system and 𝑚 is the number of resource types:
• Available: A vector of length 𝑚 indicates the number of available resources of each
type. If 𝑨𝒗𝒂𝒊𝒍𝒂𝒃𝒍𝒆[𝑗] equals 𝑘, then 𝑘 instances of resource type 𝑅𝑗 are available.

• Max: An 𝑛 × 𝑚 matrix defines the maximum demand of each process. If 𝑴𝒂𝒙[𝑖][𝑗]


equals 𝑘, then process 𝑃𝑖 may request at most 𝑘 instances of resource type 𝑅𝑗 .

• Allocation: An 𝑛 × 𝑚 matrix defines the number of resources of each type currently


allocated to each process. If 𝑨𝒍𝒍𝒐𝒄𝒂𝒕𝒊𝒐𝒏[𝑖][𝑗] equals 𝑘, then process 𝑃𝑖 is currently

allocated 𝑘 instances of resource type 𝑅𝑗 .

• Need: An 𝑛 × 𝑚 matrix indicates the remaining resource need of each process. If


𝑵𝒆𝒆𝒅[𝑖][𝑗] equals 𝑘, then process 𝑃𝑖 may need 𝑘 more instances of resource type 𝑅𝑗

to complete its task. Note that 𝑵𝒆𝒆𝒅[𝑖][𝑗] equals 𝑴𝒂𝒙[𝑖][𝑗] − 𝑨𝒍𝒍𝒐𝒄𝒂𝒕𝒊𝒐𝒏[𝑖][𝑗].

The Banker's Algorithm is the combination of the safety algorithm and the resource
request algorithm to control the processes and avoid deadlock in a system.

Arifur Refat
41

Safety Algorithm
We can now present the algorithm for finding out whether or not a system is in a safe state.
This algorithm can be described as follows:
1. Let Work and Finish be vectors of length m and n, respectively.
Initialize Work = Available and Finish[i] = false for i = 0, 1, ..., n − 1.
2. Find an index i such that both
a. Finish[i] == false
b. 𝑵𝒆𝒆𝒅𝑖 ≤ Work
If no such i exists, go to step 4.
3. Work = Work + 𝑨𝒍𝒍𝒐𝒄𝒂𝒕𝒊𝒐𝒏𝑖 Finish[i] = true Go to step 2.
4. If Finish[i] == true for all i, then the system is in a safe state.
This algorithm may require an order of m × 𝑛2 operations to determine whether a state is safe.
Q. Write and explain the Resource-Request Algorithm. (17-18) 4
Resource-Request Algorithm
Next, the algorithm for determining whether requests can be safely granted.

Let 𝑹𝒆𝒒𝒖𝒆𝒔𝒕𝑖 be the request vector for process 𝑃𝑖 . If 𝑹𝒆𝒒𝒖𝒆𝒔𝒕𝑖 [j] == k, then process 𝑃𝑖
wants k instances of resource type 𝑅𝑗 . When a request for resources is made by process 𝑃𝑖 ,
the following actions are taken:
1. If 𝑹𝒆𝒒𝒖𝒆𝒔𝒕𝑖 ≤ 𝑵𝒆𝒆𝒅𝑖 , go to step 2. Otherwise, raise an error condition, since the
process has exceeded its maximum claim.
2. If 𝑹𝒆𝒒𝒖𝒆𝒔𝒕𝑖 ≤ Available, go to step 3. Otherwise, 𝑃𝑖 must wait, since the resources are
not available.
3. Have the system pretend to have allocated the requested resources to process 𝑃𝑖 by
modifying the state as follows:
Available = Available – 𝑹𝒆𝒒𝒖𝒆𝒔𝒕𝑖 ;
𝑨𝒍𝒍𝒐𝒄𝒂𝒕𝒊𝒐𝒏𝑖 = 𝑨𝒍𝒍𝒐𝒄𝒂𝒕𝒊𝒐𝒏𝑖 + 𝑹𝒆𝒒𝒖𝒆𝒔𝒕𝑖 ;
𝑵𝒆𝒆𝒅𝑖 = 𝑵𝒆𝒆𝒅𝑖–𝑹𝒆𝒒𝒖𝒆𝒔𝒕𝑖 ;
If the resulting resource-allocation state is safe, the transaction is completed, and
process 𝑃𝑖 is allocated its resources. However, if the new state is unsafe, then 𝑃𝑖 must
wait for 𝑹𝒆𝒒𝒖𝒆𝒔𝒕𝑖 , and the old resource-allocation state is restored.

Example: Consider a system that contains five processes P0, P1, P2, P3, P4 and the three
resource types A, B and C. Following are the resources types: A has 10, B has 5 and the
resource type C has 7 instances.
Process Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2

Arifur Refat
42

P4 0 0 2 4 3 3

Answer the following questions using the banker's algorithm:


1. What is the reference of the need matrix?

2. Determine if the system is safe or not.


3. What will happen if the resource request, 𝑅𝑒𝑞𝑢𝑒𝑠𝑡1= (1, 0, 2) for process P1 can the
system accept this request immediately?

Answer-1: The content of the matrix Need is defined to be Max − Allocation and is as
follows:
Process Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

Answer-2: Applying the Banker's Algorithm,


Available Resources of A, B and C are 3, 3, and 2.
Now we check if each type of resource request is available for each process.
Step 1: For Process P0:
Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P1.

Step 2: For Process P1:


Need <= Available
1, 2, 2 <= 3, 3, 2 condition true
New Available = Available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2
Similarly, we examine another process P2.

Arifur Refat
43

Step 3: For Process P2:


Need <= Available
6, 0, 0 < = 5, 3, 2 condition is false.
Similarly, we examine another process, P3.

Step 4: For Process P3:


Need <= Available
0, 1, 1 <= 5, 3, 2 condition is true
New Available = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Similarly, we examine another process P4.
Step 5: For Process P4:
Need <= Available
4, 3, 1 <= 7, 4, 3 condition is true
New Available = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
Now, we again examine each type of resource request for processes P0 and P2.

Step 6: For Process P0:

Need <= Available


7, 4, 3 <= 7, 4, 5 condition is true
New Available = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
So, we examine another process P2.

Step 7: For Process P2:


Need <= Available
6, 0, 0 <= 7, 5, 5 condition is true
New Available = Available + Allocation
7, 5, 5 + 3, 0, 2 => 10, 5, 7
Hence, we execute the banker's algorithm to find the safe state and the safe sequence like

P1, P3, P4, P0 and P2.

Arifur Refat
44

Answer-3: For granting the 𝑅𝑒𝑞𝑢𝑒𝑠𝑡1= (1,0,2), first we have to check that
𝑅𝑒𝑞𝑢𝑒𝑠𝑡1<=Available, that is (1,0,2)<=(3,3,2), since the condition is true. So the process P1
gets the request immediately.

Q. Write the differences between Deadlock and Starvation. (17-18) 2


Difference between Deadlock and Starvation:
Deadlock Starvation
A deadlock is a condition in operating Starvation is the problem that occurs when
systems in which no process proceeds for high priority processes keep executing and
execution and wait for resources that have low priority processes get blocked for
been acquired by some other processes. indefinite time.
All processes keep waiting for each other to High priority processes keep executing and
complete and none get executed. low priority processes are blocked.
Resources are blocked by the processes. Resources are continuously utilized by high
priority processes.
Deadlock is a process design/distributed Starvation is a scheduler issue.
design issue.
Also known as Circular wait. Also known as lived lock.

Arifur Refat
45

Chapter 8

Arifur Refat
46

Arifur Refat
47

Arifur Refat
48

Arifur Refat
49

Arifur Refat
50

Arifur Refat
51

Arifur Refat
52

Arifur Refat
53

Arifur Refat
54

Arifur Refat
55

Arifur Refat
56

Q. Define the term Swapping. (17-18 & 18-19) 1 & 1.5


Swapping in OS is a memory management technique that temporarily swaps processes from
main memory to secondary memory or vice versa which helps to increase the degree of
multi-programming and increase main memory utilization.
There are two steps in the process of Swapping in the operating system:
⸰ Swap-out is a technique for moving a process from RAM to hard disc.
⸰ Swap-in is a technique for bringing a process from hard disc to RAM.

In the above diagram, process P1 is swap out so the process with more memory requirement
or higher priority will be executed to increase the overall efficiency of the operating system.
While the process P2 is swap in for its execution from the secondary memory to the main
memory (RAM).
Let's understand this with the help of an example. The process size of the user's process is
4096 KB, and the transfer rate is 1 MBps. Now we'll find out how long it will take to move
from main memory to secondary memory.
Here,
User process size = 4096 KB and Data transfer Rate = 1024 KBps
Time = User process size = 4096 = 4 𝑠𝑒𝑐𝑜𝑛𝑑𝑠 =
4000 𝑚𝑖𝑙𝑙𝑖𝑠𝑒𝑐𝑜𝑛𝑑𝑠 Data transfer Rate 1024

The process will now take 4000 milliseconds.


So, it will take 8000 milliseconds to complete the swapping process.

Arifur Refat
57

Q. Assume that the size of the user process = 4096 KB and data transfer rate of a hard
disk = 1 MB/s. How much time will take to complete the swapping process? (17-18) 7
Q. What is fragmentation? (17-18) 1
The problem due to which memory space is not getting utilized at all is commonly known as
Fragmentation in the operating system.
Fragmentation is further divided into two types:
(i) Internal Fragmentation
(ii) External Fragmentation

Q. Define the term Internal Fragmentation. (18-19) 1.5


Internal Fragmentation is a problem that occurs when the process is allocated to a memory
block whose size is more than the size of that process and due to which some part of the
memory is left unused.

External Fragmentation: When the memory space in the system can easily satisfy the
requirement of the processes, but this available memory space is non-contiguous. So, it can’t
be utilized further. Then this problem is referred to as External Fragmentation.

Difference Between Internal and External Fragmentation


Internal Fragmentation External Fragmentation
Internal Fragmentation occurs when the External Fragmentation occurs when the
memory blocks of fixed-size are allocated to memory blocks of variable-size are
the processes. allocated to the processes dynamically.
It mainly refers to the unused space in the It mainly refers to the unused blocks of the
partition that resides in the allocated region. memory that are not contiguous.
Best-fit block can be used to overcome the Compaction, segmentation, and paging can
problem of Internal fragmentation. be used to overcome the problem of
External fragmentation.
Paging suffers from Internal fragmentation. First-fit and best-fit suffer from external
fragmentation.

Arifur Refat
58

Arifur Refat
59

Arifur Refat
60

Arifur Refat
61

Arifur Refat
62

Arifur Refat
63

Q. Describe the general dynamic storage allocation strategies (first-fit, best-fit and
worst fit). (18-19) 3
Q. Show the compaction process with a diagram. (17-18) 3

Arifur Refat
64

Arifur Refat
65

Arifur Refat
66

Non-Contiguous Memory Allocation:


As we know at dynamic memory allocation still has external fragmentation due to contiguous
memory allocation. So, one of the solution to this problem is to use non-contiguous memory
allocation.
Frames (Physical address page): A frame is a unit of physical memory. Actually, physical
memory space is divided into blocks of the same size which is called frames.
Pages (Logical or virtual address page): A process is divided into some units with the same size
is called a page. Every process has logical memory and logical memory is divided into blocks of
the same size is called pages.
Page size and frame size must be equal.
Each portion of the program is divided into equal size pages and is stored in the physical memory’s
frames.
Let a process with size 40MB and main memory is 160MB. Now we divided the process with
equal size which is divided 5 pages with each size 8MB. As we know page size and memory
frame size is the same, so also divided the memory into 20 frames with size 8MB.

Let’s take an example that how non-contiguous memory allocation work, let we have three
processes P1 with size 8KB, P2 with size 16KB, and P3 with size 8KB. Here main memory
size is 32KB. Every process is divided into pages of the same size like 2KB. So the size of
every page is 2KB and the same as each memory frame size is 2KB.

Arifur Refat
67

Assume the initial entire memory is free and three processes P1, P2 and P3 want to get
memory than processes obviously allocate contiguous manner because not require to allocate
as non-contiguous way because memory is free, so P1, P2 and P3 allocate contiguously as
memory is entirely free.

Now assume that process P1 and P3 has been finished, so they are swapped out from the
memory and create a chance for other processes, assume that process P4 comes with size
14KB.
P1 individually free 8KB (4 frames) and P3 individually free 8KB (4 frames), but if see above
the memory picture 8KB+8KB=16KB (8 frames) will not free contiguously or consecutively.
So, here because of not enough space to allocate P4 in a place contiguous manner, it allocates
two places of memory in a non-contiguous manner.

Arifur Refat
68

There are three fundamental approaches to implementing non-contiguous memory allocation. Such
as: (i) Paging (ii) Segment (iii) Paging with the segment.

Q. Define the term Paging. (17-18) 1


Paging is a memory management scheme where physical memory is divided into fixed-size blocks
called ‘frames’ and logical memory is divided into equally sized blocks called ‘pages’.
The MMU maps pages to frames, enabling efficient allocation and management of memory.
Basic concepts of Paging:
CPU generates a logical address which is called a relocatable address because the CPU assumes
the process will be allocated at the memory from the 0th location.
Memory Management Unit (MMU) converts the logical address (Relocatable address) to physical
address (Absolute address) to map the process into memory with the actual address.
The technique of mapping the CPU to generate logical address to physical address is called
paging.
Paging Requirements:
1. Memory Management Unit converts the logical address to the corresponding physical
address.
2. Page Table: For every process, there is a page table that holds the corresponding frame
number of a page and other page-related information.

Arifur Refat
69

3. Every frame in the main memory holds some information that is free or allocated with
other pages.
Q. Define the term Logical Address Space. (18-19) 1.5
Logical Address Space (LAS): The set of all logical addresses generated by a CPU for a
process is a logical address space. The logical addresses in a process constitute the logical
address space of the process.
Logical address space = Size of the process
Number of pages = Process size(LAS)
Page size

Physical Address Space (PAS): The set of all physical addresses corresponding to these logical
addresses is a physical address space. The set of physical addresses in the system constitutes the
physical address space of the system.
Physical address space = Size of the Memory
Number of frames = Memory size(PAS)
Frame size

Let, Process size = 16MB = Logical Address Space (LAS)


Memory size = 64MB = Physical Address Space (PAS)
Q. Explain the paging basic method with appropriate diagram. (18-19) 5 Basic
Method of Paging:
The basic method for implementing paging involves breaking physical memory into
fixedsized blocks called frames and breaking logical memory into blocks of the same size
called pages. When a process is to be executed, its pages are loaded into any available
memory frames from their source (a file system or the backing store). The backing store is
divided into fixed-sized blocks that are the same size as the memory frames. The hardware
support for paging is illustrated in Figure 8.10.

Arifur Refat
70

Logical address contains two parts: Page number(p) and Offset(d)


Physical address contains two parts: Frame number(f) and Offset(d)
Base address = Frame number
Page table and Pages both reside on the main memory.

Every address generated by the CPU is divided into two parts: a page number (p) and a page
offset (d). The page number is used as an index into a page table. The page table contains the
base address of each page in physical memory. This base address is combined with the page
offset to define the physical memory address that is sent to the memory unit. The paging model
of memory is shown in Figure 8.11.

Arifur Refat
71

Q. What are the differences between logical and physical address spaces? (17-18) 2 Address
Translation: When the CPU tries to fetch a word or data from the main memory, the CPU generates
a Logical address (Relocatable address) for this word or data but this address is not the actual location
of the memory. Memory Management Unit (MMU) converts the Logical address (Relocatable
address) to the corresponding Physical address (Absolute address) to map the actual location of main
memory for this word or data.

Arifur Refat
72

The page size (like the frame size) is defined by the hardware. The size of a page is a power of
2, varying between 512 bytes and 1 GB per page, depending on the computer architecture.
The selection of a power of 2 as a page size makes the translation of a logical address into a
page number and page offset particularly easy. If the size of the logical address space is 2𝑚,

Arifur Refat
73

and a page size is 2𝑛 bytes, then the high-order 𝑚 − 𝑛 bits of a logical address designate the
page number, and the 𝑛 low-order bits designate the page offset. Thus, the logical address is
as follows:

where 𝑝 is an index into the page table and 𝑑 is the displacement within the page. As an example,
consider the memory in Figure 8.12.

Here, in the logical address, 𝑛 = 2 and 𝑚 = 4. Using a page size of 4 bytes and a physical
memory of 32 bytes (8 pages), we show how the programmer’s view of memory can be
mapped into physical memory. Logical address 0 is page 0, offset 0. Indexing into the page
table, we find that page 0 is in frame 5. Thus, logical address 0 maps to physical address 20 [=
(5 × 4) + 0]. Logical address 3 (page 0, offset 3) maps to physical address 23 [= (5 × 4) + 3].
Logical address 4 is page 1, offset 0; according to the page table, page 1 is mapped to frame

Arifur Refat
74

6. Thus, logical address 4 maps to physical address 24 [= (6 × 4) + 0]. Logical address 13


maps to physical address 9.
Paging itself is a form of dynamic relocation. Every logical address is bound by the paging
hardware to some physical address. Using paging is similar to using a table of base (or relocation)
registers, one for each frame of memory.
Advantages:
(i) No external fragmentation

(ii) Simple memory management algorithm


(iii) Swapping is easy Disadvantages:
(i) Internal fragmentation
(ii) Page table may consume more memory
(iii) Multi-level paging leads to memory reference overhead
Q. Define the term Segmentation. (18-19) 1.5
Segmentation: Segmentation is a memory management technique in which memory is divided
into variable sized chunks which can be allocated to processes. Each chunk is called a
segment. A table stores the information about all such segments called the segment table.
Segmentation is a memory management scheme that supports the user(programmer) view of
memory. A logical address space is a collection of segments. Each segment has a name and a
length. The addresses specify both the segment name and the offset within the segment. The
user therefore specifies each address by two quantities: a segment name and an offset. Thus, a
logical address consists of a two tuple: <segment-number, offset>.
Normally, when a program is compiled, the compiler automatically constructs segments reflecting
the input program. A C compiler might create separate segments for the following:
1. The code
2. Global variables
3. The heap, from which memory is allocated
4. The stacks used by each thread
5. The standard C library
Libraries that are linked in during compile time might be assigned separate segments. The loader
would take all these segments and assign them segment numbers.
Types of Segmentation:
• Virtual Memory Segmentation
With this type of segmentation, each process is segmented into n divisions and the
most important thing is they are not segmented all at once.

Arifur Refat
75

• Simple Segmentation
With the help of this type, each process is segmented into n divisions and they are all
together segmented at once exactly but at the runtime and can be non-contiguous (that
is they may be scattered in the memory).

Segmentation Hardware: Each entry in the segment table has a segment base and a segment limit.
The segment base contains the starting physical address where the segment resides in memory, and
the segment limit specifies the length of the segment.

Figure: Segmentation Hardware


A logical address consists of two parts: a segment number (s) and an offset into that segment
(d). The segment number is used as an index to the segment table. The offset d of the logical
address must be between 0 and the segment limit. If it is not, we trap to the operating system.
When an offset is legal, it is added to the segment base to produce the address in physical
memory of the desired byte. The segment table is thus essentially an array of base-limit register
pairs.
Correct offset + Segment base = Address in Physical memory

Arifur Refat
76

Arifur Refat
77

An Example:

Figure: Example of Segmentation


We have five segments numbered from 0 through 4. The segments are stored in physical
memory as shown. The segment table has a separate entry for each segment, giving the
beginning address of the segment in physical memory (or base) and the length of that segment
(or limit). For example, segment 2 is 400 bytes long and begins at location 4300. Thus, a
reference to byte 53 of segment 2 is mapped onto location 4300 + 53 = 4353. A reference to
segment 3, byte 852, is mapped to 3200 (the base of segment 3) + 852 = 4052. A reference to
byte 1222 of segment 0 would result in a trap to the operating system, as this segment is only
1,000 bytes long.

Advantages of Segmentation Disadvantages of Segmentation


Segmentation generally allows us to divide Segments are of unequal size in
the program into modules that provide segmentation and thus are not suitable for
better visualization. swapping.
There is no Internal Fragmentation. There is External Fragmentation.
Segments are of variable size. This technique is expensive.

The segment table occupies less space as Maintaining a segment table for each
compared to the paging table. process leads to overhead

Arifur Refat
78

Q. What is TLB? What is the function of TLB? (FEC-CT) 2


TLB: A translation lookaside buffer (TLB) is a type of memory cache that stores recent translations
of virtual memory to physical addresses to enable faster retrieval.

Translation Lookaside Buffer (TLB) in Paging:

Figure: Paging hardware with TLB.


Steps in TLB hit
1. CPU generates a virtual (logical) address.
2. It is checked in TLB (present).
3. The corresponding frame number is retrieved, which now tells where the main memory page
lies.
Steps in TLB miss
1. CPU generates a virtual (logical) address.
2. It is checked in TLB (not present).
3. Now the page number is matched to the page table residing in the main memory
4. The corresponding frame number is retrieved, which now tells where the main memory page
lies.

Arifur Refat
79

5. The TLB is updated with new Page Table Entry.

Functions of TLB:
(i) The TLB improves the performance of the virtual memory by reducing the average
time required to translate a logical address to a physical address.
(ii) The TLB also reduces the number of memory or disk accesses, which can save
energy and bandwidth.
(iii) The TLB also enables faster context switches, which are the operations that switch
the execution of one process to another.
(iv) The TLB can store the mappings of different processes, so that when a context switch
occurs, the CPU does not have to reload the entire page table.

Q. Write down the formula for effective access time (EAT). (17-18) 4
Effective Access Time (EAT) refers to the total time it takes to complete a memory access
operation, taking into account the possibility of both TLB hits and TLB misses.
EAT
where, hit ratio of TLB
𝑚 Memory access time
𝑐 TLB access time
Q. If TLB search takes 𝟐𝟎𝒏𝒔; memory access takes 𝟏𝟎𝟎𝒏𝒔 and hit ratio is 𝟗𝟎%.
Calculate the effective access time. (17-18) 4

EAT = 0.9 × (20 + 100) + (1 − 0.9) × (20 + 2 × 100) = 130𝑛𝑠


Q. What will be the EAT if hit ratio is 𝟕𝟎%, time for TLB is 𝟑𝟎𝒏𝒔 and access to main memory
is 𝟗𝟎𝒏𝒔?
EAT = 0.7 × 120 + (1 − 0.7) × 210 = 147𝑛𝑠
Q. What is the formula for EAT for a demand page memory? (FEC-CT) 2

EAT = (1 − 𝑝) × 𝑀𝑒𝑚𝑜𝑟𝑦 𝐴𝑐𝑐𝑒𝑠𝑠 𝑇𝑖𝑚𝑒 + 𝑝 × 𝑃𝑎𝑔𝑒 𝐹𝑎𝑢𝑙𝑡 𝑆𝑒𝑟𝑣𝑖𝑐𝑒 𝑇𝑖𝑚𝑒 Where,


𝑝 is the probability of a page fault occurring.
Number of page faults
𝑝=
Total number of accesses
Q. If memory access time is 300ns. Average page fault service time is 10 milliseconds.
And if 3 access out of 1000 causes the page fault. What will be the effective access time
(EAT)? (FEC-CT) 3

EAT = (1 − ) × 300 + × 107 = 30299.1𝑛𝑠

Arifur Refat
80

Q. In the demand paging memory, a page table is held in registers. If it takes 𝟏𝟎𝟎𝟎𝒎𝒔 to
service a page fault and if the memory access time is 𝟏𝟎𝒎𝒔, what is the effective access
time for a page fault rate of 𝟎. 𝟎𝟏?

EAT = (1 − 0.01) × 10 + 0.01 × 1000 = 19.9𝑚𝑠

Chapter 9

Q. Define the terms virtual memory. (17-18) 2


Q. What is virtual memory? Write the advantages of use it. (18-19) 3
Virtual memory is a storage allocation scheme in which secondary memory can be addressed
as though it were part of the main memory.
The key advantages of using it:
(i) Increased Memory Capacity: Programs can use more memory than physically
available.
(ii) Multiprogramming Environment: It enables running multiple applications
simultaneously.
(iii) Memory Protection: Isolates processes, enhancing security.
(iv) Sharing Libraries: Memory used by libraries can be shared between processes.
(v) Conceptual Use of more Memory: Allows conceptual use of more memory than
physically available.

Q. Define the terms page fault. (17-18) 2


Page fault is a type of error that occurs when a program tries to access data that is not currently
in the main memory or random access Memory (RAM).
When it happens, the Operating System tries to retrieve the required data from the hard disk or
another storage medium, and this process is known as a page fault. This can cause a delay in
the program’s execution and slow down the overall performance of the system.

Q. Under what circumstances do page faults occur? (18-19) 2


Page faults occur when a program tries to access a page of memory that is not currently in
RAM. This can happen due to various reasons, such as the page being swapped out to disk, the
page not being allocated yet, or the page being paged out to make room for other pages.

Arifur Refat
81

Q. Consider the page-reference string: 1, 2, 3, 4, 2, 1, 5, 2, 1, 2, 3, 3, 2, 1, 2, 3. How many page


faults would occur for the following replacement algorithms (assume three frames)
(i) LRU (ii) FIFO (iii) Optimal. (18-19) 9
(i) Using LRU: (Replace a page which is not in use for a long time) ←
Reference Strings 1 2 3 4 2 1 5 2 1 2 3 3 2 1 2 3
Frame 1 1 1 1 4 4 4 5 5 5 5 3 3 3 3 3 3
Frame 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Frame 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1
M M M M H M M H H H M H H H H H
Number of page fault (M) = 7 Number of Hit (H) = 9

Miss Ratio = × 100% = 43.75% Hit Ratio = × 100% = 56.25%

(ii) Using FIFO:


Reference Strings 1 2 3 4 2 1 5 2 1 2 3 3 2 1 2 3
Frame 1 1 1 1 4 4 4 4 2 2 2 2 2 2 2 2 2
Frame 2 2 2 2 2 1 1 1 1 1 3 3 3 3 3 3
Frame 3 3 3 3 3 5 5 5 5 5 5 5 1 1 1
M M M M H M M M H H M H H M H H
Number of page fault (M) = 9 Number of Hit (H) = 7

Miss Ratio = × 100% = 56.25% Hit Ratio = × 100% = 43.75%

(iii) Using Optimal: (Replace a page that will not be used in near future) ⟶
Reference Strings 1 2 3 4 2 1 5 2 1 2 3 3 2 1 2 3
Frame 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Frame 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Frame 3 3 4 4 4 5 5 5 5 3 3 3 3 3 3
M M M M H H M H H H M H H H H H
Number of page fault (M) = 6 Number of Hit (H) = 10

Miss Ratio = × 100% = 37.5% Hit Ratio = × 100% = 62.5%

Arifur Refat
82

Q. Suppose that the Memory Size = 900 bytes, Page Size = 300 bytes and Reference
String is 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0. Find out the page fault rate for Optimal Page
Replacement and Least Recently Used (LRU) algorithms. (17-18) 8

Number of frames = Memory Size = = 3 frames.


Page Size

Q. Case 1: Memory Size = 600 bytes, Page Size = 200 bytes


Case 2: Memory Size = 800 bytes, Page Size = 200 bytes
Reference String is 8 0 1 2 0 3 1 4 2 3 0 3 2 1 2 3
Which Case will show better performance for Optimal Page Replacement algorithm?
Explain. (FEC-CT) 5

Arifur Refat
83

Chapter 11 File System Interface

A file is a named collection of related information that is recorded on secondary storage.

File Attributes: A file’s attributes vary from one operating system to another but typically consist
of these:
• Name: The symbolic file name is the only information kept in human readable form. •
Identifier: This unique tag, usually a number, identifies the file within the file
system; it is the non-human-readable name for the file.
• Type: This information is needed for systems that support different types of files.
• Location: This information is a pointer to a device and to the location of the file on
that device.
• Size: The current size of the file (in bytes, words, or blocks) and possibly the
maximum allowed size are included in this attribute.
• Protection: Access-control information determines who can do reading, writing,
executing, and so on.
• Time, date, and user identification: This information may be kept for creation, last
modification, and last use. These data can be useful for protection, security,
and usage monitoring.

Q. Describe the different file operations. (18-19) 4


File Operations: The Operating system is responsible for performing the following file operations
using various system calls.
• Creating a file: Two steps are necessary to create a file. First, space in the file
system must be found for the file. Second, an entry for the new file must be made
in the directory.
• Writing a file: To write a file, you need to know two things. One is the name of the
file and the other is the information or data written to the file
• Reading a file: The OS needs a read pointer to read a file from a specific location in
the file.

Arifur Refat
84

• Repositioning within a file: The directory is searched for the appropriate entry, and
the current-file-position pointer is repositioned to a new given value.
• Deleting a file: To delete a file, first browse the directory of the named file, then
free up the file area and delete the directory entry.

• Truncating a file: To truncate a file, delete only the contents of the file, but don’t
delete its file structure.
There are also other file operations like appending a file, creating a duplicate of the file, and
renaming a file.

File Access Method


File access method is a way of accessing and manipulating data stored in a file. There are several
file access methods. Such as:
• Sequential access
• Random access
• Direct access
• Indexed access method

Q. Briefly describe the sequential and random file access mechanisms. (17-18) 4
Sequential access is a file access method in which data is accessed in a linear or sequential order.
This means that data can only be accessed in the order in which it is stored in the file.
Random access is a file access method in which data can be accessed from any location within
the file. It provides the ability to directly access any record or data element in the file.
Direct access is a file access method that allows data to be accessed directly by using the data's
physical location within the file. It does not use an index or address like random access, and
instead relies on the physical location of the data within the file.
Indexed access method involves accessing files through an index or directory that contains a list
of file names and their corresponding locations on the disk.

Arifur Refat
85

Q. Write the advantages and disadvantages of different file access methods. (18-19) 5
Access Methods Advantages Disadvantages
Sequential access Simple and easy to Not efficient for accessing
implement, suitable for specific data or making
storing large amounts of changes to the data, slow for
data, requires less memory. reading or writing data in
the middle of the file.
Random access Provides fast and efficient Requires more memory to
access to specific data store index or address
within the file, efficient for information, file size can be
editing and updating data, larger than with sequential
suitable for devices that access, data can become
require fast access to inaccessible if index or
specific data. address information
becomes corrupted.
Direct access Provides fast and efficient Requires knowledge of the
access to specific data physical layout of the data
within the file, suitable for within the file, may require
devices that require fast special hardware or software
access to specific data, file to access the data directly,
gaps can be left in the file
size is smaller than with
which can impact
random access.
performance.
Indexed access method Provides fast and efficient The index must be
access to files by name or maintained, which can
attributes, making it suitable require additional disk space
for applications that require and processing time.
searching and retrieving
specific files quickly.

Arifur Refat

You might also like