定义:
unsigned int alarm(unsigned int seconds);
表头文件:
#include<unistd.h>
说明:
alarm()用来设置信号SIGALRM(14)在经过参数seconds指定的秒数后传送给目前的进程。如果参数seconds 为0, 则之前设置的闹钟会被取消, 并将剩下的时间返回。
返回值:
返回之前闹钟的剩余秒数, 如果之前未设闹钟则返回0。
相关函数:
signal, sleep
示例:
#include <unistd.h> #include <stdio.h> int main() { int counter; alarm(1); for (counter=0; 1; counter++) printf("counter=%d ", counter); return 0; }
运行结果:
counter=32041
counter=32042
counter=32043
counter=32044
counter=32045
counter=32046
counter=32047
counter=32048
counter=32049
counter=32050
counter=32051
counter=32052
counter=32053
counter=32054
counter=32055
counter=32056
counter=32057
counter=32058
counter=32059
counter=32060
counter=32061
counter=32062
counter=32063
counter=32064
counter=32065
counter=32066
counter=32067
counter=32068Alarm clock
可以利用此函数测试CPU性能。