Os All Merged
Os All Merged
Os All Merged
INTRODUCTION
TO TO
O P E R ATOI PNEGR AS TYISNTGE SMY SS T E M S
MODULE - I
Dr. A.Padmavathi
Assistant Professor(SG)
MODULE - I
Department of Computer Science and Engineering
Amrita School of Engineering, Chennai
LECTURE - 1
ØIf there is no operating system, then the user need to write separate
program to access each of the hardware.
q Are application programs eg. Excel, chrome etc. are part of OS?
qNo
qOS (Kernel) is helping in running these processes
qIf any process wants to communicate with any hardware devices, it uses system calls.
q Ubuntu OS – Red hat is the Kernel (Linux 1.6% market share for desktop
computers)
– In server class machines, Linux is used and more than 70% market share is acquired by
Linux
– In smart watches, smart phones, cars, smart TV etc. Linux Kernel is used.
• The special program called monitor manages all the jobs in a batch
Eg. Palm OS, Pocket PC, Symbian OS, Android, Linux etc.
Single processor uses multiple controllers (co-processors) to In multiprocessors two approaches are used:
handle special tasks & can execute limited instruction sets a. Symmetric multiprocessing (SMP)
b. Asymmetric multiprocessing (AMP)
Cost is more because each processor requires separate Cost is less because they uses same resources on sharing
resources. i.e. Mass Storage, Peripherals, Power Supplies basis.
etc.
Less reliable because failure in one processor will result in More reliable
failure of entire system. A computer’s capability to
process more than one
Throughput is low (each and every task is performed by the Throughput is high. ( For N Processors - throughput will be task simultaneously is
same processor) slightly less than N because synchronization must be
maintained between two processors and they also share
called multiprocessing
resources which add certain overhead)
Easy to design Complex design
Multicore systems
Use of two or more processors handled by one Use of two or more processors sharing a common
master processor. memory space.
There is one master processor that controls the Processors shares the same memory
data structure of the system.
Only the master processor run task in OS. All the processor in the system run tasks in OS
Processors need not communicate as they are All processors communicate with another processor
controlled by the master processor. via a shared memory.
Simple as master processor access the data Complex as all the processors need to be
structure. synchronized to maintain the load balance.
Master-slave approach Peer-to-peer approach
Ø To ensure the proper execution of OS, need to distinguish between the execution of OS code & user
defined code.
Ø The approach taken - provide hardware support to differentiate among various modes of execution.
Ø Two separate modes (Dual mode) of operation
Ø User mode
Ø Kernel mode (supervisor/system/privileged mode)
Ø Here, more than one bit is used by the CPU to set and handle the mode.
Ø Eg. of multimode systems can be understood from systems that support virtualization.
These, CPUs have a separate mode that specifies when the virtual machine manager (VMM) and
the virtualization management software—is in control of the system.
Ø In this mode, the VMM has more privileges than user processes but fewer than the kernel.
Ø Characteristics:
Ø If any attempt is made to execute a privileged instruction in USER mode, it will not be executed. The
hardware traps it to the OS.
Ø Before transferring the control to any user program, it is the responsibility of the OS to ensure that the
TIMER is set to interrupt.
ØThus, if the timer interrupts, then the OS regains the control.
What is non-
Ø So, any instruction that can modify the contents of the TIMER is a privileged instruction.
privileged
Ø Privilege instructions are used by OS in order to achieve correct operation. instruction?
Ø Eg. of privileged instructions: IO instructions, context switching, disabling the interrupt, set the time of
clock
Ø In order to understand more about the system calls, we need to understand the
Ø User mode
Ø Kernel mode
Modes of Operation of a Program
Ø User mode and Kernel mode are the two modes in which a program can execute
Ø If a program is running in user mode, the program do not have direct access to memory, hardware and such
resources.
Ø But, if a program is running in Kernel mode it has access to hardware, memory etc.
Ø So, if a program is running in a Kernel mode, we say that it is in privileged mode
Ø But, is a program is running in Kernel mode and the program gets crashed, then the entire system will
crash, or the system would halt, that is a problem of a Kernel mode.
User Mode
Ø If the program is running in user mode and if it crashes, then the entire system would not crash
Kernel
Ø User mode – SAFER MODE of operation, though Kernel mode – PRVILEGED MODE of operation
Model
System call
Ø when a program, is executing in system mode, it may need access to some of the resources like:
memory, hardware etc, then:
Ø The program makes a call to the OS informing that it need some resources
Ø When the call is made, the program switches from user mode to kernel mode so that it can use those
resources
Ø So, when a program switched from user mode to kernel mode and vice versa it is called as context
switching
Ø So the call which was made to the kernel to access the system resources is known as system call
System call is the programmatic way in which a computer program requests a service from the kernel of the
OS. These calls are available as routines written in C or C++
System call
ØUser operates in USER mode and if user wants any functionality of the OS, it needs to be in KERNEL mode.
Ø To switch from USER mode to KERNEL mode to access the functionalities of OS, system call is used.
Ø Eg, a C program to add two numbers (USER mode) and print the result in monitor (KERNEL mode).
Ø printf is a function, which uses the system call write() to print the value
Ø Wondows 7 has around 700 system calls and some other OS have 300 etc.
An Example to Understand…
Ø Example of a SYSTEM CALL sequence for writing a simple program to read data from one file and copy them to another file.
Ø The required set of system calls for read data from the file:
Ø Acquire input file name – we need a system call to acquire the file name
Ø Write to screen – a system call to display in the screen (for using the output device)
Ø Accept input – a system call to use keyboard or mouse (for using input devices)
Ø The required set of system call for write data to the file:
Ø Acquire output file name
Ø Write to screen - a system call to display in the screen (for using the output device)
Ø Accept input - a system call to use keyboard or mouse (for using input devices)
Ø Open input file
Ø If file doesn’t exist – ABORT
Ø Create file name
Ø If file doesn’t exist – ABORT
Ø Read from input file
Ø Write to output file ( it a loop until read fails)
Ø Close output file
Ø Write completion message on the screen
Ø Terminate normally
Types of System Calls
Ø System calls are grouped into 6 major categorize.
1. Process Control
2. File manipulation
3. Device manipulation
4. Information maintenance
5. Communications
6. Protection
1. Process Control
Ø The system calls that are used to control the processes.
Ø There are various processes running in a system and that needs to be controlled. Some of
the examples are:
Ø end, abort ( a process when is running, on completion needs to end normally or abnormally)
Ø load, execute
Ø create, terminate
Ø get process attributes, set process attributes
Ø wait for time
Ø wait event, signal event
Ø allocate and free memory
2. File Manipulation
Ø These are the system calls that are used to manipulate or manage files.
Ø Protection is a concern with of all computer systems from servers to mobile handheld
devices
Ø set permission, get permission
Simple Structure
q The structure followed in designing olden OS - MS-DOS operating systems
q They do not have a very well defined structure
q ROM BIOS (Basic Input/Output System)device drivers – to perform hardware initialization during the booting process
q Device drivers, System program and Application program can also access system hardware
q Thus, in this structure interfaces and levels of functionalities are not well separated
q Even though it looks like a layered structure it is not actually a layered structure
q Such freedom makes MS-DOS vulnerable to errors or malicious programs, causing the entire system to crash when the user program fails.
q So, it is not well protected, well structed and not well defined. MS-DOS layer structure
q Eg. MS DOS,
OPERATING SYSTEM STRUCTURE 3
DIFFERENT STRUCTURES OF OPERATING SYSTEMS –
II. UNIX STRUCTURE (MONOLITHIC STRUCTURE)
¡ Instead, they combine different structures, resulting in hybrid systems that address performance, security,
and usability issues.
¡ Eg. both Linux and Solaris are monolithic, however, they are also modular, so that new functionality can be
dynamically added to the kernel
¡ Windows is largely monolithic, but it retains some behavior typical of microkernel systems
¡ The Apple Mac OS X uses a hybrid structure. It is a layered system and below these layers is the kernel
environment, consists primarily of the Mach microkernel and the BSD UNIX kernel.
¡ iOS is a mobile operating system designed by Apple. OS is structured on the Mac OS X, with added
functionality pertinent to mobile devices, but does not directly run Mac OS X applications.
¡ iOS is designed to run on Apple mobile devices and is close-sourced, Android runs on a variety of mobile
platforms and is open-sourced,
¡ Android has layered stack of software and at the bottom Linux kernel. 8
OPERATING SYSTEM STRUCTURE
Lecture – 7
Ø A system can have adequate protection but still be prone to failure and allow
inappropriate access.
Ø It is the job of security to defend a system from external and internal attacks.
Ø Security ensures the authentication of system users to protect the integrity of the
information stored in the system as well as the physical resources of the computer system.
Ø It involves guarding of user’s data and programs against interferences by external entities
(unauthorized persons)
Ø The security system prevents unauthorized access, malicious destruction or alteration of
data, and accidental introduction of inconsistency.
Ø Modern protection concepts have evolved to increase the reliability of any complex system that
makes use of shared resources.
Ø Protection is provided for various reasons:
Ø To prevent the mischievous, intentional violation of an access restriction by a user
Ø To ensure each program components to use system resources only in ways consistent with stated
policies.
Ø Protection can improve reliability by detecting latent errors
Ø An unprotected resource cannot defend against use (or misuse) by an unauthorized or incompetent
user.
Ø A protection-oriented system provides means to distinguish between authorized and unauthorized
usage
Ø A process should be allowed to access only those resources for which it has authorization.
Ø Furthermore, at any time, a process should be able to access only those resources that it
currently requires to complete its task - referred to as the need-to-know principle, is useful
in limiting the amount of damage a faulty process can cause in the system.
Ø A process operates with in a protection domain - the resources that the process may access
Ø The ability to execute an operation on an object is an access right. A domain is a collection of
access right.
Ø The association between a process and a domain may be either static, if the set of resources
available to the process is fixed throughout the process’s lifetime, or dynamic.
Ø If the association is dynamic, a mechanism is available to allow domain switching, enabling the
process to switch from one domain to another.
Protection and security of Operating Systems
11
Aspect of protection of information
Computer systems contain many objects, and they need to be protected from
misuse. Objects may be hardware or software. An access right is permission to
perform an operation on an object. A domain is a set of access rights. Processes
execute in domains and may use any of the access rights in the domain to access
and manipulate objects. During its lifetime, a process may be either bound to a
protection domain or allowed to switch from one domain to another.
Ø A system is secure if its resources are used and accessed as intended under all
circumstances.
Ø Unfortunately, total security cannot be achieved.
Ø Security violations (or misuse) of the system can be categorized as intentional
(malicious) or accidental.
Ø It is easier to protect against accidental misuse than against malicious misuse.
Ø Intruder and cracker for those attempting to breach security.
Ø A threat is the potential for a security violation, such as the discovery of a
vulnerability, whereas an attack is the attempt to break security.
Protection and security of Operating Systems
15
Security Breaches
Ø Breach of confidentiality – This type of violation involves unauthorized reading of data (or theft of
information).
Ø Eg. credit-card information or identity information for identity theft, can result directly in money for the intruder.
Ø Denial of service (DOS) – This violation involves preventing legitimate use of the system. DOS attacks are
sometimes accidental.
Ø For example, a website click could download a Java applet that proceeds to use all available CPU time or to pop up
windows infinitely. The second category involves disrupting the network.
Protection and security of Operating Systems
16
Reasons for taking security measures
Basic Control the access to the system Provides the system access to legitimate
resources users only
Handles Simple queries More complex concerns
Policy Specifies what file can be access by Describes which person is allowed to use
a particular user the system
Type of threat Internal External
◦ User interface allows a user to interact with the OS or interact with the computer.
◦ All OSs have a user interface and this can take several forms; such as CLI and GUI
◦ Eg. of CLI: the command prompt we have in Windows or terminal in Uduntu systems.
◦ CLI – we can provide text-based command through keyboard in order to perform certain tasks.
◦ Most commonly used user interface is GUI: it is an interface where we have a Windows system with pointing
device like mouse and there are menus through which you can provide input and also using keyboard.
◦ Most user friendly interface is GUI
◦ Thus, user interface is one of the most important services provided by an operating system.
3
Operating System Services
2. Program Execution
◦ OS must provide environment for execution of the programs.
◦ A user must be able to run the programs or softwares that you have.
◦ For this, the system must load the program inti the memory and it should be able to run
that program.
◦ The program must be able to end its execution, either normally or abnormally (indicating
error).
Output
Operating System Services 4
3. Input / Output Operations
◦ A running program may require I/O, which may involve a file or an I/O device.
◦ For efficiency and protection, users usually cannot control I/O devices directly.
◦ For eg. When you use keyboard or mouse, you may feel that you are using it directly, but it
is not so; there is the OS which is between you and the computer which actually controls
the usage of the IO devices.
◦ Therefore, the operating system must provide a means to do I/O.
◦ We want to keep track of which users use how much and what kinds of computer
resources.
◦ This record keeping may be used for accounting (so that users can be billed) or
simply for accumulating usage statistics.
◦ Usage statistics may be a valuable tool for researchers who wish to reconfigure
the system to improve computing services.
◦ In order to protect the computer it should be protected from all the angles possible.
◦ As the saying goes: a chain is as strong as its weakest link.
Introduction to Processes
Introduction to Processes 2
Introduction
• A process is a program in execution.
• A process will need certain resources: CPU time, memory, files, and I/O devices to accomplish its
task.
• These resources are allocated to the process either when it is created or while it is executing.
• Systems consist of a collection of processes: operating-system processes execute system code, and user
processes execute user code. All these processes may execute concurrently.
• Traditionally a process contained only a single thread but, most modern OS support processes that have
multiple threads.
• The OS is responsible for several important aspects of process and thread management:
• creation and deletion of both user and system processes
• scheduling of processes
• provision of mechanisms for synchronization, communication, and deadlock handling for processes.
Introduction to Processes 3
How a program is developed
• Write the program using a high level language: C, C++, Java etc.
• But, computer does not understand high level language, it understand only binary 1/0
• So the program needs to be converted to binary.
• Using a compiler, the program is compiled which helps to convert the program into a machine
understandable language.
• Now the program is converted to a machine understandable language and is ready for execution.
• The program need to be loaded into the memory and it needs some resources of the computer.
• The OS provide these resources for the execution of the program.
• So, the OS loads the executable program into the memory, allocate the resources and then the
program will begin its execution.
• Until the executable code of the program is generated, it is a passive entity; it doesn’t do anything.
• But the moment it begins execution, the program is called as a process.
Introduction to Processes 6
Process Explorer - Threads
Introduction to Processes 7
Processes (contd…)
• A process includes the current activity, as represented by the value of the program
counter and the contents of the processor’s registers.
• A process generally also includes the process stack, which contains temporary data and
a data section, which contains global variables.
• A process may also include a heap, which is memory that is dynamically allocated
during process run time.
Introduction to Processes 9
Diagram of Process States
Introduction to Processes 10
Lecture – 9
Process Scheduling
Process Scheduling-Basics
The objective of multiprogramming is to have some process running all times, to maximize the CPU
utilization.
Time sharing is to switch the CPU between the processes so frequently that users can interact with
each program while it is running.
To meet these objectives, the process scheduler selects an available process from a set of available
processes, for program execution on the CPU.
◦ For a single processor system, there will never be more than one running process
◦ Of there are more processes, the rest will have to wait until the CPU is free and can be
rescheduled.
Scheduling Queues:
In order to help the process scheduling we have scheduling queues.
There are two types of queues:
◦ Job queue
◦ Ready queue
PROCESS SCHEDULING 3
Scheduling Queues
Ø Job Queue – As the process enters the system, they are put into a job queue, which consist of
all processes of the system.
Ø The processes may not be executing at this time, but these are the list of all processes we have
in the system.
Ø Ready Queue – The processes that are residing in the main memory and are ready and waiting
to execute are kept on a list called the ready queue.
CPU
Processes
Ready Queue – processes that are ready and waiting to execute Job Queue – all processes in the system
PROCESS SCHEDULING 4
Queueing-diagram representation of
process scheduling
job queue assigned to CPU end/terminated
PROCESS SCHEDULING 5
Queueing-diagram contd…
Ø A new process is initially put in the ready queue.
Ø It waits there until it is selected for execution, or dispatched.
Ø Once the process is allocated to the CPU and is executing, one of several events could
occur:
Ø The process could issue an I/O request and then be placed in an I/O queue.
Ø The process could create a new child process and wait for the child’s termination.
Ø The process could be removed forcibly from the CPU, as a result of an interrupt, and be
put back in the ready queue.
Ø In the first two cases, the process eventually switches from the waiting state to the ready
state and is then put back in the ready queue.
Ø A process continues this cycle until it terminates, at which time it is removed from all
queues and has its PCB and resources deallocated.
PROCESS SCHEDULING 6
Schedulers
Ø A process migrates among the various scheduling queues throughout its lifetime.
Ø The OS must select processes from these queues for scheduling and the selection process is carried out by a scheduler.
Ø In a batch system, more processes are admitted than can be executed immediately. These processes are send to a mass-
storage device (like disk), where they are kept for later execution.
Ø The job scheduler (long-term scheduler) – selects processes from this pool and loads them into memory for execution.
Ø The CPU scheduler (short-term scheduler) - selects from among the processes that are ready to execute and allocates the
CPU to one of them.
Ø The primary distinction between these two schedulers lies in frequency of execution.
Ø The short-term scheduler must select a new process for the CPU frequency. Often, the short-term scheduler executes at
least once every 100 milliseconds. Because of the short time between executions, the short-term scheduler must be fast.
Ø The long-term scheduler executes much less frequently; minutes may separate the creation of one new process and the
next. The long-term scheduler controls the degree of multiprogramming .
PROCESS SCHEDULING 7
Addition of medium-term scheduling to
queueing diagram
Ø Processes can be described as either I/O bound or CPU bound.
Ø An I/O-bound process is one that spends more of its time doing I/O than it spends doing computations.
Ø A CPU-bound process in contrast, generates I/O requests infrequently, using more of its time doing
computations.
Ø It is important that the long-term scheduler select a good process mix of I/O-bound and CPU-bound
processes.
Ø Some operating systems, such as time-sharing systems, introduce an intermediate level of scheduling -
medium-term scheduler.
PROCESS SCHEDULING 8
Context Switch
What is a context switch?
When does a context switch occur?
What does a context switch do in an operating system?
Ø Interrupts cause the operating system to change a CPU from its current task and to run a
kernel routine.
Ø Such operations happen frequently on general purpose systems.
Ø When an interrupt occurs, the system needs to save the current context of the process
currently running on the CPU so that it can restore that context when its processing is done,
essentially suspending the process and then resuming it.
Ø The context is saved in the PCB of the process.
PROCESS SCHEDULING 9
Context Switch contd….
Ø Switching the CPU to another process requires performing a state save of the current process
and a state restore of a different process.
Context Switch
Ø Context switch time (milli sec) is an overhead, as the system does no useful work during
switching.
PROCESS SCHEDULING 10
Lecture – 11
Operations on Processes
Process Creation
■ The processes in most systems can execute concurrently, and they may
be created and deleted dynamically.
■ Thus, these systems must provide a mechanism for process creation
and termination.
Process Creation
■ A process may create several new processes, via a create-process
system call, during the course of execution.
■ The creating process is called a parent process and the new processes
are called the children of that process.
■ Each of these new processes may in turn create other processes,
forming a tree of processes.
Process Creation contd…
■ When a process creates a new process, two possibilities exist in terms of execution:
– The parent continues to execute concurrently with its children
– The parent waits until some or all of its children have terminated
■ With respect to the resource requirements:
– The children may have all of the resources of the parent process or
– A subset or a part of the resources of the parent.
■ There are also two possibilities in terms of the address space of the new processes:
– The child process is a duplicate of the parent process (same program and data as
the parent)
– The child process has a new program loaded into it
Process Termination
■ A process terminates when it finishes executing its final instruction and ask the OS to
delete it by using the exit() system call.
■ At that point, the process may return a status value to its parent process (via wait()
system call)
■ All the resources of the process - including physical and virtual memory, open files and
I/O buffers - are deallocated by the OS
Termination can occur in other circumstances as well:
■ A process can cause the termination of another process via an appropriate system call
■ Usually, such a system call can be invoked only by the parent of the process that is to be
terminated.
■ Otherwise, users could arbitrarily kill each other’s job
Process Termination contd…
■ A parent may terminate the execution of one of its children for a variety of reasons:
– The child had exceeded the usage of some of the resources that it has been
allotted
– The task assigned to the child is no longer required
– The parent is exiting, and the OS does not allow a child to continue if its parent
terminates
Lecture – 13
Threads
THREADS 2
INTRODUCTION
THREADS 3
SINGLE-THREADED & MULTI-THREADED PROCESSES
Single process
¡ In multi-threaded process, there are multiple threads in a
single process and each of the thread has its own stack and
registers
¡ At the same time, the code section, data section and files of the
Single-threaded process
process are shared among various threads of the process.
¡ Multiple tasks can be executed by multi-threaded process
Single process
¡ Multi-threaded processes are more efficient than single-
threaded processes
¡ It will make the computations more faster and efficient.
4
Different processes running on the system Different threads running on the system
Different threads perform different tasks and multiple tasks can be performed at the same time in a
THREADS
multi-threaded process Improving the speed and efficiency of computation. 5
BENEFITS OF MULTI-THREADED PROGRAMMING
Utilization of
Responsiveness Resource sharing Economy multiprocessor
architecture
Multi threading an interactive Threads share memory and the Allocating memory and Benefit of multithreading can be
application may allow a resources of the process to resources for process creation is greatly increased in a
program to continue running which they belong. The benefit costly. Because threads share multiprocessor architecture,
even if part of it is blocked or is of sharing code and data is that the resources of the process to where threads may be running
performing a lengthy operation, it allows an application to have which they belong, it is more in parallel on different
thereby improving several different threads of economical to create and processors. A single threaded
responsiveness to the users. activity within the same address context switch threads process thread can run only on a
space. single CPU, whereas multi
threading on a multi-CPU
machine increases the
concurrency.
THREADS
6
MULTI-THREADING MODELS
¡ Types of threads:
¡ User threads – supported above the kernel and are managed with out the kernel support
¡ Kernel threads – supported and managed directly by the operating system
¡ There must exist a relationship between the user threads and kernel threads
How can we establish a relationship between the user threads and kernel threads?
Ans: through multi threaded models
¡ Multi threading models are the types of relationships that can be there between the user threads and
kernel threads.
¡ There are three ways to establishing the relationship
¡ Many-to-One Model
¡ One-to-One Model
¡ Many-to-Many Model
THREADS 7
many to one
MANY-TO-ONE MODEL
¡ There is a many-to-one relationship established between the user threads and kernel threads
¡ Here, many user threads are accessing one kernel thread
¡ Maps many user-level threads to one kernel thread
¡ Advantages:
¡ Thread management is done by the thread library in user space, so it is efficient.
¡ Here, we are able to manage the threads in the user space
¡ Disadvantages:
¡ The entire process will block if a thread makes a blocking system call
¡ Because only one thread can access the kernel at a time, multiple threads are unable to run in parallel on
multiprocessors.
THREADS 8
one to one
ONE-TO-ONE MODEL
THREADS 9
MANY-TO-MANY MODEL many to many
¡ So, many-to-many model is advantageous than one-to-one model and many-to-one model
¡ It is the model that is implemented in most of the systems
¡ This is the best model we can have in a multi threading system to establish a relationship between the user
threads and kernel threads.
THREADS 10
THREADING ISSUES
¡ What are the issues that we face in threading and how do address them?
¡ Fork() exec() system calls
¡ These system calls are used for specific purposes such as:
¡ Fork() is used to duplicate a process or for creating a child process from a parent
process with a different ID
¡ Exec() system call is used for replacing the contents of a process with another process,
but retaining the same process ID.
¡ The semantics of the fork() and exec() system calls change in a multi threaded
programming
¡ Because a process can consist of multiple threads and one thread in the system
requested for a fork() system call
¡ When a fork() system call is made, it is to create a duplicate process.
THREADS 11
THREADING ISSUES – FORK() SYSTEM CALL
¡ Issue:
¡ If one thread in a program calls a fork() call, does the new process duplicate all threads, or is the new
process single threaded?
¡ Solution:
¡ Some UNIX system have chosen to have two versions of fork() – one that duplicates all threads and
another that duplicates only the thread that invoked the fork()system call
THREADS 12
THREADING ISSUES – EXEC() SYSTEM CALL
THREADS 13
WHICH VERSION OF FORK() TO USE AND WHEN?
¡ If a thread invokes an exec() system call, the program specified in the parameter to exec() will
replace the entire process – including all threads
¡ Which of the two versions of fork() to use depends on the application.
If exec() is called immediately after forking If the separate process does not call exec()
Then duplicating all threads in unnecessary, as the after forking
program specified in the parameters to exec() will
replace the process.
In this instance, duplicating only the calling thread is Then the separate process should duplicate all the
appropriate threads.
THREADS 14
THREADING ISSUES – THREAD CANCELLATION
¡ If multiple threads are concurrently searching through a database, and one threads
returns the result, the remaining threads might be cancelled.
¡ When a user presses a button on the web browser that stops a web page from loading
any further, all threads loading the page are cancelled.
¡ A thread that is to be cancelled is often referred to as the target thread.
THREADS 15
THREADING ISSUES – THREAD CANCELLATION
¡ Cancellation of a target thread may occur in two different scenarios:
THREADS 16
THREADING ISSUES – THREAD CANCELLATION
THREADS 17
Lecture - 14
Basics of CPU Scheduling
INTRODUCTION TO CPU SCHEDULING
• If there are processes available, then we want the CPU to be utilized as much as possible. We do
not want the CPU to idle.
• In order to accomplish this, we want to introduce CPU scheduling
• With multiprogramming, we try to use the CPU idling time productively.
• Several processes are kept in memory at one time.
• When one process has to wait, the OS takes the CPU away from that process and give the CPU to
another process and this pattern continues, making the computation more efficient.
• By doing this, the CPU will not be idle, it will be used by one or the other process for execution.
• What is the rule that CPU scheduling follows in order to accomplish this task?
• Many rules are followed (different scheduling algorithms)
Scheduling is a fundamental operating-system function. Almost all computer resources are scheduled
before use. The CPU is, of course, one of the primary computer resources. Thus, CPU scheduling is
central to operating-system design.
6
Basics of CPU Scheduling
CPU AND I/O BURST CYCLES
• The success of CPU scheduling depends on how to manage the process execution
between CPU execution and I/O wait.
• Processes alternate between these two states.
CPU Burst is
• CPU execution – when a process has begun its execution, it is under the CPU execution when the
state. It is using CPU for its execution. process is
being executed
• I/O wait state - the process may require an input/output operation in order to in the CPU
complete its execution.
• CPU burst – is the time when the process is in CPU execution.
I/O Burst is
• I/O burst – when a process is waiting for an I/O, it is called as I/O burst. when is
• Process execution begins with a CPU burst, followed by an I/O burs, CPU burst, I/O waiting for
burst and so on. I/O for further
execution
• The final CPU burst ends with a system request to terminate execution.
Basics of CPU Scheduling 7
CPU SCHEDULER
• Whenever the CPU becomes idle, the OS must select one of the processes in the ready queue
to be executed. The selection process is carried out by the short-term scheduler, or CPU
scheduler.
• The scheduler selects a process from the processes in memory that are ready to execute and
allocates the CPU to that process.
• Ready queue is not necessarily a first-in, first-out (FIFO) queue.
• A ready queue can be implemented as a FIFO queue, a priority queue, a tree, or simply an
unordered linked list.
• All the processes in the ready queue are lined up waiting for a chance to run on the CPU.
• The information contained in the queues are generally process control blocks (PCB) of the
processes.
Basics of CPU Scheduling 8
DISPATCHER
• The dispatcher should be as fast as possible, the time it takes for the dispatcher to stop one
process and start another running is known as the dispatch latency.
For a process, an important criteria is how long it takes to execute that process.
• The interval from the time of submission of a process to the time of completion is turnaround time.
TURNAROUND • Turnaround time the sum of time periods spent to get into memory, waiting in the ready queue, executing
TIME on CPU and doing I/O.
§ Another measure is the time from the submission of a request until the first response is produced.
RESPONSE TIME § This measure is the response time, and is the time it takes to start responding, not the time it takes
to output the response
§ The CPU scheduling does not affect the amount of time during which a process executes or does I/O.
§ It affects only the amount of time that a process spends waiting in the ready queue.
WAITING TIME
§ Waiting time is the some of the periods spent waiting in the ready queue.
12
Basics of CPU Scheduling
Lecture - 15
Scheduling Algorithms
I. FIRST COME, FIRST SERVED SCHEDULING
(FCFS)
q The simplest CPU scheduling algorithm
q The process that requests the CPU first is allocated the CPU first
q The implementation of FCFS is easily managed with a First-in First-out (FIFO) queue.
q When a process enters the ready queue, its PCB is linked onto the tail of the queue.
q When the CPU is free, it is allocated to the process at the head of the queue.
q The running process is then removed from the queue.
WHETHER FCFS IS AN EFFICIENT SCHEDULING ALGORITHM?
! K.)% #$% G0L $+* 8%%. +DD()+#%/ #( + &'()%**2 #$+# &'()%** M%%&* #$% G0L 9.#,D ,# '%D%+*%* #$% G0L2 %,#$%' 8I
#%'7,.+#,.< (' 8I '%N9%*#,.< !BKE
! FGFH +D<(',#$7 ,* #'(98D%*(7% "(' #,7%O*$+',.< *I*#%7*2 6$%'% ,# ,* ,7&('#+.# #$+# %+)$ 9*%' <%#* + *$+'% ("
#$% G0L +# '%<9D+' ,.#%'-+D*E
! !# 6(9D/ 8% /,*+*#'(9* #( +DD(6 (.% &'()%** #( M%%& #$% G0L "(' +. %P#%./%/ &%',(/E
! !"# $% $& '"% (' )**$+$)'% (,-".$%/01
II. SHORTEST JOB FIRST SCHEDULING (SJF)
q SJF - The process that requires shortest time run on CPU will be the first process to get the
CPU.
q This algorithm associates with each process the length of the process’s next CPU burst.
q When the CPU is available, it is assigned to the process that has the smallest next CPU burst.
q If the next CPU bursts of two processes are the same, FCFS scheduling is used to break the tie.
! E 2*)' .(()*()1.-' -')2 :*) -&1, ,+&'B0813; 2'-&*B 7*08B 9' -&' ,89:;<=;>-<?;>4&5>@A:=;
sncba
%BC9:D;8E#
! F'+.0,' ,+&'B0813; B'('3B, *3 -&' 8'3;-& *: -&' 3'>- 456 90),- *: . ()*+',,A ).-&') -&.3 1-,
-*-.8 8'3;-&<
EXAMPLE OF SJF SCHEDULING (NON-PREEMPTIVE)
q Consider the following set of processes, with the length of the CPU burst given in
millisecond:
q Waiting time for P1 = 3ms
q Waiting time for P2 = 16ms
q Waiting time for P3 = 9ms
q Waiting time for P4 = 0ms
q Average waiting time = (3+16+9+0)/4 = 7ms
q If we were using the FCFS algorithm, the average waiting time would be 10.25 ms
q From this, we can see that SJF algorithm is better than FCFS algorithm.
EXAMPLE OF SJF SCHEDULING (PREEMPTIVE)
q Consider the following set of processes, with the length of the CPU burst given in millisecond:
Waiting time = Total waiting time - No. of milliseconds process executed – Arrival time
q The problem with SJF is knowing the length of the next CPU request.
q Although the SJF algorithm is optimal, it cannot be implemented at the level of short-term
CPU scheduling.
q There is no way to know the length of the next CPU burst.
One approach is:
! !3 )49 )3 2''43H%&2)# 8G7 $-"#./(%01
! A# &29 03) D03: )"# (#01)" 36 )"# 0#H) *+, F/4$)B F/) :# &29 F# 2F(# )3 '4#.%-) %)$
E2(/#@
! A# #H'#-) )"2) )"# 0#H) *+, F/4$) :%(( F# $%&%(24 %0 (#01)" )3 )"# '4#E%3/$ 30#$@
! !"/$B F9 -3&'/)%01 20 2''43H%&2)%30 36 )"# (#01)" 36 )"# 0#H) *+, F/4$)B :# -20 '%-D )"#
'43-#$$ :%)" )"# $"34)#$) '4#.%-)#. *+, F/4$)@
III. PRIORITY SCHEDULING
q A priority is associated with each process, and the CPU is allocated to the process with
the highest priority.
q Equal priority processes are scheduled in FCFS order.
q An SJF algorithm is a priority algorithm, where priority is the inverse of next CPU burst.
q The larger the CPU burst, the lower the priority, and vice versa.
! I '4##&')%E# '4%34%)9 $-"#./(%01 2(134%)"& :%(( '4##&') )"# *+, %6 )"# '4%34%)9 36 )"#
0#:(9 244%E#. '43-#$$ %$ "%1"#4 )"20 )"# '4%34%)9 36 )"# -/44#0)(9 4/00%01 '43-#$$#$@
! I 030;'4##&')%E# '4%34%)9 $-"#./(%01 2(134%)"& :%(( $%&'(9 '/) )"# 0#: '43-#$$ 2) )"#
"#2. 36 )"# 4#2.9 5/#/#@
EXAMPLE OF PRIORITY SCHEDULING
q Consider the following set of processes, assumed to have arrive time 0, in the order P1,
P2, P3, P4 and P5:
! F('%<'(9./ &'()%**%* 7+I $+-% &',(',#I %P#%'.+DDI /%",.%/ (-%' 8+)M<'(9./ &'()%**%*E
! @ 79D#,D%-%D N9%9% *)$%/9D,.< +D<(',#$7 &+'#,#,(.* #$% '%+/I N9%9% ,.#( *%-%'+D *%&+'+#% N9%9%*E
! C$% &'()%**%* +'% &%'7+.%.#DI +**,<.%/ #( (.% N9%9%2 <%.%'+DDI 8+*%/ (. *(7% &'(&%'#I (" #$%
&'()%**%*2 *9)$ +* 7%7('I *,T%2 &'()%** &',(',#I2 (' &'()%** *,T%E
! R+)$ (.% $+* ,#* (6. *)$%/9D,.< +D<(',#$7E
MULTILEVEL QUEUE SCHEDULING CONTD….
Example:
q Separate queues might be used for foreground and background processes.
q Foreground processes may be scheduled by RR algorithm, while the background processes queue is
scheduled by an FCFS algorithm.
q Foreground processes being interactive processes, cannot wait for long time, need to be quick.
q In order for them to be quick, there need to be a proper time sharing between them.
! ;$,)$ ,* #$% 8%*# *)$%/9D,.< +D<(',#$7 #$+# 6% )+. 9*%V K9# (" #$% /,*)9**%/ +D<(',#$72 #$+# 8%*#
*9,#* #,7% *$+',.< *I*#%7* +'% UU *)$%/9D,.<E
! W+)M<'(9./ &'()%**%* 8%,.< .(# ,.#%'+)#,-%2 )+. 6+,# "(' *(7% #,7% ,. #$% N9%9%E H(2 6% )+. 9*%
FGFH +D<(',#$7E
! !. +//,#,(.2 #$%'% 79*# 8% + *)$%/9D,.< +7(.< #$% N9%9%*2 6$,)$ ,* ",P%/ &',(',#I &'%%7&#,-%
*)$%/9D,.<E
! R<2E F('%<'(9./ N9%9% 7+I $+-% &',(',#I (-%' 8+)M<'(9./ N9%9%E
AN EXAMPLE – MULTILEVEL QUEUE SCHEDULING
q There are five queues and within queues, we have processes.
q Consider the low priority queue: student queue - it can execute only when all the above
queues are empty.
q Similarly, batch processes can execute only when the processes in the above queues are
empty.
DRAWBACK – Processes belonging to one queue cannot move from that queue to another, until its execution completion.
VI. MULTILEVEL FEEDBACK SCHEDULING
q In a multilevel scheduling, processes belong to one queue cannot move to another queue,
until its execution is complete.
q A multilevel feedback scheduling allows a process to move between queues.
q This is the main difference between multilevel scheduling and multilevel feedback
scheduling.
q The idea here is to separate processes according to the characteristics of their CPU
bursts.
! =6 2 '43-#$$ /$#$ )33 &/-" *+, )%&#B %) :%(( F# &3E#. )3 2 (3:#4 '4%34%)9 5/#/#@
! !"%$ $-"#&# (#2E#$ =W> F3/0. 20. %0)#42-)%E# '43-#$$#$ %0 )"# "%1"#4 '4%34%)9 5/#/#$@
! =0 2..%)%30B 2 '43-#$$ )"2) :2%)$ )33 (301 %0 2 (3: '4%34%)9 5/#/# &29 F# &3E#. )3 2 "%1"
'4%34%)9 5/#/#@
! !"%$ 634& 36 21%01 '4#E#0)$ $)24E2)%30@
EXAMPLE – MULTILEVEL FEEDBACK SCHEDULING
q Processes belonging to queues of lower priority can execute only when
queues of higher priorities are empty.
q Different queues will follow different scheduling algorithms.
q Also, processes in different queues are allowed to execute with different
quantum of time.
q Eg. QUEUE 0 is the highest priority queue and each process in QUEUE 0 will
run for a time quantum of 8ms.
q Then, next higher priority queue is QUEUE 1, where each process will run for
time quantum of 16ms.
! A5% (#0%" 1"+#"+,2 -.%.% +& =><>< D; +) 05+$5 1"#$%&&%& ".) '3&%9 #) EFEG7
! H%"%; +/ 1"#$%&&%& +) =><>< ? $#61(%,%& +,& %4%$.,+#) 0+,5+) @6&; +, +& /+)%I
#,5%"0+&% ,5#&% 1"#$%&&%& 0+(( '% 6#J%9 /"#6 =><>< ? ,# A:KL #/ =><>< B7
! G+6+(3"(2; 1"#$%&&%& +) =><>< B +& 3((#0%9 ,# %4%$.,% /#" BC6&7 M,5%
1"#$%&&%& +) =><>< B 0+(( ".) #)(2 3/,%" 1"#$%&&%& +) =><>< ? $#61(%,%&N;
3)9 +/ $3))#, $#61(%,%; 0+(( '% 6#J%9 /"#6 =><>< B ,# A:KL #/ =><>< D7
! :)9 +, $#),+).%&7
PARAMETERS DEFINING MULTILEVEL FEEDBACK
QUEUE SCHEDULING
Lecture - 16
Introduction to
Process Synchronization
3
z
Process Synchronization
Cooperating
processes
Problem: Concurrent
Directly share a logical Or be allowed to share access to shared data
address space (both only data through files or may result in data
code and data) messages
inconsistency
4
z
Process Synchronization
§ So they will be able to access the shared region and will be able to manipulate that shared
data.
§ if multiple processes are concurrently trying to manipulate data, then that will lead to data
inconsistency.
§ That is the problem we have and that is why we need process synchronization.
We will discuss:
z
An Example to understand why Process
Synchronization is important
§ We have learnt about shared memory systems - and understood a problem called as
Producer-Consumer Problem.
§ To allow producer and consumer process to run concurrently, we must have available a
buffer of items that can be filled by producer and emptied by the consumer.
§ This buffer will reside in a region of memory that is shared by the producer and consumer
processes.
§ A producer can produce one item while the consumer is consuming another item.
§ The producer and consumer must be synchronized, so that the consumer does not try to
consume an item that has not yet been produced.
6
z
Example contd….
§ Two kind of buffers: unbounded buffer (no practical limit on the size of the buffer) and
bounded buffer (fixed buffer size).
§ lets try to use the bounded buffer and try to modify it.
§ In a bounded buffer we need to keep track of the items that are present in the buffer,
because we need to know when the buffer is full and empty.
§ So lets use a variable: counter to count the no. of items present in a buffer
z
Example contd…
§ Counter is incremented every time we add a new item to the buffer: counter++
§ Counter is decremented every time we remove one item to the buffer: counter—
§ Example:
§ The producer and consumer processes execute the statements counter++ and
counter– concurrently.
§ Following the execution of these two statements, the value of the variable counter may
be 9, 10 or 11!
§ The only correct result, though is counter==10, which is generated correctly of the
producer and consumer execute separately.
8
z
Implementation of Counter
z
Race condition
T0 producer execute Register1 = counter {Register1 = 10}
T1 producer execute Register1 = Register1 + 1 {Register1 = 11}
T2 consumer execute Register2 = counter {Register2 = 10}
T3 consumer execute Register2 = Register2 – 1 {Register2 = 9}
T4 producer execute Counter = Register1 {counter = 11}
T5 consumer execute Counter = Register2 {counter = 9}
We want the resulting changes not to interfere with one another. Hence, we need process synchronization.
PROCESS
SYNCHRONIZATION
UNIT – 2
Lecture - 18
Critical Section Problem
&
Peterson’s Solution
CRITICAL SECTION
• Consider a system consisting of n processes {P0, P1,P2,…..Pn}
• Each process has a segment of code, called a critical section in which the process may be changing
common variables, updating a table, writing a file and so on.
• Critical section is a segment of code in a process where, the process will be changing common
variables, updating a table, writing a file and so on.
• Whenever the process is accessing and doing some manipulations in the shared memory, then the
code that is responsible for doing that particular operation is known as a critical section.
• When one process is executing in its critical section, no other process is to be allowed to execute in
its critical section.
• That is no two processes are executing in their critical sections at the same time.
3
CRITICAL SECTION
• In process synchronization, when two different processes are simultaneously trying to manipulate a shared
variable, then data inconsistency may occur. So in order to avoid that we are using the critical section.
• Different processes have different critical sections.
• So when one process is executing its critical section, it implies that it is making some change to the shared data.
During that time we should not allow other processes to execute in their critical section.
• That is when one process is accessing and manipulating the shared data no other process will be allowed to
access and manipulate the shared data.
• If this can be accomplished, then we can guarantee that multiple processes will not concurrently try to manipulate
the shared data.
• The critical section problem is to design a protocol that the processes can use to cooperate.
4
RU L E S TO B E F O L LOW E D BY P RO C E S S E S
WHILE USING CRITICAL SECTIONS
5
R E Q U I R E M E N T S T O B E S AT I S F I E D
• A solution to the critical section problem must satisfy the following three requirements:
a) Mutual exclusion:
• If process Pi is executing in its critical section, then no other processes can be executing in their critical
sections
b) Progress:
• If no process is executing in its critical section and some processes wish to enter their critical section,
then only these processes that are not executing in their reminder sections can participate in the
decision on which will enter its critical section next, and this selection cannot be postponed
indefinitely.
c) Bounded waiting
• There exists a bound, or limit, on the number of times that other processes are allowed to enter their
critical sections after a process has made a request to enter its critical section and before that request
is granted.
6
PETERSON’S SOLUTION
• However, it provides a good algorithmic description of saving the critical section problem and illustrates some of the
complexities involved in designing software that addresses the requirements of mutual exclusion, progress and bounded
waiting requirements.
• Peterson’s solution is restricted to two processes that alternate execution between their critical sections and remainder
sections. Let’s call the processes Pi and Pj.
• Peterson’s solution requires two data items to be shared between the two processes:
int turn boolean flag[2]
If turn= I, then process Pi is ready to enter If flag(i) = process Pi is ready to enter
its critical section and Indicate whose turn it is to Used to indicate if a process is ready
its critical section and
If turn=2, process Pj is ready to enter enter its critical section If falg(j) = process Pj is ready to enter
its critical section to enter its critical section its critical section
7
HOW PETERSON’S SOLUTION WORKS
• In Peterson’s solution, when a process (Pi) wants to enter into critical section, it will set its
flag=true, indicating that the process (Pi) is ready to enter the critical section.
• If a process (Pi) wants to enter into its critical section, it will set turn=other process (Pj)
• It is a humble algorithm because, when one process wants to enter into its critical section,
instead of entering the critical section, it is giving the turn to another process.
8
HOW PETERSON’S SOLUTION WORKS
• After Pi completes its critical section, flag[i]=false and while of Pj becomes false and it will execute its critical section.
10
W H E T H E R R E Q U I R E M E N T S A R E M E T O R N OT ?
11
PROCESS
SYNCHRONIZATION
UNIT – 2
Lecture 19
Hardware & Software based Solutions
for Process Synchronization
Hardware Instructions for Critical Section Problems
Solution 2:
q Many modern computer systems provide special hardware instructions that allow us either to
test and modify the content of a word or to swap the contents of two words atomically as one
uninterruptible unit.
q We can use these special instructions to solve the critical-section problem.
q Two instructions:
• Test_and_set()
• Compare_and_swap()
Test and Set Lock
q When the process P1 wants to run its CS, it will come to do-while loop
q In while loop, we have test_and_set function
q We are passing as argument – lock (address of lock, bz lock is a
shared variable and we need the current value of lock )
q Lock = 0 initially
Mutual exclusion implementation for P1 with test and set
q So we have test_and_set(0=target, rv=target=0, return 0)
q While(0) – while condition is FALSE, it will break out of while loop and run CS.
q While, P1 is executing CS, the target value is set to 1, no other process can enter CS
q After P1 executes, CS lock value is set to FALSE and P1 runs the reminder section.
q Now, if any other process want to execute CS, can run
Test and Set ()
P1
P2
q Then it is while(1), TRUE, it will keep looping while and will not come out of loop, do nothing.
q When P1 is executes its CS, even though P2 wants to execute its CS, cannot execute it
q After P1 completes its CS, lock is set to 0, and if P2 is still trying to execute its CS, lock=0 and
P2 can execute its CS.
Advantages:
q It satisfies mutual exclusion
q Does not satisfy bounded waiting – multiple preemptions of a low priority tasks
Compare_and_swap()
Definition of wait()
q P is used to denote wait operation and S is semaphore
q When one process wants to access a resource or run its CS, at that time the variable S that is shared
between the processes,
Will take care of who should access the resource or who should enter the CS
q The process will check whether the variable S is <=0, some process is already using the resource or
running CS
q S is shared variable and if S is TRUE, it will be stuck in the while loop, doing no operation
q But, if S not <=0, (FALSE), then S is decremented by 1 and enters the CS and start executing it.
q The process decrements S, because it wants other processes to know that the value of S is decremented,
so that if any other process wants to enter the CS, can check the condition and enter the CS
q Wait operation is used to test whether any process is using the resource or is there any processes
running the CS and if it is not there, will decrement S and start using it or if any process is already running
its CS then, the while condition will be TRUE, and the requesting process will be stuck in the while loop.
q Wait is used for TESTING
Signal ()
! !085$9 '%"($30'5 0) 4$99"* <&"5 3&" %('4")) 3&$3 <$) #$2058 7)" 'B 3&" )"#$%&'("
3' "53"( 053' 3&" ?! '( <$) 7)058 $ (")'7(4"; 4'#%9"3") 03) '%"($30'5 $5* 0) '73 'B 03.
! M5 )085$9 '%"($30'5 03 054("#"53) 3&" :$97" 'B !; #"530'5058 3&$3 03 &$) 4'#%9"3"*
7)058 3&" )"#$%&'(" $5* '3&"( %('4"))") 4$5 7)" 03.
! !085$9 0) 7)"* +, $ %('4")) 3' !MNOAP '3&"( %('4"))") 3&$3 03 &$) 4'#%9"3"* 7)058
3&" (")'7(4"; $5* 0B $5, '3&"( %('4")) <$53) 3' 7)" 03 4$5 $44")) 03.
! O'3"Q A99 3&" #'*0B04$30'5) 3' 3&" 053"8"( :$97" 'B 3&" )"#$%&'(" 05 3&" <$03CE $5*
)085$9CE '%"($30'5) #7)3 +" "R"473"* 05*0:0)0+9,.
! >&$3 0); <&"5 '5" %('4")) #'*0B0") 3&" )"#$%&'(" :$97"; 5' '3&"( %('4")) 4$5
)0#793$5"'7)9, #'*0B, 3&$3 )$#" )"#$%&'(" :$97".
Types of Semaphores
q There are two types of semaphores:
1. Binary Semaphore: The value of a binary semaphore can range only between 0 and 1. On
some systems binary semaphores are known as mutex locks, or they are locks that provide
mutual exclusion.
q Assume that process P1 wants to run CS and checks for the value of S.
q S is free (S=1 initially), in signal instruction it checks whether S<=0, FALSE does not enter while loop, do
S– - (S=0)
q If another process P2 wants to execute CS, it checks the value of S. S is now 0. That process will be stuck
in the while loop, and will not enter the CS
q Now P1, after competing execution of its CS, it is exiting from CS, then P1 will call the SIGNAL operation.
q P1 will increment S (S=0+1=1), that means P1 is signaling other process that it has completed using CS,
and is now free to use by others.
Types of Semaphores
q The second type is:
2. Counting semaphore – Its value can range over an unrestricted domain (1,2,3…). It is used to
control access to a resource that has multiple instances.
q There are processes P1, P2 and P3 sharing the same resource which has two instances R1 and R2.
q In this case P1 can use R1 and P2 can use R2. But, it is limited.
q Here, S=2. initial value of S=2.
q P1 want resource. It checks the wait() operation. P1 checks whether S<=0? (2<=0) FALSE.
Decrement S (S=1) and use the resource R1.
q If P2 wants to use the same resource, it will execute wait() operation. S<=0? (1<=0) FALSE.
Decrement S (S=0) and use the resource R2.
q If P3 wants to use the same resource, it will execute wait() operation. S<=0? (0<=0) TRUE. P3 will
get stuck in the loop doing no operation and cannot use the resource. If P1 or P2 completes using
the resource, will call the signal() operation.
q It will increment S (S=1), resource is free and other process can use it. Now, P3 can make use of the
resource.
Disadvantages of Semaphores
q The implementation of a semaphore with a waiting queue may result in a situation where two
or more processes are waiting indefinitely for an event that can be caused only by one of the
waiting processes. – DEADLOCK!!
q The event here is execution of a signal() operation. When such a state is reached, these
processes are said to be deadlocked.
Buffer of n slots
q The producer tries to insert a data into an empty clot of a buffer. Consumer
q The consumer tries to remove data from a filled slot in the buffer.
q The producer must not insert data when the buffer is full.
q The consumer must not remove data when the buffer is empty.
q The producer and consumer should not insert and remove data simultaneously.
Solution to Bounded Buffer Problem using Semaphores
Producer
! 85& +*"%#3&* 4*.&/ 4" .$/&*4 0 %040 .$4" 0$ &-+46 /,"4 ") 0 (#))&*: Buffer of n slots
Consumer
q The consumer tries to remove data from a filled slot in the buffer.
q The producer must not insert data when the buffer is full.
q The consumer must not remove data when the buffer is empty.
q The producer and consumer should not insert and remove data simultaneously.
Solution To Bounded Buffer Problem Using
Semaphores
q Three semaphores are used.
1. M (mutex), a binary semaphore which is used to acquire and release the lock
2. Empty, a counting semaphore whose initial value is the number of slots in the
buffer, since initially all slots are empty. (No. Of empty slots in the buffer)
1. Full, a counting semaphore whose initial value is 0. (Initially buffer=empty)
Structure of a producer process
Producer process:
q There is a do-while loop. Inside the do: wait operation on empty semaphore – wait until empty > 0 and
then decrement empty.
q When a producer wants to produce something it has to check whether there are any empty slots, if there
are empty slots it can insert data to the buffer.
q After writing data, it will decrement empty by one, to notify that one of the empty slot is filled.
q Wait (empty) operation will decrement semaphore and signal will increment semaphore.
Solution to bounded buffer problem using
semaphores
q Wait (mutex) – mutex is a binary semaphore and is for acquiring the lock
q After it is done, it will signal (mutex). That means it will release the lock. Now, the producer signals the
consumer that the mutex is free and if consumer want, it can acquire the buffer and make changes to it.
q It will then signal (full) – it will increment the full semaphore. Signal will have increment operation.
q It is incrementing full, because the producer has now added one data into the buffer and the full variable
has to be increased.
Solution to bounded buffer problem using
semaphores
Consumer process:
q There is a do-while loop and if a consumer process wants to read or consume any data from the buffer; it will
perform the wait operation on the full semaphore.
q It means wait until full>0; means if there are at least one data in the buffer then only consumer can read or
consume the data and then decrement full.
q After the consumer consumes one data, the full semaphore has to be decremented by one.
q Next is wait operation on mutex, which is a binary semaphore. The consumer will be acquire the lock and the
producer won’t be able to disturb the buffer at that time, until consumer frees the mutex lock. Because mutex is
shared between producer and consumer.
q After acquiring the lock, the consumer removes the data from the buffer.
q After data is removed, it will signal (mutex) – it will release the lock.
q Obviously, if two readers access the shared data simultaneously, no adverse affects will result.
q However, of a writer and some other processes access the database simultaneously chaos may result.
q To ensure that these difficulties do not arise, we require that the writers have exclusive access to the
shared database.
q This synchronization problem is referred to as the readers-writers problem.
Solution to the readers-writers problem using
semaphores
Mutex =1, initially. When reader1 calls the wait(mutex), mutex value will be decrement to 0.
When reader 2 or 3 tries to call wait(mutex), mutex will become negative. And they need to
wait. So, this solution is not allowing multiple reader to reader simultaneously.
Solution to the readers-writers problem using
semaphores
qThe readers–writers problem is generalized to provide reader–writer locks on some systems.
q Acquiring a reader–writer lock requires specifying the mode of the lock: either read or write
access.
qWhen a process wishes only to read shared data, it requests the reader–writer lock in read mode.
q A process wishing to modify the shared data must request the lock in write mode.
q Multiple processes are permitted to concurrently acquire a reader-writer lock in read mode, but
only one process may acquire the lock.
qFor writing, an exclusive access is required for writers.
III. The Dining Philosophers Problem P0
F1 F0
• One of the classical problems of synchronization P1
• There few philosophers (in this case 5) sitting around a round table.
rice P4
F2 F4
• The philosopher can be in any of the two states:
• Thinking state F3 P3
P2
• Eating state
• When philosophers are thinking they don’t interact with their colleagues. They mind their own business and
do not interact with others.
• When they are hungry they eat the rice that is available in the plate.
• When they want to eat the food, they have to use the forks/chopsticks that are placed adjacent to them.
• When a philosopher wants to eat he needs two forks, not just one.
• When he is eating no one can disturb him or take away the fork from him.
• Once he finishes eating, he will keep the forks back at respective places.
The Dining Philosophers Problem
Philosopher
Thinks Eats
• There are some limitations of semaphores such as: timing errors and deadlock.
• Another method to solve process synchronization problem is called as monitors.
• It is a high level abstraction that provides a convenient and effective mechanism for process
synchronization.
• A monitor type presents a set of programmer-defined operations that provide mutual
exclusion within the monitor.
• The monitor type also contains the declaration of variables whose values define the state of
an instance of that type, along with the bodies of procedures or functions that operate on
those variables.
Syntax of Monitor
• The data type is monitor, because it is an abstract type
• Functions P1, P2…Pn are operations that can be performed upon these shared variables.
• In monitors, the shared variables itself are declared inside the monitor and functions will be making
changes to the share variables as and when required.
• A procedure (function) defined within a monitor can access only those variables declared locally
within the monitor and its formal parameters.
• Similarly, the local variables of a monitor can be accesses by only the local procedures.
• The monitor construct ensure that only one process at a time can be active within the monitor.
Monitors
• The extra thing that we need to define in monitors is:
• Condition construct: condition x,y;
• The only operations that can be invoked on a condition variable are wait() and signal()
• The operation x.wait(): means that the process invoking this operation is suspended until another
process invokes x.signal();
• Let’s assume there is a shared variable x, and one of the process wants to use x. That process will put
x.wait() and if there is some other process that is making use of the x variable, then this process will
have to wait. And it will be keep on waiting until and unless another process invokes the x.signal()
operation. x.signal() is used for releasing the shared resource.
• Shared data are defined which will be shared between different processes.
• Operations are the functions(procedures) Schematic diagram of a monitor
• Procedures are the functions that will take of the operations that will be performed over the shared data.
• Then there is initialization code, that will initialize the variables that we will use.
• There is an entry queue, where processes are waiting to make use of the monitors in order to use the shared
data.
• Within the monitors itself, there are queues associated with x and y conditions.
• In the case of semaphore, we had only a semaphore variable, which was modified by different processes.
Each of the process has to be defined in such a way that it will correctly execute in the way it is expected. If
there were some problems in these definition, it would lead to deadlock and other errors.
• In case of monitors, since all the functions and operations are done within the monitor itself, the process
doesn’t have to worry about that too much as in the case of semaphores.
PROCESS SYNCHRONIZATION
UNIT – 2
Lecture 21
DEADLOCKS
Deadlocks
q In a multiprogramming environment, several process may compete for a finite number of
resources.
q A process requests resources, and if the resources are not available at that time, the process
enters a waiting state.
! Sometimes, the waiting process is never again able to change state, because the resources
has requested are held by other waiting processes.
q This situation is called a deadlock.
System Model
q A system consists of a finite number of resources to be distributed among a
number of competing processes.
q The resources are partitioned into several types, each consisting of some
number of identical instances.
Deadlock
Deadlock
Deadlock Characterization
q Features that characterize deadlocks and the conditions must hold for a deadlock to occur.
q In a deadlock, processes never finish executing and system resources are tied up, preventing
other jobs from starting.
q Features that characterize deadlocks – Necessary conditions
q A deadlock situation can arise if the following four condition hold simultaneously in a
system.
1. Mutual Exclusion
§ At least one resource must be held in a non-sharable mode, that is only one process at a time can use
the resource.
§ If another process requests that resource, the requesting process must be delayed until the resource has
been released.
Deadlock Characterization
2. Hold and wait
§ A process must be holding at least one resource and waiting to acquire additional resources that are
currently being held by other processes.
3. No preemption
§ Resources cannot be preempted, that is, a resource can be released only voluntarily by the process
holding it, after that process has completed its task.
4. Circular wait
§ A set {P0, P1, P2…Pn} of waiting processes must exist such that P0 is waiting for a resource held by
P1, P1 is waiting for P2 and so on.
We emphasis that all four conditions must hold for a deadlock to occur.
The circular wait condition implies the hold and wait condition. All the 4 conditions are
not completely independent.
Methods for Handling Deadlocks
Provides a set of methods for ensuring that at least Requires that the OS be given in advance
one of the necessary conditions cannot hold: additional information concerning which resources
1. Mutual exclusion 2. Hold and wait a process will request and use during its lifetime.
3. No preemption 4. Circular wait
With the additional knowledge, it can decide for
each request whether or not the process should
wait.
Methods for Handling Deadlocks
q If a system does not employ either a deadlock-prevention or deadlock avoidance algorithm, then
a deadlock situation may arise.
q In this environment, the system can provide an algorithm that examines the state of the system
to determine whether a deadlock has occurred and an algorithm to recover from the deadlock (if
a deadlock has indeed occurred)
! If the system either ensure that a deadlock will never occur nor provides a mechanism for
deadlock detection and recovery, then we may arrive at a situation where the system is in a
deadlocked state yet has no way of recognizing what has happened.
In this case, the undetected deadlock will result in deterioration of the system’s performance,
because resources are being held by processes that cannot run and because more and more
processes as they make requests for resources, will enter a deadlock state.
q Eventually, the system will stop functioning and will need to be restarted manually.
PROCESS SYNCHRONIZATION
UNIT – 2
Lecture 22
DEADLOCK – AVOIDANCE, DETECTION,
PREVENTION AND RECOVERY
Resource Allocation Graph
v P1àR1àP3àR2àP1
v We have a cycle here.
v However there is no deadlock
v Observe that P4 may release its instance of R2. That resource can then be
allocated to P3, breaking the cycle.
v Cycle is a necessary condition for deadlock, not sufficient condition. Because if
there is a cycle in the system it does mean that there will be a deadlock for
sure. Because, resources may have multiple instances and the cycle could be
broken.
v If there is only single instances of resources, and there exist a cycle in the
system, it will result in deadlock.
v If a RAG does not have a cycle, then the system is not in a deadlock state.
Deadlock Avoidance
rag
Resource-Allocation-Graph Algorithm
! >0" &"#$%&'"# 9%#+ ;" '*),9"3 /&,$& ,- +0" #7#+"95 >0)+ ,#G ;"2$&" /&$'"## C,
#+)&+# "K"'%+,-.G )** ,+# '*),9 "3."# 9%#+ )*&")37 )//")& ,- +0" !LM5
v Suppose that process Pi requests resource Rj. The request can be granted only if
converting the request edge PiàRj to an assignment edge RjàPi does not result
in the formation of a cycle in the RAG.
v Note that we check for safety by using a cycle-detection algorithm.
v If no cycle exists, then the allocation of the resources will leave the system in a
safe state.
v If a cycle is found, then the allocation will put the system in an unsafe state.
Deadlock Avoidance
Resource-Allocation-Graph Algorithm
Unsafe State
By using a RAG algorithm, we should not allow the assignment of R2 to P2. Thus, we can make sure that the
Deadlock is avoided. Because, by the RAG algorithm, the process could claim, and if we allow one of the
claim edges to converted to a request edge, there is a possibility of deadlock to occur, the request by the
process will not be granted. So, if we know what are the claims from each processes, then see that which of
the requests could be granted and then which should not be granted and when should a process wait even if
the request is made then, we can avoid the deadlock. So by checking the claims, we check whether there
is a cycle to occur and can be avoided.
Important- RAG algorithm can be used only in systems where, there is a single instance of a resource.
Deadlock Avoidance – Banker’s Algorithm
v Some data structures that are used to implement the banker's algorithm are:
vAvailable
vMax Allocation Max Available
vAllocation
vNeed
v Consider the system with 5 processes: P0, P1, P2, P3, P4 and A B C A B C A B C
3 resources: A, B, C. P0 0 1 0 7 5 3 3 3 2
vThe following snapshot of the system is given. P1 2 0 0 3 2 2
vFind the needy matrix.
P2 3 0 2 9 0 2
vIs the system in a safe state?
P3 2 1 1 2 2 2
vIf yes, find the safe sequence.
P4 0 0 2 4 3 3
Need Matrix
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1
Safety Algorithm
vFor P0:
vneed = <7, 4,3> and available = <3, 3,2>
vHence, need NOT<= available,
vDO NOT EXECUTE P0, go forward with P1
vFor P1:
!4%%"Q R?1@1AT 64" 6>6#86;8% Q R@1 @1 AT
!C%4&%1 4%%" RQ 6>6#86;8%1 WXWHYGW .?
!U%9 6>6#86;8% Q 6>6#86;8% Z 688*&6'#*4 Q R@ZAQ[1 @ZOQ@1 AZOQAT
Safety Algorithm
vFor P2:
vneed = <6,0,0> and available = <5,3,2>
vHence, need > available,
vDO NOT EXECUTE P2, go forward with P3
vFor P3:
vneed= <0,1,1> and available = <5, 3, 2>
vHence, need <= available, EXECUTE P3
vNew available = available + allocation = <5+2=7, 3+1=4, 2+1=3>
vFor P4:
vneed= <4,3,1> and available = <7,4,3>
vHence, need <= available, EXECUTE P4
vNew available = available + allocation = <7+0=7, 4+0=4, 3+2=5>
Safety Algorithm
DEAD LOCK!!
Recovery from Deadlock – Process Termination
v Here we successively preempt some resources from processes and give these
resources to other processes until the deadlock cycle is broken.
v If preemption is required to deal with deadlocks, then three issues need to be
addressed:
v Selecting a victim
v which resources and which processes are to be preempted?
v minimize cost
v Rollback
v what should be done with a process if we preempt its resources?
v we must roll back the process to some safe state and restart it from that state
v Starvation
v how do we ensure that starvation will not occur?
v how can we guarantee that resources will not always be preempted from the same process?
Deadlock Prevention
INTRODUCTION TO
MEMORY MANAGEMENT
2
Memory Management in OS
◦ It is a kind of method or it is a kind of functionality to manage the various kinds of
memories.
◦ Eg. If we take our laptops, there are different kinds of memories such as: RAM, disk,
registers etc. So, memory management deals with how can we manage the different
memory units in a more efficient manner. This is the functionality of the OS.
◦ So, it is a method of managing the primary memory.
◦ Out of the majority of memory units, the majority isRAM.
Registers RAM
CPU (Primary Secondary
Cache memory) memory
2
Main Memory
◦ CPU will execute each instruction one by one and CPU is connected with registers, cache and
RAM. There is a direct connection.
◦ But the secondary memory is not directly connected with the CPU. The reason is SPEED.
◦ Secondary memory is very slow and CPU is very fast. So, we can’t connect them directly.
◦ So, we connect fast memories like register, cache and RAM.
◦ There is a limited size of registers, cache and RAM.
◦ If we increase the size of RAM, or registers or cache, then it will directly increase the cost of the system.
◦ So, all the programs are stored in secondary memory, which need to be executed.
◦ How CPU will execute all these programs lying in the secondary memory?
◦ So, all the user written programs are sitting idle in the secondary memory.
◦ For running programs in CPU, the program needs to be brought into RAM, the main memory, so
that CPU can directly interact with the processes and it can be executed.
◦ Registers and cache are very fast but they are very small in size.
3
P0
qLets consider that the CPU communicate with the primary memory directly
and the programs/processes in the secondary memory needs to be swapped
or brought into the primary memory for executing in the CPU.
qThis is referred as degree of multiprogramming. Because, when we push the
programs from the secondary memory to the primary memory (RAM), we
need to bring as many programs as possible so that CPU can execute all of
them resulting in increased CPU utilization.
4
Memory Organization
◦ Memory is central to the operation of a modern computer system
◦ Memory consists of a large array of bytes, each with its own address.
◦ The CPU fetches instructions from memory according to the value of the program
counter.
◦ A typical instruction-execution cycle, for example, first fetches an instruction from
memory.
◦ The instruction is then decoded and may cause operands to be fetched from memory.
◦ After the instruction has been executed on the operands, results may be stored back in
memory.
◦ The memory unit sees only a stream of memory addresses; it does not know how they are
generated or what they are for.
◦ To have fast memory between the CPU and main memory, cachememoryis added.
5
Swapping
◦ A process can be swapped temporarily out of memory to a backing store, and then brought back
into memory for continued execution.
◦ Swapping makes it possible for the total physical address space of all processes to exceed the real
physical memory of the system, thus increasing the degree of multiprogramming in a system.
◦ Swapping requires a backing store
◦ Backing store
◦ It is a fast disk
◦ Should be large enough to accommodate copies of all memory images for all users
◦ Must provide direct access to these memory images
◦ Roll-in, Roll-out
◦ When a higher priority process arrives, a lower priority process is swapped out, and then
swapped back in, when the higher priority process finishes
◦ Major part of swap time if transfer time
◦ Totaltransfer time is directly proportional to the amount of memory swapped. 6
Swapping
◦ The system has a ready queue with all processes whose memory images are on the
backing store or in memory and ready to run
◦ The CPU scheduler calls the dispatcher before running a
process
◦ The dispatcher checks if the next process in queue is in primary
memory
◦ If not, and if there is no free region, the dispatcher swaps out a
process currently in primary memory and
swaps in the desired one from backing store.
◦ It then reloads registers and transfers control to the process
◦ The context switch time in such a swapping system is fairly high
◦ If we want to swap a process, it must be completely idle
Schematic view of swapping 8
Memory Management Techniques
◦ Operating system uses various memory management methods to manage the primary
memory (RAM).
◦ Memory is a large area of bytes and every byte is having an address.
◦ We are trying to divide the memory or partition the memory and to keep maximum
number of processes in RAM.
◦ Also, more number of processes should be in ready state, so that these processes can be
changed to execute state by assigning it to the CPU, and also have plenty of ready
processes if a process does an I/O operation (file operations, using hardware devices,
display etc.), not idling the CPU.
◦ The main memory must accommodate both the operating system and the various user
processes.
◦ We therefore need to allocate main memory in the most efficient way possible.
8
Memory Management Techniques - Types
Fixed Partitioning
(static)
Contiguous
Variable Partitioning
Memory (dynamic)
management
techniques
Non-contiguous Paging
Multilevel Paging
Inverted Paging
Segmentation
Segmented Paging
9
OS OS
Memory Management Types P1
P2
P1
P3 P4
P4 P2
P5 P3
◦ Basically there are two types: contiguous and non-contiguous.
Contiguous Non-contiguous
◦ Contiguous memory allocation is further divided into static and dynamic allocation.
◦ Basically, the memory is divided into two partitions: one for the resident OS and one for the user
processes.
◦ We want several user processes to be present in the main memory
◦ In contiguous memory allocation, each process is allocated to the memory addresses continuously.
◦ Fixed portioning is used long back, in main frame systems.
◦ In non-contiguous memory allocation, the processes are not allocated in continuous addresses,
rather they are in different addresses.
10
Fixed Partioning (Static)
OS OS
◦ In fixed portioning, the number of partions are fixed
P1 4MB P1 8MB
◦ When ever a system is started, the OS is mounted in the
P2 8MB P2 8MB
RAM
P3 8MB P3 8MB
P4 16MB P4 8MB
◦ In memory allocation, we want to allocate more user processes in RAM, before that the number of partitions are fixed in the memory.
◦ The time when the system is configured, that time itself, the number of partions are fixed.
◦ Size of each partition may be same or different.
◦ In contiguous allocation, sparing is not allowed.
◦ Sparing means, when a process needs to be allocated to the memory, it will be assigned to a particular partition. In case, it is not possible to be
allocated within a partition, then it cannot be allocated to any other partition.
◦ Disadvantages:
◦ Eg. In case there is a 2MB process; then the process will be assigned to the 4MB partition in Fig.1 and the rest 2MB memory will be wasted.
This is called as internal fragmentation.
◦ In case a process of size 32MB is there, then it cannot be allocated to any partition – there is a limit in the process size.
◦ Here the no. of partitions are fixed (4) , if we have 5 or more than 4 processes, we can’t bring that to main memory – limitation on degree of
multi programming
◦ Externalfragmentation
11
OS
2MB P1
15
Segmentation – Non contiguous Memory
d
•MMU will help to convert logical BA Size
Logical
address to physical address. 0 add. d 0 OS For segment S5-
3300 200 BA
•This conversion is done with compared 1500 1500 is base
the help of segmenttable. 1 1800 400 with size S5 address
d≤size Yes 1800
•Segment table will help to fetch a 2 2700 600 ≤ + 2200
S1 And size of
S5=300
data from the phy. memory S4
3 2300 400
2300
Segment Segment 2200 100 S3
no. (S) size (d) 4 No 2700
Logical address. 1500 300 S2
5 3300
00001(S1) 10000 Trap S0
Segment Table 3500
CPU generates LA
Main memory
16
OS
◦ In non-contiguous memory allocation, sparing is supported. Ie, one process can be divided and
allocated to different memory locations. P1
◦ If we have a process P1 of 6KB size, cannot be put into main memory with the available free space
by contiguous memory allocation.
◦ But, in non-contiguous allocation, the process P1 can be divided into 3 partitions of 2KB each and
can be allocated to main memory. Thus, removing external fragmentation. Here, partitions are 6KB
made based on the available hole sizes.
◦ But, portioning is a costly method. Because holes are created dynamically and it changes at run-
time.
17
Segmentation Vs Paging
◦ Now, if a new process P2 arrives of size 2KB. This process want to occupy the main memory, then it will check the
holes in the memory and its sizes. It will find that there are 2 holes of 1KB each. Then, the process will be divided
into 2 partitions of 1KB size each.
◦ But, there are difficulties as holes are created dynamically and its number and size keep changing at run-time. Every
time, when a process tries to occupy the space in memory, it has to analyze the holes and size and location. Bases on
this, the process needs to be partitioned and thus, it is a time consuming job.
◦ So, the alternate to this is, before we bring a process into the main memory, we divide the process into partitions and
each partition is called a PAGE. This partitioning is done in the secondary memory itself.
◦ At the same time, we divide the main memory also into partitions. These partitions in the main memory is called as Main memory
FRAMES. OS
◦ In this case, the size of the page will always be equal to the size of the frame. Frame 0
◦
Page Size = Frame Size Page 0
Frame 1
◦ Here, external fragmentation is completely avoided. Page 1
Page 2
Frame 2
P1
18
Need of Paging
◦ Eg. RAM of 8KB, frame size is 1KB.
◦ Then., there will be 8 frames.
Frame OS
◦ Let the process P1 be of size 2KB. Process need to be divided into pages of size=frame size=1KB P1
0
◦ Similarly, if there processes P2 (2KB), P3(2KB), P4(2KB) are available, then each process pages P1
1
can be allocated to frames in main memory. 2 P2
◦ Later, of P2 and P4 finished its execution and completes. Then, 4 holes of 1KB size will be created 3 P2
4 P3
◦ If now, a process P5 of size 4KB arrives, we divide it into pages of size 1KB, so totally 4 pages.
5 P3
◦ In non-contiguous allocation we can allocate these 4 pages into frames 2, 3, 6 and 7.
6 P4
◦ Thus, in conclusion in paging, we divide each process before allocating to the main memory into pages. P4
7
The size of the page is decided by the size of the frames in main memory. Here, pages of process will
exactly fit in frames of main memory, thus removing external fragmentation completely. This is the
reason of using paging.
19
Paging
Frame No. Bytes
0 0 1
1 2 3
◦ In paging, the process is divided into equal size pages and stored in the main memory frames. 2 40 15 P0
◦ Lets assume we have a process P1 of size 4 Byte and page size if 2 Byte.
3 6 7
◦ Therefore, the number of pages = 4/2 = 2 pages
◦ Size of main memory = 16 Bytes 4 828 939 P1
◦ Frame size = 2 Byte Bytes 5 10 11
◦ Note: frame size and page size should always be the same.
Page No. 0 00 11
6 12 13
◦ Because, when we divide the process to save in the pages, these pages should be saved into main 1
22 33
7 14 15
Memory frames. So to fit it correctly, the page size and main memory frame size should be same.
Page table Main memory
◦ Number of frames = 16/2 = 8 frames
◦ Main memory is byte addressable.
◦ Now, when CPU is executing process P1, CPU is not aware of the frames, it only asks for a byte of data from main memory to execute P1. For eg. When CPU asks
for byte no. 3, it is mapped to the respective byte in the page but is actually available in frames of the main memory.
◦ For eg. If frames 0,1,3 and 5 are filled already, then page 0, will be stored in frame 2 and page 1 will be stored in frame 4. now, if CPU asks for byte no. 3, then byte
3 can be present in any of the frames (here, in frame 4).
◦ So, we need mapping here. The CPU when generating addresses, are not absolute addresses. So, there should be a mechanism to convert this 3 to 9 in the main
memory and fetch the data from there. The mapping is done by memory management unit. Ie, the MMU, is responsible for conversion of the address generated
by CPU, to absolute address MMU takes the help of page table. It contains frame numbers where that page is actually present in the main memory.
20
Paging Page No. 0 0 1
Bytes
1 2 3
◦ Every process has its own page tables.
◦ For P1 process, how many entries will be there in the page table? How many pages that process
has, those many entries will be there.
Page no.
◦ How is this process happening? 0 F2
◦ CPU will generate logical addresses. Logical address is generated with two things: page number 1 F4
and page offset (size of the page). Page table of
Process 1
◦ To represent page no., in this case 2, we need one bit (page0, page1)
Page no. Page offset
◦ To represent page size, in this case 2, we need one bit 1 bit 1 bit
◦ Now, CPU has generated an address: 3 (11-binary) ie. Page no-1 and page offset-1 1 1
◦ In page no-1, there are 2 bytes (0th byte and 1st byte). Logical address
21
Paging
◦ Now, the CPU generated address 3, we need to convert to 9
◦ The logical address generated by CPU need to be converted to physical address
◦ We understood what is logical address is.
◦ Now, physical address (PA) (it says where actually the byte is lying) is directed connected with the
main memory
◦ PA is related to the size of the main memory (16 bytes in this case)
◦ To represent 16 in binary, we need 4 bits. Frame no. Frame offset
3 bits 1 bit
◦ We understood that we need to go to frame no.4 from page 1, using logical address. 1001 =9
100 (4) 1
◦ Next is frame offset is 2bytes, which is same as page offset, because the 2nd position in page
Physical address
should be same as second position in frame as well.
◦ In conclusion, CPU will say the byte address, which need to be converted to logical
address and later to physical address and then read the byte from the main memory.
22
Paging – Another example
◦ CPU is asking for byte no.1
◦ 1 is represented as 01 – that means in 0th page, the 1st byte.
◦ Next we will take the page table, in page table page 0 – refers to frame no. 2. In frame 2, we need
to take the 1st byte: 5
◦ In physical address: frame no 2 is represented as 010 using 3 bits. Next is frame offset: 1. thus,
physical address is now: 0101 = 5
◦ This is how we map, logical address to physical address. For this mapping, we need page table. In
page table we have frame number. From frame number, we take the byte and give it to CPU
23
LECTURE - 24
VIRTUAL M E M O RY
2
Virtual Memory
v Virtual memory provides an illusion to the programmer that the process whose size is larger than
the size of main memory can also b e executed.
v How is it that possible?
v The size of the main memory is finite. But, there is no limitation to the size of the processes.
v In case, if the size of a process is larger than the main memory size, how can we generate the
pages and provide data to the C PU? How C P U can execute the process?
v We know that to improve the degree of multiprogramming, we need to bring more and more
processes to the main memory. But, its size, being limited, how can we bring more processes for
executing in the C PU?
v In logical address space (ie, hard disk) there are multiple processes and are divided into pages.
v In case, where a full process cannot b e brought into the main memory, we can bring the required
pages of the process for execution.
v Which pages are required at a particular time, only those pages of the process will b e brought into
the main memory – this uses the P R I N C I P L E O F L O C A L I T Y O F R E F E R E N C E
v When a particular p a ge is brought into the main memory, not only that pages, but all the related
pages of that process are also brought into the main memory.
Virtual Memory
v So, main memory will contain some p a ge s of process P1, some p a ge s of P2, etc. so, the advantage is that
the user will find multiple process for execution even though the entire process are not present in the
main memory.
v This is done with the help of swap-in and swap-out operation/roll-in or roll-out.
v Swap-in means we bring the required p ages of a process to the main memory for execution and keep it
there until the execution is complete, and then swap-out to secondary storage. Then we, will bring the
relevant p a g e s of another process to the main memory. This is called as PA G E R E P L A C E M E N T
P3
P1 P0
OS
P0 P1 P0
Page 0 of P1 Swap-in/ Roll-in
P1 P2 P1
Page table of P2
P2 P3 P2
Page 2 of P2
P3 P4 P3
Page 2 of P1 Swap-out/ Roll-out
P4 P5 P4
Page table of P1
P5 P2 P5
Page 0of P2
Logical Address S p ace
M a in M e m o r y
Virtual Memory
v Advantag es of V irtual memory:
v no limitation on the number of processes that can b e in the main memory
v no limitation to the size of the process
v Disadvantages
v Let say, C PU is executing process P1, it will demand for some data (eg. Page 1 of process P1).
Where is this p a ge lying in the main memory? With the help of p a g e table, MMU will identify
the data. But, there could b e instances, where pages are not present in the frames. This is
identified by the entry in the p a ge table called as valid/invalid bits. This bit, tells, whether that
p a g e is actually present or absent in the main memory
v So, when a p a g e that is required by the C PU for execution is not present in the main memory, it
will generate a PAGE FAULT. And TRAP will b e generated and the control will change from
Valid/invalid bit
user to O S and context switch will happen.
v W hen O S gets activated, it will check the user who dem and that p a g e F1
CPU
whether he an authenticated user or not. If the user, is an authenticated - 0
user, then O S will provide that p a g e from the hard disc. F2
- 0
Page table of P1
Demand Paging
v Demand paging is the method by which virtual memory is implemented in any O S
v In this technique, a p a g e is brought into the memory for its execution only when it is
demanded.
v It is a combination of paging and swapping.
v The demand paging requires the complete process should b e in the secondary storage area in
the form of pages.
v It is called lazy swapper method, because it swaps the p a g e only when it is n e e de d
v Advantages
v Reduces the memory requirement
v Swap time is reduced
v It increases the d e g r e e of multi programing Frames
of A
v Disadvantages
v The occurrence of p a g e fault
Frames
of B