1. Trang chủ
  2. » Ngoại Ngữ

Beginning linux programming

804 642 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 804
Dung lượng 3,76 MB

Nội dung

B LI U N TT http://elib.ntt.edu.vn/ Table of Contents Beginning Linux Programming, Second Edition Foreword Introduction .6 Welcome .6 Who's this Book For? What's Covered in the Book What You Need to Use this Book Source Code Conventions Tell Us What You Think .10 Why Should I Return the Reply Card? .10 N TT U LI B Chapter 1: Getting Started 11 Overview .11 What is UNIX? 11 What is Linux? 11 Distributions 11 The GNU Project and the Free Software Foundation 12 Programming Linux 13 UNIX Programs 13 The C Compiler 14 Try It Out − Our First UNIX C Program 14 How It Works 15 Getting Help 15 Try It Out − Manual Pages and info 16 Development System Roadmap 17 Programs .17 Header Files 18 Library Files 19 Static Libraries .19 Try It Out − Static Libraries 20 Shared Libraries 22 UNIX Philosophy .23 Simplicity .23 Focus 23 Reusable Components 23 Filters 23 Open File Formats 23 Flexibility .23 Summary .24 Chapter 2: Shell Programming .25 Overview .25 What is a Shell? 26 Pipes and Redirection 27 Redirecting Output .27 Redirecting Input 28 Pipes .28 i http://elib.ntt.edu.vn/ Table of Contents N TT U LI B Chapter 2: Shell Programming The Shell as a Programming Language 29 Interactive Programs .29 Creating a Script 30 Making a Script Executable 31 Shell Syntax 32 Variables .33 Conditions 36 Control Structures 38 Functions 49 Try It Out − A Simple Function 50 How It Works 50 Try It Out − Returning a Value .51 How It Works 52 Commands 52 Command Execution 62 Here Documents 66 Try It Out − Using Here Documents .66 Try It Out − Another Use for a Here Document 67 How It Works 67 Debugging Scripts 67 Putting it All Together 68 Requirements 68 Design 69 Try It Out − A CD Application .70 Notes 76 Summary .77 Chapter 3: Working with Files .78 Overview .78 UNIX File Structure 78 Directories 79 Files and Devices 79 System Calls and Device Drivers .81 Library Functions 82 Low−level File Access 82 write .83 read 83 open .84 Initial Permissions 85 umask .86 close .87 ioctl 87 Try It Out − A File Copy Program 87 Other System Calls for Managing Files .89 The Standard I/O Library 91 fopen 92 fread .92 fwrite .93 fclose .93 ii http://elib.ntt.edu.vn/ Table of Contents N TT U LI B Chapter 3: Working with Files fflush 93 fseek .93 fgetc, getc, getchar 94 fputc, putc, putchar 94 fgets, gets .94 Formatted Input and Output 95 Other Stream Functions 98 Try It Out − Another File Copy Program .99 Stream Errors 99 Streams and File Descriptors 100 File and Directory Maintenance 101 chmod 101 chown 101 unlink, link, symlink 101 mkdir, rmdir 102 chdir, getcwd .102 Scanning Directories 103 opendir 103 readdir 103 telldir 104 seekdir 104 closedir 104 Try It Out − A Directory Scanning Program 105 How It Works 106 Errors 107 Advanced Topics 107 fcntl 108 mmap 109 Try It Out − Using mmap 110 Summary 111 Chapter 4: The UNIX Environment 112 Overview 112 Program Arguments 112 Try It Out − Program Arguments .113 How It Works 114 getopt 114 Try It Out − getopt .115 How It Works 116 Environment Variables .116 Try It Out − getenv and putenv0 117 Use of Environment Variables 118 The environ Variable 119 Try It Out − environ 119 How It Works 119 Time and Date 119 Try It Out − time 120 How It Works 121 Try It Out − gmtime 121 iii http://elib.ntt.edu.vn/ Table of Contents N TT U LI B Chapter 4: The UNIX Environment How It Works 122 Try It Out − ctime .123 How It Works 123 Try It Out − strftime and strptime 125 How It Works 126 Temporary Files 126 Try It Out − tmpnam and tmpfile .127 How It Works 127 User Information .128 Try It Out − User Information .129 How It Works 130 Other User Information Functions .130 Host Information .131 Try It Out − Host Information .131 How It Works 132 Licensing 132 Logging .132 Try It Out − syslog 134 How It Works 134 Configuring Logs .134 Try It Out − logmask 135 How It Works 136 Resources and Limits 136 Try It Out − Resource Limits .138 How It Works 140 Summary 140 Chapter 5: Terminals 141 Overview 141 Reading from and Writing to the Terminal .141 Try It Out − Menu Routines in C 141 How It Works 142 Why It Doesn't Quite Work 143 Handling Redirected Output .144 Try It Out − Checking for Output Redirection 144 How It Works 145 Talking to the Terminal 145 Try It Out − Using /dev/tty 146 The Terminal Driver and the General Terminal Interface 147 Overview 147 Hardware Model 148 The termios Structure 149 Input Modes 150 Output Modes .151 Control Modes 152 Local Modes .152 Special Control Characters 153 Terminal Speed 156 Additional Functions 156 iv http://elib.ntt.edu.vn/ Table of Contents Chapter 5: Terminals Try It Out − A Password Program with termios 157 How It Works 158 Try It Out − Reading Each Character 158 How It Works 159 Terminal Output 159 Terminal Type 159 Identify Your Terminal Type .160 Using terminfo Capabilities 162 Detecting Keystrokes 167 Try It Out − Your Very Own kbhit .167 How It Works 169 Pseudo Terminals .169 Summary 169 N TT U LI B Chapter 6: Curses 170 Overview 170 Compiling with curses 170 Concepts 171 Try It Out − A Simple curses Program .172 Initialization and Termination 173 Output to the Screen 173 Reading from the Screen 174 Clearing the Screen 175 Moving the Cursor 175 Character Attributes 175 Try It Out − Moving, Inserting and Attributes 176 The Keyboard 177 Keyboard Modes 177 Keyboard Input 178 Try It Out− Keyboard Modes and Input 178 How It Works 179 Windows 180 The WINDOW Structure 180 Generalized Functions 180 Moving and Updating a Window .181 Try It Out − Multiple Windows 182 Optimizing Screen Refreshes .184 Subwindows 185 Try It Out − Subwindows 185 How It Works 187 The Keypad .187 Try It Out − Using the Keypad 188 Color 189 Try It Out − Colors .190 Redefining Colors .191 Pads 191 Try It Out − Using a Pad .192 The CD Collection Application 193 Try It Out − A New CD Collection Application 194 v http://elib.ntt.edu.vn/ Table of Contents Chapter 6: Curses Try It Out − Looking at main 196 Try It Out − The Menu 196 Try It Out − Database File Manipulation 198 Try It Out − Querying the CD Database .202 Summary 206 N TT U LI B Chapter 7: Data Management 207 Overview 207 Managing Memory 207 Simple Memory Allocation 207 Try It Out − Simple Memory Allocation 208 How It Works 208 Allocating Lots of Memory 208 Try It Out − Asking for all Physical Memory 209 How It Works 209 Try It Out − Available Memory 210 How It Works 210 Abusing Memory 211 Try It Out − Abuse Your Memory 211 How It Works 212 The Null Pointer 212 Try It Out − Accessing a Null Pointer 212 How It Works 213 How It Works 213 Freeing Memory 213 Try It Out − Freeing Memory .214 How It Works 214 Other Memory Allocation Functions 214 File Locking 215 Creating Lock Files 215 Try It Out − Creating a Lock File .216 How It Works 216 Try It Out − Cooperative Lock Files 217 How It Works 218 Locking Regions 218 Use of read and write with Locking 221 Try It Out − Locking a File with fcntl 221 How It Works 222 Try It Out − Testing Locks on a File 223 How It Works 225 Competing Locks .226 Try It Out − Competing Locks 226 How It Works 228 Other Lock Commands 228 Deadlocks 229 Databases 229 The dbm Database 229 The dbm Routines 230 dbm Access Functions 232 vi http://elib.ntt.edu.vn/ Table of Contents Chapter 7: Data Management Additional dbm Functions 235 The CD Application 237 The CD Application Using dbm 238 Try It Out − cd_data.h 238 Try It Out − app_ui.c 239 Try It Out − cd_access.c .247 Summary 253 N TT U LI B Chapter 8: Development Tools 254 Overview 254 Problems of Multiple Source Files 254 The make Command and Makefiles 255 The Syntax of Makefiles 255 Options and Parameters to make 255 Comments in a makefile .258 Macros in a makefile 258 Try It Out − A Makefile with Macros 259 How It Works 259 Multiple Targets 260 Try It Out − Multiple Targets 260 How It Works 262 Built−in Rules 262 Suffix Rules 263 Try It Out − Suffix Rules 263 How It Works 264 Managing Libraries with make 264 Try It Out − Managing a Library 264 How It Works 265 Advanced Topic: Makefiles and Subdirectories 266 GNU make and gcc 266 Try It Out − gcc −MM 267 How It Works 267 Source Code Control 267 RCS 267 SCCS 273 CVS 274 Writing a Manual Page .278 Distributing Software 281 The patch Program .281 Other Distribution Utilities 283 Summary 285 Chapter 9: Debugging 286 Types of Error 286 Specification Errors 286 Design Errors 286 Coding Errors .286 General Debugging Techniques 287 A Program with Bugs 287 vii http://elib.ntt.edu.vn/ Table of Contents N TT U LI B Chapter 9: Debugging Code Inspection 289 Instrumentation 290 Try It Out − Debug Information 291 How It Works 291 Controlled Execution 292 Debugging with gdb 293 Starting gdb 293 Running a Program .294 Stack Trace 294 Examining Variables 295 Listing the Program 296 Setting Breakpoints 296 Patching with the Debugger .299 Learning more about gdb 300 More Debugging Tools .300 Lint: Removing the Fluff from Your Programs 301 Function Call Tools 302 Execution Profiling .304 Assertions 304 Problems with assert 305 Try It Out − assert 305 How It Works 306 Memory Debugging 306 ElectricFence 307 Try It Out − ElectricFence 307 How It Works 308 Checker .308 Try It Out − Checker 308 How It Works 309 Resources 310 Summary 310 Chapter 10: Processes and Signals .311 Overview 311 What is a Process? 311 Process Structure .311 The Process Table 313 Viewing Processes 313 System Processes 314 Process Scheduling .315 Starting New Processes .316 Try It Out − system .316 How It Works 317 Replacing a Process Image 317 Try It Out − execlp .318 How It Works 319 Duplicating a Process Image .319 Try It Out − fork 320 How It Works 321 viii http://elib.ntt.edu.vn/ Table of Contents B Chapter 10: Processes and Signals Waiting for a Process 321 Try It Out − wait 322 How It Works 323 Zombie Processes .323 Try It Out − Zombies 324 How It Works 324 Input and Output Redirection .325 Try It Out − Redirection .325 How It Works 326 Threads .326 Signals .326 Try It Out − Signal Handling .328 How It Works 329 Sending Signals 330 Try It Out − An Alarm Clock 330 How It Works 331 Signal Sets 334 Summary 337 N TT U LI Chapter 11: POSIX Threads .338 What is a Thread? .338 Advantages and Drawbacks of Threads .338 Checking for Thread Support 339 Try it out − POSIX compliance test 339 How it works .340 A First Threads Program 340 Try it out − a simple threaded program .342 How it works .343 Simultaneous Execution 344 Try it out − simultaneous execution of two threads 344 How it works .345 Synchronization 345 Synchronization with Semaphores .345 Try it out − a thread semaphore 347 How it works .349 Synchronization with Mutexes 350 Try it out − a thread mutex 350 How it works .352 Thread Attributes 353 detachedstate .354 schedpolicy 354 schedparam 355 inheritsched 355 scope 355 stacksize .355 Try it out − setting the detached state attribute 355 How it works .356 Thread Attributes − Scheduling 357 Try is out − scheduling 357 ix http://elib.ntt.edu.vn/ Appendix B: FSF and the GNU Project a portion of it, either verbatim or with modifications and/or translated into another language (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you" Activities other than copying, distribution and modification are not covered by this License; they are outside its scope The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program) Whether that is true depends on what the Program does You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section above, provided that you also meet all of these conditions: N TT U LI B You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, not apply to those sections when you distribute them as separate works But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License You may copy and distribute the Program (or a work based on it, under Section 3) in object code or executable form under the terms of Sections and above provided that you also one of the following: 771 http://elib.ntt.edu.vn/ Appendix B: FSF and the GNU Project Accompany it with the complete corresponding machine−readable source code, which must be distributed under the terms of Sections and above on a medium customarily used for software interchange; or, Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine−readable copy of the corresponding source code, to be distributed under the terms of Sections and above on a medium customarily used for software interchange; or, Accompany it with the information you received as to the offer to distribute corresponding source code (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable U LI B If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License N TT However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance You are not required to accept this License, since you have not signed it However, nothing else grants you permission to modify or distribute the Program or its derivative works These actions are prohibited by law if you not accept this License Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions You may not impose any further restrictions on the recipients' exercise of the rights granted herein You are not responsible for enforcing compliance by third parties to this License If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they not excuse you from the conditions of this License If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all For example, if a patent license would not permit royalty−free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program 772 http://elib.ntt.edu.vn/ Appendix B: FSF and the GNU Project If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice B This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded In such case, this License incorporates the limitation as if written in the body of this License 10 The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns N TT U LI Each version is given a distinguishing version number If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation 11 If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally NO WARRANTY BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A ARTICULAR PURPOSE THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF 773 http://elib.ntt.edu.vn/ Appendix B: FSF and the GNU Project THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms To so, attach the following notices to the program It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the ``copyright'' line and a pointer to where the full notice is found

Ngày đăng: 20/10/2017, 12:28