OS Ch2
OS Ch2
2. Introduction to
Operating Systems
Myungsuk Kim
Virtualization
CPU Virtualization (Scheduling, Process…)
Memory Virtualization (Paging, Address Translation…)
Concurrency
Persistence
B
BBBB
C
CCCC
D
DDDD
A B D C A B D C A C B D….
COMP312 운영체제 Operating Systems
Virtualizing Memory
▪ Gives illusion that each process has its own private virtual
address space
• The physical memory is an array of bytes (or byte-addressable).
▫ In general computing systems, the physical memory is DRAM, and has
limited capacity
▫ Allows each user (or process) to think of physical memory as a contiguous
address space (or collection of contiguous segments)
• A program keeps all of its data structures in memory
▫ Read memory (load) : Specify an address to be able to access the data
▫ Write memory (store) : Specify the data to be written to the given address
User mode
. . .
space (3 GB)
0x00000000
COMP312 운영체제 Operating Systems
Virtualizing Memory
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "common.h"
5
6 int
7 main(int argc, char *argv[])
8 {
9 int *p = malloc(sizeof(int)); // a1: allocate some
memory
10 assert(p != NULL);
11 printf("(%d) address of p: %08x\n",
12 getpid(), (unsigned) p); // a2: print out the
address of the memory
13 *p = 0; // a3: put zero into the first slot of the memory
14 while (1) {
15 Spin(1);
16 *p = *p + 1;
17 printf("(%d) p: %d\n", getpid(), *p); // a4
18 }
19 return 0;
20 }
A program that Accesses Memory (mem.c)
COMP312 운영체제 Operating Systems
Virtualizing Memory
(The output of the program mem.c)
prompt> ./mem
(2134) memory address of p: 00200000
(2134) p: 1
(2134) p: 2
(2134) p: 3
(2134) p: 4
(2134) p: 5
ˆC
Virtualizing Memory
(Running mem.c multiple times)
prompt> ./mem &; ./mem &
[1] 24113
[2] 24114
(24113) memory address of p: 00200000
(24114) memory address of p: 00200000
(24113) p: 1
(24114) p: 1
(24114) p: 2
(24113) p: 2
(24113) p: 3
(24114) p: 3
...
Virtualizing Memory
▪ The OS maps address space onto the physical memory.
• A memory reference within one running program does not affect the
address space of other processes → Protection
• Physical memory is a shared and limited resource, managed by the OS
▫ (c.f.) cache memory
Address of p’
COMP312 운영체제 Operating Systems
Concurrency
▪ Concurrent access of shared resources is subtle and error-
prone
• The OS is juggling many things at once, first running one process, then
another, and so forth
• Modern multi-threaded programs also exhibit the concurrency problem
• Need protection, lock or other synchronization methods
Concurrency
A Multi-threaded Program (thread.c)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "common.h"
4
5 volatile int counter = 0;
6 int loops;
7
8 void *worker(void *arg) {
9 int i;
10 for (i = 0; i < loops; i++) {
11 counter++;
12 }
13 return NULL;
14 }
15
16 int
17 main(int argc, char *argv[])
18 {
19 if (argc != 2) {
20 fprintf(stderr, "usage: threads <value>\n");
21 exit(1);
22 }
COMP312 운영체제 Operating Systems
Concurrency
23 loops = atoi(argv[1]);
24 pthread_t p1, p2;
25 printf("Initial value : %d\n", counter);
26
27 Pthread_create(&p1, NULL, worker, NULL);
28 Pthread_create(&p2, NULL, worker, NULL);
29 Pthread_join(p1, NULL);
30 Pthread_join(p2, NULL);
31 printf("Final value : %d\n", counter);
32 return 0;
33 }
Concurrency
▪ loops determines how many times each of the two
workers will increment the shared counter in a loop
✓ Loop = 1,000
prompt> gcc -o thread thread.c -Wall -pthread
prompt> ./thread 1000
Initial value : 0
Final value : 2000
✓ Loop = 100,000
prompt> ./thread 100000
Initial value : 0
Final value : 143012 // huh??
prompt> ./thread 100000
Initial value : 0
Final value : 137298 // what the??
COMP312 운영체제 Operating Systems
Persistence
▪ Devices such as DRAM store values in a volatile
▪ Hardware and software are needed to store data persistently
• Hardware: I/O device such as a hard drive (HDDs) & solid-state
drives(SSDs)
• Software:
▫ File system manages the disk.
▫ File system is responsible for storing any files the user creates.
Persistence
▪ Example : journaling file system (JFS)
Source: http://www.howtogeek.com/howto/33552/htg-explains-which-linux-file-system-should-you-choose,
COMP312 운영체제 Operating Systems
Design Goals
▪ Build up abstractions
• Make the system convenient and easy to use
▪ Provide high performance
• Minimize the overhead of the OS
• Provide virtualization without excessive overhead
▪ Protection between applications
• Does not harm other and the OS itself via Isolation
▪ High degree of reliability
• OS must run non-stop; when it fails, all apps on the system fail as well
▪ Other goals
• Energy-efficiency, Security, Mobility