You are on page 1of 317

Operating system

Module 1

2
INDEX:
Introduction Distributed Systems

Structures Real-Time Systems

Simple Batch System components

Multiprogrammed Operating System services

Time-shared System Calls

Personal Computer

Parallel

3
An Operating System can be
defined as an interface between

Introduction user and hardware. It is


responsible for Process execution,
Resource allocation, CPU
Management, File Management
and many other taks.

4
Objective
Interface between hardware and software

Monitoring software- track resources

Remove conflicting request

Efficiency

Hide details of hardware resources(RAM, CPU, I/O device)

5
Structure of a Computer system
User

Application Programs

System programs

Operating system

Hardware

6
Types of Operating System

7
Simple Batch
1. Firstly, user prepares his job using punch cards.
2. Then, he submits the job to the computer operator.
3. Operator collects the jobs from different users and
sort the jobs into batches with similar needs.
4. Then, operator submits the batches to the processor
one by one.
5. All the jobs of one batch are executed together.

ADVANTAGES:

It saves the time that was being wasted earlier for each Disadvantages:
individual process in context switching from one
environment to another environment. Priority can not be set for the jobs.

Batch operating system may lead to starvation.


No manual intervention is needed.
CPU may remain idle for a long time.

There is a lack of interaction between a user and his job.


8
Multiprogramming Operating System
Multiprogramming is an extension to batch processing
where the CPU is always kept busy. Each process
needs two types of system time: CPU time and IO
time. In a multiprogramming environment, when a
process does its I/O, The CPU can start the execution
of other processes. Therefore, multiprogramming
improves the efficiency of the system.

Advantages:

● Throughout the system, it increased as the CPU


always had one program to execute. Disadvantages:

● Response time can also be reduced. ● Multiprogramming systems provide an environment in


which various systems resources are used efficiently, but
they do not provide any user interaction with the
computer system.
9
Multiprocessing Operating System
In Multiprocessing, Parallel computing is achieved. There
are more than one processors present in the system which
can execute more than one process at the same time. This
will increase the throughput of the system. In
Multiprocessing, Parallel computing is achieved. More than
one processor present in the system can execute more
than one process simultaneously, which will increase the
throughput of the system.

Advantages:

Increased reliability: Due to the multiprocessing system,


processing tasks can be distributed among several
processors. This increases reliability as if one processor
fails, the task can be given to another processor for
Disadvantages:
completion.
Multiprocessing operating system is more
Increased throughout: As several processors increase,
more work can be done in less. complex and sophisticated as it takes care of
multiple CPUs simultaneously.

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

Advantages:

This operating system is more suited to


supporting multiple users simultaneously.

The multitasking operating systems have


well-defined memory management.

Disadvantages:

The multiple processors are busier at the same


time to complete any task in a multitasking
environment, so the CPU generates more heat.
11
Network Operating System
An Operating system, which includes software
and associated protocols to communicate
with other computers via a network
conveniently and cost-effectively, is called
Network Operating System.

Advantages:

In this type of operating system, network


Disadvantages:
traffic reduces due to the division between
clients and the server. In this type of operating system, the failure of any node in a
system affects the whole system.
This type of system is less expensive to set up Security and performance are important issues. So trained
and maintain. network administrators are required for network
administration.

12
Real time Operating System
In Real-Time Systems, each job carries a certain
deadline within which the job is supposed to be
completed, otherwise, the huge loss will be there,
or even if the result is produced, it will be
completely useless.The Application of a Real-Time
system exists in the case of military applications,
if you want to drop a missile, then the missile is
supposed to be dropped with a certain precision.

Advantages:
Disadvantages:
Easy to layout, develop and execute real-time
applications under the real-time operating Real-time operating systems are very costly to
system. develop.
In a Real-time operating system, the maximum Real-time operating systems are very complex and
utilization of devices and systems. can consume critical CPU cycles.

13
Time sharing operating system
In the Time Sharing operating system, computer resources
are allocated in a time-dependent fashion to several
programs simultaneously. Thus it helps to provide a large
number of user's direct access to the main computer. It is a
logical extension of multiprogramming. In time-sharing, the
CPU is switched among multiple programs given by
different users on a scheduled basis.A time-sharing
operating system allows many users to be served
simultaneously, so sophisticated CPU scheduling schemes
and Input/output management are required.Time-sharing
operating systems are very difficult and expensive to build.
Disadvantages:
Advantages:
Data transmission rates are very high in comparison to other methods.
The time-sharing operating system provides effective
utilization and sharing of resources. Security and integrity of user programs loaded in memory and data need
to be maintained as many users access the system at the same time.
This system reduces CPU idle and response time.

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

Advantages:

The distributed operating system provides sharing of


resources.

This type of system is fault-tolerant.

Disadvantages:

Protocol overhead can dominate computation cost.


15
Components of Operating System

16
An operating system is a large and complex system that can only be created by partitioning into small parts. These pieces
should be a well-defined part of the system, carefully defining inputs, outputs, and functions. Although Windows, Mac, UNIX,
Linux, and other OS do not have the same structure, most operating systems share similar OS system components, such as file,
memory, process, I/O device management. The components of an operating system play a key role to make a variety of
computer system parts work together. There are the following components of an operating system, such as:
Process Management
File Management
Network Management
Main Memory Management
Secondary Storage Management
I/O Device Management
Security Management
Command Interpreter System
Operating system components help you get the correct computing by detecting CPU and memory hardware errors.

17
Process Management
The process management component is a procedure
for managing many processes running simultaneously
on the operating system. Every running software
application program has one or more processes
associated with them.

For example, when you use a search engine like


Chrome, there is a process running for that browser
program.

Process management keeps processes running


efficiently. It also uses memory allocated to them and
shutting them down when needed.

The execution of a process must be sequential so, at


least one instruction should be executed on behalf of
the process.

18
Process Management
Functions of process management

Here are the following functions of process management in the operating system, such as:

● Process creation and deletion.

● Suspension and resumption.

● Synchronization process

● Communication process

19
File management
A file is a set of related information defined by its
creator. It commonly represents programs (both
source and object forms) and data. Data files can
be alphabetic, numeric, or alphanumeric.

Function of file management

The operating system has the following important


activities in connection with file management:

● File and directory creation and deletion.

● For manipulating files and directories.

● Mapping files onto secondary storage.

● Backup files on stable storage media.

20
Network Management
Network management is the process of administering and
managing computer networks. It includes performance
management, provisioning of networks, fault analysis, and
maintaining the quality of service.

A distributed system is a collection of computers or


processors that never share their memory and clock. In this
type of system, all the processors have their local memory,
and the processors communicate with each other using
different communication cables, such as fibre optics or
telephone lines.

The computers in the network are connected through a


communication network, which can configure in many
different ways. The network can fully or partially connect in
network management, which helps users design routing and
connection strategies that overcome connection and security
issues.

21
Network Management
Functions of Network management

Network management provides the following functions, such as:

● Distributed systems help you to various computing resources in size and function. They may involve

minicomputers, microprocessors, and many general-purpose computer systems.

● A distributed system also offers the user access to the various resources the network shares.

● It helps to access shared resources that help computation to speed up or offers data availability and

reliability.

22
Main memory Management
Main memory is a large array of storage or bytes, which
has an address. The memory management process is
conducted by using a sequence of reads or writes of
specific memory addresses.

It should be mapped to absolute addresses and loaded


inside the memory to execute a program. The selection of
a memory management method depends on several
factors.

However, it is mainly based on the hardware design of


the system. Each algorithm requires corresponding
hardware support. Main memory offers fast storage that
can be accessed directly by the CPU. It is costly and
hence has a lower storage capacity. However, for a
program to be executed, it must be in the main memory.

23
Main memory Management
Functions of Memory management

An Operating System performs the following functions for Memory Management in the operating
system:

● It helps you to keep track of primary memory.

● Determine what part of it are in use by whom, what part is not in use.

● In a multiprogramming system, the OS decides which process will get memory and how much.

● Allocates the memory when a process requests.

● It also de-allocates the memory when a process no longer requires or has been terminated.

24
Secondary Storage Management
Today modern computers use hard drives/SSD as
The most important task of a computer system is
the primary storage of both programs and data.
to execute programs. These programs help you to
However, the secondary storage management
access the data from the main memory during
also works with storage devices, such as USB
execution. This memory of the computer is very
flash drives and CD/DVD drives. Programs like
small to store all data and programs permanently.
assemblers and compilers are stored on the disk
The computer system offers secondary storage to
until it is loaded into memory, and then use the
back up the main memory.
disk is used as a source and destination for
processing.

25
Secondary Storage Management
Functions of Secondary storage management

Here are some major functions of secondary storage management in the operating system:

● Storage allocation

● Free space management

● Disk scheduling

26
I/O Device Management
One of the important use of an operating system that helps to
hide the variations of specific hardware devices from the user.

Functions of I/O management

The I/O management system offers the following functions, such


as:

● It offers a buffer caching system

● It provides general device driver code

● It provides drivers for particular hardware devices.

● I/O helps you to know the individualities of a specific

device.

27
Security Management
The various processes in an operating system need to be secured from
other activities. Therefore, various mechanisms can ensure those
processes that want to operate files, memory CPU, and other hardware
resources should have proper authorization from the operating system.

Security refers to a mechanism for controlling the access of programs,


processes, or users to the resources defined by computer controls to
be imposed, together with some means of enforcement.

For example, memory addressing hardware helps to confirm that a


process can be executed within its own address space. The time
ensures that no process has control of the CPU without renouncing it.
Lastly, no process is allowed to do its own I/O to protect, which helps
you to keep the integrity of the various peripheral devices.

Security can improve reliability by detecting latent errors at the


interfaces between component subsystems. Early detection of interface
errors can prevent the foulness of a healthy subsystem by a
malfunctioning subsystem. An unprotected resource cannot misuse by
an unauthorized or incompetent user.

28
Command Interpreter System
One of the most important components of an operating system is its
command interpreter. The command interpreter is the primary interface
between the user and the rest of the system.

Many commands are given to the operating system by control


statements. A program that reads and interprets control statements is
automatically executed when a new job is started in a batch system or a
user logs in to a time-shared system. This program is variously called.

● The control card interpreter,

● The command-line interpreter,

● The shell (in UNIX), and so on.

Its function is quite simple, get the next command statement, and
execute it. The command statements deal with process management,
I/O handling, secondary storage management, main memory
management, file system access, protection, and networking.

29
Operating System Following are the services provided by

Services an operating system -


Program execution
The operating system provides the Control Input/output devices
programming environment in which a
programmer works on a computer system. Program creation
The user program requests various
resources through the operating system. The Error Detection and Response
operating system gives several services to Accounting
utility programmers and users. Applications
access these services through application Security and Protection
programming interfaces or system calls. By
File Management
invoking those interfaces, the application can
request a service from the operating system, Communication
pass parameters, and acquire the operation
outcomes.
30
Error Detection and Response
Program execution
An Error in a device may also cause malfunctioning of the entire device.
To execute a program, several tasks need to be These include hardware and software errors such as device failure,
memory error, division by zero, attempts to access forbidden memory
performed. Both the instructions and data must locations, etc. To avoid error, the operating system monitors the system for
be loaded into the main memory. In addition, detecting errors and takes suitable action with at least impact on running
input-output devices and files should be applications.
initialized, and other resources must be
prepared. The Operating structures handle these While working with computers, errors may occur quite often. Errors may
kinds of tasks. The user now no longer should occur in the:
fear the reminiscence allocation or multitasking
or anything. Input/ Output devices: For example, connection failure in the network, lack
of paper in the printer, etc.
Control Input/output devices
User program: For example: attempt to access illegal memory locations,
divide by zero, use too much CPU time, etc.
As there are numerous types of I/O devices within
the computer system, and each I/O device calls Memory hardware: For example, Memory error, the memory becomes full,
for its own precise set of instructions for the etc.
operation. The Operating System hides that info
with the aid of presenting a uniform interface. To handle these errors and other types of possible errors, the operating
Thus, it is convenient for programmers to access system takes appropriate action and generates messages to ensure correct
such devices easily. and consistent computing.

Program Creation Accounting

The Operating system offers the structures and An Operating device collects utilization records for numerous assets and
tracks the overall performance parameters and responsive time to enhance
tools, including editors and debuggers, to help overall performance. These personal records are beneficial for additional
the programmer create, modify, and debugging upgrades and tuning the device to enhance overall performance.
programs. 31
Security and Protection

Operating device affords safety to the statistics and packages of a person and protects any interference from unauthorized users.
The safety feature counters threats, which are published via way of individuals out of doors the manage of the running device.

For Example: When a user downloads something from the internet, that program may contain malicious code that may harm the
already existing programs. The operating system ensures that proper checks are applied while downloading such programs.

If one computer system is shared amongst a couple of users, then the various processes must be protected from another
intrusion. For this, the operating system provides various mechanisms that allow only those processes to use resources that have
gained proper authorization from the operating system. The mechanism may include providing unique users ids and passwords
to each user.

File management

Computers keep data and information on secondary storage devices like magnetic tape, magnetic disk, optical disk, etc. Each
storage media has its capabilities like speed, capacity, data transfer rate, and data access methods.

For file management, the operating system must know the types of different files and the characteristics of different storage
devices. It has to offer the proportion and safety mechanism of documents additionally.

Communication

The operating system manages the exchange of data and programs among different computers connected over a network. This
communication is accomplished using message passing and shared memory. 32
System Calls

33
What is System Call?
A system call is a method for a computer program to request a service from the
kernel of the operating system on which it is running. A system call is a method of
interacting with the operating system via programs. A system call is a request
from computer software to an operating system's kernel.

The Application Program Interface (API) connects the operating system's


functions to user programs. It acts as a link between the operating system and a
process, allowing user-level programs to request operating system services. The
kernel system can only be accessed using system calls. System calls are required
for any programs that use resources.

34
How are System Calls Made?
When a computer software needs to access the operating system's kernel, it makes a system call. The
system call uses an API to expose the operating system's services to user programs. It is the only
method to access the kernel system. All programs or processes that require resources for execution
must use system calls, as they serve as an interface between the operating system and user
programs.

A system call function may create and use kernel processes to execute the asynchronous processing.

A system call has greater authority than a standard subroutine. A system call with kernel-mode
privilege executes in the kernel protection domain.

System calls are not permitted to use shared libraries or any symbols that are not present in the
kernel protection domain.

The code and data for system calls are stored in global kernel memory.

35
Why do you need system calls in Operating System?
There are various situations where you must require system calls in the
operating system. Following of the situations are as follows:
It is must require when a file system wants to create or delete a file.
Network connections require the system calls to sending and receiving data
packets.
If you want to read or write a file, you need to system calls.
If you want to access hardware devices, including a printer, scanner, you need
a system call.
System calls are used to create and manage new processes.

36
Process Control
Process control is the system call that is used to direct the processes. Some process control examples include creating, load, abort, end, execute,
process, terminate the process, etc.

File Management
File management is a system call that is used to handle the files. Some file management examples include creating files, delete files, open, close,
read, write, etc.

Device Management
Device management is a system call that is used to deal with devices. Some examples of device management include read, device, write, get
device attributes, release device, etc.

Information Maintenance
Information maintenance is a system call that is used to maintain information. There are some examples of information maintenance, including getting system
data, set time or date, get time or date, set system data, etc.

Communication
Communication is a system call that is used for communication. There are some examples of communication, including create, delete
communication connections, send, receive messages, etc.

37
Process Windows Unix

Process Control CreateProcess() Fork()

ExitProcess() Exit()

WaitForSingleObject() Wait()

File Manipulation CreateFile() Open()

ReadFile() Read()

WriteFile() Write()

CloseHandle() Close()

Device Management SetConsoleMode() Ioctl()

ReadConsole() Read()

WriteConsole() Write()
38
Information Maintenance GetCurrentProcessID() Getpid()

SetTimer() Alarm()

Sleep() Sleep()

Communication CreatePipe() Pipe()

CreateFileMapping() Shmget()

MapViewOfFile() Mmap()

Protection SetFileSecurity() Chmod()

InitializeSecurityDescriptor() Umask()

SetSecurityDescriptorgroup() Chown()

39
open()
The open() system call allows you to access a file on a file system. It allocates

resources to the file and provides a handle that the process may refer to. Many processes can open a file at once or by a single process only. It's all based on
the file system and structure.

read()
It is used to obtain data from a file on the file system. It accepts three arguments in general:

● A file descriptor.
● A buffer to store read data.
● The number of bytes to read from the file.

The file descriptor of the file to be read could be used to identify it and open it using open() before reading.

wait()
In some systems, a process may have to wait for another process to complete its execution before proceeding. When a parent process makes a
child process, the parent process execution is suspended until the child process is finished. The wait() system call is used to suspend the parent
process. Once the child process has completed its execution, control is returned to the parent process.

40
write()
It is used to write data from a user buffer to a device like a file. This system call is one way for a program to generate data. It
takes three arguments in general:

● A file descriptor.
● A pointer to the buffer in which data is saved.
● The number of bytes to be written from the buffer.

fork()
Processes generate clones of themselves using the fork() system call. It is one of the most common ways to create
processes in operating systems. When a parent process spawns a child process, execution of the parent process is
interrupted until the child process completes. Once the child process has completed its execution, control is returned to the
parent process.

close()
It is used to end file system access. When this system call is invoked, it signifies that the program no longer requires the file,
and the buffers are flushed, the file information is altered, and the file resources are de-allocated as a result.

41
exec()

When an executable file replaces an earlier executable file in an already executing process, this system function is invoked.
As a new process is not built, the old process identification stays, but the new process replaces data, stack, data, head, etc.

exit()

The exit() is a system call that is used to end program execution. This call indicates that the thread execution is complete,
which is especially useful in multi-threaded environments. The operating system reclaims resources spent by the process
following the use of the exit() system function.

42
Module 2
Process and CPU Scheduling
Process and CPU Scheduling -

Process concepts and scheduling

Operations on processes

Cooperating Processes

INDEX:
Threads

Interprocess Communication

Scheduling Criteria

Scheduling Algorithms

Multiple -Processor Scheduling

System call interface for process management-

fork, exit, wait, waitpid, exec

44
Process

45
Process
● An operating system executes a variety of programs:
● Batch system – jobs
● Time-shared systems – user programs or tasks
● Textbook uses the terms job and process almost interchangeably
● Process – a program in execution; process execution must progress in sequential fashion
● Multiple parts
● The program code, also called text section
● Current activity including program counter, processor registers
● Stack containing temporary data
Function parameters, return addresses, local variables
● Data section containing global variables
● Heap containing memory dynamically allocated during run time
46
Process
● Program is passive entity stored on disk (executable file), process is active
● Program becomes process when executable file loaded into memory
● Execution of program started via GUI mouse clicks, command line entry of its
name, etc
● One program can be several processes
● Consider multiple users executing the same program

47
Process in Memory

48
Process State
● As a process executes, it changes state
● new: The process is being created
● running: Instructions are being executed
● waiting: The process is waiting for some event to occur
● ready: The process is waiting to be assigned to a processor
● terminated: The process has finished execution

49
Diagram of Process State

50
Process Control Block (PCB)
Information associated with each process
(also called task control block)
● Process state – running, waiting, etc
● Program counter – location of instruction to next execute
● CPU registers – contents of all process-centric registers
● CPU scheduling information- priorities, scheduling queue
pointers
● Memory-management information – memory allocated to the
process
● Accounting information – CPU used, clock time elapsed since
start, time limits
● I/O status information – I/O devices allocated to process, list of
open files

51
CPU Switch From Process to Process

52
Schedulers
● Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates
CPU
● Sometimes the only scheduler in a system
● Short-term scheduler is invoked frequently (milliseconds) ⇒ (must be fast)
● Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready
queue
● Long-term scheduler is invoked infrequently (seconds, minutes) ⇒ (may be slow)
● The long-term scheduler controls the degree of multiprogramming
● Processes can be described as either:
● I/O-bound process – spends more time doing I/O than computations, many short CPU bursts
● CPU-bound process – spends more time doing computations; few very long CPU bursts
● Long-term scheduler strives for good process mix

53
Context Switch
● When CPU switches to another process, the system must save the state of
the old process and load the saved state for the new process via a context
switch
● Context of a process represented in the PCB
● Context-switch time is overhead; the system does no useful work while
switching
● The more complex the OS and the PCB the longer the context switch
● Time dependent on hardware support
● Some hardware provides multiple sets of registers per CPU 🡺 multiple
contexts loaded at once

54
Operations on Processes
● System must provide mechanisms for:
● process creation,
● process termination

55
Process Creation
● Parent process create children processes, which, in turn create other
processes, forming a tree of processes
● Generally, process identified and managed via a process identifier (pid)
● Resource sharing options
● Parent and children share all resources
● Children share subset of parent’s resources
● Parent and child share no resources
● Execution options
● Parent and children execute concurrently
● Parent waits until children terminate
56
Process Creation
● Address space
● Child duplicate of parent
● Child has a program loaded into it
● UNIX examples
● fork() system call creates new process
● exec() system call used after a fork() to replace the process’ memory
space with a new program

57
Process Termination
● Process executes last statement and then asks the operating system to delete
it using the exit() system call.
● Returns status data from child to parent (via wait())
● Process’ resources are deallocated by operating system
● Parent may terminate the execution of children processes using the abort()
system call. Some reasons for doing so:
● Child has exceeded allocated resources
● Task assigned to child is no longer required
● The parent is exiting and the operating systems does not allow a child to
continue if its parent terminates
58
Process Termination
● Some operating systems do not allow child to exists if its parent has terminated. If
a process terminates, then all its children must also be terminated.
● cascading termination. All children, grandchildren, etc. are terminated.
● The termination is initiated by the operating system.
● The parent process may wait for termination of a child process by using the
wait()system call. The call returns status information and the pid of the
terminated process
pid = wait(&status);
● If no parent waiting (did not invoke wait()) process is a zombie
● If parent terminated without invoking wait , process is an orphan
59
Multi Process Architecture – Chrome Browser
● Many web browsers ran as single process (some still do)
● If one web site causes trouble, entire browser can hang or crash
● Google Chrome Browser is multiprocess with 3 different types of processes:
● Browser process manages user interface, disk and network I/O
● Renderer process renders web pages, deals with HTML, Javascript. A new renderer created for
each website opened
4 Runs in sandbox restricting disk and network I/O, minimizing effect of security exploits
● Plug-in process for each type of plug-in

60
Interprocess Communication

61
Interprocess Communication
● Processes within a system may be independent or cooperating
● Cooperating process can affect or be affected by other processes, including sharing data
● Reasons for cooperating processes:
● Information sharing
● Computation speedup
● Modularity
● Convenience
● Cooperating processes need interprocess communication (IPC)
● Two models of IPC
● Shared memory
● Message passing
62
Communications Models
(a) Message passing. (b) shared memory.

63
Cooperating Processes
● Independent process cannot affect or be affected by the execution of another
process
● Cooperating process can affect or be affected by the execution of another
process
● Advantages of process cooperation
● Information sharing
● Computation speed-up
● Modularity
● Convenience

64
Producer-Consumer Problem
● Paradigm for cooperating processes, producer process produces information
that is consumed by a consumer process
● unbounded-buffer places no practical limit on the size of the buffer
● bounded-buffer assumes that there is a fixed buffer size

65
Certain conditions must be met by the Producer and the Consumer processes to have consistent data
synchronization:

1. The Producer process must not produce an item if the shared buffer is full.
2. The Consumer process must not consume an item if the shared buffer is empty.
3. Access to the shared buffer must be mutually exclusive; this means that at any given instance, only
one process should be able to access the shared buffer and make changes to it.

Solution

The solution to the Producer-Consumer problem involves three semaphore variables.

semaphore Full: Tracks the space filled by the Producer process. It is initialized with a value of as the
buffer will have filled spaces at the beginning

semaphore Empty: Tracks the empty space in the buffer. It is initially set to buffer_size as the whole
buffer is empty at the beginning.

semaphore mutex: Used for mutual exclusion so that only one process can access the shared buffer at a
time.

66
Bounded-Buffer – Shared-Memory Solution
● Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;

item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;

● Solution is correct, but can only use BUFFER_SIZE-1 elements


67
Bounded-Buffer – Producer
item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}

68
Bounded Buffer – Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;

/* consume the item in next consumed */


}

69
Interprocess Communication – Shared Memory
● An area of memory shared among the processes that wish to communicate
● The communication is under the control of the users processes not the
operating system.
● Major issues is to provide mechanism that will allow the user processes to
synchronize their actions when they access shared memory.

70
Interprocess Communication – Message Passing
● Mechanism for processes to communicate and to synchronize their actions

● Message system – processes communicate with each other without resorting


to shared variables

● IPC facility provides two operations:


● send(message)
● receive(message)

● The message size is either fixed or variable

71
Message Passing
● If processes P and Q wish to communicate, they need to:
● Establish a communication link between them
● Exchange messages via send/receive
● Implementation issues:
● How are links established?
● Can a link be associated with more than two processes?
● How many links can there be between every pair of communicating processes?
● What is the capacity of a link?
● Is the size of a message that the link can accommodate fixed or variable?
● Is a link unidirectional or bi-directional?

72
Message Passing

● Implementation of communication link


● Physical:
Shared memory
Hardware bus
Network
● Logical:
Direct or indirect
Synchronous or asynchronous
Automatic or explicit buffering
73
Direct Communication
● Processes must name each other explicitly:
● send (P, message) – send a message to process P
● receive(Q, message) – receive a message from process Q
● Properties of communication link
● Links are established automatically
● A link is associated with exactly one pair of communicating processes
● Between each pair there exists exactly one link
● The link may be unidirectional, but is usually bi-directional

74
Indirect Communication
● Messages are directed and received from mailboxes (also referred to as ports)
● Each mailbox has a unique id
● Processes can communicate only if they share a mailbox
● Properties of communication link
● Link established only if processes share a common mailbox
● A link may be associated with many processes
● Each pair of processes may share several communication links
● Link may be unidirectional or bi-directional

75
Indirect Communication
● Operations
● create a new mailbox (port)
● send and receive messages through mailbox
● destroy a mailbox
● Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A

76
Indirect Communication
● Mailbox sharing
● P1, P2, and P3 share mailbox A
● P1, sends; P2 and P3 receive
● Who gets the message?
● Solutions
● Allow a link to be associated with at most two processes
● Allow only one process at a time to execute a receive operation
● Allow the system to select arbitrarily the receiver. Sender is notified who
the receiver was.

77
Synchronization
● Message passing may be either blocking or non-blocking
● Blocking is considered synchronous
● Blocking send -- the sender is blocked until the message is received
● Blocking receive -- the receiver is blocked until a message is available
● Non-blocking is considered asynchronous
● Non-blocking send -- the sender sends the message and continue
● Non-blocking receive -- the receiver receives:
● A valid message, or
● Null message
● Different combinations possible
● If both send and receive are blocking, we have a rendezvous
78
Synchronization
● Producer-consumer becomes trivial

message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);

/* consume the item in next consumed */


} 79
Buffering
● Queue of messages attached to the link.
● implemented in one of three ways
1. Zero capacity – no messages are queued on a link.
Sender must wait for receiver (rendezvous)
2. Bounded capacity – finite length of n messages
Sender must wait if link full
3. Unbounded capacity – infinite length
Sender never waits

80
Thread

81
● Thread is an execution unit that consists of its own program counter, a
stack, and a set of registers where the program counter mainly keeps
track of which instruction to execute next, a set of registers mainly hold
its current working variables, and a stack mainly contains the history of
execution.

82
● Threads are also known as Lightweight processes. Threads are a
popular way to improve the performance of an application through
parallelism. Threads are mainly used to represent a software approach
in order to improve the performance of an operating system just by
reducing the overhead thread that is mainly equivalent to a classical
process.

83
● The CPU switches rapidly back and forth among the threads giving the
illusion that the threads are running in parallel.

● As each thread has its own independent resource for process execution;
thus Multiple processes can be executed parallelly by increasing the
number of threads.

84
● It is important to note here that each thread belongs to exactly one
process and outside a process no threads exist. Each thread basically
represents the flow of control separately. In the implementation of
network servers and web servers threads have been successfully used.
Threads provide a suitable foundation for the parallel execution of
applications on shared-memory multiprocessors.

85
86
Process Thread

A Process simply means any program in execution. Thread simply means a segment of a process.

The process consumes more resources Thread consumes fewer resources.

The process requires more time for creation. Thread requires comparatively less time for creation than process.

The process is a heavyweight process Thread is known as a lightweight process

The process takes more time to terminate The thread takes less time to terminate.

A thread mainly shares the data segment, code segment, files, etc.
Processes have independent data and code segments
with its peer threads.

The process takes more time for context switching. The thread takes less time for context switching.

Communication between processes needs more time as compared to Communication between threads needs less time as compared to
thread. processes.

For some reason, if a process gets blocked then the remaining In case if a user-level thread gets blocked, all of its peer threads also
processes can continue their execution get blocked. 87
Advantages of Thread
● Responsiveness

● Resource sharing, hence allowing better utilization of resources.

● Economy. Creating and managing threads becomes easier.

● Scalability. One thread runs on one CPU. In Multithreaded processes, threads can be distributed over a series of
processors to scale.

● Context Switching is smooth. Context switching refers to the procedure followed by the CPU to change from one
task to another.

● Enhanced Throughput of the system.

88
User Level threads Kernel Level Threads

These threads are implemented by users. These threads are implemented by Operating systems

These threads are not recognized by operating systems, These threads are recognized by operating systems,

In User Level threads, the Context switch requires no hardware


In Kernel Level threads, hardware support is needed.
support.

These threads are mainly designed as dependent threads. These threads are mainly designed as independent threads.

In User Level threads, if one user-level thread performs a blocking On the other hand, if one kernel thread performs a blocking operation
operation then the entire process will be blocked. then another thread can continue the execution.

Example of User Level threads: Java thread, POSIX threads. Example of Kernel level threads: Window Solaris.

Implementation of User Level thread is done by a thread library and is While the Implementation of the kernel-level thread is done by the
easy. operating system and is complex.

This thread is generic in nature and can run on any operating system. This is specific to the operating system. 89
Multithreading Models
The user threads must be mapped to kernel threads, by one of the following
strategies:

● Many to One Model

● One to One Model

● Many to Many Model

90
Many to One Model
● In the many to one model, many user-level threads
are all mapped onto a single kernel thread.
● Thread management is handled by the thread library
in user space, which is efficient in nature.
● In this case, if user-level thread libraries are
implemented in the operating system in some way
that the system does not support them, then the
Kernel threads use this many-to-one relationship
model.

91
One to One Model
● The one to one model creates a separate
kernel thread to handle each and every user
thread.
● Most implementations of this model place a
limit on how many threads can be created.
● Linux and Windows from 95 to XP
implement the one-to-one model for
threads.
● This model provides more concurrency than
that of many to one Model.

92
Many to Many Model
● The many to many model multiplexes any
number of user threads onto an equal or
smaller number of kernel threads,
combining the best features of the
one-to-one and many-to-one models.
● Users can create any number of threads.
● Blocking the kernel system calls does not
block the entire process.
● Processes can be split across multiple
processors.

93
What are Thread Libraries?
● Thread libraries provide programmers with API for the creation and management of threads.

● Thread libraries may be implemented either in user space or in kernel space. The user space involves API functions
implemented solely within the user space, with no kernel support. The kernel space involves system calls and
requires a kernel with thread library support.
Three types of Thread

● POSIX Pitheads may be provided as either a user or kernel library, as an extension to the POSIX standard.

● Win32 threads are provided as a kernel-level library on Windows systems.

● Java threads: Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon
whatever OS and hardware the JVM is running on, i.e. either Pitheads or Win32 threads depending on the system.

94
Multiple-Processor Scheduling
● CPU scheduling more complex when multiple CPUs are available
● Homogeneous processors within a multiprocessor
● Asymmetric multiprocessing – only one processor accesses the system data
structures, alleviating the need for data sharing
● Symmetric multiprocessing (SMP) – each processor is self-scheduling, all processes
in common ready queue, or each has its own private queue of ready processes
○ Currently, most common
● Processor affinity – process has affinity for processor on which it is currently running
○ soft affinity
○ hard affinity
○ Variations including processor sets

95
Multiple-Processor Scheduling – Load Balancing

● If SMP, need to keep all CPUs loaded for efficiency

● Load balancing attempts to keep workload evenly


distributed

● Push migration – periodic task checks load on each


processor, and if found pushes task from overloaded CPU to
other CPUs

● Pull migration – idle processors pulls waiting task from busy


processor

96
Scheduling Algorithms

97
First Come First Serve

98
Characteristics of FCFS method
● It supports non-preemptive and preemptive scheduling algorithm.

● Jobs are always executed on a first-come, first-serve basis.

● It is easy to implement and use.

● This method is poor in performance, and the general wait time is quite
high.

99
FCFS ALGORITHM
● 1- Input the processes along with their burst time (bt).

● 2- Find waiting time (wt) for all processes.

● 3- As first process that comes need not to wait so waiting time for process 1 will be 0 i.e. wt[0] = 0.

● 4- Find waiting time for all other processes i.e. for process i -> wt[i] = bt[i-1] + wt[i-1] .

● 5- Find turnaround time = waiting_time + burst_time for all processes.

● 6- Find average waiting time = total_waiting_time / no_of_processes.

● 7- Similarly, find average turnaround time = total_turn_around_time / no_of_processes.

100
Advantages
● It doesn't include any complex logic, it just puts the process requests in
a queue and executes it one by one.

● Hence, FCFS is pretty simple and easy to implement.

● Eventually, every process will get a chance to run, so starvation doesn't


occur.

101
Disadvantages
● There is no option for preemption of a process. If a process is started,
then CPU executes the process until it ends.

● Because there is no pre-emption, if a process executes for a long time,


the processes in the back of the queue will have to wait for a long time
before they get a chance to be executed.

102
Shortest Job Next

103
Characteristics of SJF Scheduling
● It is associated with each job as a unit of time to complete.

● This algorithm method is helpful for batch-type processing, where waiting for jobs to
complete is not critical.

● It can improve process throughput by making sure that shorter jobs are executed first, hence
possibly have a short turnaround time.

● It improves job output by offering shorter jobs, which should be executed first, which mostly
have a shorter turnaround time.

104
Advantages
● According to the definition, short processes are executed first and then
followed by longer processes.

● The throughput is increased because more processes can be executed


in less amount of time.

105
Disadvantages
● The time taken by a process must be known by the CPU beforehand,
which is not possible.

● Longer processes will have more waiting time, eventually they'll suffer
starvation.

106
Round Robin (RR)

107
Characteristics of Round Robin Scheduling
● A preemptive scheduling algorithm.

● CPU shifts to the next process after a fixed time interval known as time quantum or
time-slice.
● Preempted processes are added to the end of the queue.

● A hybrid and clock-driven model.

● Time slice is usually the minimum but differs from OS to OS.

● A real time algorithm that responds to an event within a specific time limit.

● Oldest, fairest, and easiest algorithm.

● Widely used in traditional OS.

108
Advantages
● Each process is served by the CPU for a fixed time quantum, so all
processes are given the same priority.

● Starvation doesn't occur because for each round robin cycle, every
process is given a fixed time to execute. No process is left behind.

109
Disadvantages
● The throughput in RR largely depends on the choice of the length of the
time quantum. If time quantum is longer than needed, it tends to
exhibit the same behavior as FCFS.

● If time quantum is shorter than needed, the number of times that CPU
switches from one process to another process, increases. This leads to
decrease in CPU efficiency.

110
Priority based Scheduling

111
Characteristics of Priority Scheduling
● Schedules processes on the basis of priority.

● Used to perform batch processes.

● In the case of two processes with similar priorities, we use FCFS and
Round-Robin to choose between them.

● A number is given to each process to indicate its priority level.

● Lower is the number assigned, higher is the priority level of a process.

● If a higher priority task arrives when a lower priority task is executing, the
higher priority task replaces the one with lower priority and the

● latter is put on hold until it completes execution.


112
Advantages 
● The priority of a process can be selected based on memory
requirement, time requirement or user preference. For example, a high
end game will have better graphics, that means the process which
updates the screen in a game will have higher priority so as to achieve
better graphics performance.

113
Disadvantages
● A second scheduling algorithm is required to schedule the processes
which have same priority.

● In preemptive priority scheduling, a higher priority process can execute


ahead of an already executing lower priority process. If lower priority
process keeps waiting for higher priority processes, starvation occurs.

114
Usage of Scheduling Algorithms in Different Situations
● Every scheduling algorithm has a type of a situation where it is the best choice. Let's look at
different such situations:

● Situation 1:

● The incoming processes are short and there is no need for the processes to execute in a
specific order.

● In this case, FCFS works best when compared to SJF and RR because the processes are short
which means that no process will wait for a longer time. When each process is executed one
by one, every process will be executed eventually.

115
Usage of Scheduling Algorithms in Different Situations
● Situation 2:

● The processes are a mix of long and short processes and the task will only be
completed if all the processes are executed successfully in a given time.

● Round Robin scheduling works efficiently here because it does not cause
starvation and also gives equal time quantum for each process.

116
Usage of Scheduling Algorithms in Different Situations
● Situation 3:

● The processes are a mix of user based and kernel based processes.

● Priority based scheduling works efficiently in this case because generally kernel based
processes have higher priority when compared to user based processes.

● For example, the scheduler itself is a kernel based process, it should run first so that it can
schedule other processes.

117
Multilevel Queue

118
Multilevel Queue
● Ready queue is partitioned into separate queues, eg:
○ foreground (interactive)
○ background (batch)

● Process permanently in a given queue


● Each queue has its own scheduling algorithm:
○ foreground – RR
○ background – FCFS

● Scheduling must be done between the queues:


○ Fixed priority scheduling; (i.e., serve all from foreground then from background).
Possibility of starvation.
○ Time slice – each queue gets a certain amount of CPU time which it can schedule amongst
its processes; i.e., 80% to foreground in RR
○ 20% to background in FCFS 119
Multilevel Queue Scheduling

120
Multilevel Queue
● A multi-level queue scheduling algorithm partitions the ready queue into
several separate queues. The processes are permanently assigned to
one queue, generally based on some property of the process, such as
memory size, process priority, or process type. Each queue has its own
scheduling algorithm.

121
● The Description of the processes in the above diagram is as follows:

● System Process The Operating system itself has its own process to run
and is termed as System Process.

● Interactive Process The Interactive Process is a process in which there


should be the same kind of interaction (basically an online game ).

● Batch Processes Batch processing is basically a technique in the


Operating system that collects the programs and data together in the
form of the batch before the processing starts.

● Student Process The system process always gets the highest priority


while the student processes always get the lowest priority.

122
Advantages of Multilevel Queue Scheduling
● With the help of this scheduling we can apply various kind of scheduling for different kind of
processes:

● For System Processes: First Come First Serve(FCFS) Scheduling.

● For Interactive Processes: Shortest Job First (SJF) Scheduling.

● For Batch Processes: Round Robin(RR) Scheduling

● For Student Processes: Priority Scheduling

123
Disadvantages of Multilevel Queue Scheduling

● The main disadvantage of Multilevel Queue Scheduling is the problem of


starvation for lower-level processes.

Starvation:

● Due to starvation lower-level processes either never execute or have to wait


for a long amount of time because of lower priority or higher priority process
taking a large amount of time.

124
System call interface for process management

125
In computing, a system call is the programmatic way in
which a computer program requests a service from the
kernel of the operating system on which it is executed.
127
fork
A parent process uses fork to create a new child process. The child process
is a copy of the parent. After fork, both parent and child executes the
same program but in separate processes.
exec
Replaces the program executed by a process. The child may use exec after
a fork to replace the process’ memory space with a new program
executable making the child execute a different program than the
parent.
exit
Terminates the process with an exit status.
wait
The parent may use wait to suspend execution until a child terminates.
Using wait the parent can obtain the exit status of a terminated child.

128
Fork()

129
Parent and child
The process invoking fork is called the parent. The new process created as
the result of a fork is the child of the parent.
After a successful fork, the child process is a copy of the parent. The parent
and child processes executes the same program but in separate
processes.
Fork
The fork system call is the primary (and historically, only) method of
process creation in Unix-like operating systems.

130
#include <unistd.h>
pid_t fork(void);
Return value
On success, the PID of the child process is returned in the parent, and 0 is
returned in the child. On failure, -1 is returned in the parent, no child
process is created, and errno is set appropriately.

131
Fork returns twice on success
On success fork returns twice: once in the parent and once in the child.
After calling fork, the program can use the fork return value to tell
whether executing in the parent or child.
If the return value is 0 the program executes in the new child process.
If the return value is greater than zero, the program executes in the parent
process and the return value is the process ID (PID) of the created child
process.
On failure fork returns -1.

132
133
1 #include <stdio.h> // perror() 15
2 #include <stdlib.h> // exit(), 16 case 0:
EXIT_SUCCESS, EXIT_FAILURE 17 // On success fork() returns 0 in the child.
18
3 #include <unistd.h> // fork()
19 // Add code for the child process here.
4 20
5 int main(void) { 21 exit(EXIT_SUCCESS);
6 22
7 pid_t pid; 23 default:
24 // On success fork() returns the pid of the
8 child to the parent.
9 switch (pid = fork()) { 25
10 26 // Add code for the parent process here.
11 case -1: 27
28 exit(EXIT_SUCCESS);
12 // On error fork() returns -1.
29 }
13 perror("fork failed"); 30 }
14 exit(EXIT_FAILURE);
134
Header files
On lines 1-3 a number of header files are included to get access to a few functions and constants from the C Standard
library.
pid_t
One line 7 the variable pid of type pid_t is declared. The pid_t data type is the data type used for process IDs.
fork
On line 9 the parent process calls fork and stores the return value in the variable pid.
switch
On line 9 a switch-statement is used to check the return value of fork.
Error (case -1)
On failure fork returns -1 and execution continues in the case -1 branch of the switch statement (line 11). The operating
system was not able to create a new process. The parent uses perror to print an error message (line 13) and then
terminates with exit status EXIT_FAILURE (line 14).
Child (case 0)
On success fork returns 0 in the new child process and execution continues in the case 0 branch of the switch statement
(line 16). Any code to be executed only by the child is placed here (line 19). The child terminates with exit
status EXIT_SUCCESS (line 21).
Parent (default)
If the value fork returned by fork was neither -1 (error) nor 0 (child), execution continues in the parent process in
the default branch of the switch statement (line 23). In this case, the value returned by fork is the process ID (PID) of
the newly created child process.
135
int main(void) {
pid_t pid;
 
switch (pid = fork()) {
case -1:
// On error fork() returns -1.
perror("fork failed");
exit(EXIT_FAILURE);
case 0:
// On success fork() returns 0 in the child.
child();
default:
// On success fork() returns the pid of the child to the parent.
parent(pid);
}
}

136
void child() {
printf(" CHILD <%ld> I'm alive! My PID is <%ld> and my parent got PID <%ld>.\n",
(long) getpid(), (long) getpid(), (long) getppid());
printf(" CHILD <%ld> Goodbye!\n",
(long) getpid());
exit(EXIT_SUCCESS);
}
 
void parent(pid_t pid) {
printf("PARENT <%ld> My PID is <%ld> and I spawned a child with PID <%ld>.\n",
(long) getpid(), (long) getpid(), (long) pid);
printf("PARENT <%ld> Goodbye!\n",
(long) getpid());
exit(EXIT_SUCCESS);
}

137
● PARENT <87628> Spawned a child with PID = 87629.

● PARENT <87628> Goodbye.

● CHILD <87629> I'm alive and my PPID = 1.

● CHILD <87629> Goodbye.

138
Orphans
An orphan process is a process whose parent process has terminated, though it remains
running itself. Any orphaned process will be immediately adopted by the special init
system process with PID 1.
Processes execute concurrently
Both the parent process and the child process competes for the CPU with all other processes
in the system. The operating systems decides which process to execute when and for how
long. The process in the system execute concurrently.
In our example program:
most often the parent terminates before the child and the child becomes an orphan process
adopted by init (PID = 1) and therefore reports PPID = 1
sometimes the child process terminates before its parent and then the child is able to report
PPID equal to the PID of the parent. 139
Wait()
The wait system call blocks the caller
until one of its child process
terminates. If the caller doesn’t
have any child
processes, wait returns
immediately without blocking the
caller. Using wait the parent can
obtain the exit status of the
terminated child.

140
#include <sys/types.h>
#include <sys/wait.h> 
pid_t wait(int *status);
status
If status is not NULL, wait store the exit status of the terminated child in
the int to which status points. This integer can be inspected using
the WIFEXITED and WEXITSTATUS macros.
Return value
On success, wait returns the PID of the terminated child. On failure (no
child), wait returns -1.

141
#include <sys/types.h>
#include <sys/wait.h>
WIFEXITED(status);

status
The integer status value set by the wait system call.
Return value
Returns true if the child terminated normally, that is, by calling exit or by
returning from main.

142
#include <sys/types.h>
#include <sys/wait.h>
int WEXITSTATUS(status);
status
The integer status value set by the wait system call.
Return value
The exit status of the child. This consists of the least significant 8 bits of the
status argument that the child specified in a call to exit or as the
argument for a return statement in main. This macro should be
employed only if WIFEXITED returned true.

143
1 void parent(pid_t pid) {
2
3 printf("PARENT <%ld> Spawned a child with PID = %ld.\n",
4 (long) getpid(), (long) pid);
5
6 wait(NULL);
7
8 printf("PARENT <%ld> Child with PID = %ld terminated.\n",
9 (long) getpid(), (long) pid);
10
11 printf("PARENT <%ld> Goodbye.\n",
12 (long) getpid());
13
14 exit(EXIT_SUCCESS);
15 }

144
On line 6 the parent calls wait(NULL) to wait for the child process to
terminate.
Compile and run the program. Now the parent should always wait for the
child to terminate before terminating itself. As a consequence the child
should:
newer be adopted by init
new report PPID = 1
always report PPID equal to the PID of the parent.

145
Example using wait to obtain the exit status of the
child
1 void parent(pid_t pid) {
2 int status;
3
4 printf("PARENT <%ld> Spawned a child with PID = %ld.\n",
5 (long) getpid(), (long) pid);
6
7 wait(&status);
8
9 if (WIFEXITED(status)) {
10 printf("PARENT <%ld> Child with PID = %ld and exit status = %d terminated.\n",
11 (long) getpid(), (long) pid, WEXITSTATUS(status));
12 }
13
14 printf("PARENT <%ld> Goodbye.\n",
15 (long) getpid());
16
17 exit(EXIT_SUCCESS);
18 }

146
● One line 2 the parent creates the variable status. On line 7 the parent
calls wait(&status) to wait for the child process to terminate. The & is the
address-of operator and &status returns the address of
the status variable. When the child terminates the exit status of the child
will be stored in variable status.

147
● PARENT <99340> Spawned a child with PID = 99341.

● CHILD <99341> I'm alive and my PPID = 99340.

● CHILD <99341> Goodbye, exit with status 42.

● PARENT <99340> Child with PID = 99341 and exit status = 42 terminated.

● PARENT <99340> Goodbye.

148
Zombies
A terminated process is said to be a zombie or defunct until the parent
does wait on the child.
When a process terminates all of the memory and resources associated with it
are deallocated so they can be used by other processes.
However, the exit status is maintained in the PCB until the parent picks up the
exit status using wait and deletes the PCB.
A child process always first becomes a zombie.
In most cases, under normal system operation zombies are immediately waited
on by their parent.
Processes that stay zombies for a long time are generally an error and cause a
resource leak.

149
1 void parent(pid_t pid) {
2
3 printf("PARENT <%ld> Spawned a child with PID = %ld.\n",
4 (long) getpid(), (long) pid);
5
6 printf("PARENT <%ld> Press any key to reap a zombie!\n",
7 (long) getpid());
8
9 getchar();
10
11 pid = wait(NULL);
12
13 printf("PARENT <%ld> Zombie child with PID = %ld",
14 (long) getpid(), (long) pid);
15
16 exit(EXIT_SUCCESS);
17 }

150
● On line 9 the parent uses getchar to block itself until the user presses a
key on the keyboard.

● When the child terminates, the exit status of the child is stored in the
child process control block (PCB). The operating system deallocates all
memory used by the child but the PCB cannot be deallocated until the
parent does wait on the child.

151
● PARENT <4636> Spawned a child with PID = 4637.

● PARENT <4636> Press any key to reap a zombie!

● CHILD <4637> I'm alive and my PPID = 4636.

● CHILD <4637> Goodbye.

152
Module 3

153
Process Management and Synchronization -

Index The Critical Section Problem

Synchronization Hardware

Semaphores
Deadlocks -
Classical Problems of Synchronization
System Model
Critical Regions
Deadlocks Characterization Monitors

Methods for Handling Deadlocks Interprocess Communication Mechanisms:

Deadlock Prevention IPC between processes on a single computer system

IPC between processes on different systems


Deadlock Avoidance
using pipes
Deadlock Detection
FIFOs
Recovery from Deadlock message queues

shared memory.
154
Deadlock

155
System Model
Every process needs some resources to complete its execution. However, the
resource is granted in a sequential order.
● The process requests for some resource.
● OS grant the resource if it is available otherwise let the process waits.
● The process uses it and release on the completion.
A Deadlock is a situation where each of the computer process waits for a
resource which is being assigned to some another process. In this situation,
none of the process gets executed since the resource it needs, is held by some
other process which is also waiting for some other resource to be released.

156
Let us assume that there are three processes P1,
P2 and P3. There are three different resources
R1, R2 and R3. R1 is assigned to P1, R2 is
assigned to P2 and R3 is assigned to P3.

After some time, P1 demands for R1 which is


being used by P2. P1 halts its execution since it
can't complete without R2. P2 also demands for
R3 which is being used by P3. P2 also stops its
execution because it can't continue without R3.
P3 also demands for R1 which is being used by
P1 therefore P3 also stops its execution.

In this scenario, a cycle is being formed among


the three processes. None of the process is
progressing and they are all waiting. The
computer becomes unresponsive since all the
processes got blocked.

157
Sr. Deadlock Starvation

1 Deadlock is a situation where no process got blocked and no process Starvation is a situation where the low

proceeds priority process got blocked and the high

priority processes proceed.

2 Deadlock is an infinite waiting. Starvation is a long waiting but not

infinite.

3 Every Deadlock is always a starvation. Every starvation need not be deadlock.

4 The requested resource is blocked by the other process. The requested resource is continuously

be used by the higher priority processes.

5 Deadlock happens when Mutual exclusion, hold and wait, No preemption It occurs due to the uncontrolled priority

and circular wait occurs simultaneously. and resource management.

158
Deadlock Characterization
Mutual Exclusion

A resource can only be shared in mutually exclusive manner. It implies, if two process cannot use the
same resource at the same time.

Hold and Wait

A process waits for some resources while holding another resource at the same time.

No preemption

The process which once scheduled will be executed till the completion. No other process can be
scheduled by the scheduler meanwhile.

Circular Wait

All the processes must be waiting for the resources in a cyclic manner so that the last process is
waiting for the resource which is being held by the first process.

159
Methods of handling Deadlock

160
Deadlock Ignorance
Deadlock Ignorance is the most widely used approach among all the mechanism. This is
being used by many operating systems mainly for end user uses. In this approach, the
Operating system assumes that deadlock never occurs. It simply ignores deadlock. This
approach is best suitable for a single end user system where User uses the system only for
browsing and all other normal stuff.
There is always a tradeoff between Correctness and performance. The operating systems
like Windows and Linux mainly focus upon performance. However, the performance of the
system decreases if it uses deadlock handling mechanism all the time if deadlock happens 1
out of 100 times then it is completely unnecessary to use the deadlock handling mechanism
all the time.
In these types of systems, the user has to simply restart the computer in the case of
deadlock. Windows and Linux are mainly using this approach.

161
Deadlock Prevention
Deadlock happens only when Mutual Exclusion, hold and wait, No preemption
and circular wait holds simultaneously. If it is possible to violate one of the
four conditions at any time then the deadlock can never occur in the system.

The idea behind the approach is very simple that we have to fail one of the
four conditions but there can be a big argument on its physical
implementation in the system.

162
Mutual section from the resource point
of view is the fact that a resource can

Deadlock
never be used by more than one
process simultaneously which is fair
enough but that is the main reason

Prevention
behind the deadlock. If a resource could
have been used by more than one
process at the same time then the
process would have never been waiting
Mutual Exclusion for any resource.
However, if we can be able to violate
resources behaving in the mutually
exclusive manner then the deadlock can
be prevented.

163
For a device like printer, spooling can work. There is a

Deadlock memory associated with the printer which stores jobs


from each of the process into it. Later, Printer collects
all the jobs and print each one of them according to

Prevention
FCFS. By using this mechanism, the process doesn't
have to wait for the printer and it can continue
whatever it was doing. Later, it collects the output
when it is produced.
Mutual Exclusion Although, Spooling can be an effective approach to
Spooling violate mutual exclusion but it suffers from two kinds
of problems.

This cannot be applied to every resource.

After some point of time, there may arise a race


condition between the processes to get space in that
spool.

We cannot force a resource to be used by more than


one process at the same time since it will not be fair
enough and some serious problems may arise in the
performance. Therefore, we cannot violate mutual
exclusion for a process practically.

164
Hold and wait condition lies when a process
holds a resource and waiting for some other
resource to complete its task. Deadlock
occurs because there can be more than one
process which are holding one resource and

Deadlock waiting for other in the cyclic order.

However, we have to find out some

Prevention
mechanism by which a process either
doesn't hold any resource or doesn't wait.
That means, a process must be assigned all
the necessary resources before the
Hold and Wait execution starts. A process must not wait
for any resource once the execution has
been started.

!(Hold and wait) = !hold or !wait (negation of


hold and wait is, either you don't hold or
you don't wait)

165
This can be implemented practically if a
process declares all the resources initially.
However, this sounds very practical but
can't be done in the computer system
because a process can't determine

Deadlock necessary resources initially.

Process is the set of instructions which are

Prevention
executed by the CPU. Each of the instruction
may demand multiple resources at the
multiple times. The need cannot be fixed by
the OS.
Hold and Wait The problem with the approach is:

Practically not possible.

Possibility of getting starved will be


increases due to the fact that some process
may hold a resource for a very long time.

166
Deadlock arises due to the fact that a
process can't be stopped once it starts.
However, if we take the resource away from
the process which is causing deadlock then
we can prevent deadlock.

Deadlock This is not a good approach at all since if we


take a resource away which is being used by

Prevention
the process then all the work which it has
done till now can become inconsistent.

Consider a printer is being used by any


No Preemption process. If we take the printer away from
that process and assign it to some other
process then all the data which has been
printed can become inconsistent and
ineffective and also the fact that the process
can't start printing again from where it has
left which causes performance inefficiency.

167
To violate circular wait, we can assign
a priority number to each of the

Deadlock resource. A process can't request for


a lesser priority resource. This

Prevention ensures that not a single process can


request a resource which is being
utilized by some other process and
Circular wait no cycle will be formed.

Among all the methods, violating


Circular wait is the only approach
that can be implemented practically.

168
169
Deadlock Avoidance
In deadlock avoidance, the operating system checks whether the system is in safe
state or in unsafe state at every step which the operating system performs. The
process continues until the system is in safe state. Once the system moves to unsafe
state, the OS has to backtrack one step.
In simple words, The OS reviews each allocation so that the allocation doesn't cause
the deadlock in the system.
If the system cannot fulfill the request of all processes then the state of the system is
called unsafe.
The key of Deadlock avoidance approach is when the request is made for resources
then the request must only be approved in the case if the resulting state is also a
safe state.

170
Resource allocation Graph
The resource allocation graph is the pictorial representation of the state of a
system. As its name suggests, the resource allocation graph is the complete
information about all the processes which are holding some resources or
waiting for some resources.

It also contains the information about all the instances of all the resources
whether they are available or being used by the processes.

In Resource allocation graph, the process is represented by a Circle while the


Resource is represented by a rectangle. Let's see the types of vertices and
edges in detail.

171
172
Vertices are mainly of two types, Resource and process. Each of them will be
represented by a different shape. Circle represents process while rectangle
represents resource.
A resource can have more than one instance. Each instance will be
represented by a dot inside the rectangle.
Edges in RAG are also of two types, one represents assignment and other
represents the wait of a process for a resource. The above image shows each
of them.
A resource is shown as assigned to a process if the tail of the arrow is
attached to an instance to the resource and the head is attached to a process.
A process is shown as waiting for a resource if the tail of an arrow is attached
to the process while the head is pointing towards the resource.

173
174
Deadlock Detection using RAG
If a cycle is being formed in a
Resource allocation graph where all
the resources have the single
instance then the system is
deadlocked.
In Case of Resource allocation graph
with multi-instanced resource types,
Cycle is a necessary condition of
deadlock but not the sufficient
condition.

175
Allocation Matrix
Allocation matrix can be formed by Process R1 R2 R3

using the Resource allocation graph of


a system. In Allocation matrix, an entry
will be made for each of the resource P1 0 0 1

assigned. For Example, in the following


matrix, en entry is being made in front P2 1 0 0
of P1 and below R3 since R3 is
assigned to P1. P3 0 1 0

176
Request Matrix
In request matrix, an entry will be Process R1 R2 R3

made for each of the resource


requested. As in the following
example, P1 needs R1 therefore an P1 1 0 0
entry is being made in front of P1 and
below R1. P2 0 1 0

P3 0 0 1

177
Neither we are having any resource available in the system nor a process
going to release. Each of the process needs at least single resource to
complete therefore they will continuously be holding each one of them.

We cannot fulfill the demand of at least one process using the available
resources therefore the system is deadlocked as determined earlier when we
detected a cycle in the graph.

178
Banker's Algorithm

179
Banker's algorithm is a deadlock avoidance algorithm. It is named so because
this algorithm is used in banking systems to determine whether a loan can be
granted or not.
Consider there are n account holders in a bank and the sum of the money in
all of their accounts is S. Every time a loan has to be granted by the bank, it
subtracts the loan amount from the total money the bank has. Then it checks
if that difference is greater than S. It is done because, only then, the bank
would have enough money even if all the n account holders draw all their
money at once.
Banker's algorithm works in a similar way in computers.
Whenever a new process is created, it must specify the maximum instances of
each resource type that it needs, exactly.

180
Characteristics of Banker's Algorithm
If any process requests for a resource, then it has to wait.

This algorithm consists of advanced features for maximum resource


allocation.

There are limited resources in the system we have.

In this algorithm, if any process gets all the needed resources, then it is that it
should return the resources in a restricted period.

Various resources are maintained in this algorithm that can fulfill the needs of
at least one client.

181
Data Structures used to implement the Banker’s Algorithm
1. Available

It is an array of length m. It represents the number of available resources of each type. If Available[j] = k, then there are k
instances available, of resource type Rj.

2. Max

It is an n x m matrix which represents the maximum number of instances of each resource that a process can request. If
Max[i][j] = k, then the process Pi can request at most k instances of resource type Rj.

3. Allocation

It is an n x m matrix which represents the number of resources of each type currently allocated to each process. If
Allocation[i][j] = k, then process Pi is currently allocated k instances of resource type Rj.

4. Need

It is a two-dimensional array. It is an n x m matrix which indicates the remaining resource needs of each process. If Need[i][j]
= k, then process Pi may need k more instances of resource type Rj to complete its task.

Need[i][j] = Max[i][j] - Allocation [i][j]

182
Banker’s algorithm comprises of two algorithms:

Safety algorithm

Resource request algorithm

183
If there is no such i present, then proceed to step 4.

Safety Algorithm It means, we need to find an unfinished process whose


needs can be satisfied by the available resources. If no such
process exists, just go to step 4.

A safety algorithm is an algorithm used to find 3. Perform the following:


whether or not a system is in its safe state. The
algorithm is as follows: Work = Work + Allocationi

1.Let Work and Finish be vectors of length m and n, Finish[i] = true


respectively. Initially,
Go to step 2.
Work = Available
When an unfinished process is found, then the resources are
Finish[i] =false for i = 0, 1, ... , n - 1. allocated and the process is marked finished. And then, the
loop is repeated to check the same for all other processes.
This means, initially, no process has finished and the
number of available resources is represented by the 4. If Finish[i] == true for all i, then the system is in a safe
Available array. state.

2. Find an index i such that both That means if all processes are finished, then the system is in
safe state.
Finish[i] ==false
This algorithm may require an order of mxn² operations in
Needi <= Work order to determine whether a state is safe or not.

184
Resource Request Algorithm
Let Requesti be the request vector for the process Pi. If Requesti[j]==k, then process Pi wants k instance of Resource type
Rj.When a request for resources is made by the process Pi, the following are the actions that will be taken:

1. If Requesti <= Needi, then go to step 2;else raise an error condition, since the process has exceeded its maximum claim.

2.If Requesti <= Availablei then go to step 3; else Pi must have to wait as resources are not available.

3.Now we will assume that resources are assigned to process Pi and thus perform the following steps:

Available= Available-Requesti;

Allocationi=Allocationi +Requesti;

Needi =Needi - Requesti;

If the resulting resource allocation state comes out to be safe, then the transaction is completed and, process Pi is allocated
its resources. But in this case, if the new state is unsafe, then Pi waits for Requesti, and the old resource-allocation state is
restored.

185
Example:

Processes Allocation Max Available


ABC ABC ABC

P0 112 433 210

P1 212 322

P2 401 902

P3 020 753

P4 112 112

186
1. The Content of the need matrix can be calculated by using the formula given below:

Need = Max – Allocation

187
Safe Sequence:
2. For Process P1, Need = (1, 1, 0)

1. For process P0, Need = (3, 2, 1) and Available = (2, 1, 0)

Available = (2, 1, 0) Need <= Available = True

Need <=Available = False Request of P1 is granted.

So, the system will move to the next process. Available = Available +Allocation

= (2, 1, 0) + (2, 1, 2)

= (4, 2, 2) (New Available)

188
3. For Process P2, Need = (5, 0, 1) 4. For Process P3, Need = (7, 3, 3)

Available = (4, 2, 2) Available = (4, 2, 2)

Need <=Available = False Need <=Available = False

So, the system will move to the next process. So, the system will move to the next process.

189
5. For Process P4, Need = (0, 0, 0) 6. Now again check for Process P2, Need =
(5, 0, 1)
Available = (4, 2, 2)
Available = (5, 3, 4)
Need <= Available = True
Need <= Available = True
Request of P4 is granted.
Request of P2 is granted.
Available = Available + Allocation
Available = Available + Allocation
= (4, 2, 2) + (1, 1, 2)
= (5, 3, 4) + (4, 0, 1)
= (5, 3, 4) now, (New Available)
= (9, 3, 5) now, (New Available)

190
7. Now again check for Process P3, Need = 8. Now again check for Process P0, = Need
(7, 3, 3) (3, 2, 1)

Available = (9, 3, 5) = Available (9, 5, 5)

Need <=Available = True Need <= Available = True

The request for P3 is granted. So, the request will be granted to P0.

Available = Available +Allocation Safe sequence: < P1, P4, P2, P3, P0>

= (9, 3, 5) + (0, 2, 0) = (9, 5, 5)

191
3. The total amount of resources will be calculated by the following formula:

The total amount of resources= sum of columns of allocation + Available

= [8 5 7] + [2 1 0] = [10 6 7]

192
Disadvantages of Banker's Algorithm
Some disadvantages of this algorithm are as follows:

During the time of Processing, this algorithm does not permit a process to
change its maximum need.

Another disadvantage of this algorithm is that all the processes must know in
advance about the maximum resource needs.

This algorithm permits the requests to be provided in constrained time, but


for one year which is a fixed period.

193
Deadlock Detection and Recovery
In this approach, The OS doesn't apply any mechanism to
avoid or prevent the deadlocks. Therefore the system
considers that the deadlock will definitely occur. In order to
get rid of deadlocks, The OS periodically checks the system
for any deadlock. In case, it finds any of the deadlock then
the OS will recover the system using some recovery
techniques.

The main task of the OS is detecting the deadlocks. The OS


can detect the deadlocks with the help of Resource allocation
graph.

In single instanced resource types, if a cycle is being formed


in the system then there will definitely be a deadlock. On the
other hand, in multiple instanced resource type graph,
detecting a cycle is not just enough. We have to apply the
safety algorithm on the system by converting the resource
allocation graph into the allocation matrix and request
matrix.

194
195
For Resource
Preempt the resource
We can snatch one of the resources from the owner of the resource (process)
and give it to the other process with the expectation that it will complete the
execution and will release this resource sooner. Well, choosing a resource
which will be snatched is going to be a bit difficult.
Rollback to a safe state
System passes through various states to get into the deadlock state. The
operating system can rollback the system to the previous safe state. For this
purpose, OS needs to implement check pointing at every state.
The moment, we get into deadlock, we will rollback all the allocations to get
into the previous safe state.

196
For Process

Kill a process

Killing a process can solve our problem but the bigger concern is to decide
which process to kill. Generally, Operating system kills a process which has
done least amount of work until now.

Kill all process

This is not a suggestible approach but can be implemented if the problem


becomes very serious. Killing all process will lead to inefficiency in the system
because all the processes will execute again from starting.

197
Process Management and Synchronization

198
What is Process Synchronization?
Process Synchronization is the task of coordinating the execution of processes
in a way that no two processes can have access to the same shared data and
resources.

It is specially needed in a multi-process system when multiple processes are


running together, and more than one processes try to gain access to the same
shared resource or data at the same time.This can lead to the inconsistency of
shared data. So the change made by one process not necessarily reflected
when other processes accessed the same shared data. To avoid this type of
inconsistency of data, the processes need to be synchronized with each other.

199
200
201
Race Condition
counter++ could be implemented as Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = counter
{register1 = 5}
S1: producer execute register1 = register1
register1 = counter + 1 {register1 = 6}
register1 = register1 + 1 S2: consumer execute register2 = counter
{register2 = 5}
counter = register1 S3: consumer execute register2 = register2
– 1 {register2 = 4}
counter-- could be implemented as S4: producer execute counter = register1
{counter = 6 }
S5: consumer execute counter = register2
register2 = counter {counter = 4}

register2 = register2 - 1
counter = register2

202
where several processes access and manipulate the same data concurrently
and the outcome of the execution depends on the particular order in which
the access takes place, is called a race condition.

To guard against the race condition above, we need to ensure that only one
process at a time can be manipulating the variable counter. To make such a
guarantee, we require that the processes be synchronized in some way

203
204
Critical Section Problem

Consider system of n processes {p , p , … p }


0 1 n-1
Each process has critical section segment of code
Process may be changing common variables, updating table, writing file, etc
When one process in critical section, no other may be in its critical section
Critical section problem is to design protocol to solve this
Each process must ask permission to enter critical section in entry section,
may follow critical section with exit section, then remainder section

205
Entry Section: It is part of the process
which decides the entry of a particular
process.

Critical Section: This part allows one

Sections of a process to enter and modify the shared


variable.

Program
Exit Section: Exit section allows the other
process that are waiting in the Entry Section,
to enter into the Critical Sections. It also
checks that a process that finished its
execution should be removed through this
Section.

Remainder Section: All other parts of the


Code, which is not in Critical, Entry, and Exit
Section, are known as the Remainder
Section.

206
do {
General structure of entry section

a typical process Pi critical section

exit section

remainder section

} while (true);

207
What is critical section problem?
A critical section is a segment of code which can be accessed by a signal process at a specific
point of time. The section consists of shared data resources that required to be accessed by
other processes.

The entry to the critical section is handled by the wait() function, and it is represented as
P().

The exit from a critical section is controlled by the signal() function, represented as V().

In the critical section, only a single process can be executed. Other processes, waiting to
execute their critical section, need to wait until the current process completes its execution.

208
Rules for Critical Section:
Mutual Exclusion: Mutual Exclusion is a special type of binary semaphore which is used
for controlling access to the shared resource. It includes a priority inheritance mechanism
to avoid extended priority inversion problems. Not more than one process can execute in
its critical section at one time.
Progress: This solution is used when no one is in the critical section, and someone wants
in. Then those processes not in their reminder section should decide who should go in, in
a finite time.
Bound Waiting: When a process makes a request for getting into critical section, there is a
specific limit about number of processes can get into their critical section. So, when the
limit is reached, the system must allow request to the process to get into its critical
section.

209
At a given point in time, many kernel-mode processes may be active in the
operating system. As a result, the code implementing an operating system
(kernel code) is subject to several possible race conditions. Consider as an
example a kernel data structure that maintains a list of all open files in the
system. This list must be modified when a new file is opened or closed (adding
the file to the list or removing it from the list). If two processes were to open
files simultaneously, the separate updates to this list could result in a race
condition.

210
Two general approaches are used to handle critical sections in operating
systems: preemptive kernels and non preemptive kernels.

A preemptive kernel allows a process to be preempted while it is running in


kernel mode.

A non preemptive kernel does not allow a process running in kernel mode to
be preempted; a kernel-mode process will run until it exits kernel mode,
blocks, or voluntarily yields control of the CPU.

211
Solutions To The Critical Section
Peterson Solution

Synchronization Hardware

Mutex Locks

Semaphore Solution

212
Peterson’s Solution
Good algorithmic description of solving the problem
Two process solution
Assume that the load and store machine-language instructions are atomic; that is,
cannot be interrupted
The two processes share two variables:
int turn;
Boolean flag[2]
l
The variable turn indicates whose turn it is to enter the critical section
The flag array is used to indicate if a process is ready to enter the critical section.
flag[i] = true implies that process Pi is ready!
213
Peterson solution:
Peterson’s solution is widely used solution to PROCESS Pi
critical section problems. This algorithm was
FLAG[i] = true
developed by a computer scientist Peterson
that’s why it is named as a Peterson’s while( (turn != i) AND (CS is !free) ){ wait;
solution.
}
In this solution, when a process is executing in
a critical state, then the other process only CRITICAL SECTION FLAG[i] = false
executes the rest of the code, and the
turn = j; //choose another process to go to CS
opposite can happen. This method also helps
to make sure that only a single process runs
in the critical section at a specific time.

214
Synchronization Hardware
Many systems provide hardware support for implementing the critical section code.
All solutions below based on idea of locking
Protecting critical regions via locks
Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
Atomic = non-interruptible
Either test memory word and set value
Or swap contents of two memory words
215
Solution to Critical-section Problem Using Locks

do {

acquire lock

critical section

release lock

remainder section

} while (TRUE);

216
test_and_set Instruction
Definition: Executed atomically
boolean test_and_set (boolean Returns the original value of passed
*target) parameter
{
Set the new value of passed
boolean rv = *target; parameter to “TRUE”.
*target = TRUE;

return rv:

217
Solution using test_and_set()
Shared Boolean variable lock, initialized to FALSE
Solution:
do {
while (test_and_set(&lock))
; /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);

218
compare_and_swap Instruction
Definition: Executed atomically
int compare _and_swap(int *value, int
expected, int new_value) { Returns the original value of passed
parameter “value”
int temp = *value;
Set the variable “value” the value
if (*value == expected)
of the passed parameter
“new_value” but only if “value”
*value = new_value;
==“expected”. That is, the swap
return temp; takes place only under this
} condition.

219
Solution using compare_and_swap
Shared integer “lock” initialized to 0;
Solution:
do {
while (compare_and_swap(&lock, 0, 1) != 0)
; /* do nothing */
/* critical section */
lock = 0;
/* remainder section */
} while (true);

220
Bounded-waiting Mutual Exclusion with test_and_set

do { j = (j + 1) % n;
waiting[i] = true;
if (j == i)
key = true;
while (waiting[i] && key) lock = false;
key = test_and_set(&lock); else
waiting[i] = false; waiting[j] = false;
/* critical section */ /* remainder section */
j = (i + 1) % n; } while (true);
while ((j != i) && !waiting[j])

221
Mutex Locks
Previous solutions are complicated and generally inaccessible to application programmers

OS designers build software tools to solve critical section problem

Simplest is mutex lock

Protect a critical section by first acquire() a lock then release() the lock

Boolean variable indicating if lock is available or not

Calls to acquire() and release() must be atomic

Usually implemented via hardware atomic instructions

But this solution requires busy waiting

This lock therefore called a spinlock

222
acquire() and release()

acquire() { release() {
while (!available)
available = true;
; /* busy wait */
available = false;
}
}

223
acquire() and release()
do {

acquire lock

critical section

release lock

remainder section

} while (true);

224
semaphore
Synchronization tool that provides more sophisticated ways (than Mutex
locks) for process to synchronize their activities.

Semaphore S – integer variable

Can only be accessed via two indivisible (atomic) operations

wait() and signal()

Originally called P() and V()

225
semaphore
Definition of the wait() operation Definition of the signal() operation
wait(S) {
signal(S) {
while (S <= 0)
S++;
; // busy wait
}
S--;

226
semaphore
Counting semaphore – integer value can range over an unrestricted domain

Binary semaphore – integer value can range only between 0 and 1

Same as a mutex lock

227
semaphore
Can solve various synchronization problems P2:
Consider P1 and P2 that require S1 to
wait(synch);
happen before S2

Create a semaphore “synch” initialized to S2;


0
Can implement a counting
P1: semaphore S as a binary
S1; semaphore
signal(synch);

228
Semaphore Implementation
Must guarantee that no two processes can execute the wait() and signal() on
the same semaphore at the same time
Thus, the implementation becomes the critical section problem where the
wait and signal code are placed in the critical section
Could now have busy waiting in critical section implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections and therefore
this is not a good solution

229
Semaphore Implementation with no Busy waiting
With each semaphore there is an associated block – place the process invoking the
waiting queue operation on the appropriate waiting queue

Each entry in a waiting queue has two data wakeup – remove one of processes in the
items: waiting queue and place it in the ready queue

value (of type integer) typedef struct{

pointer to next record in the list int value;

Two operations: struct process *list;

} semaphore;

230
Implementation with no Busy waiting (Cont.)
wait(semaphore *S) { signal(semaphore *S) {

S->value--; S->value++;

if (S->value < 0) { if (S->value <= 0) {


add this process to S->list; remove a process P from S->list;

block(); wakeup(P);

} }

} }

231
Deadlock and Starvation
Deadlock – two or more processes are waiting Starvation – indefinite blocking
indefinitely for an event that can be caused by
only one of the waiting processes A process may never be removed from the
Let S and Q be two semaphores initialized to 1 semaphore queue in which it is suspended

P0 P1 Priority Inversion – Scheduling problem when


wait(S); wait(Q);
lower-priority process holds a lock needed by
higher-priority process
wait(Q); wait(S);
Solved via priority-inheritance protocol
... ...

signal(S); signal(Q);

signal(Q); signal(S);

232
Classical Problems
Bounded-Buffer Problem
of Synchronization Readers and Writers Problem

Dining-Philosophers Problem

233
Bounded-Buffer Problem
n buffers, each can hold one item

Semaphore mutex initialized to the value 1

Semaphore full initialized to the value 0

Semaphore empty initialized to the value n

234
Bounded Buffer Problem (Cont.)
The structure of the producer process /* add next produced
do { to the buffer */

... ...

/* produce an item in signal(mutex);


next_produced */ signal(full);
... } while (true);
wait(empty);
wait(mutex);
...

235
Bounded Buffer Problem (Cont.)
The structure of the consumer process signal(mutex);

Do { signal(empty);

wait(full); ...
/* consume the item in next consumed */
wait(mutex);
...
...
} while (true);
/* remove an item from buffer to
next_consumed */

236
Readers-Writers Problem
A data set is shared among a number of Several variations of how readers and writers
concurrent processes are considered – all involve some form of
priorities
Readers – only read the data set; they do not
perform any updates Shared Data

Writers – can both read and write Data set

Problem – allow multiple readers to read at Semaphore rw_mutex initialized to 1


the same time
Semaphore mutex initialized to 1
Only one single writer can access the shared
Integer read_count initialized to 0
data at the same time

237
Readers-Writers Problem(cont.)
The structure of a writer process

do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);

238
Readers-Writers Problem(cont.)
The structure of a reader process wait(mutex);
read count--;
do {
if (read_count == 0)
wait(mutex);
read_count++; signal(rw_mutex);
if (read_count == 1)
signal(mutex);
wait(rw_mutex);
} while (true);
signal(mutex);

...
/* reading is performed */

239
Readers-Writers Problem(cont.)
First variation – no reader kept waiting unless writer has permission to use
shared object

Second variation – once writer is ready, it performs the write ASAP

Both may have starvation leading to even more variations

Problem is solved on some systems by kernel providing reader-writer locks

240
Dining philosopher problem
The dining philosophers problem involves the allocation of limited resources
to a group of processes in a deadlock-free and starvation-free manner.

There are five philosophers sitting around a table, in which there are five
chopsticks/forks kept beside them and a bowl of rice in the center, When a
philosopher wants to eat, he uses two chopsticks - one from their left and one
from their right. When a philosopher wants to think, he keeps down both
chopsticks at their original place.

241
Dining philosopher solution using semaphore
do { Deadlock handling
wait (chopstick[i] ); Allow at most 4 philosophers to be sitting
wait (chopStick[ (i + 1) % simultaneously at the table.
5] );
Allow a philosopher to pick up the forks
// eat only if both are available (picking must be
signal (chopstick[i] ); done in a critical section.
signal (chopstick[ (i + 1) Use an asymmetric solution -- an
% 5] ); odd-numbered philosopher picks up first
// think the left chopstick and then the right
} while (TRUE); chopstick. Even-numbered philosopher
picks up first the right chopstick and then
the left chopstick.

242
Problems with semaphore
Incorrect use of semaphore operations:

signal (mutex) …. wait (mutex)

wait (mutex) … wait (mutex)

Omitting of wait (mutex) or signal (mutex) (or both)

Deadlock and starvation are possible.

243
Monitors
A high-level abstraction that provides a monitor monitor-name
convenient and effective mechanism for
{
process synchronization
// shared variable declarations
Abstract data type, internal variables only
accessible by code within the procedure procedure P1 (…) { …. }

Only one process may be active within the


monitor at a time procedure Pn (…) {……}
But not powerful enough to model some
synchronization schemes
Initialization code (…) { … }
}
} 244
245
Condition variables
condition x, y;

Two operations are allowed on a condition variable:

x.wait() – a process that invokes the operation is suspended until x.signal()

x.signal() – resumes one of processes (if any) that invoked x.wait()

If no x.wait() on the variable, then it has no effect on the variable

246
Monitor with Condition Variables

247
Condition Variables Choices
If process P invokes x.signal(), and process Q is suspended in x.wait(), what should happen next?

Both Q and P cannot execute in parallel. If Q is resumed, then P must wait

Options include

Signal and wait – P waits until Q either leaves the monitor or it waits for another condition

Signal and continue – Q waits until P either leaves the monitor or it waits for another condition

Both have pros and cons – language implementer can decide

Monitors implemented in Concurrent Pascal compromise

P executing signal immediately leaves the monitor, Q is resumed

Implemented in other languages including Mesa, C#, Java

248
Monitor Solution to Dining Philosophers

monitor DiningPhilosophers void putdown (int i) {


{
state[i] = THINKING;
enum { THINKING; HUNGRY, EATING) state [5] ;
// test left and right neighbors
condition self [5];
test((i + 4) % 5);
void pickup (int i) {
test((i + 1) % 5);
state[i] = HUNGRY;
}
test(i);

if (state[i] != EATING) self[i].wait;

249
void test (int i) { initialization_code() {
if ((state[(i + 4) % 5] != EATING) && for (int i = 0; i < 5; i++)
(state[i] == HUNGRY) && state[i] = THINKING;
}
(state[(i + 1) % 5] != EATING) ) {
}
state[i] = EATING ;

self[i].signal () ;

250
Each philosopher i invokes the operations pickup() and putdown() in the
following sequence:

DiningPhilosophers.pickup(i);

EAT

DiningPhilosophers.putdown(i);

No deadlock, but starvation is possible

251
Monitor Implementation Using Semaphores
Variables wait(mutex);

semaphore mutex; // (initially = 1) …

body of F;
semaphore next; // (initially = 0)

int next_count = 0;
if (next_count > 0)
Each procedure F will be replaced by signal(next)

else

signal(mutex);

Mutual exclusion within a monitor is ensured

252
Monitor Implementation – Condition Variables
For each condition variable x, we have: The operation x.wait can be implemented as:

x_count++;

semaphore x_sem; // (initially = 0) if (next_count > 0)

int x_count = 0; signal(next);

else

signal(mutex);

wait(x_sem);

x_count--;

253
The operation x.signal can be implemented as:

if (x_count > 0) {
next_count++;
signal(x_sem);
wait(next);
next_count--;
}

254
Resuming Processes within a Monitor
If several processes queued on condition x, and x.signal() executed, which
should be resumed?

FCFS frequently not adequate

conditional-wait construct of the form x.wait(c)

Where c is priority number

Process with lowest number (highest priority) is scheduled next

255
Single Resource allocation
Allocate a single resource among competing processes using priority numbers
that specify the maximum time a process plans to use the resource
R.acquire(t);
...
access the resurce;
...
R.release;
Where R is an instance of type ResourceAllocator

256
A Monitor to Allocate Single Resource
monitor ResourceAllocator void release() {
{ busy = FALSE;
boolean busy;
x.signal();
condition x;
}
void acquire(int time) {
initialization code() {
if (busy)
busy = FALSE;
x.wait(time);
}
busy = TRUE;
} }

257
Interprocess communication

258
Inter-process communication is
used for exchanging useful
information between numerous

Definition
threads in one or more processes
(or programs).

A thread is the smallest unit of processing that can


be performed in an OS. In most modern operating
systems, a thread exists within a process - that is, a
single process may contain multiple threads.

259
It is a set of programming interface which allow a programmer to coordinate
activities among various program processes which can run concurrently in an
operating system. This allows a specific program to handle many user
requests at the same time.

Since every single user request may result in multiple processes running in the
operating system, the process may require to communicate with each other.
Each IPC protocol approach has its own advantage and limitation, so it is not
unusual for a single program to use all of the IPC methods.

260
Role of Synchronization in Inter Process Communication
It is one of the essential parts of inter process communication. Typically, this is
provided by interprocess communication control mechanisms, but sometimes
it can also be controlled by communication processes.
These are the following methods that used to provide the synchronization:
Mutual Exclusion
Semaphore
Barrier
Spinlock

261
Mutual Exclusion:-

It is generally required that only one process thread can enter the critical section at a time.
This also helps in synchronization and creates a stable state to avoid the race condition.

Semaphore:-

Semaphore is a type of variable that usually controls the access to the shared resources by
several processes. Semaphore is further divided into two types which are as follows:

Binary Semaphore

Counting Semaphore

262
Barrier:-
A barrier typically not allows an individual process to proceed unless all the
processes does not reach it. It is used by many parallel languages, and
collective routines impose barriers.
Spinlock:-
Spinlock is a type of lock as its name implies. The processes are trying to
acquire the spinlock waits or stays in a loop while checking that the lock is
available or not. It is known as busy waiting because even though the process
active, the process does not perform any functional operation (or task).

263
Approaches for Inter-Process Communication

264
These are a few different approaches for Inter- Process Communication:
Pipes
Message Passing
Shared Memory
Message Queue
Direct Communication
Indirect communication
FIFO

265
Pipes:
The pipe is a type of data channel that is unidirectional in nature. It means
that the data in this type of data channel can be moved in only a single
direction at a time. Still, one can use two-channel of this type, so that he can
able to send and receive data in two processes. Typically, it uses the standard
methods for input and output. These pipes are used in all types of POSIX
systems and in different versions of window operating systems as well.

Pipe is widely used for communication between two related processes. This is
a half-duplex method, so the first process communicates with the second
process. However, in order to achieve a full-duplex, another pipe is needed.

266
Message passing:
It is a type of mechanism that allows processes to synchronize and
communicate with each other. Using message passing, the process
communicates with each other without resorting to shared variables.

IPC mechanism provides two operations:

Send (message)- message size fixed or variable

Received (message)

267
Shared memory:
Shared memory is a memory shared between two or more processes that are
established using shared memory between all the processes. This type of
memory requires to protected from each other by synchronizing access across
all the processes.

It can be referred to as a type of memory that can be used or accessed by


multiple processes simultaneously. It is primarily used so that the processes
can communicate with each other. Therefore the shared memory is used by
almost all POSIX and Windows operating systems as well.

268
Message queue:

269
Message queue:
A message queue is a linked list of messages stored within the kernel. It is
identified by a message queue identifier. This method offers communication
between single or multiple processes with full-duplex capacity.

In general, several different messages are allowed to read and write the data
to the message queue. In the message queue, the messages are stored or stay
in the queue unless their recipients retrieve them. In short, we can also say
that the message queue is very helpful in inter-process communication and
used by all operating systems.

270
Direct communication:
In this type of communication process, usually, a link is created or established
between two communicating processes. However, in every pair of
communicating processes, only one link can exist.

271
Indirect communication:
Indirect communication establishes like only when processes share a common
mailbox each pair of processes sharing several communication links. A link
can communicate with many processes. The link may be bi-directional or
unidirectional.

272
FIFO:
Communication between two unrelated processes. It is a full-duplex method,
which means that the first process can communicate with the second process,
and the opposite can also happen.

273
Some other different approaches
Socket:-

It acts as a type of endpoint for receiving or sending the data in a network. It is correct for data sent between
processes on the same computer or data sent between different computers on the same network. Hence, it
used by several types of operating systems.

File:-

A file is a type of data record or a document stored on the disk and can be acquired on demand by the file
server. Another most important thing is that several processes can access that file as required or needed.

Signal:-

As its name implies, they are a type of signal used in inter process communication in a minimal way. Typically,
they are the massages of systems that are sent by one process to another. Therefore, they are not used for
sending data but for remote commands between multiple processes.

274
Why we need interprocess communication?
There are numerous reasons to use inter-process communication for sharing
the data. Here are some of the most important reasons that are given below:
It helps to speedup modularity
Computational
Privilege separation
Convenience
Helps operating system to communicate with each other and synchronize
their actions.

275
UNIT - IV
Memory Management and Virtual Memory

276
Index:
Logical versus Physical Address Space

Swapping

Contiguous Allocation

Paging

Segmentation

Segmentation with Paging

Demand Paging

Page Replacement

Page Replacement Algorithms.

277
Logical and Physical Address space
Address uniquely identifies a location in the memory. We have two types of
addresses that are logical address and physical address. The logical address is
a virtual address and can be viewed by the user. The user can’t view the
physical address directly. The logical address is used like a reference, to access
the physical address. The fundamental difference between logical and physical
address is that logical address is generated by CPU during a program
execution whereas, the physical address refers to a location in the memory
unit.

278
Difference Between Logical and Physical Address
BASIS FOR COMPARISON LOGICAL ADDRESS PHYSICAL ADDRESS

Basic It is the virtual address generated The physical address is a


by CPU location in a memory unit.

Address Space Set of all logical addresses Set of all physical addresses
generated by CPU in reference mapped to the corresponding
to a program is referred as logical addresses is referred as
Logical Address Space. Physical Address.

Visibility The user can view the logical The user can never view physical
address of a program. address of program

Access The user uses the logical The user can not directly access
address to access the physical physical address.
address.

Generation The Logical Address is Physical Address is Computed


generated by the CPU by MMU
279
Definition of Logical Address
Address generated by CPU while a program is running is referred as Logical
Address. The logical address is virtual as it does not exist physically. Hence, it
is also called as Virtual Address. This address is used as a reference to access
the physical memory location. The set of all logical addresses generated by a
programs perspective is called Logical Address Space.
The logical address is mapped to its corresponding physical address by a
hardware device called Memory-Management Unit. The address-binding
methods used by MMU generates identical logical and physical address during
compile time and load time. However, while run-time the address-binding
methods generate different logical and physical address.

280
Definition of Physical Address
Physical Address identifies a physical location in a memory. MMU
(Memory-Management Unit) computes the physical address for the corresponding
logical address. MMU also uses logical address computing physical address. The user
never deals with the physical address. Instead, the physical address is accessed by
its corresponding logical address by the user. The user program generates the
logical address and thinks that the program is running in this logical address. But the
program needs physical memory for its execution. Hence, the logical address must
be mapped to the physical address before they are used.
The logical address is mapped to the physical address using a hardware called
Memory-Management Unit. The set of all physical addresses corresponding to the
logical addresses in a Logical address space is called Physical Address Space.

281
Key Differences Between Logical and Physical Address
1. The basic difference between Logical and physical address is that Logical address is generated by CPU in
perspective of a program. On the other hand, the physical address is a location that exists in the memory unit.

2. The set of all logical addresses generated by CPU for a program is called Logical Address Space. However, the
set of all physical address mapped to corresponding logical addresses is referred as Physical Address Space.

3. The logical address is also called virtual address as the logical address does not exist physically in the memory
unit. The physical address is a location in the memory unit that can be accessed physically.

4. Identical logical address and physical address are generated by Compile-time and Load time address binding
methods.

5. The logical and physical address generated while run-time address binding method differs from each other.

6. The logical address is generated by the CPU while program is running whereas, the physical addres is
computed by the MMU (Memory Management Unit).

282
Swapping
Swapping is a mechanism in which a process can be swapped temporarily out of
main memory (or move) to secondary storage (disk) and make that memory
available to other processes. At some later time, the system swaps back the process
from the secondary storage to main memory.
Swapping is a mechanism in which a process can be swapped temporarily out of
main memory (or move) to secondary storage (disk) and make that memory
available to other processes. At some later time, the system swaps back the process
from the secondary storage to main memory.
Though performance is usually affected by swapping process but it helps in running
multiple and big processes in parallel and that's the reason Swapping is also known
as a technique for memory compaction.

283
284
Contiguous memory allocation
It means freely available memory partitions are not scattered here and there across the whole
memory space. In the contiguous memory allocation, both the operating system and the user must
reside in the main memory. A single process is allocated in that fixed sized single partition.

In the contiguous memory allocation, both the operating system and the user must reside in the
main memory. The main memory is divided into two portions one portion is for the operating and
other is for the user program.

In the contiguous memory allocation when any user process request for the memory a single section
of the contiguous memory block is given to that process according to its need. We can achieve
contiguous memory allocation by dividing memory into the fixed-sized partition.

A single process is allocated in that fixed sized single partition. But this will increase the degree of
multiprogramming means more than one process in the main memory that bounds the number of
fixed partition done in memory. Internal fragmentation increases because of the contiguous memory
allocation.

285
286
Fixed sized partition
In the fixed sized partition the system divides memory into fixed size partition
(may or may not be of the same size) here entire partition is allowed to a
process and if there is some wastage inside the partition is allocated to a
process and if there is some wastage inside the partition then it is called
internal fragmentation.
Advantage: Management or book keeping is easy.
Disadvantage: Internal fragmentation

287
Variable size partition

In the variable size partition, the memory is treated as one unit and space
allocated to a process is exactly the same as required and the leftover space
can be reused again.

Advantage: There is no internal fragmentation.

288
Paging
In computer operating systems, paging is a memory management scheme by
which a computer stores and retrieves data from secondary storage for use in
main memory. In this scheme, the operating system retrieves data from
secondary storage in same-size blocks called pages.Paging is a memory
management scheme that eliminates the need for contiguous allocation of
physical memory. This scheme permits the physical address space of a
process to be non –contiguous.

289
Logical Address or Virtual Address (represented in bits): An address
generated by the CPU
Logical Address Space or Virtual Address Space( represented in words or
bytes): The set of all logical addresses generated by a program
Physical Address (represented in bits): An address actually available on
memory unit
Physical Address Space (represented in words or bytes): The set of all
physical addresses corresponding to the logical addresses
The Physical Address Space is conceptually divided into a number of fixed-size
blocks, called frames.
The Logical address Space is also splitted into fixed-size blocks, called pages.
Page Size = Frame Size

290
Address generated by CPU is divided into
Page number(p): Number of bits required to represent the pages in Logical
Address Space or Page number
Page offset(d): Number of bits required to represent particular word in a
page or page size of Logical Address Space or word number of a page or page
offset.
Physical Address is divided into
Frame number(f): Number of bits required to represent the frame of Physical
Address Space or Frame number.
Frame offset(d): Number of bits required to represent particular word in a
frame or frame size of Physical Address Space or word number of a frame or
frame offset.

291
The hardware implementation of page table can be done by using dedicated
registers. But the usage of register for the page table is satisfactory only if
page table is small. If page table contain large number of entries then we can
use TLB(translation Lookaside buffer), a special, small, fast look up hardware
cache.

292
Segmentation
Segmentation is a memory management technique in which, the memory is divided into the variable
size parts. Each part is known as segment which can be allocated to a process. The details about each
segment are stored in a table called as segment table.

In Operating Systems, Segmentation is a memory management technique in which, the memory is


divided into the variable size parts. Each part is known as segment which can be allocated to a
process.

The details about each segment are stored in a table called as segment table. Segment table is stored
in one (or many) of the segments.

Segment table contains mainly two information about segment:

1. Base: It is the base address of the segment

2. Limit: It is the length of the segment.

293
Why segments are required?
Till now, we were using Paging as our main memory management technique. Paging
is more close to Operating system rather than the User. It divides all the process into
the form of pages regardless of the fact that a process can have some relative parts
of functions which needs to be loaded in the same page.
Operating system doesn't care about the User's view of the process. It may divide
the same function into different pages and those pages may or may not be loaded at
the same time into the memory. It decreases the efficiency of the system.
It is better to have segmentation which divides the process into the segments. Each
segment contain same type of functions such as main function can be included in
one segment and the library functions can be included in the other segment.

294
Translation of Logical address into physical address by
segment table
CPU generates a logical address which contains two parts:
1. Segment Number
2. Offset
The Segment number is mapped to the segment table. The limit of the
respective segment is compared with the offset. If the offset is less than the
limit then the address is valid otherwise it throws an error as the address is
invalid. In the case of valid address, the base address of the segment is added
to the offset to get the physical address of actual word in the main memory.

295
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Disadvantages
1. It can have external fragmentation.
2. it is difficult to allocate contiguous memory to variable sized partition.
3. Costly memory management algorithms.

296
Virtual Memory
Virtual memory is a memory management capability of an operating system (OS) that uses
hardware and software to allow a computer to compensate for physical memory shortages
by temporarily transferring data from random access memory (RAM) to disk storage.

Virtual Memory is a storage allocation scheme in which secondary memory can be


addressed as though it were part of main memory. The addresses a program may use to
reference memory are distinguished from the addresses the memory system uses to
identify physical storage sites, and program generated addresses are translated
automatically to the corresponding machine addresses.

The size of virtual storage is limited by the addressing scheme of the computer system and
amount of secondary memory is available not by the actual number of the main storage
locations.

297
It is a technique that is implemented using both hardware and software. It maps
memory addresses used by a program, called virtual addresses, into physical
addresses in computer memory.
1. All memory references within a process are logical addresses that are dynamically
translated into physical addresses at run time. This means that a process can be
swapped in and out of main memory such that it occupies different places in main
memory at different times during the course of execution.
2. A process may be broken into number of pieces and these pieces need not be
continuously located in the main memory during execution. The combination of
dynamic run-time address translation and use of page or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or
segments are present in the main memory during execution. This means that the
required pages need to be loaded into memory whenever required. Virtual memory
is implemented using Demand Paging or Demand Segmentation.

298
Hardware and Control Structures
Memory references are dynamically translated into physical addresses at run
time –

● A process may be swapped in and out of main memory such that it


occupies different regions
● A process may be broken up into pieces that do not need to be located
contiguously in main memory
● All pieces of a process do not need to be loaded in main memory during
execution

299
Execution of a Program
Operating system brings into main memory a few pieces of the program

• Resident set - portion of process that is in main memory

• An interrupt is generated when an address is needed that is not in main memory

• Operating system places the process in a blocking state

Piece of process that contains the logical address is brought into main memory –
Operating system issues a disk I/O Read request – Another process is dispatched to
run while the disk I/O takes place – An interrupt is issued when disk I/O complete
which causes the operating system to place the affected process in the Ready state.

300
Advantages of Breaking up a Process

• More processes may be maintained in main memory – Only load in some of


the pieces of each process – With so many processes in main memory, it is
very likely a process will be in the Ready state at any particular time

• A process may be larger than all of main memory

301
Types of Memory

• Real memory – Main memory

• Virtual memory – Memory on disk – Allows for effective multiprogramming


and relieves the user of tight constraints of main memory – Programming
convenience

302
Thrashing

• Swapping out a piece of a process just before that piece is needed

• The processor spends most of its time swapping pieces rather than
executing user instructions

Support Needed for Virtual Memory

• Hardware must support paging and segmentation

• Operating system must be able to management the movement of pages


and/or segments between secondary memory and main memory

303
Paging

• Each process has its own page table

• Each page table entry contains the frame number of the corresponding page
in main memory

• A bit is needed to indicate whether the page is in main memory or not

Modify Bit in Page Table

• Modify bit is needed to indicate if the page has been altered since it was last
loaded into main memory

• If no change has been made, the page does not have to be written to the
disk when it needs to be swapped out

304
Locality of Reference
Locality of reference, also known as the principle of locality, is the tendency of a processor to access
the same set of memory locations repetitively over a short period of time. There are two basic types
of reference locality – temporal and spatial locality.

Locality of reference is accessing a value or related storage frequently. And the types are temporal,
spatial and sequential.

Temporal: It tells us whether memory locations in a program are likely to be accessed again in the
near future.

Locality of Reference and Cache Operation in Cache Memory

Locality of reference refers to a phenomenon in which a computer program tends to access same set
of memory locations for a particular time period. In other words, Locality of Reference refers to the
tendency of the computer program to access instructions whose addresses are near one another.
The property of locality of reference is mainly shown by loops and subroutine calls in a program.

305
306
1. In case of loops in program control processing unit repeatedly refers to the
set of instructions that constitute the loop.

2. In case of subroutine calls, everytime the set of instructions are fetched


from memory.

3. References to data items also get localized that means same data item is
referenced again and again.

307
308
309
In the above figure, you can see that the CPU wants to read or fetch the data or
instruction. First, it will access the cache memory as it is near to it and provides very
fast access. If the required data or instruction is found, it will be fetched. This
situation is known as a cache hit. But if the required data or instruction is not found
in the cache memory then this situation is known as a cache miss. Now the main
memory will be searched for the required data or instruction that was being
searched and if found will go through one of the two ways:

1. First way is that the CPU should fetch the required data or instruction and use it
and that’s it but what, when the same data or instruction is required again.CPU again
has to access the same main memory location for it and we already know that main
memory is the slowest to access.

2. The second way is to store the data or instruction in the cache memory so that if it
is needed soon again in the near future it could be fetched in a much faster way.

310
Cache Operation:
It is based on the principle of locality of reference. There are two ways with which data or instruction is fetched
from main memory and get stored in cache memory. These two ways are the following:

1. Temporal Locality –

Temporal locality means current data or instruction that is being fetched may be needed soon. So we should
store that data or instruction in the cache memory so that we can avoid again searching in main memory for the
same data. When CPU accesses the current main memory location for reading required data or instruction, it
also gets stored in the cache memory which is based on the fact that same data or instruction may be needed in
near future. This is known as temporal locality. If some data is referenced, then there is a high probability that it
will be referenced again in the near future.

2. Spatial Locality –

Spatial locality means instruction or data near to the current memory location that is being fetched, may be
needed soon in the near future. This is slightly different from the temporal locality. Here we are talking about
nearly located memory locations while in temporal locality we were talking about the actual memory location
that was being fetched.

311
Cache Performance:
The performance of the cache is measured in terms of hit ratio. When CPU
refers to memory and find the data or instruction within the Cache Memory, it
is known as cache hit. If the desired data or instruction is not found in the
cache memory and CPU refers to the main memory to find that data or
instruction, it is known as a cache miss.

312
Demand Paging
According to the concept of Virtual Memory, in order to execute some process, only a part of the process needs
to be present in the main memory which means that only a few pages will only be present in the main memory
at any time. However, deciding, which pages need to be kept in the main memory and which need to be kept in
the secondary memory, is going to be difficult because we cannot say in advance that a process will require a
particular page at particular time. Therefore, to overcome this problem, there is a concept called Demand
Paging is introduced. It suggests keeping all pages of the frames in the secondary memory until they are
required. In other words, it says that do not load any page in the main memory until it is required. Whenever
any page is referred for the first time in the main memory, then that page will be found in the secondary
memory.

In computer operating systems, demand paging (as opposed to anticipatory paging) is a method of virtual
memory management. In a system that uses demand paging, the operating system copies a disk page into
physical memory only if an attempt is made to access it and that page is not already in memory (i.e., if a page
fault occurs). It follows that a process begins execution with none of its pages in physical memory, and many
page faults will occur until most of a process's working set of pages are located in physical memory. This is an
example of a lazy loading technique.

313
Basic concept
Demand paging follows that pages should only be brought into memory if the executing process demands them. This is often referred to as
lazy evaluation as only those pages demanded by the process are swapped from secondary storage to main memory. Contrast this to pure
swapping, where all memory for a process is swapped from secondary storage to main memory during the process startup.

Commonly, to achieve this process a page table implementation is used. The page table maps logical memory to physical memory. The page
table uses a bitwise operator to mark if a page is valid or invalid. A valid page is one that currently resides in main memory. An invalid page is
one that currently resides in secondary memory. When a process tries to access a page, the following steps are generally followed:

Attempt to access page.

If page is valid (in memory) then continue processing instruction as normal.

If page is invalid then a page-fault trap occurs.

Check if the memory reference is a valid reference to a location on secondary memory. If not, the process is terminated (illegal memory
access). Otherwise, we have to page in the required page.

Schedule disk operation to read the desired page into main memory.

Restart the instruction that was interrupted by the operating system trap.

314
Advantages

Demand paging, as opposed to loading all pages immediately:

Only loads pages that are demanded by the executing process.

As there is more space in main memory, more processes can be loaded, reducing the context switching time,
which utilizes large amounts of resources.

Less loading latency occurs at program startup, as less information is accessed from secondary storage and less
information is brought into main memory.

As main memory is expensive compared to secondary memory, this technique helps significantly reduce the bill
of material (BOM) cost in smart phones for example. Symbian OS had this feature.

Disadvantages

Individual programs face extra latency when they access a page for the first time.

Low-cost, low-power embedded systems may not have a memory management unit that supports page
replacement.

Memory management with page replacement algorithms becomes slightly more complex.

Possible security risks, including vulnerability to timing attacks.

315
Page replacement algorithms
are the techniques using which an Operating System decides which memory pages to swap out, write
to disk when a page of memory needs to be allocated.

Page Replacement Algorithms

In an operating system that uses paging for memory management, a page replacement algorithm is
needed to decide which page needs to be replaced when new page comes in.

Page Fault – A page fault happens when a running program accesses a memory page that is mapped
into the virtual address space, but not loaded in physical memory. Since actual physical memory is
much smaller than virtual memory, page faults happen. In case of page fault,

Operating System might have to replace one of the existing pages with the newly needed page.
Different page replacement algorithms suggest different ways to decide which page to replace. The
target for all algorithms is to reduce the number of page faults.

316
Page replacement algorithms:
First In First Out (FIFO) –
This is the simplest page replacement algorithm. In this algorithm, the operating system
keeps track of all pages in the memory in a queue, the oldest page is in the front of the
queue. When a page needs to be replaced page in the front of the queue is selected for
removal.
Optimal Page replacement –
In this algorithm, pages are replaced which would not be used for the longest duration of
time in the future.
Least Recently Used –
In this algorithm page will be replaced which is least recently used.

317

You might also like