生活随笔
收集整理的這篇文章主要介紹了
信号之函数的可重入性
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
信號(hào)之函數(shù)的可重入性
在調(diào)用某個(gè)函數(shù)過(guò)程中出現(xiàn)中斷信號(hào),且改信號(hào)處理函數(shù)中再次調(diào)用該函數(shù),訪問(wèn)全局、靜態(tài)變量的函數(shù)是不可重入函數(shù)。
前后數(shù)據(jù)不一致,函數(shù)是不可重入的,特點(diǎn):函數(shù)中使用全局變量或靜態(tài)變量。
前后數(shù)據(jù)一致,函數(shù)是可重入的,特征:函數(shù)中使用局部變量。
root@spark# cat reinside
.c
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>int g_v
[10];
int *h_v
;void set(int n
)
{printf("begin running set.....\n");int p_v
[10];for (int i
= 0; i
< 10; i
++) {p_v
[i
] = n
;h_v
[i
] = n
;g_v
[i
] = n
;sleep(1);}printf("p_v:");for (int i
= 0; i
< 10; i
++) {printf("%d ", p_v
[i
]);}printf("\n");printf("g_v:");for (int i
= 0; i
< 10; i
++) {printf("%d ", g_v
[i
]);}printf("\n");printf("h_v:");for (int i
= 0; i
< 10; i
++) {printf("%d ", h_v
[i
]);}printf("\n");printf("end running set....\n");return;
}void sig_hander(int signo
)
{printf("%d receive sigal SIGTSTP...\n", getpid());if (signal(SIGTSTP
, sig_hander
) == SIG_ERR
) {perror("sig_hander signal fail");}set(20);return;
}int main(void)
{if (signal(SIGTSTP
, sig_hander
) == SIG_ERR
) {perror("signal fail");}h_v
= (int *)malloc(sizeof(int)*10);if (h_v
== NULL) {perror("malloc h_v fail");}printf("begin running main....\n");set(10);printf("end running main....\n");return 0;
}
root@spark# ./reinside
begin running main…
begin running set…
^Z19559 receive sigal SIGTSTP…
begin running set…
p_v:20 20 20 20 20 20 20 20 20 20
g_v:20 20 20 20 20 20 20 20 20 20
h_v:20 20 20 20 20 20 20 20 20 20
end running set…
p_v:10 10 10 10 10 10 10 10 10 10
g_v:20 20 20 20 20 10 10 10 10 10
h_v:20 20 20 20 20 10 10 10 10 10
end running set…
end running main…
root@spark#
運(yùn)行5秒的時(shí)候按下ctrl+z, main函數(shù)中調(diào)用set函數(shù)中的全局變量?jī)?nèi)容被改寫(xiě)。
總結(jié)
以上是生活随笔為你收集整理的信号之函数的可重入性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。