Introduction to Operating Systems Abstractions Using Plan 9 from Bell
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Operating Systems Abstractions Using Plan 9 from Bell Labs (Draft ߞ 9/18/2006) Francisco J Ballesteros Copyright © 2006 Francisco J Ballesteros Plan 9 is Copyright © 2002 Lucent Technologies Inc. All Rights Reserved. Preface Using effectively the operating system is very important for anyone working with computers. It can be the difference between performing most tasks by hand, and asking the computer to perform them. Traditionally, Operating Systems courses used UNIX to do this. However, today there is no such thing as UNIX. Linux is a huge system, full of inconsistencies, with programs that do multiple tasks and do not perform them well. Linux manual pages just cannot be read. These lecture notes use Plan 9 from Bell Labs to teach a first (practical!) course on operating sys- tems. The system is easy to use for programmers, and is an excellent example of high-quality system design and software development. Studying its code reveals how simplicity can be more effective than contortions made by other systems. The first Operating Systems course at Rey Juan Carlos University is focused on practice. Because in theory, theory is like practice, but in practice it is not. What is important is for you to use the system, and to learn to solve problems. Theory will come later to fill the gaps and try to give more insight about what a system does and how can it be used. The whole text assumes that you have been already exposed to computer, and used at least a com- puter running Windows. This is so common that it makes no sense to drop this assumption. Furthermore, we assume that you already know how to write programs. This is indeed the case for the lecture this text is written for. One last assumption is that you attended a basic computer architecture course, and you know at least basic concepts. There is a quick review appendix in case you need to refresh your memory. Through the text, the boldface font is used when a new concept is introduced. This will help you to make quick reviews and to double check that you know the concepts. All important concepts are listed in the index, at the end of the book. The constant width teletype font is used to refer to machine data, including functions, programs, and symbol names. In many cases, text in constant width font reproduces a session with the system (e.g., typing some commands and showing their output). The text written by the user (and not by the computer) is slightly slanted, but still in constant width. Note the difference with respect to the font used for text written by a program, which is not slanted. Italics are used to emphasize things and to refer to the system manual, like in intro(1). Regarding numeric values, we use the C notation to represent hexadecimal and octal numeric bases. Unlike in most other textbooks for operating systems courses, bibliographic references are kept to the bare minimum. We cite a particular text when we think that it may be worth reading to continue learning about something said in this book. So, do not quickly dismiss references. We encourage you to read them, to learn more. There are not so many ones. If you want to get a thorough set of references for something discussed in the test, we suggest looking at a more classical operating systems textbook, like for example [1]. It is important to note that this book is not a reference for using an operating system nor a reference for Plan 9 from Bell Labs. The user’s manual that comes installed within the system is the proper reference to use. These lecture notes just shows you how things work, by using them. Once you have gone through the course, you are expected to search and use the user’s manual as a reference. One final note of caution. This text is to be read with a computer side by side. The only way to learn to use a system is by actually using it. Reading this without doing so is meaningless. I am grateful go to other colleagues who suffered or helped in one way or another to write this book. First, authors of Plan 9 from Bell Labs made an awesome system, worth describing for an Operating Sys- tems Course. It cannot be overemphasized how much help the authors of Plan 9 provide to anyone asking - 2 - questions in the 9fans list. For what is worth, I have to say that I am deeply grateful to people like Rob Pike, Dave Presotto, Jim McKie, Russ Cox, and many others. In particular, Russ Cox seems to be a pro- gram listening for questions at 9fans, at least his response time suggests that. I have learned a lot from you all (or I tried). Other colleagues from Rey Juan Carlos University helped me as well. Pedro de las Heras was eager to get new drafts for this manuscript. Sergio Arévalo was an unlimited supply of useful comments and fixes for improving this book, specially for using it as a textbook. José Centeno was scared to hell after reading our initial description of computer networks, and helped to reach a much better descrip- tion. Francisco J. Ballesteros Laboratorio de Sistemas, Rey Juan Carlos University of Madrid Madrid, Spain 2006 Table of Contents 1. Getting started 1 1.1. What is an Operating System? 1 1.2. Entering the system 3 1.3. Leaving the system 6 1.4. Editing and running commands 6 1.5. Obtaining help 8 1.6. Using files 10 1.7. Directories 12 1.8. Files and data 15 1.9. Permissions 18 1.10. Writing a C program in Plan 9 20 1.11. The Operating System and your programs 22 1.12. Where are the files? 23 1.13. The Shell, commands, binaries, and system calls 25 1.14. The Operating System and the hardware 25 2. Programs and Processes 27 2.1. Processes 27 2.2. Loaded programs 29 2.3. Process birth and death 32 2.4. System call errors 37 2.5. Environment 39 2.6. Process names and states 42 2.7. Debugging 44 2.8. Everything is a file! 46 3. Files 51 3.1. Input/Output 51 3.2. Write games 56 3.3. Read games 60 3.4. Creating and removing files 61 3.5. Directory entries 63 3.6. Listing files in the shell 68 3.7. Buffered Input/Output 71 4. Parent and Child 79 4.1. Running a new program 79 4.2. Process creation 80 4.3. Shared or not? 84 4.4. Race conditions 87 4.5. Executing another program 87 4.6. Using both calls 89 4.7. Waiting for children 90 4.8. Interpreted programs 93 5. Communicating Processes 97 5.1. Input/Output redirection 97 5.2. Conventions 102 5.3. Other redirections 102 5.4. Pipes 103 5.5. Using pipes 108 5.6. Notes and process groups 113 - 2 - 5.7. Reading, notes, and alarms 116 5.8. The file descriptor bulletin board 119 5.9. Delivering messages 121 6. Network communication 131 6.1. Network connections 131 6.2. Names 135 6.3. Making calls 137 6.4. Providing services 140 6.5. System services 146 6.6. Distributed computing 147 7. Resources, Files, and Names 149 7.1. Resource fork 149 7.2. Protecting from notes 151 7.3. Environment in shell scripts 153 7.4. Independent children 154 7.5. Name spaces 155 7.6. Local name space tricks 160 7.7. Device files 162 7.8. Unions 163 7.9. Changing the name space 165 7.10. Using names 166 7.11. Sand-boxing 168 7.12. Distributed computing revisited 170 8. Using the Shell 175 8.1. Programs are tools 175 8.2. Lists 176 8.3. Simple things 179 8.4. Real programs 182 8.5. Conditions 186 8.6. Editing text 189 8.7. Moving files around 194 9. More tools 197 9.1. Regular expressions 197 9.2. Sorting and searching 201 9.3. Searching for changes 206 9.4. AWK 210 9.5. Processing data 215 9.6. File systems 220 10. Concurrent programming 225 10.1. Synchronization 225 10.2. Locks 228 10.3. Queueing locks 235 10.4. Rendezvous 242 10.5. Sleep and wakeup 244 10.6. Shared buffers 248 10.7. Other tools 251 11. Threads and Channels 257 11.1. Threads 257 - 3 - 11.2. Thread names 260 11.3. Channels 265 11.4. I/O in threaded programs 270 11.5. Many to one communication 273 11.6. Other calls 281 12. User Input/Output 285 12.1. Console input 285 12.2. Characters and runes 288 12.3. Mouse input 291 12.4. Devices for graphics 294 12.5. Graphics 296 12.6. A graphic slider 299 12.7. Keyboard input 305 12.8. Drawing text 308 12.9. The window system 309 13. Building a File Server 317 13.1. Disk storage 317 13.2. The file system protocol 321 13.3. Semaphores for Plan 9 327 13.4. Speaking 9P 328 13.5. 9P requests 332 13.6. Semaphores 334 13.7. Semaphores as files 337 13.8. A program to make things 345 13.9. Debugging and testing 350 14. Security 353 14.1. Secure systems 353 14.2. The local machine 354 14.3. Distributed security and authentication 355 14.4. Authentication agents 358 14.5. Secure servers 364 14.6. Identity changes 367 14.7.