杂记——kill函数和raise函数

kill

kill函数用于向任何进程组或进程发送信号。

被包含在一下两个头文件中。

#include<sys/types.h>
#include<signal.h>

函数原型如下:

int kill(pid_t pid,int sig);
//成功执行返回0  失败返回-1
//失败是errno会被设置为以下值:
//EINVAL	指定的信号编号无效
//EPERM		权限不够无法传送信号给指定进程
//ESRCH		参数pid指定的进程或进程组不存在

kill函数的pid参数主要有四种情况:

pid值 意义
pid > 0 将该信号发送给进程ID为pid的进程
pid == 0 将该信号发送给与发送进程属于同一进程组的所有进程
pid < 0 将该信号发送给其进程组ID等于pid的绝对值
pid == -1 将该信号发送给发送进程有权向它们发送信号的系统上的所有进程

同时,进程将信号发送给其他进程需要权限。超级用户可以将信号发送给任意进程。对于普通用户而言,其只能发送给实际或有效用户ID相同的进程

POSIX.1 将编号为0的信号定义为空信号。如果signo参数为0,则kill扔执行正常的错误检查,但不发送信号。这常被用来确定一个特定进程是否存在。如果向一个不存在的进程发送空信号,则kill返回-1,并将errno设置成ESRCH。

但是根据所需知识我们知道:

  1. 进程ID是会被重复使用的,可能被发送信号进程并不是你所期望的那个。
  2. 用kill函数测试进程是否存在并不是原子操作,可能在测试过程中进程就已经终止,这时测试就无多大意义。

kill实例

#include<sys/types.h>
#include<sys/wait.h>
#include<signal.h>
#include<unistd.h>

#include<iostream>
using namespace std;

int main()
{
    
    
	pid_t pid;
	int status;		//save end status of child process

	if ((pid = fork()) == -1)
	{
    
    
		cout << "fork error" << endl;
		exit(0);
	}
	else if (pid == 0)
	{
    
    
		cout << "there is child process" << endl;
		sleep(100);		//wait parent process
		exit(0);
	}
	else
	{
    
    
		if ((waitpid(pid, &status, WNOHANG)) == 0)
		{
    
    
			sleep(5);		//wait child process
			if (kill(pid, SIGKILL))
			{
    
    
				cout << "kill failed" << endl;
			}
			else
				cout << pid << " has been killed" << endl;
		}
	}
	return 0;
}

result:
$ ./main
there is child process
28807 has been killed

raise函数

raise函数常被进程用来向自身发送信号
其函数原型如下:

#include<signal.h>
int raise(int signo);	//成功返回0,失败返回-1

调用raise(signo) == kill(getpid(),signo)

raise 实例

#include<signal.h>
#include<unistd.h>

#include<iostream>
using namespace std;

void sig_handler(int);

int main()
{
    
    
	int ret;

	if ((signal(SIGINT, sig_handler)) == SIG_ERR)
	{
    
    
		cout << "signal set error!" << endl;
		exit(0);
	}

	cout << "signal produce!" << endl;
	if ((raise(SIGINT)) == -1)
	{
    
    
		cout << "raise error!" << endl;
	}

	return 0;
}

void sig_handler(int signal)
{
    
    
	cout << "receive signal:" << signal << endl;
}

result:
$ ./main
signal produce!
receive signal:2

参考文献

[1]UNIX环境高级编程(第二版)
[2]sweetfather.Linux下kill函数用法.CSDN.2018.03.06
[3]C库函数-raise().RUNOOB.com

猜你喜欢

转载自blog.csdn.net/shenmingxueIT/article/details/108303341
今日推荐