kill()函数 详解
生活随笔
收集整理的這篇文章主要介紹了
kill()函数 详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[ KILL ]功能描述:
用于向任何進程組或進程發送信號。
2?
3?#include?<signal.h>
4?
5?int?kill(pid_t?pid,?int?sig);
6?
7?
?2?#include?<sys/types.h>
?3?#include?<stdio.h>
?4?#include?<stdlib.h>
?5?#include?<signal.h>
?6?
?7?int?main(?void?)
?8?{
?9?????pid_t?childpid;
10?????int?status;
11?????int?retval;
12?????
13?????childpid?=?fork();
14?????if?(?-1?==?childpid?)
15?????{
16?????????perror(?"fork()"?);
17?????????exit(?EXIT_FAILURE?);
18?????}
19?????else?if?(?0?==?childpid?)
20?????{
21?????????puts(?"In?child?process"?);
22?????????sleep(?100?);//讓子進程睡眠,看看父進程的行為
23?????????exit(EXIT_SUCCESS);
24?????}
25?????else
26?????{
27?????????if?(?0?==?(waitpid(?childpid,?&status,?WNOHANG?)))
28?????????{
29?????????????retval?=?kill(?childpid,SIGKILL?);
30?????????????
31?????????????if?(?retval?)
32?????????????{
33?????????????????puts(?"kill?failed."?);
34?????????????????perror(?"kill"?);
35?????????????????waitpid(?childpid,?&status,?0?);
36?????????????}
37?????????????else
38?????????????{
39?????????????????printf(?"%d?killed\n",?childpid?);
40?????????????}
41?????????????
42?????????}
43?????}
44?????
45?????exit(EXIT_SUCCESS);
46?}
47?//-----------------
48?[root@localhost?src]#?gcc?killer.c
49?[root@localhost?src]#?./a.out
50?In?child?process
51?4545?killed
用于向任何進程組或進程發送信號。
頭文件用法:
2?
3?#include?<signal.h>
4?
5?int?kill(pid_t?pid,?int?sig);
6?
7?
參數:
pid:可能選擇有以下四種
1. pid大于零時,pid是信號欲送往的進程的標識。
2. pid等于零時,信號將送往所有與調用kill()的那個進程屬同一個使用組的進程。
3. pid等于-1時,信號將送往所有調用進程有權給其發送信號的進程,除了進程1(init)。
4. pid小于-1時,信號將送往以-pid為組標識的進程。
sig:準備發送的信號代碼,假如其值為零則沒有任何信號送出,但是系統會執行錯誤檢查,通常會利用sig值為零來檢驗某個進程是否仍在執行。
返回值說明: 成功執行時,返回0。失敗返回-1,errno被設為以下的某個值 EINVAL:指定的信號碼無效(參數 sig 不合法(INVAL:invalid)) EPERM;權限不夠無法傳送信號給指定進程 (PERM:permission權限) ESRCH:參數 pid 所指定的進程或進程組不存在(SRCH:search)
代碼?1?#include?<sys/wait.h>?2?#include?<sys/types.h>
?3?#include?<stdio.h>
?4?#include?<stdlib.h>
?5?#include?<signal.h>
?6?
?7?int?main(?void?)
?8?{
?9?????pid_t?childpid;
10?????int?status;
11?????int?retval;
12?????
13?????childpid?=?fork();
14?????if?(?-1?==?childpid?)
15?????{
16?????????perror(?"fork()"?);
17?????????exit(?EXIT_FAILURE?);
18?????}
19?????else?if?(?0?==?childpid?)
20?????{
21?????????puts(?"In?child?process"?);
22?????????sleep(?100?);//讓子進程睡眠,看看父進程的行為
23?????????exit(EXIT_SUCCESS);
24?????}
25?????else
26?????{
27?????????if?(?0?==?(waitpid(?childpid,?&status,?WNOHANG?)))
28?????????{
29?????????????retval?=?kill(?childpid,SIGKILL?);
30?????????????
31?????????????if?(?retval?)
32?????????????{
33?????????????????puts(?"kill?failed."?);
34?????????????????perror(?"kill"?);
35?????????????????waitpid(?childpid,?&status,?0?);
36?????????????}
37?????????????else
38?????????????{
39?????????????????printf(?"%d?killed\n",?childpid?);
40?????????????}
41?????????????
42?????????}
43?????}
44?????
45?????exit(EXIT_SUCCESS);
46?}
47?//-----------------
48?[root@localhost?src]#?gcc?killer.c
49?[root@localhost?src]#?./a.out
50?In?child?process
51?4545?killed
在確信fork調用成功后,子進程睡眠100秒,然后退出。
同時父進程在子進程上調用waitpid函數,但使用了WNOHANG選項,
所以調用waitpid后立即返回。父進程接著殺死子進程,如果kill執行失敗,
返回-1,否這返回0。如果kill執行失敗,父進程第二次調用waitpid,
保證他在子進程退出后再停止執行。否則父進程顯示一條成功消息后退出。
?
?
?
總結
以上是生活随笔為你收集整理的kill()函数 详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 检测子进程的结束返回状态,status的
- 下一篇: 关于实验中的makefile相关知识