Summary of the usage of sigsetjmp _c language

Source: Internet
Author: User

related functions:longjmp, SIGLONGJMP, setjmp

table header file: #include <setjmp.h>

function definition:int sigsetjmp (sigjmp_buf env, int savesigs)

function Description:sigsetjmp () saves the current stack environment and then makes a mark on the current address.

When you call SIGLONGJMP () elsewhere in the program, you jump directly to the mark position and then restore the stack to continue executing the program.

Parameter env is used to hold the current stack environment and is generally declared as a global variable

If the parameter savesigs is not 0 then the set of signals that is used will also be saved.

When sigsetjmp () returns 0 o'clock the representative is already on the mark, and if the return is not 0, then the siglongjmp () jumps back.

return: If the direct call is 0, if returned from the SIGLONGJMP call is not 0

Instance:

Copy Code code as follows:

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
#include <sys/time.h>

Sigjmp_buf jmp_env;

static void Connect_alarm (int)
{
SIGLONGJMP (jmp_env, 1);
}

int main ()
{
printf ("running...\n") is skipped when the timeout time sec_timeout is greater than or equal to the run time run_time;
int sec_timeout = 3;
int run_time = 2;

printf ("Timeout =%d, run time =%d\n", sec_timeout, Run_time);
if (sec_timeout)
{
This signal is generated when the time set with the alarm function is exceeded, and the Connect_alarm function is called
Signal (SIGALRM, connect_alarm);
Alarm (sec_timeout);
printf ("Set timeout\n");
if (sigsetjmp (jmp_env, 1))
{
printf ("timeout\n");
Goto out;
}
}

Sleep (run_time);
printf ("running...\n");

Out
if (sec_timeout)
{
Cancel a previous set of alarm
Alarm (0);
printf ("Cancel timeout\n");
}

return 0;
}


program run:
When ec_timeout = 3, Run_time = 2 o'clock:
Timeout = 3, Run_time = 2
Set timeout
Running ...
Cancel Timeout

When ec_timeout = 3, Run_time = 4 o'clock:
Timeout = 3, run_time = 4
Set timeout
Timeout
Cancel Timeout

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.