1.信号介绍
1.在linux系统中,信号对于每个进程来说都是提前已知的,换句话说,对于进程而言,即便信号没有产生,但我们已经有了处理信号的能力。
2.信号的产生对于进程来说是异步的,所以当信号产生的时候,进程可能有以下几种情况:
(1)中断进程(执行默认动作)
(2)忽略信号
(3)自定义操作
2.信号操作
每个信号都有一个名字,可以用kill -l查看:
信号从1开始,不存在0,32,33。
信号的自定义操作:
demo:
#include<iostream>
#include<unistd.h>
#include<signal.h>
using namespace std;
void handler(int n=0)
{
cout<<"hello signal 2"<<endl;
}
void handler1(int n=0)
{
cout<<"hello signal 3"<<endl;
}
int main()
{
signal(SIGINT,handler);
signal(SIGQUIT,handler1);
while(1) {
cout<<"hello bit"<<" mypid:"<<getpid()<<endl;
sleep(1);
}
return 0;
2.杀死信号
#include<iostream>
#include<unistd.h>
#include<cstdio>
#include <sys/types.h>
#include <signal.h>
#include <sys/types.h>
#include<string>
using namespace std;
static void usage(const string str)
{
cerr<<"pid & signo"<<endl;
}
int main(int argc,char *argv[])
{
if(argc!=3)
{
usage(argv[0]);
exit(0);
}
if(kill(atoi(argv[2]),atoi(argv[1])==-1))
{
cerr<<"kill failed"<<endl;
}
return 0;
}
3.给自己的信号
在调用该函数后,会自己给自己一个信号。
4.给自己发送信号6
5.定时器中断