Operating systems and utilities
What an operating system does, scheduling, and utility software.
Do this lesson in the simulatorWhen you run a program, you never tell the processor which memory to use, how to talk to the camera, or how to share time with the other programs running. The operating system does all of that. This lesson looks at what an operating system does, then at utility software, the programs that keep a computer running well.
What an operating system does
The operating system (OS), such as Windows, macOS, Android, or Linux, is system software that manages the computer's hardware and runs the other programs. Its main jobs:
| Job | What it means |
|---|---|
| User interface | lets the user control the computer: a graphical interface with windows and icons, a command line, or a touch screen |
| Memory management | decides which programs and data go where in RAM, keeps programs from using each other's memory, and moves data to virtual memory when RAM is full |
| Multitasking | shares the processor between the running programs, switching between them so quickly they seem to run at once |
| Peripheral management and drivers | talks to devices such as printers, cameras and keyboards through device drivers, small programs that translate between the OS and each device |
| User management | user accounts, passwords, and who is allowed to use which files |
| File management | organises files into folders, names them, and lets them be moved, copied and deleted |
Firmware for ESP32 chips like BugBot's is usually built on FreeRTOS, a small real-time operating system. It has no windows or user accounts, but it still shares the processor between tasks: the motor loop, the camera, the Wi-Fi link, and your program.
Sharing the processor
A single core can only run one instruction at a time. To multitask, the OS gives each program a short time slice, then switches to the next. Taking turns like this is called round robin scheduling:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
jobs = {"music": 3, "browser": 5, "download": 2} # time slices each still needs
queue = list(jobs)
clock_tick = 0
while queue:
name = queue.pop(0)
jobs[name] = jobs[name] - 1
clock_tick = clock_tick + 1
print(f"tick {clock_tick}: {name}")
if jobs[name] > 0:
queue.append(name) # not finished: back of the queue
else:
print(f" {name} finished")
Utility software
Utility software is system software that helps to maintain, protect or tidy the computer. The ones on the exam:
- Defragmentation: on a hard disk, files end up split into pieces scattered across the disk, which slows reading because the head has to move around. A defragmenter moves the pieces of each file back together, and free space together. SSDs should not be defragmented: they have no moving head to benefit, and the extra writes wear them out.
- Compression: makes files smaller to save storage space or send them faster, as in lesson F8.9.
- Encryption: scrambles files so only someone with the key can read them, which module F11 covers.
- Backup: copies files to another place so they can be restored if the originals are lost. A full backup copies everything; an incremental backup copies only what has changed since the last backup, which is faster but needs every backup to restore.
- Antivirus / anti-malware: scans for and removes malicious software.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# a disk with files split into pieces: each letter is a block of that file, . is free
disk = "AAB.C.BBA..CA.B"
print("before:", disk)
tidy = "".join(sorted(ch for ch in disk if ch != ".")) + "." * disk.count(".")
print("after: ", tidy)
Task: a round-robin scheduler
Write a round-robin scheduler with a time slice of 2 units. Each job in jobs needs some units of processor time. Take the job at the front of the queue, run it for 2 units or for what it has left, whichever is less, and print <name> ran for <n>. If it is not finished, put it at the back of the queue; if it is, print <name> finished at <t>, where t is the total time so far.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
jobs = [("motors", 3), ("camera", 5), ("radio", 2)]
time_slice = 2
Challenges
- Try a time slice of 1 and of 10. Which is fairest? Which switches least?
- Give each job a priority and always run the highest priority job that is waiting.
- Make an incremental backup: given yesterday's files and today's, print only the files that are new or changed.