Linux下Kill函数用法
来源:互联网 发布:flexible.js 编辑:程序博客网 时间:2024/05/19 20:22
原文地址:http://www.cnblogs.com/winnxm/archive/2010/01/22/1654502.html
[ KILL ]功能描述:
用于向任何进程组或进程发送信号。
头文件用法:
1 #include <sys/types.h>
2
3 #include <signal.h>
4
5 int kill(pid_t pid, int sig);
参数:
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 所指定的进程或进程组不存在
<pre name="code" class="cpp">#include <sys/wait.h>#include <sys/types.h>#include <stdio.h>#include <stdlib.h>#include <signal.h>int main( void ){ pid_t childpid; int status; int retval; childpid = fork(); if ( -1 == childpid ) { perror( "fork()" ); exit( EXIT_FAILURE ); } else if ( 0 == childpid ) { puts( "In child process" ); sleep( 100 );//让子进程睡眠,看看父进程的行为 exit(EXIT_SUCCESS); } else { if ( 0 == (waitpid( childpid, &status, WNOHANG ))) { retval = kill( childpid,SIGKILL ); if ( retval ) { puts( "kill failed." ); perror( "kill" ); waitpid( childpid, &status, 0 ); } else { printf( "%d killed\n", childpid ); } } } exit(EXIT_SUCCESS);}//-----------------[root@localhost src]# gcc killer.c[root@localhost src]# ./a.outIn child process4545 killed
在确信fork调用成功后,子进程睡眠100秒,然后退出。
同时父进程在子进程上调用waitpid函数,但使用了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函数的用法
- Centos升级python到2.6
- 通过UUID自动挂载硬盘
- Spring Login Design
- 音频和视频同步ffplay5原理
- 删除ORACLE归档日志
- Linux下Kill函数用法
- Linux 初学者(仅一篇)
- Linux 下的串口编程(一)
- 浅尝C++——const与#define定义符号常量
- STM32 ADC的学习
- Perl 脚本在给定的文件夹目录中递归查找文件
- 创建具有管理员权限的Command命令行快捷方式
- opnet 14.5与vs2008联调
- jsp入门教程