linux进程signal,Linux 编程之【进程】signal
【說(shuō)明】
kill,可以向包括本身在內(nèi)的進(jìn)程發(fā)送一個(gè)信號(hào)。
kill有一個(gè)變體叫 killall,可以給運(yùn)行著某一命令的所有進(jìn)程發(fā)送信號(hào)。
pause,將程序掛起直到有一個(gè)信號(hào)出現(xiàn)為止。
程序中信號(hào)的使用會(huì)帶來(lái)一個(gè)特殊的問(wèn)題:如果信號(hào)出現(xiàn)在系統(tǒng)調(diào)用的執(zhí)行過(guò)程中會(huì)發(fā)生什么情況?
答案是不確定的。
因此,在程序中使用信號(hào),需要注意一些系統(tǒng)調(diào)用會(huì)因?yàn)榻邮盏揭粋€(gè)信號(hào)而失敗,而這種錯(cuò)誤情況
可能是在添加信號(hào)處理函數(shù)之前沒(méi)有考慮的。
在編寫(xiě)程序中處理信號(hào)部分的代碼時(shí)必須非常小心,因?yàn)樵谑褂眯盘?hào)的程序中會(huì)出現(xiàn)各種各樣的
“競(jìng)態(tài)條件”。例如,如果想調(diào)用 pause 等待一個(gè)信號(hào),可信號(hào)卻出現(xiàn)在調(diào)用 pause 之前,就會(huì)使
程序無(wú)限期地等待一個(gè)不會(huì)發(fā)生的事件。
【示例】
#include
#include
#include
#include
void sig_alm(int sig)
{
pid_t p;
p = getpid();
printf("### Process(%d) receive a alarm signal.\r\n", p);
return;
}
int main(int argc, char *argv[])
{
pid_t pid,wpid,pid_self,ppid;
char message[64] = {0};
int n, val;
(void)signal(SIGALRM, sig_alm);
printf("[parent]start to fork child process.\r\n");
pid = fork();
switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
pid_self = getpid();
sprintf(message, "[child]This is the child process(%d)", pid_self);
for(n=5; n>0; n--)
{
puts(message);
sleep(1);
}
ppid = getppid();
printf("[child] signal: child > parent\r\n");
kill(ppid, SIGALRM);
printf("[child]child process(%d) exit.\r\n", pid_self);
break;
default:
pid_self = getpid();
sprintf(message, "[parent]This is the parent process(%d)", pid_self);
for(n=3; n>0; n--) //for(n=8;n>0;n--)
{
puts(message);
sleep(1);
}
//pause();
printf("[parent] signal: parent > child\r\n");
kill(pid, SIGALRM);
wpid = wait(&val);
printf("[parent]wait_pid=%d, child_pid=%d \r\n", wpid, pid);
printf("[parent]parent process(%d) exit. \r\n", pid_self);
break;
}
exit(0);
}
【編譯及執(zhí)行】
(1) 原碼執(zhí)行效果
# gcc -o signal signal.c
# ./signal
[parent]start to fork child process.
[parent]This is the parent process(8964)
[child]This is the child process(8965)
[parent]This is the parent process(8964)
[child]This is the child process(8965)
[parent]This is the parent process(8964)
[child]This is the child process(8965)
[parent] signal: parent > child
### Process(8965) receive a alarm signal.
[child]This is the child process(8965)
[child]This is the child process(8965)
[child] signal: child > parent
### Process(8964) receive a alarm signal.
[child]child process(8965) exit.
[parent]wait_pid=8965, child_pid=8965
[parent]parent process(8964) exit.
(2) 若在父進(jìn)程向子進(jìn)程發(fā)送信號(hào)之前,先掛起一下(pause),其執(zhí)行的效果如下:
# ./signal
[parent]start to fork child process.
[parent]This is the parent process(9608)
[child]This is the child process(9609)
[parent]This is the parent process(9608)
[child]This is the child process(9609)
[parent]This is the parent process(9608)
[child]This is the child process(9609)
[child]This is the child process(9609)
[child]This is the child process(9609)
[child] signal: child > parent
### Process(9608) receive a alarm signal.
[parent] signal: parent > child
### Process(9609) receive a alarm signal.
[child]child process(9609) exit.
[parent]wait_pid=9609, child_pid=9609
[parent]parent process(9608) exit.
(3) 若將父進(jìn)程中的for循環(huán)次數(shù)改為8,即達(dá)到在父進(jìn)程pause之前,子進(jìn)程已經(jīng)將信號(hào)發(fā)送, ? ? ? 則程序執(zhí)行的效果如下(父進(jìn)程會(huì)無(wú)限期的等待下去): # ./signal [parent]start to fork child process. [parent]This is the parent process(9623) [child]This is the child process(9624) [parent]This is the parent process(9623) [child]This is the child process(9624) [parent]This is the parent process(9623) [child]This is the child process(9624) [parent]This is the parent process(9623) [child]This is the child process(9624) [parent]This is the parent process(9623) [child]This is the child process(9624) [parent]This is the parent process(9623) [child] signal: child > parent ### Process(9623) receive a alarm signal. [parent]This is the parent process(9623) [child]child process(9624) exit. [parent]This is the parent process(9623)
總結(jié)
以上是生活随笔為你收集整理的linux进程signal,Linux 编程之【进程】signal的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 雷鸟创新完成首轮过亿元融资:2022年国
- 下一篇: linux .o文件 复制,Linux