When a program is ran on a computer, it is copied from secondary storage into primary memory (RAM). A program stored in memory is referred to as a process. A process can be visible on screen (such as a web browser), but may also run in the background without a user interface (such as anti-malware software, or a music player) - these are sometimes called background processes or daemons.
When computers first became available they were only capable of running one process at a time and the user would need to close the process before another one could be loaded into memory, however nowadays computers are capable of holding many processes in memory and having the CPU work on different processes seemingly simultaneously.
While a multi-core CPU can execute instructions from one processes for each core it contains, a single core CPU can only execute instructions from one process at a time. The problem is that a computer may need to be running maybe as many as 100 processes at any one time function. In order for this to work and be seemless to the end user CPU must schedule how much time each process has to access the CPU.
Through scheduling the CPU can switch between each process thousands of times per second to make it appear to the user that all processes are being executed at the exact same time.
There are different options for scheduling process time on a CPU:
Scheduler | Description | Advantages | Disadvantages |
First In First Out (FIFO) | The processes and their instructions are executed in the order they arrive at the process queue | Processes are executed until they are finished, this reduces the need for task switching (which saves time) | If there are larger processes at the front of the process queue, shorter more critical processes will not run until they get to the front of the queue |
Shortest Job First |
The process which is closest to being completed goes first. |
Shorter processes get executed quickly. Reduces the need for task switching (which saves time) |
There is a potential that some longer processes never get CPU time if shorter processes keep getting added. |
Round-robin | Each process is allocated a slice of time on the CPU, if the process cannot be completed in that time it retruns to the process queue to await being allocated more time. In round-robin a process may be assigned higher or lower priority to allow processes to have more or less CPU time. |
Processes will always get time on the CPU, regardless of when they entered the queue or the processes size. Results in a good balance between number of processes completed in a given time regardless of size. |
Task switching can cause delays as processes have to be placed back in memory and other processes have to be retrived for their slice of time If there are many processes it may take some time for them to be scheduled time on the CPU |
For the most part, computers use schedulers based on the round-robin schedule.