Files
Linux-C-Notes/C16-并发/parallel/signal/5sec_sig.c
2024-05-26 15:39:14 +08:00

35 lines
745 B
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//!!! 不加volatile关键字loop变量的值不会被修改导致死循环!!!
static volatile int loop = 1;
static void alrm_handler(int sig)
{
loop = 0;
}
/***********************************************************************
* @brief 用sig实现计时
* @param argc
* @param argv
* @return int
***********************************************************************/
int main(int argc, char** argv)
{
long long count = 0;
//!!!
signal(SIGALRM, alrm_handler);
alarm(5);
//!!! signal注册时钟信号的行为需要在alarm之前
while (loop)
count++;
printf("%lld \n", count);
exit(0);
}