linux进程signal,Linux 编程之【进程】signal
【說明】
kill,可以向包括本身在內的進程發送一個信號。
kill有一個變體叫 killall,可以給運行著某一命令的所有進程發送信號。
pause,將程序掛起直到有一個信號出現為止。
程序中信號的使用會帶來一個特殊的問題:如果信號出現在系統調用的執行過程中會發生什么情況?
答案是不確定的。
因此,在程序中使用信號,需要注意一些系統調用會因為接收到一個信號而失敗,而這種錯誤情況
可能是在添加信號處理函數之前沒有考慮的。
在編寫程序中處理信號部分的代碼時必須非常小心,因為在使用信號的程序中會出現各種各樣的
“競態條件”。例如,如果想調用 pause 等待一個信號,可信號卻出現在調用 pause 之前,就會使
程序無限期地等待一個不會發生的事件。
【示例】
#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);
}
【編譯及執行】
(1) 原碼執行效果
# 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) 若在父進程向子進程發送信號之前,先掛起一下(pause),其執行的效果如下:
# ./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) 若將父進程中的for循環次數改為8,即達到在父進程pause之前,子進程已經將信號發送, ? ? ? 則程序執行的效果如下(父進程會無限期的等待下去): # ./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)
總結
以上是生活随笔為你收集整理的linux进程signal,Linux 编程之【进程】signal的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 雷鸟创新完成首轮过亿元融资:2022年国
- 下一篇: Pine64 发布 Linux 平板电脑