日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux进程signal,Linux 编程之【进程】signal

發(fā)布時間:2023/12/15 linux 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux进程signal,Linux 编程之【进程】signal 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

【說明】

kill,可以向包括本身在內的進程發(fā)送一個信號。

kill有一個變體叫 killall,可以給運行著某一命令的所有進程發(fā)送信號。

pause,將程序掛起直到有一個信號出現(xiàn)為止。

程序中信號的使用會帶來一個特殊的問題:如果信號出現(xiàn)在系統(tǒng)調用的執(zhí)行過程中會發(fā)生什么情況?

答案是不確定的。

因此,在程序中使用信號,需要注意一些系統(tǒng)調用會因為接收到一個信號而失敗,而這種錯誤情況

可能是在添加信號處理函數(shù)之前沒有考慮的。

在編寫程序中處理信號部分的代碼時必須非常小心,因為在使用信號的程序中會出現(xiàn)各種各樣的

“競態(tài)條件”。例如,如果想調用 pause 等待一個信號,可信號卻出現(xiàn)在調用 pause 之前,就會使

程序無限期地等待一個不會發(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) 若在父進程向子進程發(fā)送信號之前,先掛起一下(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) 若將父進程中的for循環(huán)次數(shù)改為8,即達到在父進程pause之前,子進程已經(jīng)將信號發(fā)送, ? ? ? 則程序執(zhí)行的效果如下(父進程會無限期的等待下去): # ./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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。