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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

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

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

【說(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)題。

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