Linux Advanced Programming {Development} Tutorial <br /> Part 1 Linux Kernel Part 2 Linux Kernel Module Programming Guide Part 3 Linux Programmer’s Guide Part 4 Linux Kernel Concept System Structure Part 5 Linux Kernel Specific System Structure
Table of Contents Introduction to Raymond Preface Linux Documentation Engineering Group "Announcement" Translator's Preface Part One Linux Kernel Preface Chapter 1 Hardware Basics and Software Basics 6 1.1 Hardware Basics 6 1.1.1 CPU 7 1.1.2 Memory 8 1.1.3 Bus 8 1.1.4 Controllers and Peripherals 8 1.1.5 Address space 9 1.1.6 Clock 9 1.2 Software Basics 9 1.2.1 Computer Language 9 1.2.2 What is an operating system 11 1.2.3 Kernel data structure 13 Chapter 2 Memory Management 15 2.1 Virtual memory abstract model 15 2.1.1 Request paging 17 2.1.2 Exchange 17 2.1.3 Shared virtual memory 18 2.1.4 Physical addressing mode and virtual addressing mode 18 2.1.5 Access control 18 2.2 Caching 19 2.3 Linux page table 20 2.4 Page allocation and recycling 21 2.4.1 Page Allocation 22 2.4.2 Page recycling 22 2.5 Memory mapping 22 2.6 Request paging 23 2.7 Linux page cache 24 2.8 Page swapping out and elimination 25 2.8.1 Reducing buffer and page cache sizes 25 2.8.2 Swapping out System V shared memory pages 26 2.8.3 Swapping out and retiring pages 27 2.9 Swap Cache 27 2.10 page change 28 Chapter 3 Process 29 3.1 Linux processes 29 3.2 Identifiers 31 3.3 Scheduling 32 3.4 Documentation 34 3.5 Virtual Memory 35 3.6 Creating a process 36 3.7 Time and Timers 37 3.8 Executing the program 38 3.8.1 ELF 39 3.8.2 Script files 40 Chapter 4 Inter-process Communication Mechanism 41 4.1 Signaling mechanism 41 4.2 Pipes 42 4.3 Sockets 44 4.3.1 System V’s inter-process communication mechanism 44 4.3.2 Message Queue 44 4.3.3 Semaphore 45 4.3.4 Shared storage 47 Chapter 5 PCI 49 5.1 PCI address space 49 5.2 PCI configuration header 50 5.3 PCI I/O and storage address space 51 5.4 PCI-ISA Bridge 51 5.5 PCI-PCI Bridge 51 5.5.1 PCI-PCI Bridge: PCI I/O and Storage Addresses Window to space 51 5.5.2 PCI-PCI Bridge: PCI Configuration Cycle and PCI Bus number 52 5.6 Linux PCI initialization 53 5.6.1 Linux kernel PCI data structure 53 5.6.2 PCI device driver 53 5.6.3 PCI BIOS functions 56 5.6.4 PCI correction process 57 Chapter 6 Interrupt Handling and Device Drivers 60 6.1 Interrupts and interrupt handling 60 6.1.1 Programmable interrupt controller 61 6.1.2 Initialize interrupt processing data structure 61 6.1.3 Interrupt handling 62 6.2 Device Drivers 63 6.2.1 Testing and Interruption 64 6.2.2 Direct Memory Access (DMA) 65 6.2.3 Memory 66 6.2.4 Interface between device driver and kernel 66 6.2.5 Hard disk 69 6.2.6 Network equipment 74 Chapter 7 File Systems 77 7.1 The second extended file system EXT2 78 7.1.1 Inode node of EXT2 system 79 7.1.2 Super block in EXT2 system 80 7.1.3 Group descriptors for EXT2 systems 80 7.1.4 Directory of EXT2 system 81 7.1.5 Finding files in the EXT2 file system 81 7.1.6 Changing files in the EXT2 file system Size 82 7.2 Virtual File System 83 7.2.1 Super block of VFS file system 84 7.2.2 Inode node of VFS file system 84 7.2.3 Registering the file system 85 7.2.4 Assembling the file system 85 7.2.5 Finding files in a virtual file system 87 7.2.6 Unmounting the file system 87 7.2.7 Inode cache of VFS file system 87 7.2.8 Directory Caching 88 7.3 Buffer caching 88 7.3.1 bdflush kernel daemon 90 7.3.2 update process 90 7.4 /proc file system 91 7.5 Special device files 91 Chapter 8 Network 92 8.1 Overview of TCP/IP Network 92 8.2 TCP/IP network hierarchy in Linux 95 8.3 BSD socket interface 96 8.4 INET’s socket layer 97 8.4.1 Creating a BSD socket 98 8.4.2 Binding address for INET BSD Socket 99 8.4.3 Establishing INET BSD Socket connection 99 8.4.4 INET BSD Socket Listening 100 8.4.5 Accepting connection requests 100 8.5 IP layer 100 8.5.1 Socket Buffer 100 8.5.2 Receiving IP messages 101 8.5.3 Sending IP packets 102 8.5.4 Data Sharding 102 8.6 Address Resolution Protocol 103 8.7 IP Routing 104 Chapter 9 Kernel Mechanisms and Modules 107 9.1 Kernel Mechanism 107 9.1.1 Bottom Half Control 107 9.1.2 Task Queue 108 9.1.3 Timers 109 9.1.4 Waiting queue 110 9.1.5 Spin lock 110 9.1.6 Semaphore 110 9.2 Modules 111 9.2.1 Module loading 112 9.2.2 Module uninstallation 113 Chapter 10 Processors 115 10.1X86 115 10.2 ARM 115 10.3 Alpha AXP Processor 115 Chapter 11 Linux Kernel Source Code 117 11.1 How to obtain the Linux kernel source code 117 11.2 Arrangement of kernel source code 117 11.3 Where to start 118 Chapter 12 Linux Data Structures 120 Appendix A Useful Web and FTP Sites 138 Appendix B Glossary 139 Part 2 Linux Kernel Module Programming Guide Acknowledgments Preface Chapter 1 Hello, World 145 1.1 Makefiles of the kernel module 146 1.2 Multi-file kernel module 147 Chapter 2 Character Device Files 149 Chapter 3 /proc File System 158 Chapter 4 Using /proc for Input 162 Chapter 5 Using Device Files for Input 170 Chapter 6 Startup Parameters 182 Chapter 7 System Calls 185 Chapter 8 Blocking Handling 190 Chapter 9 Replacing printk 199 Chapter 10 Task Scheduling 202 Chapter 11 Interrupt Handlers 207 Chapter 12 Symmetric multiprocessing 211 Chapter 13 Common Mistakes 212 Appendix A Differences between 2.0 and 2.2 213 Appendix B Other Resources 214 Appendix C Give your evaluation 215 Part 3 Linux Programmer’s Guide Chapter 1 Linux Operating System 219 Chapter 2 Linux Kernel 220 Chapter 3 Linux libc package 221 Chapter 4 System Calls 222 Chapter 5 “Swiss Army Knife”: ioctl 223 Chapter 6 Linux Inter-Process Communication 224 6.1 Introduction 224 6.2 Half-duplex Unix pipes 224 6.2.1 Basic concepts 224 6.2.2 Creating pipelines in C language 225 6.2.3 Simple method 229 6.2.4 Atomic operations on pipes 233 6.2.5 Several things to note about half-duplex pipes Issue 233 6.3 Named Pipes 234 6.3.1 Basic concepts 234 6.3.2 Creating FIFO 234 6.3.3 FIFO operation 235 6.3.4 Blocking action on FIFO 236 6.3.5 SIGPIPE signal 237 6.4 System V IPC 237 6.4.1 Basic concepts 237 6.4.2 Message Queue 239 6.4.3 Semaphore 251 6.4.4 semtool: interactive semaphore operations Program 260 6.4.5 Shared memory 267 Chapter 7 Sound Programming 274 7.1 Internal Speaker Programming 274 7.2 Sound card programming 274 Chapter 8 Character Unit Graphics 276 8.1 I/O functions in libc 277 8.1.1 Formatted output 277 8.1.2 Formatted input 278 8.2 termcap library 278 8.2.1 Preface 278 8.2.2 Obtaining terminal description 279 8.2.3 View terminal description 279 8.2.4 termcap capabilities 280 8.3 Introduction to Ncurses 280 8.4 Initialization 282 8.5 Windows 283 8.6 Output 284 8.6.1 Formatted output 285 8.6.2 Inserting characters/line 286 8.6.3 Delete characters/lines 286 8.6.4 Boxes and Lines 287 8.6.5 Background characters 287 8.7 Input 288 8.8 Options 289 8.8.1 Output options 289 8.8.2 Input options 290 8.8.3 Terminal properties 291 8.8.4 Using options 291 8.9 Update terminal 292 8.10 Video properties and colors 293 8.11 Cursor and screen coordinates 294 8.12 Rolling 294 8.13 Keypad 295 8.14 Soft tags 295 8.15 Miscellaneous 295 8.16 Low-level access 296 8.17 Screen Dump 296 8.18 Termcap simulation 296 8.19 Terminfo function 296 8.20 Debugging functions 297 8.21 Terminfo capabilities 297 8.21.1 Boolean capabilities 297 8.21.2 Numerical capabilities 298 8.21.3 String capabilities 299 8.22 [N]Curses function overview 306 Chapter 9 I/O Port Programming 307 9.1 Mouse Programming 307 9.2 Modem Programming 308 9.3 Printer Programming 308 9.4 Joystick Programming 308 Chapter 10 Porting Applications to Linux 309 10.1 Introduction 309 10.2 Signal processing 309 10.2.1 Under SVR4, BSD and POSIX.1 Signal 310 10.2.2 Linux signal options 310 10.2.3 Signals under Linux 310 10.2.4 Signals supported by Linux 311 10.3 Terminal I/O 311 10.4 Process information and control 311 10.4.1 kvm process 312 10.4.2 ptrace and /proc file system 312 10.4.3 Process control under Linux 312 10.5 Portable conditional compilation 313 10.6 Supplementary instructions 314 Appendix Alphabetical System Calls 315 Part 4 Summary of Linux Kernel Concept System Structure 323 Preface 324 Chapter 1 System Structure 325 1.1 System Overview 325 1.2 Goals of the kernel 325 1.3 Overview of kernel structure 325 1.4 Support multiple developers 327 1.5 System data structure 328 1.5.1 Task List 328 1.5.2 Memory mapping 328 1.5.3 Index nodes 328 1.5.4 Data connection 329 Chapter 2 System Structure of Subsystems 330 2.1 Process scheduler system structure 330 2.1.1 Goals 330 2.1.2 Module 330 2.1.3 Data expression 331 2.1.4 Dependencies, data flow and control flow 331 2.2 Memory manager system structure 331 2.2.1 Goals 331 2.2.2 Modules 331 2.2.3 Data representation 331 2.2.4 Data flow, control flow and dependencies 332 2.3 Virtual file system system structure 333 2.3.1 Goals 333 2.3.2 Modules 333 2.3.3 Data representation 333 2.3.4 Data flow, control flow and dependencies 334 2.4 Network interface system structure 334 2.4.1 Goals 334 2.4.2 Modules 334 2.4.3 Data representation 335 2.4.4 Data flow, control flow and dependencies 335 2.5 Inter-process communication system structure 335 Chapter 3 Conclusion 336 Appendix A Definition of Terms 337 Appendix B References 338 Part 5 Summary of the specific system structure of the Linux kernel 341 Chapter 1 Preface 342 1.1 Goals 342 1.2 Introduction to Linux 342 1.3 Background knowledge of software system structure 342 1.4 Methods and approaches 343 1.5 Readers for whom this book is intended 344 1.6 Chapter arrangement of this part 344 Chapter 2 System Structure 345 Chapter 3 Subsystem Structure 346 3.1 Process Scheduler 346 3.1.1 Goals 346 3.1.2 External interface 346 3.1.3 Subsystem description 346 3.1.4 Data structure 347 3.1.5 Subsystem structure 348 3.1.6 Subsystem dependencies 348 3.2 Memory Manager 348 3.2.1 Goals 348 3.2.2 External interface 349 3.2.3 Subsystem description 349 3.2.4 Data structure 350 3.2.5 Subsystem structure 350 3.2.6 Subsystem dependencies 351 3.3 Virtual file system 352 3.3.1 Goals 352 3.3.2 External interface 352 3.3.3 Subsystem description 353 3.3.4 Device Drivers 353 3.3.5 Logical file system 354 3.3.6 Modules 354 3.3.7 Data structure 355 3.3.8 Subsystem structure 355 3.3.9 Subsystem dependencies 355 3.4 Inter-process communication 355 3.4.1 Goals 355 3.4.2 External interface 357 3.4.3 Subsystem description 357 3.4.4 Data structure 358 3.4.5 Subsystem structure 359 3.4.6 Subsystem dependencies 359 3.5 Network interface 360 3.5.1 Goals 360 3.5.2 External interface 361 3.5.3 Subsystem description 361 3.5.4 Data structure 362 3.5.5 Subsystem structure 363 3.5.6 Subsystem dependencies 363 Chapter 4 Conclusion 365 Appendix A Definition of Terms 366 Appendix B References 368 Part Six Appendix Appendix A Linux Document Project Copy License 372 Appendix B GNU General Public License 374