. .
. - , . : Ctrl+C, , ', .
, , , . ѳ , .
-
void (* signal(int sig, void (*func)(/*int */))) (int)
sig , signal.h. .
SIGINT INT23H Ctrl+C.
SIGFPE , , , .
SIGABRT , , abort.
func . ³ SIG_DFL SIG_IGN, signal.h, . ij, , , func :
SIG_IGN . SIGFPE, .
SIG_DFL .
, sig . , , . , , signal.
fun void.
signal func, , , . SIG_ERR, :
#define SIG_ERR ((void (* _Cdecl)(int))-1).
', signal . .
. Ctrl+C, handler, , SIGINT. ϳ main. . ׳ Ctrl+C. handler . , .
|
|
.
#include <stdio.h>
#include <signal.h>
#include <process.h>
void handler(void);/* */
main()
{char ch; int i;
if (signal(SIGINT, handler)==SIG_ERR)
{rror(" Ctrl+C");
exit(1); }
printf("\n^C - , - );
scanf("%1c",&ch);
while(1)for(i=0;i<10;i++)printf("%d\n",i);
exit(0);
}
void handler()
{char ch;
printf("\npp \
Y/, .");
scanf("%1c",&ch);
if (ch == 'y' || ch == 'Y') exit(0);
signal (SIGINT,handler)
}
longjmp setjmp, , .
int setjmp(jmp_buf env) env , . void longjmp(jmp_buf env, int value).
jmp_buf, setjmp.h. longjmp , setjmp. , setjmp value, longjmp. , . setjmp , .
#include <stdio.h>
#include <setjmp.h>
jmp_buf mark;
void p(void);
main()
{if (setjmp(mark)!=0)
{printf("longjmp .\n");
exit(1); }
printf("setjmp .\n");
p();}
void p()
{printf(" .\n");
longjmp(mark, -1);
printf(" .\n");
}
:
setjmp .
.
longjmp .
" ." .
. .
.
_0 _0
³
_1 _1
³ ³
_2 _2
³ ³
_3
ʳ
, : _0, _0, _1, _1, _2, _2, _3.
setjmp longjmp.
#include <stdio.h>
#include <setjmp.h>
void f(void);
jmp_buf m,m1;
main() /* */
{printf("_0\n ");
if(!setjmp(m)) f(); /* */
printf("_1\n ");
if(!setjmp(m))longjmp(m1,1); /*³ */
printf("_2\n ");
if(!setjmp(m))longjmp(m1,1); /*³ */
printf("_3\n ");
|
|
} /*ʳ*/
void f() /* */
{ printf("_0\n ");
if(!setjmp(m1))longjmp(m,1); /*³ */
printf("_1\n ");
if(!setjmp(m1))longjmp(m,1); /*³ */
printf("_2\n ");
if(!setjmp(m1))longjmp(m,1); /*³ */
}
.
.
1. . .
2. , 䳿. : trl+C :
.
Ctrl+C, , SIGINT:
- ;
Q - ;
W - - .
: trl+ C :
, , , 0 100.
#include<stdio.h>
#include<setjmp.h>
jmp_buf m;
main()
{<³ .>
< f SIGINT.>
if(!setjmp(m))/* */
{while(1)
{< . . SIGINT. ϳ f SIGINT.>
}
else < .>
}
void f()
{< , Q W.>
< :
{case 'C': < .>
case 'Q': < .>
case'W': < main.>
}
<ϳ SIGINT .>
}
.
1. ?
2. 䳿 ?
3. ?
4. , ѳ?
5. , signal?
6. ?
7. , sigmal ?
8. ?
9. ?
10. ?
11. longjmp?
12. , setjmp?
13. , ?
14. , .
1. .. . : -, 2006. - 412 .
2. . Win32. .: , 2001.
3. ., . . -, 2006. 272 .
4. . . .: , 2002. - 704 .
̲
3
1. , , . 4
2. . . 9
|
|
3. . 16
4. . 26
5. . 31
6. . 42
48