


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Operating System: Three Easy Pieces ... OS is in charge of making sure the system operates ... The OS manage resources such as CPU, memory and disk.
Typology: Lecture notes
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















AOS@UC 1
What a happens when a program runs? p A running program executes instructions.
Virtualization p The OS takes a physical resource and transforms it into a virtual form of itself.
w The virtual form is more general, powerful and easy-to-use. w Sometimes, we refer to the OS as a virtual machine. AOS@UC 4
System call p System call allows user to tell the OS what to do. w The OS provides some interface (APIs, standard library). w A typical OS exports a few hundred system calls.
AOS@UC 5
Virtualizing the CPU p The system has a very large number of virtual CPUs. w Turning a single CPU into a seemingly infinite number of CPUs. w Allowing many programs to seemingly run at once à Virtualizing the CPU AOS@UC 7
Virtualizing the CPU (Cont.) 8 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sys/time.h> 4 #include <assert.h> 5 #include "common.h" 6 7 int 8 main(int argc, char *argv[]) 9 { 10 if (argc != 2 ) { 11 fprintf(stderr, "usage: cpu
Virtualizing the CPU (Cont.) p Execution result 2. 10 prompt> ./cpu A & ; ./cpu B & ; ./cpu C & ; ./cpu D & [1] 7353 [2] 7354 [3] 7355 [4] 7356 A B D C A B D C A C B D ... Even though if we have only one processor, all four of programs seem to be running at the same time! AOS@UC
Virtualizing Memory p The physical memory is an array of bytes. p A program keeps all of its data structures in memory. w Read memory (load):
w Write memory (store):
AOS@UC 11
Virtualizing Memory (Cont.) p The output of the program mem.c w The newly allocated memory is at address 00200000. w It updates the value and prints out the result. w CAVEAT: In current systems, ASLR (Address Space Layout Randomization) Makes this to change. To disable it:
¢ OSX: gcc - o mem mem.c - Wall - Wl,-no_pie 13 prompt> ./mem (2134) memory address of p: 00200000 (2134) p: 1 (2134) p: 2 (2134) p: 3 (2134) p: 4 (2134) p: 5 ˆC AOS@UC
Virtualizing Memory (Cont.) p Running mem.c multiple times w It is as if each running program has its own private memory.
14 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 ... AOS@UC
The problem of Concurrency p The OS is juggling many things at once, first running one process, then another, and so forth. p Modern multi-threaded programs also exhibit the concurrency problem. AOS@UC 16
Concurrency Example p A Multi-threaded Program (thread.c) 17 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 ... AOS@UC
Concurrency Example (Cont.) p loops determines how many times each of the two workers will increment the shared counter in a loop. w loops: 1000. w loops: 100000. 19 prompt> gcc - o thread thread.c - Wall - pthread prompt> ./thread 1000 Initial value : 0 Final value : 2000 prompt> ./thread 100000 Initial value : 0 Final value : 143012 // huh?? prompt> ./thread 100000 Initial value : 0 Final value : 137298 // what the?? AOS@UC
Why is this happening? p Increment a shared counter à take three instructions.