2. Introduction to Operating Systems, Lecture notes of Operating Systems

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

2022/2023

Uploaded on 03/01/2023

janeka
janeka 🇺🇸

4.1

(15)

260 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2. Introduction to Operating Systems
Operating System: Three Easy Pieces
1
AOS@UC
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download 2. Introduction to Operating Systems and more Lecture notes Operating Systems in PDF only on Docsity!

2. Introduction to Operating Systems

Operating System: Three Easy Pieces

AOS@UC 1

What a happens when a program runs? p A running program executes instructions.

  1. The processor fetches an instruction from memory. 2. Decode : Figure out which instruction this is 3. Execute : i.e., add two numbers, access memory, check a condition, jump to function, and so forth.
  2. The processor moves on to the next instruction and so on. AOS@UC 2

Virtualization p The OS takes a physical resource and transforms it into a virtual form of itself.

¢ Physical resource : Processor, Memory, Disk …

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.

¢ Run programs

¢ Access memory

¢ Access devices

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 \n"); 12 exit( 1 ); 13 } 14 char *str = argv[ 1 ]; 15 while ( 1 ) { 16 Spin( 1 ); // Repeatedly checks the time and returns once it has run for a second 17 printf("%s\n", str); 18 } 19 return 0 ; 20 } Simple Example(cpu.c): Code That Loops and Prints All Code in--> https://github.com/remzi-arpacidusseau/ostep-code AOS@UC

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):

¢ Specify an address to be able to access the data

w Write memory (store):

¢ Specify the data to be written to the given address

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:

¢ Linux: echo 0> /proc/sys/kernel/randomize_va_space

¢ 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.

¢ Each running program has allocated memory at the same address.

¢ Each seems to be updating the value at 00200000 independently.

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.

  1. Load the value of the counter from memory into register.
  2. Increment it
  3. Store it back into memory p These three instructions do not execute atomically. à Problem of concurrency happen. AOS@UC 20