CS015605-Operating System #1


この文章の内容は国民大学の黄賢泰教授のCS 015605オペレーティングシステムの授業のノートです。

Operating System #1 week


Virtualizing The CPU


Turnging a single CPU (or small set of them) into a seemingly infinite number of CPUs and thus allowing many programs to seemingly run at once is what we call virtualizing the CPU.

Resource manager


If two programs want to run at a particular time, which should run? This question is answered by a policy of the OS.

Virtualizing Memory


Meomory is accessed all the time when a program is running. A program keeps all of its data structure in memory and accesses them through various instructions. Each instruction of the program is in memory too.
If there were a program like below..
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"

int main (int argc, char *argv[])
{
	int *p = malloc(sizeof(int));
    assert(p!=NULL);
    printf("(%d) address pointed to by p: %p\n", getpid(), p);
    *p = 0;
    while(1) {
    	Spin(1);
        *p = *p + 1;
        printf("(%d) p: %d", getpid(), *p);
    }
    return 0;
}
The program will run forever. So we can halt the program.
prompt> ./mem
(2134) address pointed to by p: 0x200000
(2134) p: 1
(2134) p: 2
(2134) p: 3
(2134) p: 4
(2134) p: 5
^C

prompt> ./mem &; ./mem &
[1] 24113
[2] 24114
(24113) address pointed to by p: 0x200000
(24114) address pointed to by p: 0x200000
(24113) p: 1
(24114) p: 1
(24114) p: 2
(24113) p: 2
(24113) p: 3
(24114) p: 3
...
^C
With every print statement, it also prints out what is called the PID (process identifier) of the running program.
In multiple instances of this same program, we can see that each running program has allocated memory at the same address(0x200000) independently.
It is as if each running program has its own private memory, instead of sharting the same physical memory with other running programs.
Indeed, that is exactly what is happenings here as the OS is virtualizing memory.
A memory reference within one running program does not affect the address space of other processes.
The reality, however, is that physical memory is a shared resource, managed by the operating system.

Concurrency


The problems of concurrency arose first within the operating system itself. OS is juggling many things at once, first running one process then another, and so forth.
The problems of concurrency are no longer limited just to the OS itself. Moder multi-threaded programs exhibit the same problems.
When the mulitple theads do the same things on a shared value, the shared value takes three instructions.
One to load the value of the counter from memory into a register, one to increment it, and one to store it back into memory.
Because these instructions do not execute atomically strage things can happen.

Persistence


We need hardware and software to be able to store data persistently.
The hardware comes in the form of some kind of input/output or I/O device. In modern systems, a hard drive is a common repository for long lived information, although solid-state drives(SSDs) are making headway in this arena as well.
The software in the operating system that usually manages the disk is called the file system. It is thus responsible for storing any files the user creates in a relialbe and efficient manner on the disks of the system.