OCR A2 Computing Revision – 4.1.1, 4.1.2, 4.1.3

4.1.1 – The Main Features Of Operating Systems

The OS must manage hardware resources such as memory, and provide an interface between the user and the machine, and between applications software and the machine. It must also provide services such as data security. It also contains a number of subroutines that do simple tasks such as reading characters from keyboards or sending characters to printers. These basic input/output routines are joined together in the IOCS (Input-Output Control System).

Another important function of the OS is scheduling. To make full use of the processor, more than one program should be stored in memory and the processor should give time to each of the programs. If two programs are stored in memory and one of them is using input/output devices most of the time (Which are very slow in comparison to the processor), it makes sense to schedule the processor time to ensure that all of the jobs are completed efficiently so that the processor is used as much as it can be. It is the job of the OS to decide which processes get priority to complete all of the processes as efficiently as possible. There are a number of different methods that it can do this which we will learn about later.

While the processes are getting inputs or outputs they may hold up the processor as the I/O devices are very slow. This can be overcome by spooling (Simultaneous Peripheral Operations Online). Basically, all inputs and outputs are stored on a high-speed disk.

Input device -> Reading -> Input Spool -> Program -> Output Spool -> Writing -> Output Device.

This diagram shows the entire path from input to output. To start, a user presses a character on a keyboard. This is then read by the processor which uses some processing time. Then it is stored on the input spool, from which it can be read by the program. When the program is ready to output some data, it writes it to the output spool, which is then processed by the processor and sent to the output device, where it is output.

Another job of the OS is to manage the memory. It must make sure to allocate enough space to store everything in memory in as efficient a manner as possible, and have a record of where everything is. Suppose for example that on a multiuser system, user A and B are both using Microsoft Word. It is inefficient to store two copies of Word in the memory, so instead the OS will store one copy of Word that both users can access, and will store user A and user B’s data in different places such that user B can access Word and data B, but A can access Word and data A. Programs working in this way are called re-entrant.

4.1.2 – Interrupts

An interrupt is an instruction that interrupts the normal flow of the program.  For example, when an error occurs or when the power supply is unplugged.  There are four types of interrupts.

1) An IO interrupt, which tells the processor that a job is complete or an error has occurred, eg. a printer is out of paper.
2) A Timer interrupt, which is generated by an internal clock and tells the processor that more time critical activities need to be attended to, such as streaming video.
3) A Hardware error, such as a power failure which indicates that the OS must shut down as quickly as possible.
4) A program interrupt, which shows an error in a program such as division by zero or improper memory use.

Now the sequence of running a process is the following:

Start -> Fetch Instruction -> Execute Instruction -> Is there an interrupt? [If yes, service interrupt] -> Any more instructions? [If yes, go to 'Fetch Instruction'] -> End.

After the execution of every instruction, the OS has to check to see if an interrupt has occurred.  If it has, it must service the interrupt if it is a higher priority than the task already being carried out.  When an interrupt occurs, the OS has to arrange for the interrupted program to resume from where it left off.  To do this, the contents of all the registers in the processor have to be saved so the processor can service the interrupt.

What happens if an interrupt is interrupted by another interrupt? The simplest way to deal with this is to store the interrupts in an interrupt queue and return to the originally interrupted program when the queue is empty. The queue holds indicators to the next interrupt that needs to be serviced.

4.1.3 – Scheduling

Scheduling is one of the tasks of the OS.  It has to arrange the jobs that need to be done into an efficient order for processing.  There are different ways to schedule.  Suppose Program A is I/O bound and Program B is processor bound.  If program B has priority, it could be a long time before program A can do anything because program B will always be using the processor.  If program A has priority, program A will work when it needs to and let program B work when it is inputting and outputting, which is a lot of the time.  Program A doesn’t need the processor for much, but it does need it in between I/O.

The objectives of scheduling:

- Maximising the use of the system
- Fairness
- Providing reasonable response times
- Preventing system failure from overloads
- Making sure the system is consistent by giving similar response times to similar activities at different times.

There are a number of different criteria that can be used to determine a schedule:

- Priority – Giving some jobs greater priority than others.  Jobs with greater priority will use the processor when they need it and only let other jobs use the processor when they don’t need it.
- Bound – Whether the job is processor or I/O bound.  If a processor bound job is given priority, the I/O bound job may not run.
- Type of job – Online or realtime jobs may require a greater priority than batch processing as the first are more time sensitive.
- Resources required – The amount of time needed, memory required, processor time, etc.
- Resources used – Amount of processor time is used so far
- Waiting time – The amount of time that the job has been waiting.

Any job may be in one of three states.  A job may be ready to start, running, or blocked because it is waiting for a peripheral.  A job enters the system on Ready, and may move between Ready and Running, but can only move to Blocked from Running, and from Blocked only to Ready.  It can only finish by ending in Running.  On a standard single-processor computer, only one job can be running at a time.

When a job begins, it is placed in the Ready queue by the High Level Scheduler.  If jobs are to be swapped between the main memory and backing storage, this is done by the Medium Level Scheduler.  Moving jobs in and out of the Ready state is done by the Low Level Scheduler, which also decides which order the jobs are to run in.

In a Pre-emptive policy, the LLS can remove a job from the Running state so that another job can be placed in the Running state.  In a Non-Pre-Emptive policy, jobs can’t be removed from Running until they no longer require the processor, either when it ends or when it needs an I/O device.

There are numerous different policies that can be used in scheduling:

- FCFS (First Come First Served), in which the first job to enter the Ready queue is the first to enter the Running state.
- SJF (Shortest Job First), in which jobs have priority which is proportional to the amount of processing time required.  The problem with this is that a long job may never process.
- RR (Round Robin), in which all jobs are allocated a time slice in which to run.  Process 1 will run for a certain amoutn of time, then process 2, then process 3, then back to process 1 again, etc. so that it appears that the processes are all being processed at the same time.
- SRT (Shortest Remaining Time) – The ready queue is sorted based on the amoutn of time still required.  Like SJF, but takes into account remaining time rather than running time.
- MFQ (Multi-level Feedback Queues) – Several queues of different priorities are made, with jobs migrating downwards.

There are many other ways of allocating priorities.  Safety critical and time sensitive jobs must have high priorities.  In this scheme, any job can only use the processor if and only if all higher priority jobs are completed.