[英]Why signal handler goes to infinite loop? - SIGSEGV
知道為什么信號處理程序會進入無限循環嗎?
這是代碼。 請幫我。
enter code here
9 void SIGSEGV_handler(int signal)
10 {
11 printf("Segmentation fault caught....\n");
12 printf("Value of instance variable: i = %d\n\n", i);
13 }
16
17 int main()
18 {
19 char *mallocPtr, *callocPtr, *reallocPtr, *memalignPtr, *vallocPtr;
20 struct sigaction sa;
21
22 sa.sa_handler=SIGSEGV_handler;
23 sigaction(SIGSEGV, &sa, NULL);
24
37
38 printf("The segmentation fault handler will be entered for i = 3, 4, 5 and 6\n");
39
40
41 for(i=0; i<7; i++)
42 {
43 printf("i = %d\n",i);
44
45 mallocPtr=(char*)malloc(3);
46 printf("Malloc address : %x\n",mallocPtr);
47 strcpy(mallocPtr, "Hhvhgvghsvxhvshxv");
48 puts(mallocPtr);
SIGSEGV
的默認操作是終止您的進程。 但是你安裝了一個處理程序並覆蓋它:
/* Does nothing to "fix" what was wrong with the faulting
* instruction.
*/
void SIGSEGV_handler(int signal)
{
printf("Segmentation fault caught....\n");
printf("Value of instance variable: i = %d\n\n", i);
}
因此,對於每個觸發sigsegv的指令,都會調用此處理程序並重新啟動指令 。 但是你的處理程序沒有做任何事情來解決錯誤指令的第一個問題。
總之,當指令重新啟動時,它將再次發生故障。 又一次又一次......你明白了。
http://pubs.opengroup.org/onlinepubs/009604599/functions/xsh_chap02_04.html#tag_02_04
在正常從信號捕獲函數返回不是由kill(),sigqueue()或raise()生成的SIGBUS,SIGFPE,SIGILL或SIGSEGV信號之后,進程的行為是未定義的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.