The+Function+of+Operating+Systems

=The Function of Operating Systems=

//"In computing, an operating system (OS) is an interface between hardware and user, which is responsible for the management and coordination of activities and the sharing of the resources of a computer, that acts as a host for computing applications run on the machine." ** - wikipedia **//

Functions[[image:OSexamples.png align="right" caption="Windows, Linux and Android are all examples of operating systems."]]
An operating system must provide:
 * An interface between the user and the machine.
 * An interface between the hardware and the software.
 * Utility software to allow maintenance. (e.g. defragmention)
 * Security for the user. (e.g passwords, file access limitations)
 * A way to manage hardware resources. (e.g. memory management system)

Interrupt Handling
An interrupt is a special instruction for the processor which may stop other jobs to be run. There are a variety of different interrupts, each with different priorities, depending on what it is for. For example, a problem with the power supply would have a very high priority, as it could cause loss of data or even damage to parts of the computer. A software interrupt, however, is less likely to cause problems and will have a much lower priority. The priority of an interrupt is very important, as it decides whether it should be run instead of a different job or interrupt. If the priority is lower than the current job in the processor, it will wait until it is done before moving on. In the case of a high priority interrupt, the processor will have to stop the current job

Scheduling
Scheduling uses a set of rules from the OS to order jobs. It is useful because it avoids delay when there are lots of jobs and uses the processor's resources more efficiently. Jobs can be in 3 different states; **ready**, **running** or **blocked**. When a job is processed it enters the system and is placed in the ready queue, by the **High Level Scheduler.** The **Medium Level Scheduler** switches jobs between the main memory and backing store. The **Low Level Scheduler** moves jobs in and out of the ready state. For example, it moves the job at the front of the queue into the running state when another job stops running.These are all different sections of a main scheduling program. There are many different scheduling algorithms. Some of these are: > Served). If the job needs more processing time, it is placed in a queue with the next lowest priority. If the job is completed, it moves up a level. The lowest level is completed in a Round Robin algorithm.
 * Pre emptive** schedulers are allowed to move jobs out of the running state and back into the ready queue. Non pre emptive schedulers can only move jobs out of the running state if they are completed or if it needs a peripheral.
 * **First Come First Served** - The first job in the queue is the first to be put in the running state //(Better for long jobs because once they are running they have to be completed)//
 * **Round Robin -** Every process gets a small amount of processing time (time slice) before it moves on to the next one. When the job has completed it drops out of the queue
 * **Shortest Job First -** The jobs are sorted into ascending order of expected time of completion and the jobs are done in that order //(Better for short jobs)//
 * **Shortest Remaining Time -**The ready queue sorted into order of remaining time left and the jobs are done in that order //(Better for short jobs, but can be hard fo long jobs to ever get processing time)//
 * **Multi-Level Feedback Queues -** There are several queues with different priorities with jobs moving downwards. Each job is given a length of prcessing time. They are initailly completed FCFS (First Come First

Memory Management
Memory Management splits up memory so it can be used more efficiently. It allocates memory to different processes so more than one can run at the same time. It also protects the memory used by the operating system and other processes, so they don't interfere with each other and allocates free memory that was previously being used to a new process quickly.Programs are loaded into memory by the **loader** and the **linker** makes sure the memory location are right or if there are references in the program, they refer to the right memory locations. Memory can be split up in two different ways; **paging** or **segmentation:**
 * Paging:**
 * Memory is split into pages of equal size //(physical divisons)//
 * Pages are usually smaller than segments
 * Files that are bigger than the size of the page are put into multiple pages, but only one file can go in one page
 * If files are very large they can be many pages, and these pages may be scattered around the memory
 * It is easier for the computer to handle, but is less efficient as it fills disk space quickly and there can be gaps
 * It is easier for the computer to handle, but is less efficient as it fills disk space quickly and there can be gaps


 * Segmentation:**
 * Memory is split into unequal segments //(logical divisions)//
 * Different segments used for different purposes (e.g. code segment, data segment, stack segment)
 * It is more logical and uses the memory more efficiently, but is harder for the computer to control because of the different sizes

Pages (or segments) can be loaded in and out of memory when they are needed using **virtual memory.** When it is not needed it is moved to the hard disk by the loader, so it appears there is more memory than there really is. It allows progams that need more more memory than the computer has to run and allows multiple programs to run at the same time, when they would need much more memory if virtual memory wasn't being used. However it makes the process slower as pages need to be constantly loaded from the hard disk and swapped back and the disk is slower than the memory. There is also a chance that the processor spends more time loading and linking pages than it does actaully running the program. This is **disk theshing** (or disk thrashing) and is usually due to having too many programs running at once and not having enough memory to handle this.

Spooling

 * Spooling** is when output (eg. to a printer) is temporarily stored on the disk, so it can be sent to the device. It is used a buffer between the processor and the peripheral. For example, if a few people wanted to print something at the same time, the print jobs are stored in the buffer and completed in an order, so their processors can do other jobs while they are waiting for their printouts. Priorities can also be applied to this queue, so it isn't neccessarily in order of them being sent.
 * Spooling** is when output (eg. to a printer) is temporarily stored on the disk, so it can be sent to the device. It is used a buffer between the processor and the peripheral. For example, if a few people wanted to print something at the same time, the print jobs are stored in the buffer and completed in an order, so their processors can do other jobs while they are waiting for their printouts. Priorities can also be applied to this queue, so it isn't neccessarily in order of them being sent.

The actual files aren't stored in the buffer, but references are. **Pointers** are used to point to the correct locations and the buffer stores the order of them and send the files in the locations to the printer when their job is being completed. This means it takes less time as it is moving pointers around, not data.

Typical Operating Systems
When a computer is switched on it runs a **POST** (Power on self test). It clears the registers and loads the address of the first instruction of the boot program (which is stored in ROM). This checks the hardware and loads the BIOS structure. This is stored in the **boot file,** which is different from the boot program, as the boot file contains information about the system and can be altered, but the program loads the OS and can't be altered. The BIOS is stored in CMOS RAM, as it needs to be able to be changed by the user and it has a separate battery so it isn't switched off when the computer is. The boot program checks itself and the POST. During this, it has to:


 * find and verify the system memory
 * check all of the system buses
 * check the devices for booting
 * provide a UI for system configuration
 * construct an environment for the OS

The POST can be set up so that beep codes are used. These are a different number of beeps to signal a type of error. Usually a short beep means there are no errors.

Now the computer can laod the OS, which should be on the hard disk (although sometimes OS can be booted from other devices, like memory sticks). An inital kernel (the main part of the OS) is loaded, then hardware drivers and other configuration files, etc.

The OS has to find files on storage devices and this can be done by a **File Allocation Table.** The disk is divided into **sectors** and **tracks**. Groups of sectors are called **clusters**. The FAT shows how files are spread over these clusters. It is usually held in memory to speed up disk access.
 * File Allocation**