SETJMP(3C) SETJMP(3C)
setjmp, longjmp, sigsetjmp, siglongjmp, _setjmp, _longjmp - non-local
gotos
#include <setjmp.h>
SysV:
int setjmp (jmp_buf env);
void longjmp (jmp_buf env, int val);
POSIX:
int sigsetjmp (sigjmp_buf env, int savemask);
void siglongjmp (sigjmp_buf env, int val);
BSD:
int setjmp (jmp_buf env);
int longjmp (jmp_buf env, int val);
int _setjmp (jmp_buf env);
int _longjmp (jmp_buf env, int val);
To use the BSD versions of setjmp and longjmp, you must either
1) #define _BSD_SIGNALS or _BSD_COMPAT before including <setjmp.h>, or
2) specify one of them in the compile command or makefile:
cc -D_BSD_SIGNALS -o prog prog.c
These functions are useful for dealing with errors and interrupts
encountered in a low-level subroutine of a program.
All varieties of setjmp save their stack environment in env (whose type,
jmp_buf, is defined in the <setjmp.h> header file) for later use by all
varieties of longjmp. If the return is from a direct invocation, all
setjmps return the value 0. If the return is from a call to any of the
longjmps, all setjmp routines return a nonzero value.
All longjmps restore the environment saved by the last call of setjmp
with the corresponding env argument. After the longjmp is completed,
program execution continues as if the corresponding call of setjmp (which
must not itself have returned in the interim) had just returned the value
val. longjmps cannot cause setjmps to return the value 0. If a longjmp
is invoked with a second argument of 0, all versions of setjmp will
return 1. At the time of the second return from a setjmp, external and
Page 1
SETJMP(3C) SETJMP(3C)
static variables have values as of the time longjmp is called (see
example). The values of register and automatic variables are undefined.
Register or automatic variables whose value must be relied upon must be
declared as volatile.
SYSV-POSIX-BSD DIFFERENCES [Toc] [Back] The System V setjmp/longjmp perform identically to the 4.3BSD
_setjmp/_longjmp, i.e., they manipulate only the C stack and registers.
The 4.3BSD setjmp/longjmp also manipulate the C stack and registers, but
additionally save and restore the process's signal mask (see
sigprocmask(2), sigblock(3B), or sigsetmask(3B)). The POSIX
sigsetjmp/siglongjmp calls may act in either manner: the C stack and
registers are always saved and restored, but if the savemask parameter to
sigsetjmp is non-zero, the signal mask is saved, and a bit in env is set
to indicate that it was saved. siglongjmp checks that bit to determine
if it should restore the mask or not.
Note that the System V longjmp and POSIX siglongjmp return void, whereas
the 4.3BSD longjmp and _longjmp return an integer.
#include <setjmp.h>
jmp_buf env;
int i = 0;
main ()
{
if (setjmp(env) != 0) {
(void) printf("2nd return from setjmp: i = %d\n", i);
exit(0);
}
(void) printf("1st return from setjmp: i = %d\n", i);
i = 1;
g();
/*NOTREACHED*/
}
g()
{
longjmp(env, 1);
/*NOTREACHED*/
}
The program's output is:
1st return from setjmp: i = 0
2nd return from setjmp: i = 1
Page 2
SETJMP(3C) SETJMP(3C)
sigaction(2), sigprocmask(2), signal(2), sigblock(3B), sigsetmask(3B),
sigvec(3B), signal(3B).
If longjmp is called even though env was never primed by a call to
setjmp, or when the last such call was in a function which has since
returned, absolute chaos is guaranteed.
In multithreaded processes, if longjmp is called with an env initialized
in different thread, the result is also guaranteed to produce chaos.
Also note that the signal mask manipulated by these interfaces is per
thread.
If different versions of these jmp routines are mixed, unpredictable
signal masking may occur.
The values of the registers on the second return from the setjmps are the
register values at the time of the first call to setjmp, not those at the
time of the longjmp. This means that variables in a given function may
behave differently in the presence of setjmp, depending on whether they
are register or stack variables.
PPPPaaaaggggeeee 3333 [ Back ]
|