CS:APP第八章知识总结(异常、进程、信号)

ecf,exceptional control flow, 指的是由有/无条件跳转、call/return指令以外的原因造成的控制流转移。比如定时器、网络包处理、子进程终止等。

exceptional control flow occurs at all levels of a computer system. For example, at the hardware level, events detected by the hardware trigger abrupt control transfers to exception handlers. At the operating systems level, the kernel transfers control from one user process to another via context switches. At the application level, a process can send a signal to another process that abruptly transfers control to a signal handler in the recipient. An individual program can react to errors by
sidestepping the usual stack discipline and making non-local jumps to arbitrary locations in other functions.

自陷/系统调用就是一种ECF。IO、进程的创建与结束都需要系统调用。

高级语言提供的异常处理称为软件异常。Software exceptions allow the program to make non-local jumps (i.e., jumps that violate the usual call/return stack discipline) in response to error conditions. Non-local jumps are a form of application-level ECF and are provided in C via the setjmp and longjmp functions.

----------------------------------------exception------------------------------------------------------------
Exceptions are a form of exceptional control flow that are implemented partly by the hardware and partly by the operating system.

在这里插入图片描述
图8.1中,exception的返回有三种情况,current,next,abort。
跳到exception handler时状态信息会被存在kernel stack。handler工作在核态,拥有核态权限。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Hardware interrupts are asynchronous in the sense that they are not caused by the execution of any particular instruction. 要理解这个async,关键是图8.5中的(1)中的during,是指指令执行过程中收到网络适配器、磁盘驱动等的信号。指令本身不是触发原因。
The remaining classes of exceptions (traps, faults, and aborts) occur synchronously as a result of executing the current instruction. We refer to this instruction as the faulting instruction.
Traps are intentional exceptions that occur as a result of executing an instruction.
trap的主要作用就是提供系统调用。

在这里插入图片描述

在这里插入图片描述
Linux shells typically report divide errors as “Floating exceptions.”
Linux shells typically report general protection faults as “Segmentation faults.”

在这里插入图片描述

平时常说的上下文的详细版:
The context consists of the state that the program needs to run correctly. This state includes the
program’s code and data stored in memory, its stack, the contents of its general purpose registers, its program counter, environment variables, and the set of open file descriptors.

在这里插入图片描述
The single physical control flow of the processor is partitioned into three logical flows, one for each process. 图中,AC和AB是并发的,而BC不是。

在这里插入图片描述

在这里插入图片描述

关于fork:
Call once, return twice. 在父进程中返回子进程号,在子进程中返回0.
Concurrent execution. 父子谁先是不确定的。
Duplicate but separate address spaces
Shared files

在这里插入图片描述

关于waitpid和僵尸进程:
在这里插入图片描述

waitpid suspends execution of the calling process until a child process in its wait set terminates. waitpid returns the PID of the terminated child that caused waitpid to return. At this point, the terminated child has been reaped and the kernel removes all traces of it from the system.
此处提到的waitset由waitpid的入参决定,可以是正数、0、-1。

关于execve:
仅在错误时返回。
在这里插入图片描述
在shell中输入的指令其实会直接作为execve的参数。argv[0]是可执行文件的文件名。
int main(int argc, char *argv[], char *envp[]);

getenv、setenv、unsetenv可以对*envp[]进行操作。

If the last argument is an ‘&’ character, then parseline(原书的一个函数,解析用户的输入) returns 1, indicating that the program should be executed in the background (the shell does not wait for it to complete). Otherwise, it returns 0, indicating that the program should be run in the foreground (the shell waits for it to complete).

在这里插入图片描述
但需要注意,在上图中,t≠s。否则信号会被挂起。对于若干相同的信号(t=s),需要注意,信号是不会排队的:
the existence of a pending signal only implies that at least one signal has been delivered since the last time the process received a signal of that type. signals cannot be used to count the occurrence of events in other processes.
信号可能会有很多,但handler可能只被调用两次。所以handler需要尽可能多的进行处理(也就是说,handler的作者要做好信号丢失的心理准备,使handler能处理两次handler调用之间可能超过1个的信号)。

signal handler的写法注意事项:
Signal handlers are tricky because they can run concurrently with the main program and with each other, as we saw in Figure 8.31. If a handler and the main program access the same global data structure concurrently, then the results can be unpredictable and often fatal.
G0. Keep handlers as simple as possible.
G1. Call only async-signal-safe functions in your handlers.(原书中有一个列表,也可以查man)
G2. Save and restore errno.(errno可能会有使用冲突)
G3. Protect accesses to shared global data structures by blocking all signals.(保证handler的切入点不会造成不可预测结果)
G4. Declare global variables with volatile.(The volatile qualifier forces the compiler to read the value of g from memory instead of register cache each time it is referenced in the code.)
G5. Declare flags with sig_atomic_t.
这些注意事项,其实都是在避免并发错误。

进程和信号在这一章讲,我想是因为它们跟异常一样,都有图8.31这样的特征。

An important application of non-local jumps is to permit an immediate return from a deeply nested function call, usually as a result of detecting some error condition. If an error condition is detected deep in a nested function call, we can use a non-local jump to return directly to a common localized error handler instead of laboriously unwinding the call stack.(原书图8.43有setjmp和longjmp的demo)

在这里插入图片描述

P826,下次继续。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值