我在 ubuntu 上使用 eclipse IDE 来编译我的 c 项目。我有一个计时器。启动定时器后,睡眠或睡眠功能不工作。这是代码;
编辑
/*
============================================================================
Name : TimerTest.c
Author : FK
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h> /* , getenv() */
#include <getopt.h>
#include <unistd.h> /* exec(), daemon() */
#include <stdint.h>
#include <string.h> /* memset(), strerror() */
#include <errno.h> /* errno */
#include <libgen.h> /* basename() */
#include <signal.h>
#include <sys/timex.h> /* ntp_gettime() */
#include <time.h>
#include "timer/timer.h"
#define CONFIG_TIMER_INTERVAL 100 /* in ms */
/* global vars decalarations */
static uint8_t _terminate = 0;
static int _signo = 0;
static int init_prg(int argc, char *argv[]);
static void timer_callback(void);
/* function implementations */
static void sig_handler(int signo)
{
/* http://www.yolinux.com/TUTORIALS/C++Signals.html */
_signo = signo;
switch (signo)
{
case SIGINT: /* Program interrupt. (ctrl-c) */
_terminate = 1;
break;
case SIGTERM:
_terminate = 1;
break;
/* SIGKILL, SIGSTOP yakalanımıyor! */
default:
break;
}
}
int main(int argc, char *argv[])
{
init_prg(argc, argv);
/* start super loop */
while (1)
{
printf("test!\n");
//usleep(1000000);
sleep(1);
}
}
static int init_prg(int argc, char *argv[])
{
int res = -1;
if (signal(SIGINT, sig_handler) == SIG_ERR)
{
//exit(EXIT_FAILURE);
}
/* Termination, Generated by "kill" command. */
if (signal(SIGTERM, sig_handler) == SIG_ERR)
{
//exit(EXIT_FAILURE);
}
start_timer(2000, &timer_callback);
if (res != 0)
{
//exit(EXIT_FAILURE);
}
return res;
}
static void timer_callback(void)
{
printf("timer works!\n");
}
执行程序后,它会回显“测试!” 迅速。忽略 sleep 或 usleep 命令。如果我注释掉 start_timer 行,它会休眠,在定时器之后它不是。有什么想法吗?