A2 Chapter 4 System Software & VMs
A2 Chapter 4 System Software & VMs
Layered structure: each higher layer needs to be fully serviced by a lower layer.
1
There are two modes of Operating System:
2) Kernel mode: it has sole access to part of the memory and to certain system functions that user
mode cannot access
One operating system task is to maximize the utilization of computer resources. Resource
management can be split into three areas:
1. the CPU
2. the memory
3. the input/output (I/O) system
Resource management of the CPU involves the concept of scheduling to allow for better utilization
of CPU time and resources
Resource management of memory involves optimum usage of main memory.
any I/O operation which has been initiated by the computer user.
any I/O operation which occurs while software is being run and resources, such as printers or
disk drives, are requested.
The bus structure in following diagram shows that there can be an option for the transfer of data
between an I/O device and memory. The operating system can ensure that I/O passes via the CPU
but for large quantities of data the operating system can ensure direct transfer between memory
and an I/O device:
It must be understood that one second is a very long time for a computer system.
A CPU typically operates at GHz frequencies.
One second sees more than one trillion clock cycles.
The slow speed of I/O compared to a typical CPU clock cycle shows that management of CPU
usage is vital to ensure that the CPU does not remain inactive while I/O is taking place.
2
The Kernel:
The kernel is part of the operating system.
It is the central component responsible for communication between hardware, software and
memory.
It is responsible for process management, device management, memory management,
interrupt handling and input/output file communications as shown below:
One of the most important tasks of an operating system is to hide the complexities of the
hardware from the users in the following ways:
Example:
Transferring data from a hard disk to a floppy disk using an old computer required a command such
as:
copy C:\windows\[Link]
whereas latest/modern computers use a drag and drop method which removes any of the
complexities of interfacing directly with the computer.
3
One of the main tasks of an operating system is resource management.
Description of how an operating system can maximize the use of resources:
1) Primary memory:
The OS can move frequently accessed instructions to cache for faster recall as SRAM is used
rather than DRAM for cache.
It can make use of virtual memory with paging or segmentation to swap memory to and from
a disk.
It can partition memory by dividing main memory into static/dynamic partitions to allow for
more than one program/task to be available (multiprogramming).
It can remove unused items/tasks from RAM by marking a partition as available as soon as
the process using it has terminated.
2) Disk:
The OS can do disk caching so that a disk cache holds data that is frequently transferred
to/from the disk and the cache can be held on disk or in RAM.
It can use compression utility to decrease the size of a file stored on disk in order to fit
more/larger files on the disk.
It can use defragmentation utility so files are rearranged to occupy contiguous disk space
which reduces the time taken to access files (decreases latency)
It manages the execution of many programs that appear to run at the same time
Multitasking ensures the best use of computer resources by monitoring the state of each
process. It should give the appearance that many processes are being carried out at the same
time.
4
The following table summarizes the differences between preemptive & non-preemptive:
To ensure multitasking operates correctly (for example, making sure processes do not clash),
scheduling is used to decide which processes should be carried out.
5
Process Scheduling:
Scheduling is managing the processes running on the CPU.
Process scheduling is the process of removing the running task from the processor and
selecting another task for processing.
It schedules a process into different states like ready, waiting, and running.
Role of the high-level scheduler in a multiprogramming operating system:
It decides which processes are to be loaded from backing store into memory/ready queue
Role of the low-level scheduler in a multiprogramming operating system:
It decides which of the processes in ready state should get use of processor/be put in running
state based on position/priority
It is invoked after interrupt/OS call.
Why an operating system needs to use scheduling algorithms // why process scheduling is
needed:
Process states:
A process control block (PCB) is a data structure which contains all of the data needed for a process
to run. This can be created in memory when data needs to be received during execution time.
1) Ready
2) Running
3) Blocked
6
A process can be in one of three states: running, ready or blocked.
1) Ready:
7
The following diagram shows the simplified link between these states:
The following diagram shows the detailed link between these states along with conditions
that cause each change of state of a process:
8
For each of the following, the process is moved from the first state to the second state.
The following are the conditions that cause each of the following changes of the state of a
process:
The process is executing (running state) but it needs to perform I/O operation.
It is placed in blocked state until I/O operation completed.
4) From blocked to ready:
For the processor to be in blocked state, a process must initiate some I/O operation.
The process must be executing to initiate operation and if process is in ready state then it
cannot be executing.
Therefore, the process must be in running state to go to the blocked state.
Why a process cannot be moved directly from the blocked state to the running state:
When the I/O operation is completed for a process in blocked state then it is put into the
ready queue/ready state.
The OS then decides which process should be allocated to processor choosing from the ready
queue.
Conditions that are necessary for a process to move from the ready to the running state.
9
A process in the running state can change its state to something which is neither the ready
state nor the blocked state.
These are some of the most common strategies used by schedulers to ensure the whole system is
running efficiently and in a stable condition at all times.
List of criteria used to determine the process/schedule which will be processed using
scheduling algorithms:
10
The scheduling algorithm can be preemptive or non-preemptive. A preemptive algorithm can halt a
process that would otherwise continue running undisturbed. If an algorithm is preemptive then it
may involve prioritizing processes.
FCFS is non-preemptive.
The data added to a queue first is the data that leaves the queue first and therefore it is first
to enter the running state.
This is similar to the concept of a queue structure which uses the first-in first-out (FIFO)
principle.
This is useful for long jobs but otherwise very inefficient if it is the only algorithm employed.
It can be useful as a part of a more complex algorithm.
2) Shortest job first scheduling (SJF):
SJF is non-preemptive.
It sorts jobs in the ready queue in ascending order of time needed by each job.
The new jobs are added to the queue in a such a way as to preserve this ascending order.
The process requiring the least CPU time is executed first in SJF.
This is useful for short jobs as they are processed first.
SRTF is preemptive.
The processes are placed in the ready queue as they arrive but when a process with a shorter
burst time arrives, the existing process is removed (pre-empted) from execution.
The shorter process is then executed first.
This is useful for short jobs as they are processed first. For SJF, the short jobs are processed
first depending on time required and they will remain sorted in that order. While SRT will
mean that once an interrupt occurs, the queue will be reshuffled again with the shortest job
first and hence SRTF favors short jobs more than SJF.
RR is preemptive because a process will be halted when its time slice has run out.
It gives every job a maximum time slice after which the job is placed in the blocked queue.
Then another process from the front of ready queue is executed in its own time slice.
Context switching is used to save the state of the pre-empted processes.
The ready queue is worked out by giving each process its time slice in the correct order.
If a job is completed before the end of time slice, it leaves the system and then the next
process is brought into the ready queue.
This is useful for distributing work evenly among all available resources.
11
Interrupt handling and OS kernels
Interrupt:
It is a signal from a software source or hardware device seeking the attention of the processer.
Interrupt Handling:
The system will enter the kernel mode if any of the following type of interrupt signals are
sent:
1. Device interrupt (for example, printer out of paper, device not present, and so on).
2. Exceptions (for example, instruction faults such as division by zero, unidentified op code, stack
fault, and so on).
3. Traps/software interrupt (for example, process requesting a resource such as a disk drive).
How the kernel of the OS acts as an interrupt handler and how interrupt handling is used to
manage low-level scheduling:
1. When an interrupt is received, the kernel will consult the interrupt dispatch table (IDT) – this
table links a device description with the appropriate interrupt routine.
2. IDT will supply the address of the low level routine to handle the interrupt event received.
3. The kernel will save the state of the interrupt process on the kernel stack and the process
state will be restored once the interrupting task is serviced.
4. Interrupts will be prioritized using interrupt priority levels (IPL)(numbered 0 to 31).
5. A process is suspended only if its interrupt priority level is greater than that of the current
task.
6. The process with the lower IPL is saved in the interrupt register and is handled (serviced) when
the IPL value falls to a certain level.
12
The following diagram summarizes the interrupt process:
13
Exam Style Questions:
Question 1:
Answer:
Blank 1 JOB32
Blank 2 JOB42
Blank 3 JOB42
Question 2:
A computer process can be in one of three states: running, ready or blocked. Explain how the
processes are affected when the following events take place:
14
Question 3:
The operating system is managing the multitasking of these tasks. At one point in time:
Identify the process state for each task. Give a reason why each task is in that process state.
(iii) Sending:
Question 4:
The weather station computer uses an operating system. Explain how this operating system
uses interrupts to schedule the measuring and recording tasks.
15
Question 5:
For each of the following, the process is moved from the first state to the second state.
Describe the conditions that cause each of the following changes of the state of a process:
1) From running to ready:
When process is executing it is allocated a time slice (running state).
When time slice completes, interrupt occurs and process can no longer use processor even
though it is capable of further processing (ready state).
2) From ready to running
The process is capable of using processor (ready state).
The OS allocates processor time to process so that it can execute (running state).
3) From running to blocked
The process is executing (running state) but it needs to perform I/O operation.
It is placed in blocked state until I/O operation completed.
Question 6:
For each of the following, the process is moved from the first state to the second state.
Describe the conditions that cause each of the following changes of state of a process:
1) From blocked to ready
The process is waiting for I/O operation to complete (blocked state).
When the I/O operation is completed, process goes into ready queue (ready state).
2) From running to ready
When process is executing it is allocated a time slice (running state).
When time slice completes, interrupt occurs and process can no longer use processor even
though it is capable of further processing (ready state).
Question 7:
Explain why a process cannot move directly from the ready state to the blocked state.
For the processor to be in blocked state, a process must initiate some I/O operation.
The process must be executing to initiate operation and if process is in ready state then it
cannot be executing.
Therefore, the process must be in running state to go to the blocked state.
Explain why a process cannot be moved from the blocked state to the running state.
When the I/O operation is completed for a process in blocked state then it is put into the
ready queue/ready state.
The OS then decides which process should be allocated to processor choosing from the ready
queue.
16
Question 8:
State the conditions that are necessary for a process to move from the ready to the running
state.
Question 9:
A process in the running state can change its state to something which is neither the ready
state nor the blocked state. Name this state.
Exit/termination/completion
Identify when a process would enter this state.
The process would enter the exit/termination/completion state when the it has finished
execution.
17
Memory management:
When a process starts up, it is allocated memory; when it is completed, the OS deallocates memory
space.
The following methods are used for memory management to allocate memory to different
processes/programs and data.
With this method, all of the memory is made available to a single application.
This leads to inefficient use of main memory.
2) Paging (paged memory):
An operating system (OS) uses a memory management technique called paging. The process of
paging is given below:
The main memory is divided into equal-size blocks, called page frames.
Each process that is executed is divided into blocks of the same size, called pages.
When a process is executed, process pages from logical memory are loaded into frames in
physical memory and not all pages of program need to be initially loaded.
Each process has a page table that is used to manage the pages of this process.
The page table maps logical addresses to physical addresses.
Key terms along with their descriptions:
(i) Paging:
The main memory is divided into page frames of the same size as a page.
(iii) Page table:
The Page (Map) table shows the mapping of pages to page frames.
18
The page table will show:
1. page number
2. flag status
3. page frame address
4. the time of entry (for example, in the form 08:25:55:08).
The time of entry is important when considering page replacement algorithms.
Some of the page table status flags are shown in the table below:
19
The following diagram only shows page number and frame number (we will assume status flags
have been set and entry time entered). Each entry in a page table points to a physical address that
is then mapped to a virtual memory address – this is formed from offset in page directory + offset
in page table:
The logical address space is divided into variable-size memory blocks/partitions called
segments.
Each segment has a name and size.
Segments from logical memory are loaded into physical memory for execution and not all
segments are initially loaded.
The address is specified by the user which contains the segment name and offset value.
The segments are numbered (called segment numbers) rather than using a name and this
segment number is used as the index in a segment map table.
20
The segment map table given below contains the segment number, segment size and the start
address in physical memory:
The memory management methods of segmentation and paging work in almost similar ways with a
few differences such as pages are usually smaller than segments.
The following table summarizes the differences between paging and segmentation:
21
Linking & Loading:
The key difference between linking and loading is that the linking generates the
executable file of a program whereas, the loading loads the executable file obtained
from the linking into main memory for execution.
It means that the code for all routines called by a program becomes part of the executable
file.
It means that the code for some external routines is located and loaded when the program
is first run.
It is loading of the entire program into main memory before start of the program execution.
Program execution will be faster.
(iv) Dynamic loading:
22
4) Virtual memory:
Virtual memory is basically using secondary storage to stimulate additional main memory.
The disk/secondary storage is used to extend the RAM/memory available so the CPU can
access more memory space than available RAM.
The only part of program/data in use needs to be in RAM.
The data is swapped between RAM and disk.
Physical memory is managed using virtual memory and paging. The following points describe
how paging is used to manage virtual memory:
1. The programs can be larger than physical memory and can still be executed.
2. It leads to more efficient multi-programming with less I/O loading and swapping programs
into and out of memory.
3. There is no need to waste memory with data that is not being used (for example, during error
handling).
4. It eliminates external fragmentation/reduces internal fragmentation.
5. It removes the need to buy and install more expensive RAM memory.
1. Disk thrashing
Disk thrashing:
The pages are required back in RAM as soon as they are moved to disk.
There is a continuous swapping of the same pages.
This results in no useful processing (deadlock) because the pages that are in RAM and on disk
are inter-dependent.
This results in nearly all processing time to be used for swapping pages.
23
The following diagrams show differences between paging without virtual memory and paging
using virtual memory:
The virtual memory management now moves the oldest data to disk and the 4 GiB map is
updated so that:
24
Page replacement:
Page replacement occurs when a requested page is not in memory (P flag = 0).
When a new page is requested but is not in memory, a page fault occurs and the OS replaces
one of the existing pages with the new page(s).
There are several methods for page replacement and its purpose is to minimize the number
of page faults.
Page fault:
A page fault occurs when there is an attempt to load an instruction for a page not in memory.
This generates an interrupt.
The interrupt service routine (ISR) code is executed.
This causes the OS to load the page into memory.
1. First-in-first-out (FIFO)
2. Least-recently-used page (LRU) // Least-used-page
3. Optimal page replacement (OPR)
4. Clock page replacement // Second-chance page replacement
1) First-in-first-out (FIFO):
The page which has not been used for the longest time is replaced.
It is necessary to maintain a linked list of all pages in memory with the most recently used
page at the front and the least recently used page at the rear.
The pages are replaced with the ones that will not be used for the longest duration of time
in the future.
The pages that will be referred farthest in the future are replaced in this algorithm.
It results in the least number of page faults.
25
4) Clock page replacement:
It uses a circular queue structure with a single pointer serving as both head and tail.
When page fault occurs, the page pointed to is inspected.
The action taken next depends on the R-flag status.
If R = 0, the page is removed and a new page is inserted in its place whereas if R = 1, the next
page is looked at and this is repeated until a page where R = 0 is found.
26
Exam Style Questions:
Question 1:
27
28
29
Answer:
30
Question 2:
31
32
33
Answer:
34
Question 3:
35
Answer:
36
Question 4:
Answer:
37
Question 5:
38
Answer:
39
Virtual Machine (VM):
It is a software/program that emulates a physical/different computer system (hardware set-
up).
A virtual machine allows multiple guest operating systems to run on one computer using a
host operating system.
1. New system can be tried on different virtual hardware without need to purchase the hardware.
2. It is easier to recover if software emulating the new computer causes system crash as VM
provides protection to other software.
3. It allows to emulate programs for the new computer system that are not compatible with the
host operating system by using the guest operating system on the old computer.
4. More than one new computer system can be emulated which allows multiple operating
systems to coexist on a single computer.
1. Virtual machines may not be able to emulate the new hardware because this hardware may
have been developed since the virtual machine was developed.
2. Using virtual machine means execution of extra code and so processing time is increased.
3. A virtual machine might not be as efficient because resources e.g. memory or processor time
are shared so processing time increases and performance degrades.
4. Use of a virtual machine increases the maintenance overheads because both host system and
the virtual machine must be maintained.
40
The following diagram shows how the hardware and operating systems are
linked together to form a virtual machine:
1. The 1st layer is for the applications running on a virtual machine system.
2. The 2nd layer is for the guest operating systems which are being emulated.
3. The 3rd layer is for the virtual machine software that manages the whole machine.
4. The 4th layer is for the host operating system that is actually controlling physical hardware.
5. The 5th layer is for the hardware.
Description of steps after Guest Operating System has received a data request from an
application which is executing:
41
Exam Style Questions:
Question 1:
Answer:
42
Question 2:
43
44
Answer:
45
Question 3:
46
47
Answer:
48
Question 4:
49
Answer:
50