30.信号相关函数
1.kill--发送信号给指定进程
相当于:kill(getpid(),sig);
函数原型:void abort(void);
(1)alarm--设置定时器(每个进程只有一个定时器)
使用的是自然定时法(真实生活是多少秒,就是多少秒)
不受进程状态的影响
函数原型:unsigned int alarm(unsigned int seconds);
参数:秒
当时间到达之后,函数发出一个信号:SIGALRM
测试程序运行的时间:time 可执行程序
real=用户+内核+损耗
损耗来自文件IO操作
(2)setitimer--定时器,并实现周期性定时
函数原型:
int setitimer(int which,
ITIMER_REAL:自然定时法来定时
ITIMER_VIRTUAL:用户区运行的时间
ITIMER_PROF:用户区+内核区运行的时间
const struct itimerval* new_value,
struct itimerval{
struct timerval it_interval; //定时器循环周期
struct timerval it_value; //第一次触发定时器的时间
};
struct timerval{
time_t tv_dec; // seconds
suseconds_t tv_usec; // microsenconds
};
函数原型:int kill(pid_t pid,int sig);
相当于:kill(getpid(),sig);
函数原型:int raise(int sig);
3.abort--给自己发送异常终止信号函数原型:void abort(void);
没有参数也没有返回值,永远不会调用失败
4.闹钟(定时器)(1)alarm--设置定时器(每个进程只有一个定时器)
使用的是自然定时法(真实生活是多少秒,就是多少秒)
不受进程状态的影响
函数原型:unsigned int alarm(unsigned int seconds);
参数:秒
当时间到达之后,函数发出一个信号:SIGALRM
返回值如下:
测试程序运行的时间:time 可执行程序
real=用户+内核+损耗
损耗来自文件IO操作
(2)setitimer--定时器,并实现周期性定时
函数原型:
int setitimer(int which,
ITIMER_REAL:自然定时法来定时
ITIMER_VIRTUAL:用户区运行的时间
ITIMER_PROF:用户区+内核区运行的时间
const struct itimerval* new_value,
struct itimer* old_value // 一般填NULL
);
struct itimerval{
struct timerval it_interval; //定时器循环周期
struct timerval it_value; //第一次触发定时器的时间
};
struct timerval{
time_t tv_dec; // seconds
suseconds_t tv_usec; // microsenconds
};