Lecture Notes on Operating Systems Marvin Solomon Computer Sciences Department Unversity of Wisconsin -- Madison
[email protected] Mon Jan 24 13:28:57 CST 2000 Copyright © 1996-1999 by Marvin Solomon. All rights reserved.
Contents •
• •
•
Introduction • History • What is an OS For? • Bottom-up View • Top-Down View • Course Outline Java for C++ Programmers Processes and Synchronization • Using Processes • What is a Process? • Why Use Processes • Creating Processes • Process States • Synchronization • Race Conditions • Semaphores • The Bounded Buffer Problem • The Dining Philosophers • Monitors • Messages • Deadlock • Terminology • Deadlock Detection • Deadlock Recovery • Deadlock Prevention • Deadlock Avoidance • Implementing Processes • Implementing Monitors • Implementing Semaphores • Implementing Critical Sections • Short-term Scheduling Memory Management • Allocating Main Memory • Algorithms for Memory Management • Compaction and Garbage Collection • Swapping • Paging • Page Tables • Page Replacement • Frame Allocation for a Single Process • Frame Allocation for Multiple Processes • Paging Details • Segmentation • Multics
• • •
•
Intel x86
Disks File Systems • The User Interface to Files • Naming • File Structure • File Types • Access Modes • File Attributes • Operations • The User Interface to Directories • Implementing File Systems • Files • Directories • Symbolic Links • Mounting • Special Files • Long File Names • Space Management • Block Size and Extents • Free Space • Reliability • Bad-block Forwarding • Back-up Dumps • Consistency Checking • Transactions • Performance Protection and Security • Security • Threats • The Trojan Horse • Design Principles • Authentication • Protection Mechanisms • Access Control Lists • Capabilities • Encryption • Key Distribution • Public Key Encryption
CS 537
Lecture Notes Part 1
Introduction Contents • • • • •
History What is an OS For? Bottom-up View Top-Down View Course Outline
History The first computers were built for military purposes during World War II, and the first commercial computers were built during the 50's. They were huge (often filling a large room with tons of equipment), expensive (millions of dollars, back when that was a lot of money), unreliable, and slow (about the power of today's $1.98 pocket calculator). Originally, there was no distinction between programmer, operator, and end-user (the person who wants something done). A physicist who wanted to calculate the trajectory of a missile would sign up for an hour on the computer. When his time came, he would come into the room, feed in his program from punched cards or paper tape, watch the lights flash, maybe do a little debugging, get a print-out, and leave. The first card in the deck was a bootstrap loader. The user/operator/programmer would push a button that caused the card reader to read that card, load its contents into the first 80 locations in memory, and jump to the start of memory, executing the instructions on that card. Those instructions read in the rest of the cards, which contained the instructions to perform all the calculations desired: what we would now call the "application program". This set-up was a lousy way to debug a program, but more importantly, it was a waste of the fabulously expensive computer's time. Then someone came up with the idea of batch processing. User/programmers would punch their jobs on decks of cards, which they would submit to a professional operator. The operator would combind the decks into batches. He would precede the batch with a batch executive (another deck of cards). This program would read the remaining programs into memory, one at a time, and run time. The operator would take the printout from the printer, tear off the part associated with each job, wrap it around the asociated deck, and put it in an output bin for the user to pick up. The main benefit of this approach was that it minimized the wasteful down time between jobs. However, it did not solve the growing I/O bottleneck. Card readers and printers got faster, but since they are mechanical devices, there were limits to how fast they could go. Meanwhile the central processing unit (CPU) kept getting faster and was spending more and more time idly waiting for the next card to be read in or the next line of output to be printed. The next advance was to replace the card reader and printer with magnetic tape drives, which were much faster. A separate, smaller, slower (and persumably cheaper) peripheral computer would copy batches of input decks onto tape and transcribe output tapes to print. The situation was better, but there were still problems. Even magnetic tapes drives were not fast enough to keep the mainframe CPU busy, and the peripheral computers, while cheaper than the mainframe, were still not cheap (perhaps hundreds of thousands of dollars).
Then someone came up with a brilliant idea. The card reader and printer were hooked up to the mainframe (along with the tape drives) and the mainframe CPU was reprogrammed to swtich rapidly among several tasks. First it would tell the card reader to start reading the next card of the next input batch. While it was waiting for that operation to finish, it would go and work for a while on another job that had been read into "core" (main memory) earlier. When enough time had gone by for that card be read in, the CPU would temporarily set aside the main computation, start transfering the data from that card to one of the tape units (say tape 1), start the card reader reading the next card, and return to the main computation. It would continue this way, servicing the card reader and tape drive when they needed attention and spending the rest of its time on the main computation. Whenever it finished working on one job in the main computation, the CPU would read another job from an input tape that had been prepared earlier (tape 2). When it finished reading in and exceuting all the jobs from tape 2, it would swap tapes 1 and 2. It would then start executing the jobs from tape 1, while the input "process" was filling up tape 2 with more jobs from the card reader. Of course, while all this was going on, a similar process was copying output from yet another tape to the printer. This amazing juggling act was called Simultaneous Peripheral Operations On Line, or SPOOL for short. The hardware that enabled SPOOLing is called direct memory access, or DMA. It allows the card reader to copy data directly from cards to core and the tape drive to copy data from core to tape, while the expensive CPU was doing something else. The software that enabled SPOOLing is called multiprogramming. The CPU switches from one activity, or "process" to another so quickly that it appears to be doing several things at once. In the 1960's, multiprogramming was extended to ever more ambitious forms. The first extension was to allow more than one job to execute at a time. Hardware developments supporting this extension included decreasing cost of core memory (replaced during this period by semi-conductor randomaccess memory (RAM)), and introduction of direct-access storage devices (called DASD - pronounced "dazdy" - by IBM and "disks" by everyone else). With larger main memory, multiple jobs could be kept in core at once, and with input spooled to disk rather than tape, each job could get directly at its part of the input. With more jobs in memory at once, it became less likely that they would all be simultaneously blocked waiting for I/O, leaving the expensive CPU idle. Another break-through idea from the 60's based on multiprogramming was timesharing, which involves running multiple interactive jobs, switching the CPU rapidly among them so that each interactive user feels as if he has the whole computer to himself. Timesharing let the programmer back into the computer room - or at least a virtual computer room. It allowed the development of interactive programming, making programmers much more productive. Perhaps more importantly, it supported new applications such as airline reservation and banking systems that allowed 100s or even 1000s of agents or tellers to access the same computer "simultaneously". Visionaries talked about an "computing utility" by analogy with the water and electric utilities, which would delived low-cost computing power to the masses. Of course, it didn't quite work out that way. The cost of computers dropped faster than almost anyone expected, leading to mini computers in the '70s and personal computers (PCs) in the 80's. It was only in the 90's that the idea was revived, in the form of an information utility otherwise known as the information superhighway or the World-Wide Web. Today, computers are used for a wide range of applications, including personal interactive use (word-processing, games, desktop publishing, web browing, email), real-time systems (patient care, factories, missiles), embedded systems (cash registers, wrist watches, tosters), and transaction processing (banking, reservations, e-commerce).
What is an OS For? Beautification Principle
The goal of an OS is to make hardware look better than it is. • More regular, uniform (instead of lots of idiosyncratic devices) • Easier to program (e.g., don't have to worry about speeds, asynchronous events) • Closer to what's needed for applications: • named, variable-length files, rather than disk blocks • multiple ``CPU's'', one for each user (in shared system) or activity (in single-user system) • multiple large, dynamically-growing memories (virtual memory) Resource principle • The goal of an OS is to mediate sharing of scarce resources
•
•
Q:
What is a ``resource''?
A:
Something that costs money!
Why share? • expensive devices • need to share data (database is an ``expensive device''!) • cooperation between people (community is an ``expensive device''!!) Problems: • getting it to work at all • getting it to work efficiently • utilization (keeping all the devices busy) • throughput (getting a lot of useful work done per hour) • response (getting individual things done quickly) • getting it to work correctly • limiting the effects of bugs (preventing idiots from ruining it for everyone) • preventing unauthorized • access to data • modification of data • use of resources (preventing bad guys from ruining it for everyone)
Bottom-up View (starting with the hardware) Hardware (summary; more details later) •
components
•
• •
• one or more central processing units (CPU's) • main memory (RAM, core) • I/O devices • bus, or other communication mechanism connects them all together CPU has a PC1 • fetches instructions one at a time from location specified by PC • increments PC after fetching instruction; branch instructions can also alter the PC • responds to "interrupts" by jumping to a different location (like an unscheduled procedure call) Memory responds to "load" and "store" requests from the CPU, one at a time. I/O device • Usually looks like a chunk of memory to the CPU. • CPU sets options and starts I/O by sending "store" requests to a particular address. • CPU gets back status and small amounts of data by issuing "load" requests. • Direct memory access (DMA): Device may transfer large amounts of data directly to/from memory by doing loads and stores just like a CPU. • Issues an interrupt to the CPU to indicate that it is done.
Timing problem • •
•
• •
I/O devices are millions or even billions of times slower than CPU. E.g.: • Typical PC is >10 million instructions/sec • Typical disk takes > 10 ms to get one byte from disk ratio: 100,000 : 1 • Typical typist = 60 wpm = 1 word = 5 bytes/sec = 200 ms = 2 million instructions per key-stroke. And that doesn't include head-scratching time! Solution: start disk device do 100,000 instructions of other useful computation wait for disk to finish Terrible program to write; debug. And it would change with a faster disk! Better solution: Process 1: for (;;) { start I/O wait for it to finish use the data for something } Process 2: for (;;) { do some useful computation } Operating system takes care of switching back and forth between process 1 and process 2 as ``appropriate''. (Question: which process should have higher priority?)
Space problem •
•
Most of the time, a typical program is "wasting" most of the memory space allocated to it. • Looping in one subroutine (wasting space allocated to rest of program) • Fiddling with one data structure (wasting space allocated to other data structures) • Waiting for I/O or user input (wasting all of its space) Solution: virtual memory • Keep program and data on disk (100-1000 times cheaper/byte). • OS automatically copies to memory pieces needed by program on demand.
Top-Down View (what does it look like to various kinds of users?) •
•
•
End user. • Wants to get something done (bill customers, write a love letter, play a game, design a bomb). • Doesn't know what an OS is (or care!) May not even realize there is a computer there. Application programmer. • Writes software for end users. Uses ``beautified'' virtual machine • named files of unlimited size • unlimited memory • read/write returns immediately • Calls library routines • some really are just subroutines written by someone else • sort an array • solve a differential equation • search a string for a character • others call the operating system • read/write • create process • get more memory Systems programmer (you, at the end of this course) • Creates abstractions for application programmers • Deals with real devices
Course Outline 1. Processes. • What processes are. • Using processes • synchronization and communication • semaphores, critical regions, monitors, conditions, • messages, pipes • process structures • pipelines, producer/consumer, remote procedure call • deadlock
•
2.
3.
4.
5.
Implementing processes • mechanism • critical sections • process control block • process swap • semaphores, monitors • policy (short-term scheduling) • fcfs, round-robin, shortest-job next, multilevel queues Memory • Main-memory allocation • Swapping, overlays • Stack allocation (implementation of programming languages) • Virtual memory hardware • paging, segmentation, translation lookaside buffer • policy • page-replacement algorithms • random, fifo, lru, clock, working set I/O devices • device drivers, interrupt handlers • disks • hardware characteristics • disk scheduling • elevator algorithm File systems • file naming • file structure (user's view) • flat (array of bytes) • record-structured • indexed • random-access • metadata • mapped files • implementation • structure • linked, tree-structured, B-tree • inodes • directories • free-space management Protection and security • threats • access policy • capabilities, access-control lists • implementation
• •
1
authentication/determination/enforcement encryption • conventional • public-key • digital signatures
In this course PC stands for program counter, not personal computer or politically correct
CS 537 Lecture Notes Part 2
Java for C++ Programmers Contents • • • • • • • • • • • • • •
Introduction A First Example Names, Packages, and Separate Compilation Values, Objects, and Pointers Garbage Collection Static, Final, Public, and Private Arrays Strings Constructors and Overloading Inheritance, Interfaces, and Casts Exceptions Threads Input and Output Other Goodies
Introduction The purpose of these notes is to help students in Computer Sciences 537 (Introduction to Operating Systems) at the University of Wisconsin - Madision learn enough Java to do the course projects. The Computer Sciences Department is in the process of converting most of its classes from C++ to Java as the principal langauge for programming projects. CS 537 was the first course to make the switch, Fall term, 1996. At that time vitually all the students had heard of Java and none had used it. Over the last few years more and more of our courses were converted to Java. Finally last year (1998-99), the introductory programming prerequisites for this course, CS 302 and CS 367, were taught in Java. Nonetheless, many students are unfamiliar with Java, having learned how to program from earlier versions of 302 and 367, or from courses at other institutions.
Applications vs Applets The first thing you have to decide when writing a Java program is whether you are writing an application or an applet. An applet is piece of code designed to display a part of a document. It is run by a browser (such as Netscape Navigator or Microsoft Internet Explorer) in response to an