Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/691b01b4-1b64-800b-992e-192d846b72fe
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===== 《Head First C 嗨翻 C 语言》第 10 章 ===== ====== 1. 数据流与重定向 ====== * '''文件描述符(File Descriptor)''':进程用文件描述符表示数据流,它是一个数字,代表一条数据流 。 * '''描述符表(Descriptor Table)''':进程内部用来记录文件描述符及其对应数据流的表格 。 * '''标准数据流''':描述符表的前三项是固定的 : * '''0''':标准输入(stdin),默认连接键盘 。 * '''1''':标准输出(stdout),默认连接屏幕 。 * '''2''':标准错误(stderr),默认连接屏幕 。 * '''重定向(Redirection)''':重定向通过修改描述符表中固定编号(如1号标准输出)所对应的数据流来实现 。 * '''重定向相关的函数''': * **fileno(FILE *stream)**:返回文件指针 FILE* 对应的文件描述符编号 。 * '''dup2(oldfd, newfd)''':复制数据流,让 newfd 指向与 oldfd 相同的数据流。这是在代码中实现重定向的关键函数,例如 dup2(fileno(f), 1) 可以将标准输出重定向到文件 f 。 ====== 2. 错误处理 ====== * '''exit(int status) 系统调用''':用于快速终止程序,并设置退出状态(status)。它可以取代复杂的错误处理中的 return 语句,程序一旦调用 exit() 就会立刻终止,不需要返回主函数 。使用时需要包含 <stdlib.h> 头文件 。 ====== 3. 进程同步与控制 ====== * **waitpid(pid_t pid, int *pid_status, int options) 函数**:用于让父进程等待指定的子进程结束以后才返回 。这可以解决子进程未完成任务(如文件写入)而父进程已退出导致数据丢失的问题 。 * pid_status 用来保存进程的退出信息,必须是指针 。 * options 通常设为 0,表示等待进程结束 。 * '''WEXITSTATUS(pid_status) 宏''':用于从 pid_status 中提取子进程的退出状态(前8位),因为 pid_status 中包含了其他信息 。使用该函数需要包含 <sys/wait.h> 头文件 。 ====== 4. 管道(Pipes) ====== * '''管道的作用''':用于连接两个进程,将一个进程的输出连接到另一个进程的输入,从而实现父子进程间的实时数据通信 。 * '''pipe(int fd[2]) 函数''':创建一个管道,并返回两个文件描述符 : * fd[0]:管道的'''读取端''' 。 * fd[1]:管道的'''写入端''' 。 * '''父子进程连接管道''': * '''子进程''':关闭读取端 fd[0],将标准输出(1号描述符)重定向到写入端 fd[1] 。 * '''父进程''':关闭写入端 fd[1],将标准输入(0号描述符)重定向到读取端 fd[0] 。 * '''性质''':管道是单向通信的,通常在存储器中实现 。 ====== 5. 信号(Signals) ====== * '''信号的概念''':操作系统发送给进程的通知,用于中断、终止进程或进行定时等(例如用户按下 Ctrl-C) 。 * '''发送信号''': * '''kill 命令''':默认发送 SIGTERM 信号,可以用来发送其他信号如 SIGINT 。SIGKILL 信号不能被捕捉或忽略 。 * '''alarm(seconds) 函数''':设置一个定时器,在指定时间后发送 SIGALRM 信号(闹钟信号) 。 * '''raise(signal) 函数''':让进程向自己发送信号 。 * '''捕捉信号(Signal Handling)''': * 使用 sigaction 机制注册'''信号处理器(handler)'''函数来处理接收到的信号 。 * 信号处理器函数必须接收一个 int 参数(信号编号) 。 * '''修改信号行为''': * SIG_DFL:将信号处理方式还原为默认行为 。 * SIG_IGN:让进程忽略某个信号 。 我本周学习了第十章内容,请根据这些知识点相关知识,对我进行苏格拉底式提问,一次一个问题
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)