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

Operating Systems Summary

The document provides an introduction to operating systems, detailing their functions, components, and structure. It covers essential concepts such as processes, scheduling, and inter-process communication, explaining how operating systems manage hardware resources and execute programs. Additionally, it discusses various types of operating systems and their operational modes, emphasizing the importance of efficient resource allocation and user interaction.

Uploaded by

abdallahmowafy90
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Operating Systems Summary

The document provides an introduction to operating systems, detailing their functions, components, and structure. It covers essential concepts such as processes, scheduling, and inter-process communication, explaining how operating systems manage hardware resources and execute programs. Additionally, it discusses various types of operating systems and their operational modes, emphasizing the importance of efficient resource allocation and user interaction.

Uploaded by

abdallahmowafy90
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

operating systems

Chapter 1 ( introduction )
Chapter 2 ( process )
Chapter 3 ( Scheduling )
Chapter 4 ( Threads )
Chapter 5 ( Concurrency and Process Synchronization )
Chapter 6 ( Deadlock )
Chapter 7 ( Memory Management )
Chapter 8 ( Virtualization )

Chapter 1 ( introduction )
1.1 What is Operating System
Operating System is A program that acts as an intermediary between a user of a computer and the computer hardware.
Operating System = Kernel + System Programs

Kernel It's the core that provides basic services for all other parts of the OS.

System Programs are associated with the operating system.

Application Programs are not associated with the operating system.

Operating System is a :

resource allocator → manages all resources, Decides between conflicting requests for efficient and fair resource use.

control program → Controls execution of programs to prevent errors and improper use of the computer.

Operating System goals :

Execute user programs and make solving user problems easier

Make the computer system convenient to use

Use the computer hardware in an efficient manner

1.2 Computer System Components

Hardware provides basic computing resources ( CPU, Memory, I/O devices )

Operating System Controls and coordinates use of hardware among various applications and users

Application Program define the ways in which the system resources are used to solve the computing problems of the users

Users

1.3 Computer Startup

operating systems 1
Bootstrap

A single program stored in ROM/EPROM

generally known as firmware

is loaded at power-up or reboot,

Loads operating system kernel and starts execution

BIOS ( Basic input output system )

Set of programs stored in ROM/EPROM contains procedures that handle the H/W devices

BIOS bootstrap performs mainly 4 operations :

1. Power-on Self-Test (POST)

2. Initialization of H/W devices

3. Searches for OS to boot

4. If a device contains OS → Loading the Bootloader (Bootstrap Program)

1.4 Computer System Organization

interrupts
interrupt is a signal sent to the CPU to indicate that an event has occurred

an external event raised by hardware ( e.g. I/0 ).

an internal event trigger by software ( TRAP ) e.g. system calls → means application needs a service from the OS. It calls a
service

interrupt handler

must be executed when the interrupt occurs

Interrupt transfers control to the interrupt service routine (ISR), through the interrupt vector then the ISR handles the event

interrupt vector → an array contains the addresses of all the service routines

Interrupt architecture must save the address of the interrupted instruction


After servicing interrupt, CPU resumes execution at previously interrupted address

I/O Structure
A general-purpose computer system consists of CPUs and multiple device controllers connected through a common bus.

Device Controller ( Hardware )

Each device controller controls a specific type of device (e.g. USB).

A device controller has a local buffer storage and a set of registers.

The device controller is responsible for moving the data between the peripheral devices that it controls and its local buffer
storage.

Device Driver ( Software )

operating systems 2
The operating system provides a device driver that understands the device controller and provides a uniform interface to
the device.

Translates OS / Application requirements into device commands.

I/O Operations

1. Device driver loads the registers within the device controller.

2. Device controller determines action to take based on registers.

3. Device controller transfers data between device and its local buffer.

4. Device controller generates an interrupt to inform the device driver ( CPU ).

5. Device driver returns control to the operating system.

Direct Memory Access ( DMA )

Device controller transfers an entire block of data directly between its local buffer and main memory, with no intervention
by the CPU.

Device controller generates one interrupt per block, to inform the device driver rather than one interrupt per byte.

While the device controller is transferring a whole data block, the CPU is available to accomplish other work.

How a Modern Computer Work

Storage Structure
Main Memory

is the only large storage media that the CPU can access directly.

used to hold data is actively being processed. ( volatile )

Secondary Storage

is an extension of main memory that provides large nonvolatile storage capacity.

not directly accessible by the CPU.

Storage hierarchy

operating systems 3
Caching

Cache memory is a high-speed storage layer that temporarily stores frequently accessed data to improve overall processing
speed.

Caching refers to the use of high-speed memory to hold a copy of recently-accessed data assuming that it will be needed again
soon.

Operating system groups

Batch → users submit jobs to the system in a batch, this jobs are processed sequentially without user interaction.

Time-sharing → allows multiple users to interact with the computer at the same time.

Real-time → used in computers that controls critical systems such as: control machinery, scientific instruments.

1.5 Computer System Architecture

A general-purpose processor supports a complete instruction set. Therefore, it can execute user processes. ( CPU )

A special-purpose processor supports a limited instruction set. Therefore, it cannot execute user processes. ( include device-
specific processors, such as disk, keyboard, and graphics controllers. )

Single processor System

Has only one general-purpose processor.

Multiprocessor System

Also known as parallel systems, tightly-coupled systems.

Two or more general-purpose processor.

Advantages include

1. Increased throughput ( total number of tasks executed by all processors ).

2. economy of scale → sharing peripherals, mass storage and power supplies means less cost.

3. increased reliability → graceful reliability, fault tolerance

graceful reliability : the system's ability to continue providing service proportional to the level of surviving hardware.

fault tolerance : enable the system to detect and recover from hardware failures. if one processor fails, another can take over
without interrupting service.

Types of multiprocessing

Symmetric multiprocessing Asymmetric multiprocessing

all processors share the same memory and I/O resources each processer may have its own memory and I/O

each processor is assigned to a specific task


All processors are equal sharing tasks equally one processor called
the master control the others ( salves )

operating systems 4
Clustered system

Like multiprocessor systems, but multiple systems working together.

Usually sharing storage via a storage-area network (SAN), Provides a high-availability service which survives failures.

1. Asymmetric clustering has one machine in hot-standby mode.

2. Symmetric clustering has multiple nodes running applications, monitoring each other.

1.6 Operating System Structure

Operating Systems vary greatly in their makeup internally

Multiprogramming

Time Sharing (Multitasking)

Multiprogramming Time Sharing ( Multitasking )

multiple programs are loaded into memory, the CPU switch between a logical extension of multiprogramming in which CPU switches jobs so
them when one program is waiting for I/O operations. frequently.

the CPU is divided into small intervals ( time slices ) and each user / task gets a
share of the CPU for a short time before the CPU switches to the next one.

allow the user to interact with each job while it is running. allow multiple users
maximize CPU utilization but still no direct user interaction.
to interact with the system at the same time.

1.7 Operating System operations


Dual-mode operation ( two modes in which a program can execute ) allows OS to protect itself and other system components.

user mode

kernel mode

Mode bit → provided by hardware Provides ability to distinguish when system is running user code or kernel code.

User mode ( bit “1” ) Kernel mode ( bit “0”)

Restricted mode Privileged mode

The CPU restricts access to hardware resources and memory. has directed access to all system resources.

provide isolation between user application and the system to ensure that
if program crashed during the execution doesn’t crash the operating if program happens to crash the entire system would crash.
system.

in user mode when the program needs access to hardware / software it makes a call to the operating system telling that it needs
access ( system call ). after the call, the program is switched from user mode to kernel mode so that the program can use those
resources.

system call is a programmatic way in which computer program requests a service from the kernel.

operating systems 5
Chapter 2 ( process )
2.1 Process

a program in execution. ( Program becomes process when executable file loaded into memory )

process execution must progress in a specific order.

2.2 Process Concept


A program can be divided into many pieces when put into memory
to become a process :

Text section : executable code

data section : global variables

Heap section : dynamically allocated variables

Stack section : contains temporary data ( Function parameters, return addresses, local variables )

memory layout of a process

2.3 Process states


As a process executes, it changes state :

New: The process is being created

Ready: The process is waiting to be assigned to a processor

Running: Instructions are being executed

Waiting: The process is waiting for some event to occur

operating systems 6
Terminated: The process has finished execution

Diagram of Process State

2.4 Process Control Block (PCB)

Every process has a process control block.

A data structure that stores information about process.

PCB stores all of the information required to maintain track of a process. Such as :

1. Process state ( new, ready, etc.)

2. Program counter the address of the next instruction

3. CPU registers. stores state information when an interrupt occurs

4. CPU - Scheduling information. scheduling parameters ( Priority )

5. memory management information. process location in memory

6. Accounting information. time limits, process numbers, …

7. I/O status information. allocated I/O devices , open files , …

PCB

2.5 Process Scheduling


When there are several or more runnable processes, the operating system chooses which one to run first. this is process scheduling.
CPU Scheduler Selects a Ready Process From Memory For Execution.

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

operating systems 7
characteristics of a good scheduling algorithm

1. Response time should be kept to minimum

2. The total number of jobs processed every hour should be as high as possible

3. Maximize the CPU use

Scheduling Queues

Job Queue Set of all processes in the system

Device ( I/O )Queue Contains processes that are waiting for I/O operations to complete

Ready Queue Contains all processes that are ready to execute and waiting for CPU time

Waiting Queue Contains processes that cannot continue execution until a certain event occurs

Representation of Process Scheduling


A new process is placed in the Ready queue and waits until selected for execution. Once assigned to the CPU,
one of the following several events can occur:

it can issue an I/O request and then be placed in the I/O queue.

create a new subprocess and wait for its termination.

removed forcibly from the CPU due to an interrupt and be put back in the Ready queue.

Queueing Diagram

Schedulers
Short-term scheduler ( CPU scheduler )

operating systems 8
Selects which process should be executed next and allocates CPU

Sometimes the only scheduler in a system

Short-term scheduler is invoked frequently (milliseconds)

Long-term scheduler ( job scheduler)

Selects which processes should be brought into the ready queue

Long-term scheduler is invoked infrequently (seconds, minutes)

The long-term scheduler controls the degree of multiprogramming

Medium-term scheduler

Can be added if degree of multiple programming needs to decrease

Remove process from memory, store on disk, bring back in from disk to continue execution swapping

Context Switch

Context switch occurs when CPU switches to another process

Context of a process represented in the PCB

Context switch Saves the current context of the current process in its PCB, Loads the saved context of the other process
scheduled to run.

Context switch time is overhead, the system does no useful work while switching

2.6 Operations on Processes

Process Creation

A process may create several new processes.

The creating process is called a parent process, the new processes are called children.

operating systems 9
Each of these new processes may create other processes.

In terms of execution, there are two possibilities :

1. The parent continues to execute concurrently with its children.

2. The parent waits until some or all of its children have terminated.

In terms of address-space, there are two possibilities :

1. The child process has a new program loaded into it.

2. The child process is a duplicate of the parent process.

In terms of resource sharing, there are three possibilities :

1. Parent and child share no resources.

2. Parent and children share all resources.

3. Children share a subset of parent's resources.

Process Termination

A process terminates when it finishes execution.

It asks the OS to delete it by using the exit system call.

Once a process is terminated, the OS deallocates all of its resource.

A parent process may terminate its child using the abort system call.
Some reasons for doing so :

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 ( cascading
termination System ).

Child has exceeded allocated resources.

2.7 Inter-Process Communication


A cooperating process shares data with other executing processes.

Reasons to support process cooperation :

Information sharing. (e.g. copying and pasting).

Computation speedup. Break a task into sub-tasks that run in parallel.

Modularity. Dividing the functions of a system into separate processes.

Cooperating processes need an IPC mechanism to exchange data.

Two models of IPC

operating systems 10
models of IPC

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.

Communicating processes are responsible for :

Determining the form of the data and the location of shared data.

Ensuring that they are not writing to the same location simultaneously. ( Synchronization )

Used for High-speed data exchange

Message Passing

Allow processes to communicate without share memory

useful in distributed environment and managed by the OS

IPC facility provides two operations: send(message) receive(message)

The message size is either fixed or variable

Direct Communication
A direct communication link has the following properties

A link is established automatically.

A link is associated with exactly two processes.

Between each pair of processes, there exists exactly one link.

Direct Communication may use symmetric or Asymmetric addressing

Symmetric Asymmetric

both sender and receiver name each other only the sender names the receiver process

send(P, message): Send a message to process P send (P, message): Send a message to process P
receive(Q, message): Receive a message from process Q receive (id, message): Receive a message from any process

Indirect Communication
Messages are sent to and received from mailboxes (or ports)

Mailbox user is a process that can send messages to a mailbox

Mailbox owner is a process that can receive messages from mailbox.

operating systems 11
In indirect communication, send() and receive() are defined as:

send (A, message): Send a message to mailbox A.

receive(A, message): Receive a message from mailbox A.

An indirect communication link has the following properties:

A link is established by creating and sharing a mailbox.

A link may be associated with more than two processes.

Each pair of processes may have several links (mailboxes).

Synchronization

Blocking (synchronous) send. The sending process is blocked until the message is received by the receiving process or by the
mailbox.

Non-blocking (asynchronous) send. The sending process sends the message and resumes operation, immediately.

Blocking (synchronous) receive. The receiver blocks until a message is available. It resumes operation only after receiving the
message.

Non-blocking (asynchronous) receive. The receiver retrieves either a valid message or a null and resumes operation,
immediately.

Chapter 3 ( Scheduling )
3.1 CPU and I/O Burst Cycle
Process execution consists of a cycle of CPU execution and I/O wait.
Process execution begins and ends with a CPU burst.

CPU burst : a time interval when a process uses CPU only.

I/O burst : a time interval when a process uses I/O devices only.

3.2 CPU Scheduler

Selects a process for execution from the ready queue.

operating systems 12
CPU scheduling decisions may take place when a process :

1. Switches from running to waiting state

2. Switches from running to ready state

3. Switches from waiting to ready

4. Terminates

Preemptive vs Non-Preemptive Scheduling

Preemptive scheduling Non-preemptive (or cooperative) scheduling

Allows for preempting a running process before it completes its


No process can be forced to stop ( preempted ) while it is running
CPU burst

Scheduling decision is taken only when the running process


Scheduling decision is taken if the running process is interrupted or
completes its current CPU burst ( terminates or switches from
another process of a higher priority becomes ready for execution
running to waiting )

Scheduler Latency → time it takes for the Scheduler to perform the select

3.3 Dispatcher

module gives control of the CPU to the process selected by the scheduler. its functions involves :

Switching context

Switching to user mode

jumping to the proper location in the user program to restart that program

Dispatch Latency → time it takes for the dispatcher to stop one process and start another running.

3.4 Scheduling Criteria

CPU utilization → keep the CPU as busy as possible. Maximize

Throughput → number of processes that complete their execution per time unit. Maximize

Turnaround time → amount of time to execute a particular process ( time from submission to termination ). Minimize
T urnaroundtime = completiontime − arrivaltime
Waiting time → amount of time a process has been waiting in ready queue. Minimize
Waitingtime = T urnaroundtime − bursttime( CPU time required )
Response time → amount of time it takes from when a request is submitted until the first response is produced, not output.
Minimize
Responsetime = starttime − arrivaltime
Fairness → Give CPU to all processes in a fair manner.

Service time → The time required by a device to handle a request. Minimize

Residencrtime = servicetime + queueingtime Minimize

3.5 Scheduling Algorithms

T urnaroundtime = completiontime − arrivaltime


Waitingtime = T urnaroundtime − bursttime
Responsetime = starttime − arrivaltime
First-Come, First-Served ( FCFS )

Simple non-preemptive CPU scheduling.

Same as FIFO.

The process that requests the CPU first is allocated the CPU first.

The average waiting time is often quite long because of Convoy effect.
Convoy effect occur when processes wait for one process to get of the CPU, resulting in lower CPU and device utilization.

operating systems 13
FCFS example

Shortest-Job-First (SJF)

Associate with each process the length of its next CPU burst

Assign the CPU to the process that has the smallest next CPU burst

if the next CPU bursts of two processes are the same, then use FCFS

Non-preemptive SJF

once the process takes the CPU, it cannot be preempted.

The Scheduler must wait until the executing process completes its CPU burst.

Non-preemptive SJF example

preemptive SJF

Known as shortest-remaining-time- first ( SRTF )

If a new process arrives, the scheduler compares its CPU burst length to the remaining time of the currently
executing process.

operating systems 14
preemptive SJF example

The Real difficulty with the SJF algorithm is knowing the length of the next CPU request, We may not know but we can
predict.

Optimal for minimizing queueing time, but impossible to implement.

Priority

A priority number is associated with each process

The CPU is allocated to the process with the highest priority

Processes with similar priority are scheduled in a FCFS order

SJF is a priority scheduling where priority is the predicted next CPU burst time

Non-preemptive Priority

Once a process dispatched it runs to end

Example of Priority Scheduling ( non-preemptive )

preemptive Priority

The dispatched process continues running until finish or a comer with higher priority enters ready queue

operating systems 15
Example of Priority Scheduling ( preemptive )

A major problem with priority Scheduling is starvation ( low priority processes may never execute )
Solution to starvation is Aging increasing the priority of processes as time progresses

Round Robin ( RR )

Like FCFS scheduling, but each process gets a small unit of CPU time, called a time quantum or time slice

After the time quantum elapses

Running process is preempted and added to the end of the ready queue.

Scheduler selects the process at the head of the ready queue for execution.

Example of RR

Time Quantum Size Affects RR Performance

operating systems 16
If the time quantum is extremely large If the time quantum is extremely small

The RR scheduler becomes a FCFS scheduler Too many context switches, which can degrade system performance

Time quantum should be large enough compared to the context switch time

Multilevel Queue

Suitable for the case where processes can be classified in different groups according to some process criteria (type, size,
priority,... )

divides the ready queue into multiple separate queues, each queue has its own scheduling algorithm

Foreground processes demand high response times and carry higher external priority, typically scheduled using Round Robin
(RR).

Background processes require less interaction and lower priority, scheduled using First-Come, First-Served (FCFS).

Scheduling between these queues can be done in one of two ways :


Fixed-priority scheduling

Each queue is assigned a priority number

All processes in a higher-priority ( foreground ) queue are served first. Starvation my occur

Time slice scheduling

Each queue gets a certain amount of CPU time

The amount of time given to a queue is used to schedule the processes in that queue

a process enters a queue will remain there. System does not move it from queue to queue

Multilevel Feedback Queue

A process can move between the various queues

The idea is to Separate processes according to their CPU bursts characteristics

If a process uses too much CPU, it will be moved to lower priority queue

If a process stay too long to get CPU, move it to higher priority queue ( Aging )

Multilevel-feedback-queue scheduler defined by the following parameters :

operating systems 17
number of queues

scheduling algorithms for each queue

method used to determine when to upgrade a process

method used to determine when to demote a process

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

Chapter 4 ( Threads )
4.1 Introduction

A Thread is the unit of execution within a process.

A Thread is the basic unit of CPU utilization.

Threads Called lightweight processes (LWPs) are independently scheduled parts of a single program.

A Thread Comprises

Thread id

Program counter

Register set

operating systems 18
Stack

Shares with other Threads its

Code section

data section

Operating system resources

A traditional process has a single thread of control ( one task at a time ).

if a process has multiple threads of control, it can perform more than one task at a time. ( Kernels and Most modern
applications )

Thread is a mechanism to provide multiple execution control to the processes.

Single and Multithreaded Processes

4.2 Benefits of multithreaded programs

Responsiveness → multithreading an interactive application may allow a program to continue running even if part of it is blocked
or is performing a lengthy operation, thereby increasing responsiveness to the user.

Resource Sharing

Threads share the memory and the resources of the process to which they belongs.

allows an application to have several different threads of activity all within the same address space. ( efficiency , easy
interaction )

Economy → Thread creation has less overhead than process creation

Scalability → Threads can run in parallel on a multiprocessor machine

Utilization of Multiprocessor Architectures

the benefits of multithreading can be greatly increased in a multiprocessor architecture, where each thread may be running in
parallel on a different processor.

a single threaded process can only run on one CPU, no matter how many are available

4.3 Multi-core programming challenges

Identifying tasks divide program into tasks that can run in parallel

Balance ensure that these tasks perform equal work of equal value

Data splitting divide data between tasks that run on separate cores

Data dependency examine data to find dependencies between tasks

Testing and debugging testing a program with many execution paths

4.4 Concurrency vs Parallelism

operating systems 19
Concurrency

Supports more than one task making progress

When two or more tasks can start, run, and complete in overlapping time periods )

Single core

Parallelism

The concept of running multiple tasks simultaneously

Tasks are split into subtasks that are executed at the same time on different cores or processors.

Multiple cores

Types of Parallelism

Data parallelism distributes subsets of the same data across multiple cores, same operation on each.

Task parallelism distributing tasks across cores, each thread performing unique operation

operating systems 20
4.5 Multithreading Models

User threads

management done by user-level threads library

managed without OS support

Three primary thread libraries : POSIX Pthreads , Windows threads , Java threads

Kernel threads

supported and managed directly by the OS

most modern OSs support kernel threads ( windows , Linux , … )

Multithreading Models

Many-to-One Model

Many user-level threads mapped to single kernel thread

Thread management is done in user space, so it is efficient

only one user thread can access the kernel at a time , multiple threads are unable to run in parallel in multiprocessors

the entire process will block if a thread makes a blocking system call

One-to-One Model

Each user-level thread maps to kernel thread

multiple threads can access the kernel at a given time , multiple threads can run in parallel in multiprocessors

Creating a user-level thread creates a kernel thread ( can burden performance )

when a thread makes a blocking system call, another thread can run

Many-to-Many Model

Allows many user level threads to be mapped to many kernel threads

multiple threads can access the kernel at a given time , multiple threads can run in parallel in multiprocessors

OS creates set of kernel threads ( less than or equal to user threads )

operating systems 21
Two-level Model

Similar to M:M, except that it allows a user thread to be bound to kernel thread

4.6 Threading Issues

Thread Cancellation

Terminating a thread before it has finished

Thread to be canceled is target thread

Two general approaches :

Asynchronous cancellation terminates the target thread immediately

Deferred cancellation allows the target thread to periodically check if it should be cancelled

Invoking thread cancellation requests cancellation, but actual cancellation depends on thread state

If thread has cancellation disabled, cancellation remains pending until thread enables it

operating systems 22
Chapter 5 ( Concurrency and Process Synchronization )
Introduction

Concurrency

Concurrent : processes are executing at the same time

Concurrency can be viewed at different levels ( Principles of concurrency are similar across these categories )

Software view

multiprogramming : interaction between multiple processes running on one CPU ( pseudo parallelism )

multithreading : interaction between multiple threads running in one process

Hardware view

multiprocessors : interaction between multiple CPUs running multiple processes / threads ( real parallelism )

multicomputers : interaction between multiple computers running distributed processes / threads

Cooperating processes

Cooperating processes need inter process communication

Cooperating processes share a specific region of memory They can directly read from and write to this shared memory space (
Shared memory )

Concurrent access to shared data may result data inconsistency

Example

operating systems 23
count++ could be implemented as count-- could be implemented as

register1 = count register2 = count


register1 = register1 + 1 register2 = register2 - 1

count = register1 count = register2

Situations like this where processes access the same data concurrently and the outcome of execution depends on the particular
order in which the access takes place is called a Race condition

Hence we need Process Synchronization

A Critical Section is a segment of code, in which the process may be accessing / updating data that is shared with other processes

Critical Section Problem

The important feature of the system is that, when one process is executing in its critical section, no other process is to be
allowed to execute in its critical section

When a process executes code that manipulates shared data (or resource), we say that the process is in it’s critical section

Each process must request permission to enter its critical section.

The section of code implementing this request is the entry section.

The critical section may be followed by an exit section.

operating systems 24
Requirements for a valid solution to the critical section problem

Mutual Exclusion : If process is executing in its critical section, then no other processes can be executing in their critical
sections.

Progress : only those processes that are not executing in their remainder sections can participate in deciding which will enter
its critical section next, and this selection cannot be postponed indefinitely

Bounded Waiting : After a process has made a request to enter it’s CS, there is a bound on the number of times that the other
processes are allowed to enter their CS

Types of solutions

Hardware solutions

Software solutions

Operation System solutions

Hardware Solutions

interrupt disabling

Uniprocessor System

Mutual Exclusion is Preserved

Efficiency Degraded While a process is in its critical section, other processes that don’t need the critical section are also
blocked.

Multiprocessor System

Critical Section is now atomic ( uninterrupted ) but not mutually exclusive

Disabling interrupts only affects the processor executing the instruction. Other processors in a multiprocessor system can
continue executing may also access the shared resource

Not an acceptable solution

operating systems 25
Special hardware instructions are atomic ( execute as one uninterruptible unit )

Test and Set instruction

There is a shared lock variable which can take either of the two values 0 (unlocked) , 1(locked)

Before entering into the critical section, a process inquire about the lock

if it is locked it keeps on waiting till it becomes free , if it is not locked it takes the lock and and executes the critical section

static bool TestAndSet(ref bool target)


{
bool oldValue = target; // Get the current value of the lock
target = true; // Set the lock to "locked" (true)
return oldValue;
}

Assume first that the lock initial value = False ( The lock is open )

do {
// Try to acquire the lock (wait if already locked)
while (TestAndSet(ref lockPtr))
{
// Do nothing (busy-wait)
}
// Critical section
// Release the lock
lockPtr = false;
// Remainder section
} while (true);

Compare and Swap instruction

Shared Lock Variable: Initialize a shared lock variable which can take values 0 (unlocked) or 1 (locked).

Compare and Swap Function: a function that takes three arguments: Value , the expected value , and the new value. It
atomically compares the lock's current value with the expected value. If they match, it sets the lock to the new value and
returns the original value.

static int CompareAndSwapMethod(ref int value, int expected, int newValue)


{
int temp = value;
if (value == expected)
{
value = newValue;
}
return temp;
}

Value ( the memory location of the lock ), the expected value ( 0 ) , and the new value ( 1 )

Entering Critical Section:

A process repeatedly calls compare and swap function.

If the return value is 0, the process acquired the lock and can enter the critical section.

If the return value is 1, the lock is already held by another process, and the process continues to loop until the lock
becomes available.

operating systems 26
while (true)
{
while (CompareAndSwap(ref lockFlag, 0, 1) != 0)
; // do nothing ( busy - wait )
// Critical section
lockFlag = 0;
// Remainder section
}

Disadvantages

Busy-waiting consumes processor time.

Starvation is possible when a process leaves a critical section and more than one process is waiting.

Deadlock

Peterson’s Solution

A classic software-based solution to the critical section problem

Peterson’s solution is restricted to two processes that alternate execution between their critical sections and reminder sections

The two processes share two variables:

int turn indicates whose turn it is to enter the critical section.

bool flag[2] array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is
ready

Assume P( i ) is 1 and P( j ) is 2

while (true)
{
flag[i] = true;
turn = 2; // Give priority to process Pj
// to enter the critical section if it also wants access

// Wait until process Pj is not in its critical section or it is Pi's turn


while (flag[j] && turn == 2)
{
// Busy waiting
}
// Critical section
// Indicate that process Pi is leaving the critical section
flag[i] = false;
// Remainder section
}

while (true)
{
flag[i] = true;
turn = 1; // Give priority to process Pi
// to enter the critical section if it also wants access

// Wait until process Pj is not in its critical section or it is Pi's turn


while (flag[j] && turn == 2)
{
// Busy waiting
}

operating systems 27
// Critical section
// Indicate that process Pj is leaving the critical section
flag[i] = false;
// Remainder section
}

Semaphore

Semaphore is a synchronization tool

Semaphore is simply a variable which is non-negative and shared between threads. this variable is used to solve the critical section
problem and to achieve process synchronization

A Semaphore S is an integer variable that is accessed only through two standard atomic operations wait( ) and signal ( )

wait( ) → P( )

P from the dutch word Proberen “ to Test ”

wait(S) {
while (S <= 0)
; // busy wait
S--;
}

signal ( ) → V( )

V from the dutch word Verhogen “ to increment ”

signal(S) {
S++;
}

wait( ) and signal( ) operations must be executed atomically. That is, when one process modifies the semaphore value, no

other process can simultaneously modify that same semaphore value

Types of Semaphore

Binary semaphore

integer value can range only between 0 (resource is unavailable) and 1 (resource is available). one resource

Also known as mutex locks

Semaphore S; // initialized to 1
wait(S);
// Critical Section
signal(S);

Counting Semaphore

integer value can range over an unrestricted domain

Counting semaphores can be used to control access to a give resource consisting of a finite number of instances

Semaphore S; // initialized to the number of resources available


wait(S);
// Critical Section
signal(S);

When the count for the semaphore goes to 0, all resources being used. After that, processes that wish to use a resource it will be put
in a blocked queue of processes waiting for the same event until the count becomes greater than 0 To avoid busy waiting.

Some Problems when using semaphore

operating systems 28
Classical Problems of Synchronization

Bounded-Buffer Problem ( Producer-Consumer )

There is a buffer of n slots each slot is capable of storing one unit of data.

There are two processes running → Producer and Consumer

The Producer tries to insert data into an empty slot of the buffer

The consumer tries to remove data from a filled slot in the buffer

The Problems

The Producer must not insert data when the buffer is full.

The Consumer must not remove data when the buffer is empty.

The Producer and Consumer should not insert and remove data simultaneously.

Solutions to bounded - buffer problem

1. Count Solution

int count keeps track of the number of full buffers. initialized to 0

It is incremented by the producer after it produces a new buffer

It is decremented by the consumer after it consumes a buffer

while (true) {
// produce an item and put in nextProduced
while (count == BUFFER_SIZE)
; // do nothing

operating systems 29
buffer [in] = nextProduced;
in = (in + 1) % BUFFER_SIZE; // circular buffer
count++;
}

while (true) {
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
// consume the item in nextConsumed
}

2. Semaphore Solution ( we will make use of 3 semaphores )

m(mutex) a binary semaphore which is used to acquire and release the lock

empty a counting semaphore whose initial value is the number of slots in the buffer, since initially all slots are empty

full a counting semaphore whose initial value is 0

Implementation of wait and signal in the semaphore section

do {
wait (empty); // wait until empty > 0 then decrement empty
wait (mutex); // acquire lock
// add the item to the buffer
signal (mutex); // release lock
signal (full); // increament full
} while (true);

do {
wait (full); // wait until full > 0 then decrement full
wait (mutex); // acquire lock
// remove an item from buffer
signal (mutex); // release lock
signal (empty); // increment empty
} while (true);

Readers and Writers Problem

A data set is shared among a number of concurrent processes

Readers only read the data set; they do not perform any updates

Writers can both read and write

if two readers access the shared data simultaneously, no affects will result

if a writer and some other ( reader or writer ) access the data simultaneously, a chaos may ensue

Solution to the Readers-writers using Semaphore

we will make use of 2 semaphores and an integer variable

mutex a semaphore ( initialized to 1 ) which is used to ensure mutual exclusion when readcount is updated

wrt a semaphore ( initialized to 1 ) common to both readers and writers processes

readcount an integer variable ( initialized to 0 ) that keeps track of how many processes are currently reading the
object

operating systems 30
do {
// writer requests for critical section
wait(wrt);
// writing is performed
signal(wrt); //releasing the wrt
} while (true)

do {
wait(mutex); // Lock to update readcount
readcount++; // increase the number of readers
if (readercount == 1) {
wait(wrt); // First reader blocks writing or If there is a writer already
}
signal(mutex) // release the lock so other readers can enter
// reading is performed
wait(mutex); // Lock to update readcount
readcount--; // a reader wants to leave
if (readcount == 0) {
signal(wrt); // writers can enter if there is no readers left
}
signal(mutex); // release the lock
} while(true)

Dinning Philosopher

Philosopher either

Thinks → when a philosopher thinks, he does not interact with his colleagues

Eats

when a philosopher gets hungry he tries to pick up the two forks that are closest to him ( Left & Right )

A philosopher may pick up only one fork at a time

one cannot pick up a fork that is already in the hand of a neighbor

when he has finished eating, he puts down both of his forks and start thinking again

Philosopher represent processes & the chopsticks ( forks ) represent resources

One simple solution is to represent each fork with a semaphore

operating systems 31
A philosopher tries to grab a fork by executing a wait( ) operation

he releases his fork by executing a Signal( ) operation

Thus the shared data are Semaphore chopsticks[5] which are all elements of chopsticks initialized to 1

Do {
wait (chopstick[i]);
wait (chopStick[(i + 1) % 5]);
// eat
signal (chopstick[i]);
signal (chopstick[(i + 1) % 5]);
// think
} while (true) ;
/* Although this solution guarantees that no two neighbors are eating simultaneously
it could still create a deadlock */

Preemptive and Non-preemptive Kernel

Preemptive Kernel Non-preemptive Kernel

preempts a process while it is running in kernel mode allows a process to run until it exits kernel mode, block

Must be carefully designed to ensure that kernel shared data Because there is max one process in kernel mode, it is free from
are free from Race conditions Race conditions

More suitable for real time programming as it allows


preemption

Chapter 6 ( Deadlock )
Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource
acquired by some other process.

Each process utilizes a resource as follows:

request

use

release

Deadlock Conditions

Deadlock can arise if four conditions hold simultaneously

Mutual exclusion : only one process at a time can use a resource

Hold and wait : a process holding at least one resource is waiting to acquire additional resources held by other processes

No preemption : a resource can be released only voluntarily by the process holding it

Circular wait : there exists a set {P0, P1, …, Pn } of waiting processes such that P0 is waiting for a resource that is held by
P1, P1 is waiting
for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is
held by P0.

Resource-Allocation Graph

A set of vertices V and a set of edges E

V is partitioned into two types:

P = {P1, P2, …, Pn} / T = {T1,T2,…,Tn} the set consisting of all the processes / Threads in the system

R = {R1, R2, …, Rn}, the set consisting of all resource types in the system

edges E

request edge – directed edge Ti → Rj

operating systems 32
assignment edge – directed edge Rj → Ti

Example

deadlock vs no deadlock

If graph contains no cycles → no deadlock

If graph contains a cycle 

if only one instance per resource type, then deadlock

if several instances per resource type, possibility of deadlock

Example with Deadlock

Example with No Deadlock

operating systems 33
Methods for Handling Deadlocks

Ignore the problem and pretend that deadlocks never occur in the system

Allow the system to enter a deadlock state and then recover

Ensure that the system will never enter a deadlock state :

Deadlock Prevention

Deadlock Avoidance

Deadlock Prevention

Invalidate one of the four necessary conditions for deadlock

Mutual Exclusion

not required for sharable resources

must hold for non-sharable resources

Hold and Wait


must guarantee that whenever a process requests a resource, it does not hold any other resources

1. Require process to request and be allocated all its resources before it begins execution

2. allow process to request resources only when the process has none allocated to it

Low resource utilization , starvation possible

No Preemption

If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all
resources currently being held are released

Preempted resources are added to the list of resources for which the process is waiting

Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting

Circular Wait

Impose a specific order for all resource types, and ensure that each process requests resources in that order

Deadlock Avoidance and Safe State

Deadlock Avoidance ( Requires that the system has some additional a priori information available )

Simplest and most useful model

Requires that each process declare the maximum number of resources of each type that it may need

The deadlock-avoidance algorithm dynamically examines the Resource-allocation state to ensure that no circular wait can
occur

The Resource-allocation state is defined by

The number of available resources

The number of resources already allocated to processes

The maximum resource demands of each process

Safe State

When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state

A system is in a safe state if there exists a sequence of processes <P1,P2,…,Pn> such that for each process Pi :

The resources that Pi can still request can be satisfied by the currently available resources plus the resources held by
all previous processes Pj, where j < i

If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished

When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate

operating systems 34
When Pi terminates, Pi +1 can obtain its needed resources, and so on

If a system is in safe state → no deadlocks

If a system is in unsafe state → possibility of deadlock

Avoidance → ensure that a system will never enter an unsafe state.

Deadlock Detection

Single Instance of Each Resource Types

Maintain wait-for graph

Nodes are processes

Pi → Pj if Pi is waiting for Pj

Periodically invoke an algorithm that searches for a cycle in the graph. If there is a cycle, there exists a deadlock

operating systems 35
Recovery from Deadlock

Process Termination

1. Abort all deadlocked processes

2. Abort one process at a time until the deadlock cycle is eliminated

In which order should we choose to abort?

1. Priority of the process

2. How long process has computed, and how much longer to completion

3. Resources the process has used

4. Resources process needs to complete

5. How many processes will need to be terminated

6. Is process interactive or batch?

Resource Preemption

Selecting a victim : which resources and which processes are to be preempted ? ( minimize cost )

Rollback : return to some safe state, restart process for that state

Starvation : same process may always be picked as victim, include number of rollback in cost factor

Pick to kill criteria’s

Sharing many deadlocks

Process that does not make any updates

Process has least running time

Chapter 7 ( Memory Management )


Background

operating systems 36
Base and Limit Registers

operating systems 37
base and limit registers define the logical address space for a process

When a logical address is generated, the value from the base register is added to it to convert it into a physical address

Together, they ensure memory protection, separating each process’s memory space from others to avoid unauthorized memory
access

The base register holds the starting address of a process's memory segment.

The limit register specifies the size of that segment.

Address Binding

The mapping of data and instructions to actual memory locations

Types of Address Binding

Compile Time Load Time Execution Time

Binding is delayed until runtime, allowing the


The process’s memory location is known at The memory location is not known at compile
process to be moved between memory segments
compile time time, so relocatable code is generated
during execution

Occurs After the program is loaded into


Occurs During compilation Occurs During program execution
memory but before execution

The program can be relocated at load time. If


The program’s memory location is fixed during
the starting address changes, the program can The process can be moved dynamically during
compilation. If the starting address changes,
be reloaded with the new address. execution.
recompilation is needed.

Logical vs. Physical Address Space

Logical address

generated by the CPU

also referred to as virtual address

Physical address

address seen by the memory unit

They are the same in compile-time and load-time address-binding schemes

They differ in execution-time address-binding scheme

Memory-Management Unit ( MMU )

MMU is the hardware device that maps virtual to physical address

The value in the relocation register ( base register ) is added to every address generated by a user process at the time it is sent to
memory

The user program deals with logical addresses, it never sees the real physical addresses

operating systems 38
Swapping

A process can be swapped temporarily out of memory to a backing store, and then brought back into memory for continued
execution

Backing store : fast disk large enough to accommodate copies of all memory images for all users

Roll out, roll in : swapping variant used for priority-based scheduling algorithms

System maintains a ready queue of ready-to-run processes which have memory images on disk

Swapped process should be idle

I/O problem

Assume that the I/O operation is queued because the device is busy. If we were to swap out process P1 and swap in process P2,
I/O operation might then attempt to
use memory that now belongs to process P2

Solution

Keep job in memory while it is involved in I/O

The major part of the swap time is transfer time

operating systems 39
Contiguous Allocation

Main memory usually two partitions

Resident operating system, usually held in low memory with interrupt vector

User processes then held in high memory

Relocation registers ( Base and Limit ) used to protect user processes from each other and from changing operating-system code
and data

MMU maps logical address dynamically, Can then allow actions such as kernel code being transient and kernel changing size

Multiple-partition allocation

Hole : block of available memory

When a process arrives, it is allocated memory from a hole large enough to accommodate it

Operating system maintains information about

allocated partitions

free ( Hole ) partitions

How to satisfy a request of size n from a list of free holes

First-fit : Allocate the first hole that is big enough ( Fastest )

Best-fit : Allocate the smallest hole that is big enough

Worst-fit : Allocate the largest hole

First-fit and best-fit better than worst-fit in terms of speed and storage utilization

operating systems 40
Fragmentation

External Fragmentation : total memory space exists to satisfy a request, but it is not contiguous

Internal Fragmentation : allocated memory may be slightly larger than requested memory. this size difference is memory
internal to a partition, but not being used

Reduce external fragmentation by compaction

Shuffle memory contents to place all free memory together in one large block

EXAMPLE

operating systems 41
Logical address space of a process can be noncontiguous

Segmentation ( non-Contiguous )

Memory-management scheme that supports user view of memory

A program is a collection of segments

A segment is a logical unit such as:

main program, procedure, function, method, object, local variables, global variables, common block, stack, symbol table,
arrays

Segmentation Architecture

Logical address consists of a two tuple: <segment-number, offset>

Segment table maps two-dimensional physical addresses <segment-number, offset> each table entry has:

base : contains the starting physical address where the segments reside in memory

limit : specifies the length of the segment

Segment-table base register (STBR) points to the segment table’s location in memory

Segment-table length register (STLR) indicates number of segments used by a program

Sharing

shared segments → same segment number

Allocation

first fit/best fit → external fragmentation

Since segments vary in length, memory allocation is a dynamic storage-allocation

EXAMPLE

operating systems 42
Physical Address = Base + Offset ( if offset < Length / Limit )

Paging ( non-Contiguous )

process is allocated physical memory whenever the latter is available

Divide physical memory into fixed-sized blocks called frames ( size is 2n )

Divide logical memory into blocks of same size called pages

To run a program of size n pages, need to find n free frames and load program

Set up a page table to translate logical to physical addresses ( OS maintains a page table for each process )

internal fragmentation → allocated memory may be slightly larger than requested memory. this size difference is memory internal
to a partition, but not being used

operating systems 43
Logical Address is divided into :

Page number ( p ) – used as an index into a page table which contains base address of each page in physical memory

Page offset ( d ) – combined with base address to define the physical memory address that is sent to the memory unit

Page table Contains the frame location for each page in the process

Paging hardware

Paging EXAMPLE ( without offset )

operating systems 44
Problem

Assuming a 1-KB page size, what are the page numbers and offsets for the following address references (provided as decimal
numbers):
a. 3085
b. 42095

Answer

Page number = Logical address / page size ( integer )

page offset = Logical address % page size

a. page number = 3085 / 1024 = 3 , page offset = 3085% 1024 = 13

b. page number = 42095 / 1024 = 41 ,page offset = 42095 % 1024 = 111

Calculating internal fragmentation EXAMPLE

operating systems 45
Shared Pages

Shared code

One copy of read-only (reentrant) code shared among processes (i.e., text editors, compilers, window systems).

Shared code must appear in same location in the logical address space of all processes

Private code and data

Each process keeps a separate copy of the code and data

The pages for the private code and data can appear anywhere in the logical address space

Shared pages example

Implementation of Page Table and TLBs

Page table is kept in main memory

Page-table base register (PTBR) points to the page table

Page-table length register (PRLR) indicates size of the page table

In this scheme every data/instruction access requires two memory accesses. ( One for the page table and one for the data/instruction
)

can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers
(TLBs)

Associative Memory / TLB

parallel search

TLB has a key ( page number ) and value ( frame number )

If the page number is found in the TLB ( TLB Hit )

The corresponding frame number is retrieved instantly

If the page number is not in the TLB ( TLB miss )

The system retrieves the frame number from the page table in main memory

Value is loaded into TLB for faster access next time

operating systems 46
Page Size

Smaller page size, less amount of internal fragmentation But more pages required per process means larger page tables

Page Table Structure

Hierarchical Paging

Break up the logical address space into multiple page tables

Two-Level Paging Example

A logical address (on 32-bit machine with 4K page size) is divided into:

a page number consisting of 20 bits

a page offset consisting of 12 bits

Since the page table is paged, the page number is further divided into:

p1 is an index into the outer page table ( a 10-bit page number )

p2 is the displacement ( offset ) within the page of the outer page table ( a 10-bit page offset )

logical address

Address-Translation Scheme

operating systems 47
Two-Level Page-Table Scheme

Three-level Paging Scheme

A logical address (on 64-bit machine with 8K page size) is divided into 3 levels :

Hashed Page Tables

Common in address spaces > 32 bits

The virtual page number is hashed into a page table. This page table contains a chain of elements hashing to the same location

Virtual page numbers are compared in this chain searching for a match

If a match is found, the corresponding physical frame is extracted

operating systems 48
Inverted Page Tables

One entry for each real page of memory ( frame ) instead of one entry foreach virtual page

Entry consists of the virtual address of the page stored in that real memory location, with information about the process that
owns that page

Decreases memory needed to store each page table ( Only one page table in the system ), but increases time needed to search
the table when a page reference occurs

Use a hash table to limit the search to one or, at most, a few page-table entries

Demand Paging

A page is brought into memory only when needed

Why ?

Less I/O needed

Faster response, because we load only first few pages

Less memory needed for each process 

More processes can be admitted to the system

How ?

Process generates logical (virtual) addresses which are mapped to physical addresses using a page table

operating systems 49
If the requested page is not in memory, kernel brings it from hard disk by making a page fault

Valid-Invalid Bit

Memory protection implemented by associating protection bit with each frame Valid-invalid bit

Each page table entry has a valid–invalid bit ( Initially, it is set to i for all entries )

v → in-memory

i → not-in-memory

it could be :

Illegal reference ( outside process’ address space ) → abort process

Legal reference but not in memory → page fault (bring the page from the disk

Page table when some pages are not in main memory

Page fault

When a process references a page that hasn't been brought into memory, the first reference causes a page fault ( trap to operating
system )

1. Operating system looks at another table to decide:

Invalid reference → abort

Just not in memory

2. Get empty frame

3. Swap page into frame

4. Reset tables

5. Set validation bit = V

6. Restart the instruction that caused the page fault

operating systems 50
Handling a Page Fault

What happens if there is no free frame? Page replacement

Page Replacement

is used to find some page in memory, but not really in use, then swap it out

Basic Page Replacement

1. Find the location of the desired page on disk

2. Find a free frame

If there is a free frame, use it

If there is no free frame, use a page replacement algorithm to select a victim frame

3. Bring the desired page into the (newly) free frame. update the page and frame tables

4. Restart the process

Page Replacement Algorithms

First-In-First-Out (FIFO) Algorithm

operating systems 51
It Replaces page that has been in memory the longest ( oldest page ).

it focuses on the length of time a page has been in memory rather than on how much the page is being use.

Its behavior is not particularly well suited to the behavior of most programs. Few programs use it.

Example

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

3 frames (3 pages can be in memory at a time per process)

remove 1 ( oldest
pages remove 2 remove
page )

1 1 1 1 4 4 4

2 - 2 2 2 1 1

3 - - 3 3 3 2

page fault ? yes yes yes yes yes yes

9 page faults

4 frames

pages

1 1 1 1 1 1 1

2 - 2 2 2 2 2

3 - - 3 3 3 3

4 - - - 4 4 4

page fault ? yes yes yes yes no no

10 Page faults

Belady’s Anomaly : more frames → more page faults

Advantages

Easy to understand and implement

Disadvantages

Performance may not always be good

It suffers from Belady’s anomaly

Optimal Page Replacement Algorithm

It replace page that will not be used for longest period of time

Optimal Algorithm is difficult to implement, because it requires future knowledge of the reference string

has the lowest page-fault rate of all algorithms and will never suffer from Belady’s anomaly.

Used for comparing algorithms

Example

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

4 Frames

pages

1 1 1 1 1 1 1

2 - 2 2 2 2 2

3 - - 3 3 3 3

4 - - - 4 4 4

page fault ? yes yes yes yes no no

6 Page faults

operating systems 52
Least Recently Used (LRU) Algorithm

Use past knowledge rather than future.

Replace page that has not been used in the most amount of time.

Generally good algorithm and frequently used

Example

12 Page faults

better than FIFO but worse than OPT

Paging vs Segmentation

Paging Segmentation

memory is partitioned into fixed-size pages memory is partitioned into the variable size segments.

with the help of hardware page size is determine the user gives the size of the segments

faster than the segmentation

there may be a chance of internal fragmentation there may be a chance of external fragmentation

the logical address is partitioned into the page number and the the logical address is partitioned into the Segment number and the
page offset Segment offset

Segmentation with Paging – MULTICS

The MULTICS system (Multiplexed Information and Computing Service) solved problems of external fragmentation and lengthy
search times by paging the segments.

The solution differs from pure segmentation in that the segment-table entry contains not the base address of the segment, but rather
the base address of a page table for this segment.

operating systems 53
Chapter 8 ( Virtualization )
Definition
Fundamental idea behind a virtual machine is to abstract hardware of a single computer ( CPU, Memory,…) into several different
execution environments.

creating the illusion that each environment is running on its own private computer.

Virtualization can be seen as a layered approach, where a layer creates virtual systems on which operating systems or applications
can run.

Components
Host : The physical hardware system

Virtual machine manager VMM / hypervisor : Creates and runs virtual machines (VMs) by providing an interface identical to the
host

Guest : A virtual copy of the host, usually an operating system (OS)

A single physical machine can run multiple operating systems concurrently, each in its own virtual machine.

Types of Hypervisors
Type 0 hypervisors Type 1 hypervisors Type 2 hypervisors

Applications that run on top of a standard


Hardware-based solutions, implemented in Operating-system-like software built
operating system, provide VMM features to guest
firmware specifically for virtualization.
operating systems

Runs directly on physical hardware Runs directly on physical hardware Runs on top of a host operating system

Direct interaction with hardware, no host OS Direct interaction with hardware, no host OS Interacts with hardware indirectly through the host
required required OS

Guests are unaware they are running in a


Guests do not know they are running in a VM. Host OS is unaware of the VMM running guests.
VM.

I/O is a challenge as difficult to have enough


devices
Efficient I/O handling, optimized for data
VMM implements a control partition running Relies on host OS for I/O, less efficient.
centers.
daemons ( background service ) that other
guests communicate with for shared I/O

Advanced features like live migration, resource Easy to install and use, no changes required to the
Can run other hypervisors inside a VM.
balancing, Cloning and snapshots. host OS.

operating systems 54
Benefits and features
Isolation and Security : Host system is protected from VMs and VMs are protected from each other. Viruses are less likely to
spread.

Flexibility

VMs can be frozen, suspended, moved, copied, and resumed.

Snapshots can be taken of a system’s state, allowing for restoration back to that point.

Cloning : Ability to create copies of VMs

Multiple OS Support : run multiple different operating systems on a single machine

Templating : You can create an OS + application VM, provide it to customers, use it to create multiple instances of that
combination

Live Migration : Running VMs can be moved between hosts with no interruption of user access

Virtualization Techniques
Paravirtualization

Technique in which the guest operating system is modified to work in cooperation with the VMM to optimize performance.

where the guest OS interacts with virtualized hardware as if it's real hardware, in paravirtualization, the guest OS is aware of the
virtualization layer.

Programming-environment virtualization

Instead of virtualizing hardware, VMMs create an optimized virtual system for specific languages.

used by Java ( through the JVM ).

Emulators

Allow applications written for one hardware environment to run on a very different hardware environment, such as a different
type of CPU.

Application containment

Provides virtualization-like features by segregating ( ‫ ) فصل او عزل‬apps from the OS for improved security and
manageability.

Building Blocks and Methods of Virtual Machines


Generally difficult to provide an exact duplicate of underlying machine

SO

Most VMMs implement a Virtual CPU (VCPU) to represent the state of the CPU for each guest .

The VMM loads and stores the information in the VCPU when switching a guest's context onto the physical CPU.

Methods of virtual machines

Emulation

the virtualizing software mimics( ‫ ) يقلد أو ُي حاكي‬hardware portions for the guest OS in the VM.

Guest OS interacts only with the emulated hardware.

Typically implemented via interpretation ( slow performance ).

because every instruction from the guest OS is translated by the VMM to work on the physical hardware.

Trap and Emulate

takes the basic of the emulation but improves performance by using interpretation selectively.

The guest OS runs in user mode, but when it tries to access kernel mode, it triggers a "trap" to the hypervisor.

operating systems 55
Instead of interpreting every instruction like in emulation, only privileged instructions that require kernel mode access are
intercepted and handled by the hypervisor.

Binary Translation

Solves issues with CPUs that lack clean separation between privileged and non-privileged instructions.

Special instructions, like popf , behave differently in user and privileged modes and don't generate traps, making them
unsuitable for Trap and Emulate

The VMM examines instructions ahead of the program counter.

Regular instructions execute natively ( directly on physical hardware )

special instructions are translated into equivalent instructions ( that can be executed directly ) without waiting for a trap

Code translation is done in the VMM

caching improves performance by reusing translations for repeated instructions.

Hardware Assistance

Hardware support improves virtualization performance and stability.

Intel’s VT-x and AMD-V instructions :

eliminate the need for binary translation.

define more CPU modes guest and host.

Privileged instructions trigger traps to the VMM for handling.

The CPU manages VCPU and performs context switching between the guest and host.

operating systems 56

You might also like