EEE3535 Operating Systems Notes PDF
Document Details
Uploaded by SpiritedWave
Yonsei University
Won Woo Ro
Tags
Summary
These notes detail introductory concepts of operating systems, focusing on virtualization and system calls. The document explains how operating systems manage resources and interact with applications. The overview provides foundational knowledge on the subject.
Full Transcript
So What Happens When a Program Runs? Well, a running program does one very simple thing: it executes instructions. EEE3535 Many milli...
So What Happens When a Program Runs? Well, a running program does one very simple thing: it executes instructions. EEE3535 Many millions (even billions) of times every second, Operating Systems the processor fetches an instruction from memory, decodes it, and executes it. No. 1: 2. Introduction to Operating Systems After it is done with this instruction, the processor moves on to the next instruction, and so on, and so on, until the program finally completes. Won Woo Ro, Ph.D. Virtualization System Calls (Standard Library) The OS takes a physical resource (such as the In order to allow users to tell the OS what to do and processor, or memory, or a disk) and transforms it thus make use of the features of the virtual machine into a more general, powerful, and easy‐to‐use (such as running a program, or allocating memory, or virtual form of itself. accessing a file), the OS also provides some interfaces (APIs). Thus, we sometimes refer to the operating system as A typical OS, in fact, exports a few hundred system calls a virtual machine. that are available to applications. Because the OS provides these calls to run programs, access memory and devices, and other related actions, we also sometimes say that the OS provides a standard library to applications. Resource Manager 2.1 Virtualizing the CPU Virtualization allows many programs to run (thus 1 2 #include #include sharing the CPU), and many programs to 3 4 #include #include concurrently access their own instructions and data 5 6 #include "common.h" (thus sharing memory), and many programs to 7 int 8 main(int argc, char *argv[]) access devices (thus sharing disks and so forth). 9 { 10 if (argc != 2) { 11 fprintf(stderr, "usage: cpu \n"); 12 exit(1); 13 } 14 char *str = argv; 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 } Virtualizing the CPU 2.2 Virtualizing Memory prompt> gcc -o cpu cpu.c -Wall prompt>./cpu "A" A A Memory is just an array of bytes To read memory, one must specify an address to be able to A ˆC prompt> access the data stored there. Run forever; Only by pressing “Control-c” can we halt the program To write (or update) memory, one must also specify the prompt>./cpu A & ;./cpu B & ;./cpu C & ;./cpu D & data to be written to the given address. 7353 7354 7355 7356 Memory is accessed all the time when a program is A running. B D A program keeps all of its data structures in memory, and C A accesses them through various instructions, like loads and B D stores or other explicit instructions that access memory in C doing their work. A C B D... 7 8 A Program that Accesses Memory (mem.c) Output 1 #include 2 #include 3 #include prompt>./mem 4 #include "common.h" (2134) memory address of p: 00200000 5 (2134) p: 1 6 int (2134) p: 2 7 main(int argc, char *argv[]) (2134) p: 3 8 { (2134) p: 4 9 int *p = malloc(sizeof(int)); // a1: allocate some (2134) p: 5 memory ˆC 10 assert(p != NULL); 11 printf("(%d) address of p: %08x\n", 12 getpid(), (unsigned) p); // a2: print out the address of the memory The newly allocated memory is at address 00200000. 13 *p = 0; // a3: put zero into the first slot of the memory 14 while (1) { It updates the value and prints out the result. 15 Spin(1); 16 *p = *p + 1; 17 printf("(%d) p: %d\n", getpid(), *p); // a4 18 } 19 return 0; 20 } 9 10 Case 2: Running mem.c Multiple Times Virtualization: Memory Each process accesses its own private virtual address prompt>./mem &;./mem & space. 24113 24114 (24113) memory address of p: 00200000 The OS maps address space onto the physical (24114) memory address of p: 00200000 memory. (24113) p: 1 (24114) p: 1 (24114) p: 2 A memory reference within one running program (24113) p: 2 (24113) p: 3 does not affect the address space of other processes. (24114) p: 3 Physical memory is a shared resource, managed by... the OS. 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. 11 12 2.3 Concurrency A Multi‐Threaded Program (thread.c) We use this conceptual term to refer to a host of 1 #include 2 #include problems that arise, and must be addressed, when 3 #include "common.h" working on many things at once (i.e., concurrently) in 4 5 volatile int counter = 0; the same program. 6 7 int loops; 8 void *worker(void *arg) { The problems of concurrency arose first within the 9 10 int i; for (i = 0; i < loops; i++) { operating system itself; as you can see in the 11 12 } counter++; examples above on virtualization, the OS is juggling 13 return NULL; 14 } many things at once, first running one process, then 15 another, and so forth. 16 17 int main(int argc, char *argv[]) 18 { 19 if (argc != 2) { 20 fprintf(stderr, "usage: threads \n"); 21 exit(1); 22 } 13 14 A Multi‐Threaded Program (thread.c) Concurrency Example (Cont.) 23 24 loops = atoi(argv); pthread_t p1, p2; loops determines how many times each of the 25 printf("Initial value : %d\n", counter); two workers will increment the shared counter in a 26 27 Pthread_create(&p1, NULL, worker, NULL); loop. 28 Pthread_create(&p2, NULL, worker, NULL); 29 Pthread_join(p1, NULL); loops: 1000. 30 Pthread_join(p2, NULL); 31 printf("Final value : %d\n", counter); prompt> gcc -o thread thread.c -Wall -pthread 32 return 0; prompt>./thread 1000 33 } Initial value : 0 Final value : 2000 The main program creates two threads. Thread: a function running within the same memory space. Each thread loops: 100000. start running in a routine called worker(). worker(): increments a counter prompt>./thread 100000 Initial value : 0 Final value : 143012 // huh?? prompt>./thread 100000 Initial value : 0 Final value : 137298 // what the?? 15 16 Why is This happening? Persistence Increment a shared counter take three Devices such as DRAM store values in a volatile. instructions. Hardware and software are needed to store data 1. Load the value of the counter from memory into register. persistently. 2. Increment it Hardware: I/O device such as a hard drive, solid‐state 3. Store it back into memory drives(SSDs) Software: File system manages the disk. These three instructions do not execute atomically. File system is responsible for storing any files the user creates. Problem of concurrency happen. 17 18 Persistence (Cont.) Persistence (Cont.) Create a file (/tmp/file) that contains the string What OS does in order to write to disk? “hello 1 2 world” #include #include Figure out where on disk this new data will reside 3 #include 4 #include Issue I/O requests to the underlying storage device 5 #include 6 7 int 8 main(int argc, char *argv[]) 9 { File system handles system crashes during write. 10 int fd = open("/tmp/file", O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); Journaling or copy‐on‐write 11 assert(fd > -1); 12 13 int rc = write(fd, "hello world\n", 13); assert(rc == 13); Carefully ordering writes to disk 14 close(fd); 15 return 0; 16 } open(), write(), and close() system calls are routed to the part of OS called the file system, which handles the requests 19 20 Design Goals Design Goals (Cont.) Build up abstraction High degree of reliability Make the system convenient and easy to use. The OS must also run non‐stop. Provide high performance Other issues Minimize the overhead of the OS. Energy‐efficiency OS must strive to provide virtualization without excessive Security overhead. Mobility Protection between applications Isolation: Bad behavior of one does not harm other and the OS itself. 21 22