展示函数setjmp()和longjmp()的用法

//展示函数setjmp()和longjmp()的用法
#include <setjmp.h>
#include "tlpi_hdr.h"
#define EXIT_SUCESS 0
static jmp_buf env;
static void
f2(void)
{
    longjmp(env,2);
}

static void
f1(int argc)
{
    if (argc == 1)
        longjmp(env,1);
    f2();
}

int
main(int argc,char *argv[])
{
    switch (setjmp(env)) {
        case 0: /*This is the return after after the initial setjmp()*/
            printf("Calling f1() after initial setjmp()\n");
            f1(argc);   /*Nerver returns...*/
            break;      /*...but this is good form*/

        case 1:
            printf("We jumped back from f1()\n");
            break;

        case 2:
            printf("We jumped back from f2()\n");
            break;
    }
    exit(EXIT_SUCCESS);
}
/*
 程序使用示例:
 [root@localhost linux-test]# gl++ test.c
 [root@localhost linux-test]# ./a.out
Calling f1() after initial setjmp()
We jumped back from f1()
[root@localhost linux-test]# ./a.out 1
Calling f1() after initial setjmp()
We jumped back from f2()
 */

猜你喜欢

转载自blog.csdn.net/liao__ran/article/details/108872023
今日推荐