setjmp functions and LONGJMP functions for Linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.