# Operating Systems
### Why have an OS?

In the early days of computing, if a program needed any inputs or outputs, it would need to contain specific code to handle communication with input and output devices. Short subroutines were developed to carry out these basic tasks. Eventually these subroutines were joined together to make an Input Output Control System (IOCS). As the IOCS became more complex, it turned into an Operating System. As more programs began to be written in high level code, translators were needed, which were then also incorporated into operating systems.

#### Features of an OS

* Security - Provides encryption services and anti-virus/malware tools
* Utilities - Provides small applications with a single function, usually these are to do with the upkeep and maintenance of a system.
* Network Management - Provides facilities and software for communications over a network
* Device drivers - Inbuilt software used to communicate with specific hardware devices.
* Error Reporting - Storing the details of errors that occur in the OS/software, so developers are able to find the source of the errors (bugs).
* Memory Management - The allocation of memory for use by various programs, as well as storage and retrieval of data from memory.
* Resource Management - The management of limited resources, including CPU time, memory & disk space, electrical power, external devices and I/O devices.
* I/O Management - The management of input and output devices connected to a system.
* Device Management - Management of all connected devices, such as storage devices and graphics cards (this section includes I/O management)
* Interrupt Management - The management of interrupts.
* User Interfaces - Management of the User Interfaces (UI), for example a GUI (graphical user interface) for the end user and a CLI (command line interface) for a technician to diagnose faults.
* File Management(Allocation Tables) - A map of where various files are stored. It also contains various metadata, such as file names, access rights and file sizes. It is used by the OS when files are saved, deleted and accessed.
* Boot File - Used when the OS is loading, when the computer is switched on after its POST (power on self test), to provide personal settings.
* The Kernel - Manages the core operations of the OS, including scheduling, resource management and memory management. It lies below the user interface, for example Linux is the kernel, and the UI is separate, so the UI can be changed without affecting the OS.
Describe three typical functions of the operating system from the user’s point of view. [3]
Answer:
Three functions of the operating system from the users point of view – any three of: 3x1
* Provides access to hardware
* Provides different interfaces such as GUI / command line
* Allows customisation of interface e.g. change desktop colours / layout
* Allows copying / deleting / moving / sorting / searching of files or folders
* Controls security using passwords or access permissions
* Manages running of software / programs
* Provides utilities such as disc defragmentation
* Boots up / closes down the system
* Allows creation of shortcuts
* Allows user to run more than one program / have more than one window open
Accept any other valid point

## Types of Operating System

All operating systems provide the following functionality:
* They control the hardware of the system
** This includes memory, resources and device management
* They provide a UI for human access and communication
* They provide application software execution and management
** i.e. system installation, memory access, software translation via a compiler/interpreter/assembler
* They provide utilities to carry out computer "housework" and common tasks:
** File Handling
** Formatting
** Defragmentation
** Virus Protection
* They provide security and privacy to users:
** Backup procedures
** User logins and passwords
There are several different types of operating system:

### Multitasking OS

The user of a multitasking operating system is given the impression that the system can carry out more than one task at a time. This can be achieved by giving each task a very short time period with the CPU while rotating through different jobs, or by using multiple cores to run tasks concurrently.

### Multi-access/Multi-user OS

This operating system allows for multiple users to access the same system at the same time. This is achieved by giving each terminal a time slice and rotating through each terminals requests to the CPU. The CPU can differentiate between different users data and programs by storing them in different areas of its main memory. Flags prevent time slices from terminals with no task wasting the CPU's time, and priorities can increase the size or quantity of time slices a terminal is given.

### Distributed OS

This OS uses many different locations to store files and process data. It is software run over a collection of independently networked computational “nodes”. They handle jobs that are serviced by multiple separate CPUs.

### Real Time OS

With a Real Time OS, the data processed or the action taken at the time of input is quick enough to then affect the next output. It is defined as online, as the user can communicate directly with the processor. It is normally used when an immediate response is required, for example a nuclear power station.
A real time OS is guaranteed to give an output in a given time period (normally a short one).

#### Transaction Processing

This is similar to a real time OS, although not as immediate. It only allows pre-defined, structured transactions. Each transaction is usually short and the processing activity for each transaction is usually programmed in advance. It is largely used in databases.

#### Process Control

This OS is extensively used in industry, as it allows for mass production and continuous process systems. It can provide automation, which reduced the requirement for staffing. It may make use of a feedback or a continuous loop. The controls may be continuous (e.g. a cruise control system) or a sequence of discreet events (e.g. a sprinkler system). The sensors in this system do not directly control the outputs, they are all controlled and coordinated by the CPU.

### Embedded

An embedded OS is usually developed for a single task. It is usually device specific and operates on low powered CPUs and RAM. They can be found in TV's, washing machines etc. They are optimized for their task, so can run faster because they are simpler than a general OS.
Intensive Care Units in hospitals are for patients in need of round the clock monitoring and support. Computerised systems can be used to monitor patients’ vital signs (temperature, heart rate, blood pressure and breathing). They can then alert medical professionals to any significant changes.
These systems usually run on an embedded, real-time, operating system.
(a) (i) State what is meant by the term real-time.
(ii) Explain why a real-time operating system would be suitable for this purpose.
(b) (i) Many household appliances contain embedded microprocessors.
Explain why most of these microprocessors don’t need operating systems.
Answer:
(a) (i) The system gives a response within a guaranteed time frame (1).
For 1 mark.
(ii) If something happens to a patient, a response must be immediate (1). Other types of system may have delays in response (1). This could result in a patient not receiving treatment in time (1).
(b) Up to 2 marks for a valid explanation that demonstrates application of knowledge and understanding to given context.
Any two points from:
for example, a washing machine or microwave oven
ROM stores the whole instruction set
microprocessor has just one set of tasks to perform
system expects simple inputs (e.g. keypad on front of washing machine)
the microprocessor has simple, never-changing hardware functions to control [2]

## Memory Management
##### The Need for Memory Management

* The allocation of memory allows separate processes to run at the same time.
* It allows memory to be reallocated and shared when necessary.
* It protects different data and processes from each other, thus provides security

### Paging

With the physical process of paging, memory is split into equal sized sections, known as pages. The programs are then allocated the correct number of pages for their size. Pages are of a fixed size, so some programs may have excess memory that they do not require.

### Segmentation

Segmentation is a logical process, where the memory is split into variable sized blocks which reflect the logic of the program - these divisions are known as segments. This relies heavily on the OS and linkers and loaders. In this way, programs are less likely to have excess memory space allocated. It should be noted logical operations are kept together, for example a statements in a loop.

### Virtual Memory

When there is not enough RAM to store all the running programs, an area of the hard drive will be assigned as 'virtual memory'. Programs that have been inactive for the longest will be stored here, then loaded back into the RAM when needed. This allows a computer to multi-task using more, or bigger, programs than would normally be possible.
Explain how the following memory management techniques may be used:
[6]
Answer:
Paging
-memory is divided into equal-sized units called page frames
-program/data file is divided into equal-size units called pages
-one or more pages may be loaded into memory at any one time
-Pages may be discontiguous
-Pages swapped in and out as required
-pages not in main memory are stored in virtual memory/backing store
-page table/Index of pages/processes kept
-absolute address is calculated by adding page address to relative address in instruction
-paging is transparent to the programmer
Segmentation
-Memory is divided into variable length blocks
-Programs can consist of many segments
-Segments normally match natural divide in jobs/logical blocks
-Index of segments stored which must…
-store base address and length of segment
-programmer will organise code modules into segments
(1 per -, max 4 per dotty, max 6) [6]

## BIOS

The BIOS stands for Basic Input Output System. It is a small program stored on a ROM chip on the motherboard. It runs before the OS starts up, and is used to ensure the health of a computer system. The first thing that occurs when a computer turns on is a reset signal is sent to the CPU by the power supply, which tells the CPU to begin executing code from the ROM chip. This then runs a series of checks known as POST (Power On Self Test) which include:
* Checking system memory is OK.
* Checking system clock / timer is running.
* Checking processor is OK.
* Ensuring keyboard is present.
* Ensuring screen display memory is working.
* Checking the BIOS is not corrupted.
Once these checks are completed, the system will boot the OS using the bootloader to load the OS's kernel into memory. If these checks fail, the BIOS will use beep codes to inform the user of the error. Beep codes are specific to manufacturers so they are usually documented in their instruction manual.
When a computer is powered up, it performs a booting sequence. Explain the purpose of the BIOS in booting a computer [6]
Answer:
The BIOS provides the sequence (1) and initialisation tasks (1) for the boot-up process. Answers should list the checks that are made, e.g. memory, keyboard, and hard disk spin-up. Allow 1 mark each for a maxmum of 4 marks

## Device Drivers

Device drivers are small programs which accompany a piece of hardware associated with a computer. They are designed to inform the operating system about how to communicate with the peripheral devices.

## Virtual Machines

A virtual machine is a piece of software that allows a computer to emulate another machine. An advantage of a virtual machine is that it can be backed up and duplicated. You can even run more than one virtual machine on a single physical machine.
A virtual machine can be used to emulate another OS. This can allow a developer to test programs on multiple platforms, or can be used to run software that isn't supported on the host OS. Apple OSX, Ubuntu and Windows can all be virtualised. Whole networks can also be made up of virtual machines, which is often used by businesses and schools as it provides a cheap solution for providing many people a machine to work on.
Intermediate code is halfway between object code and machine code. It is faster to run than source code but is also highly portable, as it can run on many different platforms by using a virtual machine to read it. When a computer is powered up, it performs a booting sequence. Explain the purpose of the BIOS in booting a computer [6]
Spitfire Protection is a company that specialises in fire protection systems for educational institutions and hospitals. It is especially important in these types of premises that occupants clear the buildings in the event of fire alarm activation.
The computer system installed by the company informs fire marshals and fire wardens whether a specific area of the school or hospital is clear using round robin scheduling.
The company uses virtual machines for development purposes. Describe the impact on the development of software using virtual machines.' [9]
Answer:
Allow any reasonable answer relating to the use of different operating systems in virtual instances. Answers should include the potential for executing intermediate code Allow I mark for each valid answer.

## Interrupts

At any point in time, proccesses are either:
* Ready - The task is waiting to be run
* Running - The CPU is executing the task
* Blocked - The task is waiting for a device that is in use / unable to execute at this time
Interrupts allow ready applications to request CPU time.
An interrupt is a signal sent to the CPU which is generated by source such as an I/O device or a system software routine, which causes a break in the execution of the current routine. There are various different types of interrupt:
* I/O Interrupts - Generated by I/O devices to signal a job is complete or an error has occurred.
* Timer Interrupts - Generated by an internal clock to signal the processor should attend to a time-critical activity.
* Hardware Error - E.g. a power failure, where the computer must attempt to shut down as safely as possible.
* Program Interrupt - Generated due to an error in a program, such as a violation of memory usage.
An interrupt is detected by checking the interrupt register at the end of each FDE cycle. The system will compare the priority of the currently running task to the priority of the interrupt - if the current task has the higher priority, the current task will continue running. However if the interrupt has the higher priority, the Interrupt Service Routine (ISR) begins. It copies the contents of the CPU registers to a stack, then runs the interrupt until it is completed. Next, the interrupt's flag in the interrupt queue is reset, and the system checks for any further priority interrupts before restoring the contents of the registers from the stack. During storage, interrupts are stored in a queue, and their position in the queue determines their importance to the system, user and current job. Priorities are in place to allow the system to decide between an interrupt and the current task. It allows the system to choose between interrupts, if two occur at the same time. It ensures that the most urgent task is performed first, and efficient use of the processor. Notably, one interrupt could interrupt another if a higher priority interrupt was triggered before a lower priority interrupt was finished executing.
Operating systems use interrupts to manage tasks.
(i) A task can be in one of three states: ready, running or blocked. Briefly describe each of these states. [3]
(ii) What is a computer interrupt? [1]
(iii) Give two examples of computer interrupts. [2]
(iv) The operating system is processing an interrupt when another interrupt with a higher priority is generated. Explain in detail the sequence of events following the second higher priority interrupt [3]
[6]
Answer:
(i) Ready - The task is waiting to be run 1
Running - The CPU is executing the task 1
Blocked - The task is waiting for a device that is in use / unable to execute at this time 1
(ii) Interrupt - any one of: 1x1
* An interrupt is a signal generated by a device or software, which may cause a break in the execution of the current routine
* An interrupt is a signal generated by a device or software, requesting attention
NOTE – do not accept an interrupt is generated when there is a fault (this is an example)
(iii) Interrupts that could be generated – any two of: 2x1
* Hardware or Software fault
* Input/output device requesting attention e.g. printer out of paper / requesting more data / key press / mouse click
* User interrupt e.g. <ctrl> <break>
* Operating system generated interrupt e.g. end of time slice
* Run time error eg division by zero
Note – Answers may be a specific example of any of the above
(iv) Interrupt is generated – answers must show sequence of events
* O/S halts /current interrupt routine 1
* O/S runs new higher priority interrupt routine 1
* OS returns to original interrupt routine (or another interrupt routine) and continues 1

## Scheduling

Scheduling algorithms determine how to share out processing time.

### Levels of Scheduling

There are 3 different levels of scheduling:
* Low Level Scheduling - The operating system must have a method for deciding when a program is going to be given use of the processor. Scheduling algorithms place jobs in and out of the ready state and decides which order they are placed to run.
* Medium Level Scheduling - Swaps jobs between the memory and backing store.
* High Level Scheduling - This is the method by which a new program is loaded into memory.
There also 2 different types of scheduling:
* Non Pre-Emptive - The system places a job into the running state and leaves it there until the job no longer needs to run, but must go into another state, for example it enters the blocked state as an input is required from the user.
* Pre-Emptive - The system has the power to decide that the job currently running should be stopped to make way for another job.

### Scheduling Algorithms

First Come First Serve (FCFS) - The first job to enter the running queue is the first to enter the running state. This algorithm favours longer jobs.
Shortest Job First (SJF) - Sorts the jobs in the ready queue in ascending order of the time needed.
Round Robin (RR) - Each job is given a maximum amount of processor time (a time slice). After this time it will return to the queue. Once the job is finished, it leaves the queue.
Shortest Remaining Time (SRT) - The ready queue is sorted on the amount of expected time left. This favours shorter jobs as longer jobs may never be started.
Multi Level Feedback Queues - A number of different queues are used, each with a differing priority. The algorithm can move jobs between the queues depending on their behavior.
The purpose of scheduling is to ensure all jobs are processed, and change priorities where necessary. It ensures as many jobs are processed in the shortest amount of time, whilst maximising the number of users and the system response times. Its key goal is to utilise the processor efficiently.
Operating systems use scheduling.
(i) Explain the purpose of scheduling. [4]
(ii) Some multi-user operating systems use round-robin scheduling. Describe round-robin scheduling. [4]
iii) Describe one other scheduling method that may be used. [2]
Answer:
(a) (i) ensure all jobs are processed/changing priorities where necessary
process as many jobs as possible (in the least possible time)
maximise number of interactive users…
…receiving fast response times
utilise resources/processor time efficiently
Accept “to be fair to all users”
(ii) each user allocated a time slice
time slices are very small time/fractions of seconds
at end the time slice the user/job moves to back of queue
repeat this for all users in turn
order may depend on users’ priorities
users unaware of any delays preemptive scheduler
Accept answers based on jobs instead of users
(iii) first come, first served
jobs processed in order of arrival or system of priorities highest priority first or length of job
shortest job first
2 for FCFS, accept FIFO if described accept other examples