C++ / a working model

106 / 163   ·   C11   ·   8 min

Introduction to Operating Systems

Keep this sentence

This chapter outlines how an operating system virtualizes limited physical hardware into convenient abstractions while serving as both a resource manager and a standard interface provider.

In this lesson
  1. The Basic Instruction Execution Cycle
  2. Turning Physical Resources into Virtual Ones
  3. System Calls and Policy Decisions
  4. Example
  5. Exercise

Official chapter PDF

The Basic Instruction Execution Cycle

Whenever a program runs, the processor continuously retrieves an instruction from memory, determines its meaning, and immediately performs the corresponding action, repeating until the program ends. Software can typically ignore low-level hardware accelerations such as pipelining and out-of-order execution and treat the process as strictly sequential.

Turning Physical Resources into Virtual Ones

The central task of an operating system is to convert real processors, memory, and disks into a larger number of more convenient virtual counterparts. Users therefore feel they possess many independent CPUs and private memory spaces even though the underlying hardware is scarce.

System Calls and Policy Decisions

Applications communicate with the operating system via a set of system calls that together form a de-facto standard library. When several programs compete for the same resource, the operating system must also apply policies such as scheduling and allocation to decide who receives the resource and when.

Pitfalls

  • Treating the operating system as nothing more than an ordinary function library and overlooking its privileged isolation duties
  • Believing virtualization is merely round-robin hardware sharing while forgetting the necessity of address-space isolation

Run an example

Minimum C11 · complete program · Download .c

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    printf("Starting program execution.\n");
    int *mem = malloc(sizeof(int) * 3);
    if (!mem) {
        return 1;
    }
    mem[0] = 10;
    mem[1] = 20;
    mem[2] = mem[0] + mem[1];
    printf("Computed result: %d\n", mem[2]);
    free(mem);
    printf("Program completed.\n");
    return 0;
}

Compile locally

gcc -std=c11 -Wall -Wextra -Wpedantic -Werror ostep-02-introduction.c -o example && ./example

Expected result

Starting program execution.
Computed result: 30
Program completed.

CHECK YOUR UNDERSTANDING

Close the answer. Explain it.

Why can a user start several programs at once even when only a single physical processor exists?

Show a reference answer

The operating system rapidly switches among running programs so that each briefly occupies the processor, thereby creating the illusion of many virtual processors working simultaneously.

Check the sources

Drafts and official chapters change. The version mark is only the example’s minimum.

Back to the catalog