这个东西很高级,当出现某种情况的时候,可以直接跳到指定的地方。 而不用返回出现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后,就重启程序。