没有合适的资源?快使用搜索试试~ 我知道了~
首页Linux与UNIX系统编程手册:Linux编程接口详解
Linux与UNIX系统编程手册:Linux编程接口详解
需积分: 10 3 下载量 41 浏览量
更新于2024-07-24
收藏 6.92MB PDF 举报
"《Linux编程接口》是一本深入探讨Linux和UNIX系统编程的手册,作者是Michael Kerrisk。这本书获得了业界的高度评价,被赞誉为Linux编程领域的权威参考书。"
《Linux编程接口》这本书详细阐述了Linux操作系统下编程的各种接口,涵盖了从基本的系统调用到复杂的网络编程、进程间通信等多个方面。对于无论是初学者还是经验丰富的开发者,都能从中获得宝贵的知识。
1. 系统调用:书中详尽解释了Linux系统调用的工作原理,包括常见的read、write、open、close等文件操作,以及fork、execve、waitpid等进程管理调用。这些系统调用是Linux程序与操作系统交互的基础。
2. 进程管理:讲解了进程的创建、销毁、调度、同步和通信等概念,包括信号、线程、进程组和会话等高级特性,帮助读者理解多进程环境下的编程实践。
3. 内存管理:深入剖析了Linux的虚拟内存机制,包括内存分配、释放、映射和页面替换等核心概念,同时介绍了内存对齐和内存泄漏检测等相关技巧。
4. 文件系统和I/O:讨论了文件系统的基本操作,如文件打开、关闭、读写等,并探讨了异步I/O和缓冲I/O的实现,以及磁盘I/O优化策略。
5. 网络编程:涵盖了套接字编程,包括TCP/IP、UDP以及套接字选项和套接字级I/O复用。书中还介绍了IPv4/IPv6、套接字地址结构以及并发服务器的设计模式。
6. 信号处理:详细解析了信号的发送、接收、处理机制,包括信号在进程间通信中的应用,以及异步信号安全函数的概念。
7. 标准库函数:对比了C标准库函数与底层系统调用的区别,如stdio流与低级I/O,以及时间管理、字符串处理和内存管理函数的使用。
8. 错误处理和调试:指导如何正确处理错误,使用strerror、perror等工具,以及如何利用gdb进行程序调试。
9. 安全性和权限:涵盖了用户和组的管理,文件权限,以及setuid、setgid等程序权限控制机制,讲解了如何编写安全的程序。
10. 程序接口设计:探讨了如何设计符合POSIX标准的接口,以确保代码的可移植性。
这本书不仅提供了丰富的理论知识,还包含了大量的实际示例,帮助读者理解和应用所学内容。此外,作者Michael Kerrisk对书中发现的错误及时修正,并改进了相关man页,使Linux编程变得更加易懂和高效。
《Linux编程接口》是一本全面而深入的Linux系统编程指南,它将帮助读者建立起坚实的Linux系统编程基础,无论是在开发新项目还是维护现有系统,都是不可或缺的参考资料。
xiv Contents in Detail
11 SYSTEM LIMITS AND OPTIONS 211
11.1 System Limits............................................................................................................. 212
11.2 Retrieving System Limits (and Options) at Run Time ........................................................ 215
11.3 Retrieving File-Related Limits (and Options) at Run Time.................................................. 217
11.4 Indeterminate Limits ................................................................................................... 219
11.5 System Options......................................................................................................... 219
11.6 Summary.................................................................................................................. 221
11.7 Exercises .................................................................................................................. 222
12 SYSTEM AND PROCESS INFORMATION 223
12.1 The /proc File System................................................................................................. 223
12.1.1 Obtaining Information About a Process:
/proc/PID................................... 224
12.1.2 System Information Under
/proc.............................................................. 226
12.1.3 Accessing
/proc Files ............................................................................ 226
12.2 System Identification:
uname() .................................................................................... 229
12.3 Summary.................................................................................................................. 231
12.4 Exercises .................................................................................................................. 231
13 FILE I/O BUFFERING 233
13.1 Kernel Buffering of File I/O: The Buffer Cache .............................................................. 233
13.2 Buffering in the
stdio Library ....................................................................................... 237
13.3 Controlling Kernel Buffering of File I/O........................................................................ 239
13.4 Summary of I/O Buffering .......................................................................................... 243
13.5 Advising the Kernel About I/O Patterns........................................................................ 244
13.6 Bypassing the Buffer Cache: Direct I/O........................................................................ 246
13.7 Mixing Library Functions and System Calls for File I/O .................................................. 248
13.8 Summary.................................................................................................................. 249
13.9 Exercises .................................................................................................................. 250
14 FILE SYSTEMS 251
14.1 Device Special Files (Devices) ..................................................................................... 252
14.2 Disks and Partitions ................................................................................................... 253
14.3 File Systems.............................................................................................................. 254
14.4 I-nodes..................................................................................................................... 256
14.5 The Virtual File System (VFS) ....................................................................................... 259
14.6 Journaling File Systems............................................................................................... 260
14.7 Single Directory Hierarchy and Mount Points ................................................................ 261
14.8 Mounting and Unmounting File Systems ....................................................................... 262
14.8.1 Mounting a File System:
mount() ............................................................ 264
14.8.2 Unmounting a File System:
umount() and umount2() ................................ 269
14.9 Advanced Mount Features.......................................................................................... 271
14.9.1 Mounting a File System at Multiple Mount Points....................................... 271
14.9.2 Stacking Multiple Mounts on the Same Mount Point................................... 271
14.9.3 Mount Flags That Are Per-Mount Options ................................................. 272
14.9.4 Bind Mounts......................................................................................... 272
14.9.5 Recursive Bind Mounts........................................................................... 273
14.10 A Virtual Memory File System:
tmpfs............................................................................ 274
14.11 Obtaining Information About a File System:
statvfs()...................................................... 276
14.12 Summary.................................................................................................................. 277
14.13 Exercise ................................................................................................................... 278
Contents in Detail xv
15 FILE ATTRIBUTES 279
15.1 Retrieving File Information: stat() ................................................................................. 279
15.2 File Timestamps......................................................................................................... 285
15.2.1 Changing File Timestamps with
utime() and utimes()................................. 287
15.2.2 Changing File Timestamps with
utimensat() and futimens() ........................ 289
15.3 File Ownership ......................................................................................................... 291
15.3.1 Ownership of New Files ........................................................................ 291
15.3.2 Changing File Ownership:
chown(), fchown(), and lchown()....................... 291
15.4 File Permissions......................................................................................................... 294
15.4.1 Permissions on Regular Files................................................................... 294
15.4.2 Permissions on Directories...................................................................... 297
15.4.3 Permission-Checking Algorithm ............................................................... 297
15.4.4 Checking File Accessibility:
access()......................................................... 299
15.4.5 Set-User-ID, Set-Group-ID, and Sticky Bits ................................................. 300
15.4.6 The Process File Mode Creation Mask:
umask()........................................ 301
15.4.7 Changing File Permissions:
chmod() and fchmod()..................................... 303
15.5 I-node Flags (
ext2 Extended File Attributes) ................................................................... 304
15.6 Summary.................................................................................................................. 308
15.7 Exercises .................................................................................................................. 309
16 EXTENDED ATTRIBUTES 311
16.1 Overview ................................................................................................................. 311
16.2 Extended Attribute Implementation Details .................................................................... 313
16.3 System Calls for Manipulating Extended Attributes......................................................... 314
16.4 Summary.................................................................................................................. 318
16.5 Exercise ................................................................................................................... 318
17 ACCESS CONTROL LISTS 319
17.1 Overview ................................................................................................................. 320
17.2 ACL Permission-Checking Algorithm............................................................................. 321
17.3 Long and Short Text Forms for ACLs............................................................................. 323
17.4 The
ACL_MASK Entry and the ACL Group Class................................................................ 324
17.5 The
getfacl and setfacl Commands ............................................................................... 325
17.6 Default ACLs and File Creation ................................................................................... 327
17.7 ACL Implementation Limits .......................................................................................... 328
17.8 The ACL API ............................................................................................................. 329
17.9 Summary.................................................................................................................. 337
17.10 Exercise ................................................................................................................... 337
18 DIRECTORIES AND LINKS 339
18.1 Directories and (Hard) Links........................................................................................ 339
18.2 Symbolic (Soft) Links .................................................................................................. 342
18.3 Creating and Removing (Hard) Links:
link() and unlink()............................................... 344
18.4 Changing the Name of a File:
rename() ....................................................................... 348
18.5 Working with Symbolic Links:
symlink() and readlink().................................................. 349
18.6 Creating and Removing Directories:
mkdir() and rmdir() ............................................... 350
18.7 Removing a File or Directory:
remove()......................................................................... 352
18.8 Reading Directories:
opendir() and readdir() ................................................................ 352
18.9 File Tree Walking:
nftw() ........................................................................................... 358
18.10 The Current Working Directory of a Process ................................................................. 363
18.11 Operating Relative to a Directory File Descriptor ........................................................... 365
18.12 Changing the Root Directory of a Process:
chroot() ........................................................ 367
18.13 Resolving a Pathname:
realpath() ................................................................................ 369
xvi Contents in Detail
18.14 Parsing Pathname Strings: dirname() and basename() .................................................... 370
18.15 Summary.................................................................................................................. 372
18.16 Exercises .................................................................................................................. 373
19 MONITORING FILE EVENTS 375
19.1 Overview ................................................................................................................. 376
19.2 The
inotify API .......................................................................................................... 376
19.3
inotify Events ............................................................................................................ 378
19.4 Reading
inotify Events................................................................................................ 379
19.5 Queue Limits and
/proc Files....................................................................................... 385
19.6 An Older System for Monitoring File Events:
dnotify....................................................... 386
19.7 Summary.................................................................................................................. 386
19.8 Exercise ................................................................................................................... 386
20 SIGNALS: FUNDAMENTAL CONCEPTS 387
20.1 Concepts and Overview............................................................................................. 388
20.2 Signal Types and Default Actions ................................................................................ 390
20.3 Changing Signal Dispositions:
signal()......................................................................... 397
20.4 Introduction to Signal Handlers ................................................................................... 398
20.5 Sending Signals:
kill() ............................................................................................... 401
20.6 Checking for the Existence of a Process........................................................................ 403
20.7 Other Ways of Sending Signals:
raise() and killpg() ..................................................... 404
20.8 Displaying Signal Descriptions .................................................................................... 406
20.9 Signal Sets ............................................................................................................... 406
20.10 The Signal Mask (Blocking Signal Delivery) .................................................................. 410
20.11 Pending Signals ........................................................................................................ 411
20.12 Signals Are Not Queued............................................................................................ 412
20.13 Changing Signal Dispositions:
sigaction()..................................................................... 416
20.14 Waiting for a Signal:
pause()...................................................................................... 418
20.15 Summary.................................................................................................................. 418
20.16 Exercises .................................................................................................................. 419
21 SIGNALS: SIGNAL HANDLERS 421
21.1 Designing Signal Handlers ......................................................................................... 422
21.1.1 Signals Are Not Queued (Revisited) ........................................................ 422
21.1.2 Reentrant and Async-Signal-Safe Functions............................................... 422
21.1.3 Global Variables and the
sig_atomic_t Data Type..................................... 428
21.2 Other Methods of Terminating a Signal Handler ........................................................... 428
21.2.1 Performing a Nonlocal Goto from a Signal Handler.................................. 429
21.2.2 Terminating a Process Abnormally:
abort()............................................... 433
21.3 Handling a Signal on an Alternate Stack:
sigaltstack() ................................................... 434
21.4 The
SA_SIGINFO Flag................................................................................................... 437
21.5 Interruption and Restarting of System Calls ................................................................... 442
21.6 Summary.................................................................................................................. 445
21.7 Exercise ................................................................................................................... 446
22 SIGNALS: ADVANCED FEATURES 447
22.1 Core Dump Files ....................................................................................................... 448
22.2 Special Cases for Delivery, Disposition, and Handling ................................................... 450
22.3 Interruptible and Uninterruptible Process Sleep States..................................................... 451
22.4 Hardware-Generated Signals...................................................................................... 452
22.5 Synchronous and Asynchronous Signal Generation ....................................................... 452
Contents in Detail xvii
22.6 Timing and Order of Signal Delivery ........................................................................... 453
22.7 Implementation and Portability of
signal()..................................................................... 454
22.8 Realtime Signals........................................................................................................ 456
22.8.1 Sending Realtime Signals....................................................................... 458
22.8.2 Handling Realtime Signals ..................................................................... 460
22.9 Waiting for a Signal Using a Mask:
sigsuspend() .......................................................... 464
22.10 Synchronously Waiting for a Signal............................................................................. 468
22.11 Fetching Signals via a File Descriptor........................................................................... 471
22.12 Interprocess Communication with Signals ..................................................................... 474
22.13 Earlier Signal APIs (System V and BSD) ........................................................................ 475
22.14 Summary.................................................................................................................. 477
22.15 Exercises .................................................................................................................. 478
23 TIMERS AND SLEEPING 479
23.1 Interval Timers........................................................................................................... 479
23.2 Scheduling and Accuracy of Timers ............................................................................. 485
23.3 Setting Timeouts on Blocking Operations...................................................................... 486
23.4 Suspending Execution for a Fixed Interval (Sleeping) ..................................................... 487
23.4.1 Low-Resolution Sleeping:
sleep().............................................................. 487
23.4.2 High-Resolution Sleeping:
nanosleep()...................................................... 488
23.5 POSIX Clocks............................................................................................................ 491
23.5.1 Retrieving the Value of a Clock:
clock_gettime() ........................................ 491
23.5.2 Setting the Value of a Clock:
clock_settime() ............................................. 492
23.5.3 Obtaining the Clock ID of a Specific Process or Thread ............................. 493
23.5.4 Improved High-Resolution Sleeping:
clock_nanosleep() ............................... 493
23.6 POSIX Interval Timers................................................................................................. 495
23.6.1 Creating a Timer:
timer_create() ............................................................. 495
23.6.2 Arming and Disarming a Timer:
timer_settime() ........................................ 498
23.6.3 Retrieving the Current Value of a Timer:
timer_gettime()............................. 499
23.6.4 Deleting a Timer:
timer_delete() .............................................................. 499
23.6.5 Notification via a Signal........................................................................ 499
23.6.6 Timer Overruns..................................................................................... 503
23.6.7 Notification via a Thread....................................................................... 504
23.7 Timers That Notify via File Descriptors: the
timerfd API ................................................... 507
23.8 Summary.................................................................................................................. 511
23.9 Exercises .................................................................................................................. 512
24 PROCESS CREATION 513
24.1 Overview of fork(), exit(), wait(), and execve() .............................................................. 513
24.2 Creating a New Process:
fork()................................................................................... 515
24.2.1 File Sharing Between Parent and Child .................................................... 517
24.2.2 Memory Semantics of
fork() ................................................................... 520
24.3 The
vfork() System Call .............................................................................................. 522
24.4 Race Conditions After
fork() ....................................................................................... 525
24.5 Avoiding Race Conditions by Synchronizing with Signals............................................... 527
24.6 Summary.................................................................................................................. 529
24.7 Exercises .................................................................................................................. 530
25 PROCESS TERMINATION 531
25.1 Terminating a Process: _exit() and exit()....................................................................... 531
25.2 Details of Process Termination..................................................................................... 533
25.3 Exit Handlers ............................................................................................................ 533
25.4 Interactions Between
fork(), stdio Buffers, and _exit() ..................................................... 537
xviii Contents in Detail
25.5 Summary.................................................................................................................. 538
25.6 Exercise ................................................................................................................... 539
26 MONITORING CHILD PROCESSES 541
26.1 Waiting on a Child Process ........................................................................................ 541
26.1.1 The
wait() System Call........................................................................... 541
26.1.2 The
waitpid() System Call ...................................................................... 544
26.1.3 The Wait Status Value ........................................................................... 545
26.1.4 Process Termination from a Signal Handler .............................................. 549
26.1.5 The
waitid() System Call........................................................................ 550
26.1.6 The
wait3() and wait4() System Calls...................................................... 552
26.2 Orphans and Zombies ............................................................................................... 553
26.3 The
SIGCHLD Signal .................................................................................................... 555
26.3.1 Establishing a Handler for
SIGCHLD .......................................................... 555
26.3.2 Delivery of
SIGCHLD for Stopped Children ................................................. 559
26.3.3 Ignoring Dead Child Processes ............................................................... 559
26.4 Summary.................................................................................................................. 561
26.5 Exercises .................................................................................................................. 562
27 PROGRAM EXECUTION 563
27.1 Executing a New Program: execve() ............................................................................. 563
27.2 The
exec() Library Functions......................................................................................... 567
27.2.1 The
PATH Environment Variable ............................................................... 568
27.2.2 Specifying Program Arguments as a List................................................... 570
27.2.3 Passing the Caller’s Environment to the New Program ............................... 570
27.2.4 Executing a File Referred to by a Descriptor:
fexecve()................................ 571
27.3 Interpreter Scripts ...................................................................................................... 572
27.4 File Descriptors and
exec() .......................................................................................... 575
27.5 Signals and
exec() ..................................................................................................... 578
27.6 Executing a Shell Command:
system() .......................................................................... 579
27.7 Implementing
system() ................................................................................................ 582
27.8 Summary.................................................................................................................. 588
27.9 Exercises .................................................................................................................. 589
28 PROCESS CREATION AND PROGRAM EXECUTION IN
MORE DETAIL 591
28.1 Process Accounting.................................................................................................... 591
28.2 The
clone() System Call .............................................................................................. 598
28.2.1 The
clone() flags Argument ..................................................................... 603
28.2.2 Extensions to
waitpid() for Cloned Children ............................................. 609
28.3 Speed of Process Creation.......................................................................................... 610
28.4 Effect of
exec() and fork() on Process Attributes.............................................................. 612
28.5 Summary.................................................................................................................. 616
28.6 Exercise ................................................................................................................... 616
29 THREADS: INTRODUCTION 617
29.1 Overview ................................................................................................................. 617
29.2 Background Details of the Pthreads API ........................................................................ 620
29.3 Thread Creation........................................................................................................ 622
29.4 Thread Termination.................................................................................................... 623
29.5 Thread IDs................................................................................................................ 624
29.6 Joining with a Terminated Thread................................................................................ 625
29.7 Detaching a Thread................................................................................................... 627
剩余1555页未读,继续阅读
2013-01-13 上传
2023-07-27 上传
2019-03-15 上传
2012-12-11 上传
2012-01-06 上传
2018-05-18 上传
2018-12-27 上传
点击了解资源详情
hapyday
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 前端协作项目:发布猜图游戏功能与待修复事项
- Spring框架REST服务开发实践指南
- ALU课设实现基础与高级运算功能
- 深入了解STK:C++音频信号处理综合工具套件
- 华中科技大学电信学院软件无线电实验资料汇总
- CGSN数据解析与集成验证工具集:Python和Shell脚本
- Java实现的远程视频会议系统开发教程
- Change-OEM: 用Java修改Windows OEM信息与Logo
- cmnd:文本到远程API的桥接平台开发
- 解决BIOS刷写错误28:PRR.exe的应用与效果
- 深度学习对抗攻击库:adversarial_robustness_toolbox 1.10.0
- Win7系统CP2102驱动下载与安装指南
- 深入理解Java中的函数式编程技巧
- GY-906 MLX90614ESF传感器模块温度采集应用资料
- Adversarial Robustness Toolbox 1.15.1 工具包安装教程
- GNU Radio的供应商中立SDR开发包:gr-sdr介绍
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功