Chapter 8 Exceptional Control Flow -- Nonlocal Jumps

这个东西很高级,当出现某种情况的时候,可以直接跳到指定的地方。 而不用返回出现exception的地方。

 

#include <setjmp.h>
int setjmp(jmp buf env);
int sigsetjmp(sigjmp buf env, int savesigs);

 

#include <setjmp.h>
void longjmp(jmp buf env, int retval);
void siglongjmp(sigjmp buf env, int retval);

 

一个例子。

1 #include "csapp.h"
2
3 jmp_buf buf;
4
5 int error1 = 0;
6 int error2 = 1;
7
8 void foo(void), bar(void);
9
10 int main()
11 {
12 int rc;
13
14 rc = setjmp(buf);
15 if (rc == 0)
16 foo();
17 else if (rc == 1)
18 printf("Detected an error1 condition in foo/n");
19 else if (rc == 2)
20 printf("Detected an error2 condition in foo/n");
21 else
22 printf("Unknown error condition in foo/n");
23 exit(0);
24 }
25
26 /* deeply nested function foo */
27 void foo(void)
28 {
29 if (error1)
30 longjmp(buf, 1);
31 bar();
32 }
33
34 void bar(void)
35 {
36 if (error2)
37 longjmp(buf, 2);
38 }

 

Nonlocal Jump的重要应用场景

For example,
if a Web server attempts to send data to a browser that has unilaterally aborted the network connection
between the client and the server, (e.g., as a result of the browser’s user clicking the STOP button), the
kernel will send a SIGPIPE signal to the server. The default action for the SIGPIPE signal is to terminate
the process, which is clearly not a good thing for a server that is supposed to run forever. Thus, a robust Web
server will install a SIGPIPE handler to catch these signals. After cleaning up, the SIGPIPE handler should
jump to the code that waits for the next request from a browser, rather than returning to the instruction that
was interrupted by the receipt of the SIGPIPE signal. Nonlocal jumps are the only way to handle this kind
of error recovery.

 

 

另一个例子

1 #include "csapp.h"
2
3 sigjmp_buf buf;
4
5 void handler(int sig)
6 {
7 siglongjmp(buf, 1);
8 }
9
10 int main()
11 {
12 Signal(SIGINT, handler);
13
14 if (!sigsetjmp(buf, 1))
15 printf("starting/n");
16 else
17 printf("restarting/n");
18
19 while(1) {
20 Sleep(1);
21 printf("processing.../n");
22 }
23 exit(0);
24 }

 

 

这样每次收到 SIGINT后,就重启程序。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值