Introduction to Operating Systems

Introduction to Operating Systems


What happens when a computer program run?


Why do we need OS?


How do the OS help (1)?

This is possible due to virtualization.


How do the OS help (2)?

Hands-on: Getting started

1
2
cd ~/ostep-code/intro
make

Hands-on: CPU Virtualization

CPU limit

In docker-compose.yml, the amount of cpus made available to the containers are only 2:

1
2
3
4
deploy:
  resources:
    limits:
      cpus: 2.0
1
./cpu A & ./cpu B & ./cpu C &./cpu D 
1
ps aux | grep cpu
The illusion of infinite CPU resources
  • A limited number of physical CPUs can still be represented as infinite number of CPUs through virtualization.
  • The OS will manage the scheduling and allocation of the actual run on physical resources.

Hands-on: Memory Virtualization

1
2
3
clear
sudo setarch `uname -m` -R /bin/bash
./mem 100 &./mem 200
Do programs running concurrently occupy the same memory locations (addresses)?

No

The illusion of dedicated memory resources
  • Many running program share the physical memory space.
  • Each runnning program is presented with the illusion that they have access to their own private memory. This is called virtual address space, which is mapped to physical memory space by the OS.
  • Making memory references within one running program (within one’s own virtual address space) does not affect the private virtual address space of others.
  • Without the setarch command, the location of variable p will be randomize within the virtual address space of a process. This is a security mechanism to prevent others from guessing and applying direct manipulation techniques to the physical memory location that acually contains p.

Concurrency

1
2
3
./threads 50
./threads 100
./threads 200
1
2
3
4
./threads 20000
./threads 30000
./threads 30000
./threads 30000
Problem with concurrency
  • Naive concurrency gives you wrong results.
  • Naive concurrency gives you wrong and inconsistent results.
Why does this happen?
  • At machine level, incrementing counter involves three steps:
    • Load value of counter from memory into register,
    • Increment this value in the register, and
    • Write the value of counter back to memory.
  • What should have happened:
    • One thread increments counter (all three steps), then the other thread increments counter, now with the updated value.
  • What really happened:
    • One thread increments counter.
    • While this thread has not done with all three steps, the other thread steps in and attempts to increment the stale content of counter in memory.

Persistency


A brief history of operating system research and development

A good paper to read: Hanser, Per Brinch. “The evolution of oeprating systems” 2001