[OSTEP] 1. Introduction to Operating Systems

ch1 ~ 2: 인트로

Table Of Contents

Three pieces

In learning about these three pieces, we'll learn about

Introduction to Operating Systems

Operating System

There is a body of software(= operating system, OS) which is responsible for

Virtualization

1. Virtualizing The CPU

Example

#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <assert.h> #include "common.h" int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "usage: cpu <string>\n"); exit(1); } char* str = argv[1]; while (1) { Spin(1); printf("%s\n", str); } return 0; }

Figure 2.1: Simple Example: Code That Loops And Prints (cpu.c)

virtualiziing the CPU = the OS turns 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

2. Virtualizing Memory

(Physical) Memory

Example

#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include "common.h" int main(int argc, char* argv[]) { int* p = malloc(sizeof(int)); // a1 assert(p != NULL); printf("(%d) address pointed to by p: %p\n", getpid(), p); // a2 *p = 0; // a3 while (1) { Spin(1); *p = *p + 1; printf("(%d) p: %d\n", getpid(), *p); // a4 } return 0; }

Figure 2.3: A Program That Accesses Memory (mem.c)

Each process accesses its own private virtual address space (address space)

3. Concurrency

Example

#include <stdio.h> #include <stdlib.h> #include "common.h" volatile int counter = 0; int loops; void* worker(void* arg) { int i; for (i = 0; i < loops; i++) { counter++; } return NULL; } int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "usage: threads <value>\n"); exit(1); } loops = atoi(argv[1]); pthread_t p1, p2; printf("Initial value : %d\n", counter); Pthread_create(&p1, NULL, worker, NULL); Pthread_create(&p2, NULL, worker, NULL); Pthread_join(p1, NULL); Pthread_join(p2, NULL); printf("Final value : %d\n", counter); return 0; }

Figure 2.5: A Multi-threaded Program (threads.c)

prompt> ./thread 100000 Initial value : 0 Final value : 143012 // huh?? prompt> ./thread 100000 Initial value : 0 Final value : 137298 // what the??

4. Persistence

Example

#include <stdio.h> #include <unistd.h> #include <assert.h> #include <fcntl.h> #include <sys/types.h> int main(int argc, char* argv[]) { int fd = open("/tmp/file", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); assert(fd > -1); int rc = write(fd, "hello world\n", 13); assert(rc == 13); close(fd); return 0; }

Figure 2.6: A Program That Does I/O (io.c)

5. Design Goals