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