*nix Documentation Project
·  Home
 +   man pages
·  Linux HOWTOs
·  FreeBSD Tips
·  *niX Forums

  man pages->IRIX man pages -> setjmp (3c)              


SETJMP(3C)							    SETJMP(3C)

NAME    [Toc]    [Back]

     setjmp, longjmp, sigsetjmp, siglongjmp, _setjmp, _longjmp - non-local

SYNOPSIS    [Toc]    [Back]

     #include <setjmp.h>

     int setjmp	(jmp_buf env);

     void longjmp (jmp_buf env,	int val);

     int sigsetjmp (sigjmp_buf env, int	savemask);

     void siglongjmp (sigjmp_buf env, int val);

     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

DESCRIPTION    [Toc]    [Back]

     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.


     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.

EXAMPLE    [Toc]    [Back]

     #include <setjmp.h>

     jmp_buf env;
     int i = 0;
     main ()
	 if (setjmp(env) != 0) {
	  (void) printf("2nd return from setjmp: i = %d\n", i);
	 (void)	printf("1st return from	setjmp:	i = %d\n", i);
	 i = 1;

	 longjmp(env, 1);

     The program's output is:

	  1st return from setjmp: i = 0
	  2nd return from setjmp: i = 1

									Page 2

SETJMP(3C)							    SETJMP(3C)

SEE ALSO    [Toc]    [Back]

     sigaction(2), sigprocmask(2), signal(2), sigblock(3B), sigsetmask(3B),
     sigvec(3B), signal(3B).

WARNINGS    [Toc]    [Back]

     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

     If	different versions of these jmp	routines are mixed, unpredictable
     signal masking may	occur.

BUGS    [Toc]    [Back]

     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 ]
 Similar pages
Name OS Title
tt_netfile_file HP-UX map between canonical and local pathnames on the local host
tt_file_netfile HP-UX map between local and canonical pathnames on the local host
tzsetup FreeBSD set local timezone
gcc-local OpenBSD local modifications to gcc
_longjmp OpenBSD non-local jumps
_setjmp OpenBSD non-local jumps
longjmp OpenBSD non-local jumps
siglongjmp FreeBSD non-local jumps
gethostname Tru64 Get the name of the local host
setjmp NetBSD non-local jumps
Copyright © 2004-2005 DeniX Solutions SRL
newsletter delivery service