2 Introduction to Operating Systems
If you are taking an undergraduate operating systems course, you should already have some idea of what a computer program does when it runs. If not, this book (and the corresponding course) is going to be difficult — so you should probably stop reading this book, or run to the near- est bookstore and quickly consume the necessary background material before continuing (both Patt & Patel [PP03] and Bryant & O’Hallaron [BOH10] are pretty great books).
So what happens when a program runs?
Well, a running program does one very simple thing: it executes in- structions. Many millions (and these days, even billions) of times ev- ery second, the processor fetches an instruction from memory, decodes it (i.e., figures out which instruction this is), and executes it (i.e., it does the thing that it is supposed to do, like add two numbers together, access memory, check a condition, jump to a function, and so forth). 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 completes1.
Copyright By PowCoder代写 加微信 powcoder
Thus, we have just described the basics of the model of computing2. Sounds simple, right? But in this class, we will be learning that while a program runs, a lot of other wild things are going on with the primary goal of making the system easy to use.
There is a body of software, in fact, that is responsible for making it easy to run programs (even allowing you to seemingly run many at the same time), allowing programs to share memory, enabling programs to interact with devices, and other fun stuff like that. That body of software
1Of course, modern processors do many bizarre and frightening things underneath the hood to make programs run faster, e.g., executing multiple instructions at once, and even issu- ing and completing them out of order! But that is not our concern here; we are just concerned with the simple model most programs assume: that instructions seemingly execute one at a time, in an orderly and sequential fashion.
2 was one of the early pioneers of computing systems. He also did pioneer- ing work on game theory and atomic bombs, and played in the NBA for six years. OK, one of those things isn’t true.
OPERATING SYSTEMS [VERSION 1.01]
2 INTRODUCTION TO OPERATING SYSTEMS
THE CRUX OF THE PROBLEM:
HOW TO VIRTUALIZE RESOURCES
One central question we will answer in this book is quite simple: how does the operating system virtualize resources? This is the crux of our problem. Why the OS does this is not the main question, as the answer should be obvious: it makes the system easier to use. Thus, we focus on the how: what mechanisms and policies are implemented by the OS to attain virtualization? How does the OS do so efficiently? What hardware
support is needed?
We will use the “crux of the problem”, in shaded boxes such as this one, as a way to call out specific problems we are trying to solve in building an operating system. Thus, within a note on a particular topic, you may find one or more cruces (yes, this is the proper plural) which highlight the problem. The details within the chapter, of course, present the solution, or at least the basic parameters of a solution.
is called the operating system (OS)3, as it is in charge of making sure the system operates correctly and efficiently in an easy-to-use manner.
The primary way the OS does this is through a general technique that we call virtualization. That is, the OS takes a physical resource (such as the processor, or memory, or a disk) and transforms it into a more gen- eral, powerful, and easy-to-use virtual form of itself. Thus, we sometimes refer to the operating system as a virtual machine.
Of course, in order to allow users to tell the OS what to do and thus make use of the features of the virtual machine (such as running a pro- gram, or allocating memory, or accessing a file), the OS also provides some interfaces (APIs) that you can call. A typical OS, in fact, exports a few hundred system calls 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.
Finally, because virtualization allows many programs to run (thus shar- ing the CPU), and many programs to concurrently access their own in- structions and data (thus sharing memory), and many programs to access devices (thus sharing disks and so forth), the OS is sometimes known as a resource manager. Each of the CPU, memory, and disk is a resource of the system; it is thus the operating system’s role to manage those re- sources, doing so efficiently or fairly or indeed with many other possible goals in mind. To understand the role of the OS a little bit better, let’s take a look at some examples.
3 Another early name for the OS was the supervisor or even the master control program. Apparently, the latter sounded a little overzealous (see the movie Tron for details) and thus, thankfully, “operating system” caught on instead.
WWW.OSTEP.ORG
10 11 12 13 14 15 16 17 18 19 20
if(argc!=2){
fprintf(stderr, “usage: cpu
char *str = argv[1];
while (1) {
printf(“%s\n”, str);
return 0; }
Figure 2.1: Simple Example: Code That Loops And Prints (cpu.c) Virtualizing The CPU
Figure 2.1 depicts our first program. It doesn’t do much. In fact, all it does is call Spin(), a function that repeatedly checks the time and returns once it has run for a second. Then, it prints out the string that the user passed in on the command line, and repeats, forever.
Let’s say we save this file as cpu.c and decide to compile and run it on a system with a single processor (or CPU as we will sometimes call it). Here is what we will see:
prompt> gcc -o cpu cpu.c -Wall
prompt> ./cpu “A”
ˆC prompt>
Not too interesting of a run — the system begins running the program, which repeatedly checks the time until a second has elapsed. Once a sec- ond has passed, the code prints the input string passed in by the user (in this example, the letter “A”), and continues. Note the program will run forever; by pressing “Control-c” (which on UNIX-based systems will ter- minate the program running in the foreground) we can halt the program.
⃝c 2008–20, ARPACI-DUSSEAU
INTRODUCTION TO OPERATING SYSTEMS 3
1 #include
2 #include
3 #include
4 #include
5 #include “common.h”
8 main(int argc, char *argv[])
THREE EASY PIECES
OPERATING SYSTEMS [VERSION 1.01]
Now, let’s do the same thing, but this time, let’s run many different in- stances of this same program. Figure 2.2 shows the results of this slightly more complicated example.
Well, now things are getting a little more interesting. Even though we have only one processor, somehow all four of these programs seem to be running at the same time! How does this magic happen?4
It turns out that the operating system, with some help from the hard- ware, is in charge of this illusion, i.e., the illusion that the system has a very large number of virtual CPUs. Turning a single CPU (or a small set of them) into a seemingly infinite number of CPUs and thus allowing many programs to seemingly run at once is what we call virtualizing the CPU, the focus of the first major part of this book.
Of course, to run programs, and stop them, and otherwise tell the OS which programs to run, there need to be some interfaces (APIs) that you can use to communicate your desires to the OS. We’ll talk about these APIs throughout this book; indeed, they are the major way in which most users interact with operating systems.
You might also notice that the ability to run multiple programs at once raises all sorts of new questions. For example, if two programs want to run at a particular time, which should run? This question is answered by a policy of the OS; policies are used in many different places within an OS to answer these types of questions, and thus we will study them as we learn about the basic mechanisms that operating systems implement (such as the ability to run multiple programs at once). Hence the role of the OS as a resource manager.
4 Note how we ran four processes at the same time, by using the & symbol. Doing so runs a job in the background in the zsh shell, which means that the user is able to immediately issue their next command, which in this case is another program to run. If you’re using a different shell (e.g., tcsh), it works slightly differently; read documentation online for details.
WWW.OSTEP.ORG
4 INTRODUCTION TO OPERATING SYSTEMS
prompt> ./cpu A & ./cpu B & ./cpu C & ./cpu D &
[4] 7356 A
Figure 2.2: Running Many Programs At Once
10 11 12 13 14 15 16 17 18 19 20
int *p = malloc(sizeof(int));
assert(p != NULL);
printf(“(%d) address pointed to by p: %p\n”,
getpid(), p);
while (1) {
*p = *p + 1;
printf(“(%d) p: %d\n”, getpid(), *p);
return 0; }
Figure 2.3: A Program That Accesses Memory (mem.c) Virtualizing Memory
INTRODUCTION TO OPERATING SYSTEMS
1 #include
2 #include
3 #include
4 #include “common.h”
7 main(int argc, char *argv[])
// a2 // a3
Now let’s consider memory. The model of physical memory pre- sented by modern machines is very simple. Memory is just an array of bytes; to read memory, one must specify an address to be able to access the data stored there; to write (or update) memory, one must also specify the data to be written to the given address.
Memory is accessed all the time when a program is running. A pro- gram keeps all of its data structures in memory, and accesses them through various instructions, like loads and stores or other explicit instructions that access memory in doing their work. Don’t forget that each instruc- tion of the program is in memory too; thus memory is accessed on each instruction fetch.
Let’s take a look at a program (in Figure 2.3) that allocates some mem- ory by calling malloc(). The output of this program can be found here:
prompt> ./mem
(2134) address pointed to by p: 0x200000
(2134) p: 1
(2134) p: 2
(2134) p: 3
(2134) p: 4
(2134) p: 5
⃝c 2008–20, ARPACI-DUSSEAU
THREE EASY PIECES
OPERATING SYSTEMS [VERSION 1.01]
6 INTRODUCTION TO OPERATING SYSTEMS
prompt> ./mem &; ./mem &
(24113) address pointed to by p: 0x200000
(24114) address pointed to by p: 0x200000
(24113) p: 1
(24114) p: 1
(24114) p: 2
(24113) p: 2
(24113) p: 3
(24114) p: 3
(24113) p: 4
(24114) p: 4
Figure 2.4: Running The Memory Program Multiple Times
The program does a couple of things. First, it allocates some memory (line a1). Then, it prints out the address of the memory (a2), and then puts the number zero into the first slot of the newly allocated memory (a3). Finally, it loops, delaying for a second and incrementing the value stored at the address held in p. With every print statement, it also prints out what is called the process identifier (the PID) of the running program. This PID is unique per running process.
Again, this first result is not too interesting. The newly allocated mem- ory is at address 0x200000. As the program runs, it slowly updates the value and prints out the result.
Now, we again run multiple instances of this same program to see what happens (Figure 2.4). We see from the example that each running program has allocated memory at the same address (0x200000), and yet each seems to be updating the value at 0x200000 independently! It is as if each running program has its own private memory, instead of sharing the same physical memory with other running programs5.
Indeed, that is exactly what is happening here as the OS is virtualiz- ing memory. Each process accesses its own private virtual address space (sometimes just called its address space), which the OS somehow maps onto the physical memory of the machine. A memory reference within one running program does not affect the address space of other processes (or the OS itself); as far as the running program is concerned, it has phys- ical memory all to itself. The reality, however, is that physical memory is a shared resource, managed by the operating system. Exactly how all of this is accomplished is also the subject of the first part of this book, on the topic of virtualization.
5For this example to work, you need to make sure address-space randomization is dis- abled; randomization, as it turns out, can be a good defense against certain kinds of security flaws. Read more about it on your own, especially if you want to learn how to break into computer systems via stack-smashing attacks. Not that we would recommend such a thing…
WWW.OSTEP.ORG
INTRODUCTION TO OPERATING SYSTEMS 7
2.3 Concurrency
1 #include
2 #include
3 #include “common.h”
4 #include “common_threads.h”
6 volatile int counter = 0;
7 int loops;
9 void *worker(void *arg) {
for (i = 0; i < loops; i++) {
counter++; }
return NULL;
main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: threads
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);
Figure 2.5: A Multi-threaded Program (threads.c)
Another main theme of this book is concurrency. We use this concep- tual term to refer to a host of problems that arise, and must be addressed, when working on many things at once (i.e., concurrently) in the same program. The problems of concurrency arose first within the operating system itself; as you can see in the examples above on virtualization, the OS is juggling many things at once, first running one process, then an- other, and so forth. As it turns out, doing so leads to some deep and
interesting problems.
Unfortunately, the problems of concurrency are no longer limited just
to the OS itself. Indeed, modern multi-threaded programs exhibit the same problems. Let us demonstrate with an example of a multi-threaded program (Figure 2.5).
⃝c 2008–20, ARPACI-DUSSEAU
THREE EASY PIECES
OPERATING SYSTEMS [VERSION 1.01]
In this run, when we gave an input value of 100,000, instead of getting a final value of 200,000, we instead first get 143,012. Then, when we run the program a second time, we not only again get the wrong value, but also a different value than the last time. In fact, if you run the program over and over with high values of loops, you may find that sometimes you even get the right answer! So why is this happening?
As it turns out, the reason for these odd and unusual outcomes relate to how instructions are executed, which is one at a time. Unfortunately, a key part of the program above, where the shared counter is incremented,
6 The actual call should be to lower-case pthread create(); the upper-case version is our own wrapper that calls pthread create() and makes sure that the return code indicates that the call succeeded. See the code for details.
WWW.OSTEP.ORG
8 INTRODUCTION TO OPERATING SYSTEMS
Although you might not understand this example fully at the moment (and we’ll learn a lot more about it in later chapters, in the section of the book on concurrency), the basic idea is simple. The main program creates two threads using Pthread create()6 . You can think of a thread as a function running within the same memory space as other functions, with more than one of them active at a time. In this example, each thread starts running in a routine called worker(), in which it simply increments a counter in a loop for loops number of times.
Below is a transcript of what happens when we run this program with the input value for the variable loops set to 1000. The value of loops determines how many times each of the two workers will increment the shared counter in a loop. When the program is run with the value of loops set to 1000, what do you expect the final value of counter to be?
prompt> gcc -o thread thread.c -Wall -pthread
prompt> ./thread 1000
Initial value : 0
Final value : 2000
As you probably guessed, when the two threads are finished, the final value of the counter is 2000, as each thread incremented the counter 1000 times. Indeed, when the input value of loops is set to N , we would expect the final output of the program to be 2N . But life is not so simple, as it turns out. Let’s run the same program, but with higher values for loops, and see what happens:
prompt> ./thread 100000
Initial value : 0
Final value : 143012
prompt> ./thread 100000
Initial value : 0
Final value : 137298
// what the??
INTRODUCTION TO OPERATING SYSTEMS 9
THE CRUX OF THE PROBLEM:
HOW TO BUILD CORRECT CONCURRENT PROGRAMS
When there are many concurrently executing threads within the same memory space, how can we build a correctly working program? What primitives are needed from the OS? What mechanisms should be pro- vided by the hardware? How can we use them to solve the problems of
concurrency?
takes three instructions: one to load the value of the counter from mem- ory into a register, one to increment it, and one to store it back into mem- ory. Because these three instructions do not execute atomically (all at once), strange things can happen. It is this problem of concurrency that we will address in great detail in the second part of this book.
2.4 Persistence
The third major theme of the course is persistence. In system memory, data can be easily lost, as devices such as DRAM store values in a volatile manner; when power goes away or the system crashes, any data in mem- ory is lost. Thus, we need hardware and software to be able to store data persistently; such storage is thus critical to any system as users care a great deal about their data.
The hardware comes in the form of some kind of input/output or I/O device; in modern systems, a hard drive is a common repository for long- lived information, although solid-state drives (SSDs) are making head- way in this arena as well.
The software in the operating system that usually manages the disk is called the file system; it is thus responsible for storing any files the user creates in a reliable and efficient manner on the disks of the system.
Unlike the abstractions provided by the OS for the CPU and memory, the OS does not create a private, virtualized disk for each application. Rather, it is assumed that often times, users will want to share informa- tion that is in files. For example, when writing a C program, you might first use an editor (e.g., Emacs7) to create and edit the C file (emacs -nw main.c). Once done, you might use the compiler to turn the source code into an executable (e.g., gcc -o main main.c). When you’re finished, you might run the new executable (e.g., ./main). Thus, you can see how files are shared across different processes. First, Emacs creates a file that serves as input to the compiler; the compiler uses that input file to create a new executable file (in many steps — take a compiler course for details); finally, the new executable is then run. And thus a new program is born!
7 You should be using Emacs. If you are using vi, there is probably something wrong with you. If you are using something that is not a real code editor, that is even worse.
⃝c 2008–20, ARPACI-DUSSEAU
THREE EASY PIECES
assert(fd > -1);
10 INTRODUCTION TO OPERATING SYSTEMS
1 #include
2 #include
3 #include
4 #include
5 #include
main(int argc, char *argv[]) {
int fd = open(“/tmp/file”, O_WRONLY|O_CREAT|O_TRUNC,
OPERATING SYSTEMS [VERSION 1.01]
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com