1setjmp(3UCB)       SunOS/BSD Compatibility Library Functions      setjmp(3UCB)
2
3
4

NAME

6       setjmp, longjmp, _setjmp, _longjmp - non-local goto
7

SYNOPSIS

9       /usr/ucb/cc [ flag ... ] file ...
10       #include <setjmp.h>
11
12       int setjmp(env)
13       jmp_buf env;
14
15
16       void longjmp(env, val)
17       jmp_buf env;
18       int val;
19
20
21       int _setjmp(env)
22       jmp_buf env;
23
24
25       void _longjmp(env, val)
26       jmp_buf env;
27       int val;
28
29

DESCRIPTION

31       The setjmp() and longjmp() functions are useful for dealing with errors
32       and interrupts encountered in a low-level subroutine of a program.
33
34
35       The setjmp() function saves its stack environment in env for later  use
36       by  longjmp().  A  normal  call to setjmp() returns zero. setjmp() also
37       saves the register environment.  If a longjmp() call will be made,  the
38       routine  which  called  setjmp()  should  not  return  until  after the
39       longjmp() has returned control (see below).
40
41
42       The longjmp() function restores the environment saved by the last  call
43       of setjmp(), and then returns in such a way that execution continues as
44       if the call of setjmp() had just returned the value val to the function
45       that  invoked setjmp(); however, if val were zero, execution would con‐
46       tinue as if the call of setjmp() had returned one.  This ensures that a
47       ``return''  from  setjmp() caused by a call to longjmp() can be distin‐
48       guished from a regular return from setjmp(). The calling function  must
49       not  itself  have  returned in the interim, otherwise longjmp() will be
50       returning control to a possibly nonexistent  environment.  All  memory-
51       bound  data  have  values as of the time longjmp() was called.  The CPU
52       and floating-point data registers are restored to the values  they  had
53       at  the time that setjmp() was called.  But, because the register stor‐
54       age class is only a hint to the C compiler, variables declared as  reg‐
55       ister  variables  may not necessarily be assigned to machine registers,
56       so their values are unpredictable after a longjmp(). This is especially
57       a  problem  for  programmers trying to write machine-independent C rou‐
58       tines.
59
60
61       The setjmp() and longjmp() functions save and restore the  signal  mask
62       while  _setjmp()  and _longjmp() manipulate only the C stack and regis‐
63       ters.
64
65
66       None of these functions save or restore any  floating-point  status  or
67       control registers.
68

EXAMPLES

70       Example 1 Examples of setjmp() and longjmp().
71
72
73       The  following  example  uses both setjmp() and longjmp() to return the
74       flow of control to the appropriate instruction block:
75
76
77         #include <stdio.h>
78         #include <setjmp.h>
79         #include <signal.h>
80         #include <unistd.h>
81         jmp_buf env; static void signal_handler();
82         main()  {
83                 int returned_from_longjump, processing = 1;
84                 unsigned int time_interval = 4;
85                 if ((returned_from_longjump = setjmp(env)) != 0)
86                     switch (returned_from_longjump)     {
87                       case SIGINT:
88                         printf("longjumped from interrupt %d\n",SIGINT);
89                         break;
90                       case SIGALRM:
91                         printf("longjumped from alarm %d\n",SIGALRM);
92                         break;
93                     }
94                 (void) signal(SIGINT, signal_handler);
95                 (void) signal(SIGALRM, signal_handler);
96                 alarm(time_interval);
97                 while (processing)        {
98                   printf(" waiting for you to INTERRUPT (cntrl-C) ...\n");
99                   sleep(1);
100                 }  /* end while forever loop */
101         }
102
103         static void signal_handler(sig)
104         int sig; {
105                 switch (sig)     {
106                   case SIGINT:         ...   /* process for interrupt */
107                                        longjmp(env,sig);
108                                              /* break never reached */
109                   case SIGALRM:        ...   /* process for alarm */
110                                        longjmp(env,sig);
111                                              /* break never reached */
112                   default:             exit(sig);
113                 }
114         }
115
116
117
118       When this example is compiled and  executed,  and  the  user  sends  an
119       interrupt signal, the output will be:
120
121
122         longjumped from interrupt
123
124
125
126       Additionally,  every  4  seconds the alarm will expire, signalling this
127       process, and the output will be:
128
129
130         longjumped from alarm
131
132

SEE ALSO

134       sigvec(3UCB), setjmp(3C), signal(3C)
135

NOTES

137       Use of these interfaces should be restricted to only applications writ‐
138       ten  on  BSD platforms.  Use of these interfaces with any of the system
139       libraries or in multi-thread applications is unsupported.
140

BUGS

142       The setjmp() function does not save the current notion of  whether  the
143       process  is  executing  on  the  signal  stack.   The  result is that a
144       longjmp() to some place on the signal stack  leaves  the  signal  stack
145       state incorrect.
146
147
148       On  some  systems  setjmp() also saves the register environment. There‐
149       fore, all data that are bound to registers are restored to  the  values
150       they  had  at  the time that setjmp() was called. All memory-bound data
151       have values as of the time longjmp() was called.  However, because  the
152       register  storage  class  is  only  a hint to the C compiler, variables
153       declared as register variables  may  not  necessarily  be  assigned  to
154       machine registers, so their values are unpredictable after a longjmp().
155       When using compiler options that specify automatic register allocation,
156       the  compiler will not attempt to assign variables to registers in rou‐
157       tines that call setjmp().
158
159
160       The longjmp() function never causes setjmp() to return 0,  so  program‐
161       mers  should  not  depend  on longjmp() being able to cause setjmp() to
162       return 0.
163
164
165
166SunOS 5.11                        7 Apr 1993                      setjmp(3UCB)
Impressum