What is an operating system?
What is an operating system?
Operating Systems are software systems that control the computer’s resources. It is software that allows applications to interact with the hardware on a computer. Software can interact with hardware directly. But most software isn’t written to a particular piece of hardware - the operating system takes care of interacting with the hardware.
The earliest computers did not have Operating Systems. Every program that ran on the computer had to include all of the code to run the computer and communicate with the hardware, as well as perform its actual function. This made each computer program complex and difficult to create. Each program could only run on the machine it was made for.
Before Operating Systems, computer programs were run by individuals. A literal queue of people would line up in front of a computer with their programs loaded on magnetic tape or punch cards. They would physically load their program into the machine and wait for it to finish. As computers became faster at processing information, the time it took an operator to load a program into a computer was sometimes longer than the program took to run. These operators would have to schedule different programs depending on the resources the programs needed.
Operating systems replaced these human operators by scheduling programs into memory. For a program to run, the following steps would occur:
- The program would be loaded into the computer.
- The operating system would reserve a section of RAM for the program.
- The program would be copied from magnetic tape or punch cards into the RAM.
- Once scheduled to run, the CPU retrieved the instructions from the memory location of the first instruction in the program.
- The program would then begin.
In later years, programs existed inside the computer on the hard drive (rather than loaded using punch cards or tape). As the storage space of RAM got bigger, multiple programs could be loaded into it at the same time. The Operating system would give a second program a different section of RAM.
This scheduling led to a problem called memory fragmentation.
Memory fragmentation occurs as programs load to RAM at different times, when space becomes available. Here’s an example of how it could occur:
1. Program A loads into address 0 in the RAM and given 5MB of RAM to run. Program B loads into address 5242880 and given 3MB of RAM to run.
2. Program A finishes. Program C (3MB) loads into address 0. As Program C doesn’t need 5MB, there’s now a gap in RAM.
3. Program D needs 5MB of memory. The gap of 2MB is too small, so Program D is placed somewhere else in RAM. The gap remains.
As more programs load and finish, more gaps occur. Eventually you will run out of memory, even though you have plenty of memory in these gaps.
The operating system solves this problem by using virtual memory to map physical memory.
Virtual memory is a memory management technique. A piece of hardware called a Memory Management Unit maps a virtual address () to a physical address.
When loaded, programs are given a virtual address of 0 to however much they need. The MMU puts the program wherever there is space on the RAM.
It can also split the program around the RAM, filling in any gaps.
Let’s look at the scenario above using the MMU and virtual memory.
1. Program A loads into address 0 in the RAM, and given 5MB of RAM to run. It’s given a virtual address of 0 to 5242880 Program B loads into address 5242880 and given 3MB of RAM to run. It’s given a virtual address of 0 to 3145728
2. Program A finishes. Program C receives a virtual address of 0 to 3145728. We place C in 0 in RAM. There’s now a gap in RAM.
3. Program D needs 5MB of memory. The gap is too small. Program D is split into two: 2MB are placed in 3145728, 3MB are placed in 8388608. Program D is given the virtual memory address of 0 to 5242880.
Using this technique of virtual memory, there are no gaps in RAM and you won’t run out of memory as easily as before.
Each program is self contained. It doesn’t write over any other program. It doesn’t matter where the program is in memory, the MMU does all the mapping. Programs no longer need to be in one continued block.
Other roles of an OS
As well as scheduling programs into memory, Operating Systems schedule how programs use the CPU (we’ll look at this in more detail in the next step). Operating systems also have a role in managing access to different levels of resources. This protects the integrity of the system - which we’ll also be looking at later.
Other roles of an operating system include:
- Interacting with hardware through drivers written for a specific piece of hardware and OS.
- Providing utility software, such as disk management or security software.
- Dealing with input/output events, like a key press.
Also, operating systems also need to provide user interfaces, allowing users to interact with them and start programs. The OS also manages the file structure on storage devices, as well as controlling which users have access to these files and programs.
How would an OS running on an automated factory machine differ from one running on a smartphone? Share your thoughts in the comments section.