Linux下Kill函数用法
来源:互联网 发布:全球购海淘宝 编辑:程序博客网 时间:2024/05/19 18:40
[ KILL ]功能描述:
用于向任何进程组或进程发送信号。
1 #include <sys/types.h>
2
3 #include <signal.h>
4
5 int kill(pid_t pid, int sig);
6
7
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 不合法) EPERM;权限不够无法传送信号给指定进程 ESRCH:参数 pid 所指定的进程或进程组不存在
代码
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
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选项(WNOHANG如果没有任何已经结束的子进程则马上返回,不予以等待),
所以调用waitpid后立即返回。父进程接着杀死子进程,如果kill执行失败,
返回-1,否这返回0。如果kill执行失败,父进程第二次调用waitpid,
保证他在子进程退出后再停止执行。否则父进程显示一条成功消息后退出。
0 0
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下Kill函数用法
- Linux下kill函数用法
- Linux下Kill()函数 + QProcess 用法
- Linux 下的KILL函数的用法
- 谈谈singelton单例模式
- 指针和引用的区别
- MSSQL:参数数据类型 ntext 对于 replace 函数的参数 1 无效。
- 初识java集合2
- EL表达式简述
- Linux下Kill函数用法
- Sort Colors -- LeetCode
- 操作系统--处理器调度
- poj 1575 Easier Done Than Said?【字符串处理】
- KMP算法(uva 1328)
- 项目进阶 之 集群环境搭建(一)概述
- poj 3420 Quad Tiling(状态压缩矩阵递推)
- Subsets -- LeetCode
- Leetcode: Permutations 理解分析