1.2.1 Operating systems
An operating system (OS) is software that controls the computer. It manages the computer's hardware and the programs running on it.
Operating systems have many roles, such as:
- resource management
- file management - keeping a directory of where various files are stored so they can be quickly accessed, and allowing the movement, editing, and deletion of files and file folders
- interrupt management
- security - the OS may provide a firewall, encryption services and anti-virus/ anti-malware tools
- providing a platform for software to run
- providing a user interface, allowing users to interact with their computer's hardware
- providing utilities (small application with a single function to help configure, optimise and maintain a computer, e.g disk defragmentation, backing up and restoring data etc)
Memory management is one of the key function of an OS. It is necessary to:
- organise the use of the main memory (by converting logical addresses to physical addresses)
- allow programs to share to memory/ to allocate memory between programs
- keep data in the memory secure, as programs shouldn't access the data of other programs
- allow programs larger than the main memory to run
An OS may use two processes to manage memory:
- paging - memory is physically divided into equally sized sections of a fixed size, called pages. Programs are allocated an appropriate number of pages for their size, and are divided into their allocated pages.
- segmentation - memory is logically divided into unequally sized sections, called segments. The program will then be divided logically, such as into modules and routines. Logical operations are kept together, such as statements in a loop.
Both paging and segmentation:
- are ways of partitioning / splitting up memory
- use virtual memory to swap parts of programs
- allow programs larger than memory to run
- allows programs to be stored in memory non contiguously
When there isn't enough RAM to store all the running programs, the system will use an area of the secondary storage as virtual memory as an extension of the computer's RAM. Sections of programs which are not currently in use are moved into the virtual memory by paging or segmentation, freeing up space in the RAM for other programs. These sections are swapped back to the main memory when/if they are needed.
Virtual memory is used because RAM is more significantly more expensive than secondary storage, so a system would have far more secondary storage than RAM. However, if sections are swapped back and forth too frequently, the computer slows down. This is called dish thrashing and it gets progressively worse as the virtual memory fills up.
An interrupt is a signal which can be sent to the CPU to request immediate attention. Computers use interrupts for multiple reasons:
- to obtain processor time for a higher priority task
- to avoid delays
- to avoid the loss of data
- as an indicator to the processor that a device needs to be serviced
There are a number of sources of interrupts:
- if an input/output (I/O) operation is complete
- if an I/O device has an error (e.g printer out of ink)
- if there is a power failure, the computer would need to shut down as safely as possible
- a timer may trigger interrupts regularly to indicate that it is the turn of the next process to have processor time (processor scheduling)
- if there is an error in a program
At the end of each FDE cycle, the CPU checks for any interrupts. If there are, it compares the priority of the interrupt and of the current task. If the interrupt is of a higher priority than the current task:
- The current contents of the special purpose registers in the CPU are copied to an area of memory called a stack
- The appropriate interrupt service routine (ISR) is loaded into the RAM, by loading the relevant value into the PC
- The interrupt service routine is the program code called when an interrupt is triggered; each interrupt will have a specific ISR associated with it. When the ISR is run, it carries out the appropriate actions to handle the situation that triggered the interrupt
- When the ISR is complete, the interrupt queue is rechecked, and any further interrupts of higher priority than the original task are serviced
- If there are no remaining interrupts of a higher priority to be processed, the contents of the stack will be transferred back into the registers, and the FDE cycle resumes as before.
If multiple interrupts occur at the same time, the higher priority interrupt will be serviced first. If a higher priority interrupt is raised while an interrupt is being serviced, the interrupt currently being processed will be added to a stack while the new, higher priority interrupt is serviced.
Scheduling is the method used by the OS to manage the amount of processing time given to different processes. It is necessary for many reasons:
- to make the most efficient use of the computer's resources/processor
- to ensure all jobs are processed
- to be fair to all users/applications
- to provide a reasonable response time
- to process as many jobs as possible in the least possible time
- to prevent processes from failing to run
Scheduling algorithms may be preemptive or non-preemptive:
- In preemptive scheduling algorithms, the operating system controls when jobs are started and stopped
- Examples include round robin, multi-level feedback queues and shortest time remaining
- In non-preemptive scheduling algorithms, once a job starts, it is left to complete
- Examples include first come first served and shortest job first
The different scheduling algorithms work in different ways, and each has benefits and drawbacks for tasks with specific characteristics:
- Round Robin
- Each job is allocated a time slice (a very small time where it runs on the CPU)
- After each time slice, the system moves onto the next job
- If a job is uncompleted once it has used its time slice, it is given another and moved to the back of the queue
- Completed jobs are removed from the queue
- In turn, the process is repeated for all jobs
- All jobs will eventually be attended to, however long jobs will take much longer to complete
- First Come First Served
- Jobs are processed in the order in which they arrive
- Simple to implement, but doesn't take into account job priority/urgency
- Multi-level Feedback Queue
- Multiple queues of varying levels of priority are used
- Jobs can be moved between the queues
- The jobs in the highest priority queue are processed first, followed by the other queues in decreasing levels of priority
- Accounts for different job priorities, but is difficult to implement
- Shortest Job First
- Processes the jobs in ascending order of how long it will take to complete the job
- Reduces the average waiting time, but requires knowledge of how long each job will take and and doesn't account for different job priorities
- Shortest Remaining Time
- Processes the jobs in ascending order of the estimated time until completion of the job
- This can be different from shortest job first scheduling if a job with a long total time has a short amount of time left until completion, which can occur in shortest remaining scheduling but not shortest job scheduling first as shortest remaining time is preemptive
- Short processes can be completed very quickly, but it doesn't account for different job priorities
There are a number of different types of operating systems:
- Distributed
- Runs across multiple devices (nodes), allowing all nodes to work together as a single system
- The load of a task is spread across multiple processors, so tasks can be completed more quickly and efficiently
- Distributed operating systems are useful for tasks which require lots of computing power, by allowing multiple computers to work on the same task, using their collective resources
- Embedded
- Software used to manage an embedded system (specialised devices that perform a specific task), so it is usually developed to perform a small number of very specific tasks
- It is built into the device itself, and stored in the ROM
- Multi-tasking
- Allows multiple programs to be used simultaneously
- Uses separate windows for each task
- Each task is given an amount of processing time before moving onto the next task
- The processor is so fast that it seems all the tasks are done simultaneously
- Multi-user
- Multiple users can use the same system and its resources simultaneously
- The computer has multiple terminals (device/software which allows the user(s) to interact with the OS)
- Each terminal is given a time slice (very small amount of processing time) in turn
- Each user's data is kept separately
- May provide user privileges, controlling which actions each user is allowed to perform
- Multi-user operating systems are used on larger, more powerful mainframe computers able to service large numbers of users at the same time
- Real time
- Performs actions in a guaranteed, very short amount of time, in response to any inputs/sensors
- Can handle multiple simultaneous inputs
- Has failsafe mechanisms in case of component failures
- Likely to have interrupt handling to quickly deal with external events
- Real-time operating systems are used in applications where timeliness is critical, such as in safety-critical systems
Exam questions may require a type of operating system to be recommended for a specific scenario, with justification required for the choice made.
The Basic Input Output System (BIOS) is the first program that runs when a computer is first turned on.
- It is usually stored in the ROM, but may also be stored on flash memory to allow the program to be updated or to allow boot setting to be changed by the user
- t is responsible for booting the computer (it is a boot program)
- It runs the power-on self test, which ensures that all the necessary hardware is correctly connected and functional
- The processor, memory, CPU clock and other components are checked
- External memory devices are checked for
- Finally, it loads [some of] the operating system into the RAM, and starts the OS, which then takes over the boot process
A device driver is software that controls a piece of hardware and allows the operating system to communicate with the hardware device. The driver is needed as it allows the operating system to communicate with the hardware device without knowing the details of the hardware device (there are many different hardware devices and the OS cannot possibly know the details of each one).
Drivers are specific to a computer's architecture and to the operating system.
A virtual machine (VM) is a theoretical computer on which programs can run. They have the same functionality as a physical computer. They have many advantages:
- they can be backed up and duplicated
- multiple can be run at the same time on the same physical machine
- they provide a degree of isolation between the host computer and programs running on the VM, which can be useful for security purposes
VMs can be used to execute intermediate code which is run on the VM using a translator:
- Intermediate code is partially translated code (between high-level code and machine code), which has been produced by a compiler, and can be run on any computer/VM by using an appropriate translator
- This increases the portability of the code/program
- It allows sections of code to be written in different languages, and by different programmers (different languages may be more suitable for specific tasks)
- However, the intermediate code takes longer than machine code to execute
VMs can also be used to run an operating system within another:
- The VM will act as if it were a machine running the other operating system
- This allows the program to be tested for different operating systems using only one physical computer, saving the costs and time associated with setting up multiple different systems
- It can also allow someone to use programs that aren't designed for their primary OS
- However, this requires a powerful computer, as the computer must run both its own OS and the VM
- It may also be more difficult to set up a VM than to set up a new physical system