function prototype int setjmp (JMP_BUF env)
the setjmp parameter env type is a special type of jmp_buf. This data type is a form of an array that holds all the information that can be used to restore the stack state when calling longjmp. because the env variable needs to be referenced in another function, the specification is handled by defining the env variable as a global variable . The setjmp function returns a value of 0 when it is first enabled.
function prototype void longjmp (Jmp_buf env, int val)
The LONGJMP parameter env is the return value of the SETJMP function that is stored by the SETJMP function, the stack environment, and the parameter Val. longjmp function Ben
The body has no return value, it performs a jump to the SETJMP function call that holds the Env parameter, and is returned by the SETJMP function call, at which point the setjmp function
The return value is Val.
#include <setjmp.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h>int j = 0;jmp _buf Env;int Main () {int I, k=0;//setjmp function is used to set the destination stack of jumps, call the function directly, then return 0, if called by longjmp, //Cause setjmp to be called, Then Val (the second parameter of the LONGJMPD)//env retains the stack condition where it needs to be returned i = setjmp (env);p rintf ("setjmp=[%d];j=[%d];k=[%d]\n", I, J + +, k++); if (j > 5) exit (0); sleep (1);//Jump LONGJMP (env, j); return 0;}
Perform
setjmp functions and LONGJMP functions for Linux