0cm [UNIX 6th Edition Commentary] []UNIX 6th Edition Commentary [1] []1
SIXTH EDITION
UNIX OPERATING SYSTEM
J. Lions.
Department of Computer Science The University of New South Wales
This booklet has been produced for students at the University of New South Wales taking courses 6.602B and 6.657G. It is intended as a companion to, and commentary on, the booklet UNIX Operating System Source Code, Level Six.
The UNIX Software System was written by K. Thompson and D. Ritchie of Bell Laboratories, Murray Hill, NJ. It has been made available under a license from the Western Electric Company.
This document may contain information covered by one or more licenses, copyrights and non-disclosure agreements. Circulation of this document is restricted to holders of a license for the UNIX Software System from Western Electric. All other circulation or reproduction is prohibited.
c
Copyright 1977 J. Lions
Contents
1 Introduction 1
1.1 The UNIX Operating System : : : : 1 1.2 Utilities : : : : : : : : : : : : : : : : 1 1.3 Other Documentation : : : : : : : : 1 1.4 UNIX Programmer's Manual : : : : 1 1.5 UNIX Documents: : : : : : : : : : : 2 1.6 UNIX Operating System Source Code 2 1.7 Source Code Selections: : : : : : : : 3 1.8 Source Code Files: : : : : : : : : : : 3 1.9 Use of these notes : : : : : : : : : : 3 1.10 A Note on Programming Standards: 3
2 Fundamentals 4
2.1 The Processor: : : : : : : : : : : : : 4 2.2 Processor Status Word : : : : : : : : 4 2.3 General Registers : : : : : : : : : : : 4 2.4 Instruction Set : : : : : : : : : : : : 4 2.5 Addressing Modes : : : : : : : : : : 6 2.6 Unix Assembler : : : : : : : : : : : : 7 2.7 Memory Management : : : : : : : : 7 2.8 Segmentation Registers. : : : : : : : 7 2.9 Page Description Register : : : : : : 8 2.10 Memory Allocation : : : : : : : : : : 8 2.11 Memory Management Status Registers 8 2.12 \i" and \d" Spaces : : : : : : : : : : 8 2.13 Initial Conditions: : : : : : : : : : : 8 2.14 Special Device Registers : : : : : : : 8
3 Reading \C" Programs 10
3.1 Some Selected Examples : : : : : : : 10 3.2 Example 1: : : : : : : : : : : : : : : 10 3.3 Example 2: : : : : : : : : : : : : : : 10 3.4 Example 3: : : : : : : : : : : : : : : 10 3.5 Example 4: : : : : : : : : : : : : : : 11 3.6 Example 5: : : : : : : : : : : : : : : 12 3.7 Example 6: : : : : : : : : : : : : : : 13 3.8 Example 7: : : : : : : : : : : : : : : 13 3.9 Example 8: : : : : : : : : : : : : : : 13 3.10 Example 9: : : : : : : : : : : : : : : 14 3.11 Example 10 : : : : : : : : : : : : : : 14 3.12 Example 11 : : : : : : : : : : : : : : 14 3.13 Example 12 : : : : : : : : : : : : : : 15 3.14 Example 13 : : : : : : : : : : : : : : 15 3.15 Example 14 : : : : : : : : : : : : : : 15 3.16 Example 15 : : : : : : : : : : : : : : 15 3.17 Example 16 : : : : : : : : : : : : : : 16 3.18 Example 17 : : : : : : : : : : : : : : 16
4 An Overview 18
4.1 Variable Allocation : : : : : : : : : : 18 4.2 Global Variables : : : : : : : : : : : 18 4.3 The `C' Preprocessor : : : : : : : : : 18 4.4 Section One : : : : : : : : : : : : : : 18 4.5 The First Group of `.h' Files: : : : : 19 4.6 Assembly Language Files : : : : : : 19
4.7 Other Files in Section One: : : : : : 19 4.8 Section Two: : : : : : : : : : : : : : 19 4.9 Section Three : : : : : : : : : : : : : 20 4.10 Section Four: : : : : : : : : : : : : : 20 4.11 Section Five : : : : : : : : : : : : : : 21
5 Two Files 22
5.1 The File `malloc.c' : : : : : : : : : : 22 5.2 Rules for List Maintenance : : : : : 22 5.3 malloc (2528) : : : : : : : : : : : : : 23 5.4 mfree (2556): : : : : : : : : : : : : : 23 5.5 In conclusion ... : : : : : : : : : : : : 24 5.6 The File `prf.c' : : : : : : : : : : : : 24 5.7 printf (2340) : : : : : : : : : : : : : 24 5.8 printn (2369) : : : : : : : : : : : : : 25 5.9 putchar (2386) : : : : : : : : : : : : 25 5.10 panic (2419): : : : : : : : : : : : : : 26 5.11 prdev (2433), deverror (2447) : : : : 26 5.12 Included Files : : : : : : : : : : : : : 26
6 Getting Started 28
6.1 Operator Actions : : : : : : : : : : : 28 6.2 start (0612) : : : : : : : : : : : : : : 28 6.3 main (1550) : : : : : : : : : : : : : : 29 6.4 Processes : : : : : : : : : : : : : : : 30 6.5 Initialisation of proc[0] : : : : : : : : 30 6.6 The story continues ... : : : : : : : : 30 6.7 sched (1940): : : : : : : : : : : : : : 31 6.8 sleep (2066) : : : : : : : : : : : : : : 31 6.9 swtch (2178) : : : : : : : : : : : : : 31 6.10 main revisited: : : : : : : : : : : : : 32
7 Processes 33
7.1 The Process : : : : : : : : : : : : : : 33 7.2 The proc Structure (0358) : : : : : : 33 7.3 The user Structure (0413) : : : : : : 34 7.4 The Per Process Data Area : : : : : 34 7.5 The Segments : : : : : : : : : : : : : 34 7.6 Execution of an Image : : : : : : : : 35 7.7 Kernel Mode Execution : : : : : : : 35 7.8 User Mode Execution : : : : : : : : 35 7.9 An Example: : : : : : : : : : : : : : 35 7.10 Setting the Segmentation Registers : 36 7.11 estabur (1650) : : : : : : : : : : : : 36 7.12 sureg (1739) : : : : : : : : : : : : : : 36 7.13 newproc (1826) : : : : : : : : : : : : 37
8 Process Management 38
8.1 Process Switching: : : : : : : : : : : 38 8.2 Interrupts : : : : : : : : : : : : : : : 38 8.3 Program Swapping : : : : : : : : : : 38 8.4 Jobs : : : : : : : : : : : : : : : : : : 38 8.5 Assembler Procedures : : : : : : : : 38 8.6 savu (0725) : : : : : : : : : : : : : : 38 8.7 retu (0740) : : : : : : : : : : : : : : 39 8.8 aretu (0734) : : : : : : : : : : : : : : 39 8.9 swtch (2178) : : : : : : : : : : : : : 39
8.10 setpri (2156) : : : : : : : : : : : : : 39 8.11 sleep (2066) : : : : : : : : : : : : : : 40 8.12 wakeup (2113) : : : : : : : : : : : : 40 8.13 setrun (2134) : : : : : : : : : : : : : 40 8.14 expand (2268): : : : : : : : : : : : : 40 8.15 swtch revisited : : : : : : : : : : : : 41 8.16 Critical Sections : : : : : : : : : : : 41
9 Hardware Interrupts and Traps 42
9.1 Hardware Interrupts : : : : : : : : : 42 9.2 The Interrupt Vector : : : : : : : : : 42 9.3 Interrupt Handlers : : : : : : : : : : 42 9.4 Priorities : : : : : : : : : : : : : : : 43 9.5 Interrupt Priorities : : : : : : : : : : 43 9.6 Rules for Interrupt Handlers: : : : : 43 9.7 Traps : : : : : : : : : : : : : : : : : 43 9.8 Assembly Language `trap' : : : : : : 44 9.9 Return : : : : : : : : : : : : : : : : : 44
10 The Assembler \Trap" Routine 45
10.1 Sources of Traps and Interrupts : : : 45 10.2 fuibyte (0814), fuiword (0844) : : : : 45 10.3 Interrupts : : : : : : : : : : : : : : : 46 10.4 call (0776) : : : : : : : : : : : : : : : 46 10.5 User Program Traps : : : : : : : : : 46 10.6 The Kernel Stack : : : : : : : : : : : 47
11 Clock Interrupts 48
11.1 clock (3725) : : : : : : : : : : : : : : 48 11.2 timeout (3845) : : : : : : : : : : : : 49
12 Traps and System Calls 50
12.1 trap (2693) : : : : : : : : : : : : : : 50 12.2 Kernel Mode Traps : : : : : : : : : : 50 12.3 User Mode Traps : : : : : : : : : : : 50 12.4 System Calls : : : : : : : : : : : : : 51 12.5 System Call Handlers : : : : : : : : 52 12.6 The File `sysl.c' : : : : : : : : : : : : 52 12.7 exec (3020) : : : : : : : : : : : : : : 52 12.8 fork (3322) : : : : : : : : : : : : : : 53 12.9 sbreak (3354) : : : : : : : : : : : : : 53 12.10The Files `sys2.c' and `sys3.c' : : : : 53 12.11The File `sys4.c' : : : : : : : : : : : 53
13 Software Interrupts 54
13.1 Anticipation: : : : : : : : : : : : : : 54 13.2 Causation : : : : : : : : : : : : : : : 54 13.3 Eect : : : : : : : : : : : : : : : : : 54 13.4 Tracing : : : : : : : : : : : : : : : : 54 13.5 Procedures : : : : : : : : : : : : : : 54 13.6 A. Anticipation : : : : : : : : : : : : 55 13.7 B. Causation : : : : : : : : : : : : : 55 13.8 C. Eect: : : : : : : : : : : : : : : : 55 13.9 D. Tracing: : : : : : : : : : : : : : : 55 13.10ssig (3614): : : : : : : : : : : : : : : 55 13.11kill (3630) : : : : : : : : : : : : : : : 55 13.12signal (3949) : : : : : : : : : : : : : 55 13.13psignal (3963): : : : : : : : : : : : : 55
13.14issig (3991) : : : : : : : : : : : : : : 56 13.15psig (4043) : : : : : : : : : : : : : : 56 13.16core (4094) : : : : : : : : : : : : : : 56 13.17grow (4136) : : : : : : : : : : : : : : 56 13.18exit (3219): : : : : : : : : : : : : : : 57 13.19rexit (3205) : : : : : : : : : : : : : : 57 13.20wait (3270) : : : : : : : : : : : : : : 57 13.21Tracing : : : : : : : : : : : : : : : : 57 13.22stop (4016) : : : : : : : : : : : : : : 58 13.23wait (3270) (continued) : : : : : : : 58 13.24ptrace (4164) : : : : : : : : : : : : : 58 13.25procxmt (4204) : : : : : : : : : : : : 59
14 Program Swapping 60
14.1 Text Segments : : : : : : : : : : : : 60 14.2 sched (1940): : : : : : : : : : : : : : 60 14.3 xswap (4368) : : : : : : : : : : : : : 61 14.4 xalloc (4433) : : : : : : : : : : : : : 61 14.5 xfree (4398) : : : : : : : : : : : : : : 62
15 Introduction to Basic I/O 63
15.1 The File `buf.h' : : : : : : : : : : : : 63 15.2 devtab (4551) : : : : : : : : : : : : : 63 15.3 The File `conf.h' : : : : : : : : : : : 63 15.4 The File `conf.c' : : : : : : : : : : : 63 15.5 System Generation : : : : : : : : : : 63 15.6 swap (5196) : : : : : : : : : : : : : : 63 15.7 Race Conditions : : : : : : : : : : : 64 15.8 Reentrancy : : : : : : : : : : : : : : 65 15.9 For the Uninitiated: : : : : : : : : : 65 15.10Additional Reading: : : : : : : : : : 65
16 The RK Disk Driver 66
16.1 The le `rk.c' : : : : : : : : : : : : : 66 16.2 rkstrategy (5389) : : : : : : : : : : : 67 16.3 rkaddr (5420) : : : : : : : : : : : : : 67 16.4 devstart (5096) : : : : : : : : : : : : 67 16.5 rkintr (5451) : : : : : : : : : : : : : 67 16.6 iodone (5018) : : : : : : : : : : : : : 67
17 Buer Manipulation 68
17.1 Flags: : : : : : : : : : : : : : : : : : 68 17.2 A Cache-like Memory : : : : : : : : 68 17.3 clrbuf (5038) : : : : : : : : : : : : : 68 17.4 incore (4899) : : : : : : : : : : : : : 68 17.5 getblk (4921) : : : : : : : : : : : : : 68 17.6 brelse (4869) : : : : : : : : : : : : : 69 17.7 binit (5055) : : : : : : : : : : : : : : 69 17.8 bread (4754) : : : : : : : : : : : : : 69 17.9 breada (4773) : : : : : : : : : : : : : 70 17.10bwrite (4809) : : : : : : : : : : : : : 70 17.11bawrite (4856) : : : : : : : : : : : : 70 17.12bdwrite (4836) : : : : : : : : : : : : 70 17.13bush (5229) : : : : : : : : : : : : : 70 17.14physio (5259) : : : : : : : : : : : : : 70