定时器源代码C语言

本文分享了使用C语言实现定时器的源代码,详细解析了代码执行过程,帮助读者理解定时器的工作原理和编程技巧。
摘要由CSDN通过智能技术生成

源代码:

/*snmp_alarm.c*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>

#include <sys/time.h>
#include <time.h>

#include "snmp_alarm.h"

#define SNMP_MALLOC_STRUCT(s) (struct s *) calloc(1, sizeof(struct s)) 

static struct snmp_alarm *thealarms = NULL;
static int start_alarms = 0;
static unsigned int regnum = 1;

int init_alarm_post_config()
{
   
start_alarms = 1;
set_an_alarm();
return 0;
}
void sa_update_entry(struct snmp_alarm *a)
{
   



if(a->t_last.tv_sec == 0 && a->t_last.tv_usec == 0) {
   
        struct timeval t_now;
/* 
* Never been called yet, call time `t' from now. 
*/

        gettimeofday(&t_now, NULL);




        a->t_last.tv_sec = t_now.tv_sec;
        a->t_last.tv_usec = t_now.tv_usec;




        a->t_next.tv_sec = t_now.tv_sec + a->t.tv_sec;
        a->t_next.tv_usec = t_now.tv_usec + a->t.tv_usec;


        while(a->t_next.tv_usec >= 1000000) {
   

        a->t_next.tv_usec -= 1000000;

        a->t_next.tv_sec += 1;
        }
}




        else if(a->t_next.tv_sec == 0 && a->t_next.tv_usec == 0) {
   
/* 
* We've been called but not reset for the next call. 
*/
                if(a->flags & SA_REPEAT) {
   





                        if(a->t.tv_sec == 0 && a->t.tv_usec == 0) {
   
                                printf("update_entry: illegal interval specified\n");
                                snmp_alarm_unregister(a->clientreg);
                                return;
}
                        a->t_next.tv_sec = a->t_last.tv_sec + a->t.tv_sec;
                        a->t_next.tv_usec = a->t_last.tv_usec + a->t.tv_usec;

                        while(a->t_next.tv_usec >= 1000000) {
   
                                a->t_next.tv_usec -= 1000000;
                                a->t_next.tv_sec += 1;
                }
        }
        else {
   
/* 
* Single time call, remove it. 
*/
                snmp_alarm_unregister(a->clientreg);
                }
        }
}
void snmp_alarm_unregister(unsigned int clientreg
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用线程定时器实现的 C语言贪吃蛇源代码: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> #define WIDTH 50 #define HEIGHT 20 #define SNAKE_LEN 5 #define FOOD_SCORE 10 #define SPEED 100 int score = 0; int rx = 0; int ry = 0; int over = 0; int map[HEIGHT][WIDTH] = {0}; int snake[SNAKE_LEN][2] = {0}; int dir = 1; // 0:up 1:right 2:down 3:left void init_map(); void init_snake(); void init_food(); void display(); void move(); void update_snake(int x, int y); void update_food(); void game_over(); void set_timer(); void init_map() { int i, j; for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { if (i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1) { map[i][j] = -1; } else { map[i][j] = 0; } } } } void init_snake() { int i; for (i = 0; i < SNAKE_LEN; i++) { snake[i][0] = 5; snake[i][1] = 5 + i; map[snake[i][0]][snake[i][1]] = i == SNAKE_LEN - 1 ? 1 : 2; } } void init_food() { srand((unsigned)time(NULL)); rx = rand() % (HEIGHT - 2) + 1; ry = rand() % (WIDTH - 2) + 1; if (map[rx][ry] != 0) { init_food(); } else { map[rx][ry] = -2; } } void display() { int i, j; system("cls"); printf("Score: %d\n", score); for (i = 0; i < HEIGHT; i++) { for (j = 0; j < WIDTH; j++) { switch (map[i][j]) { case 0: printf(" "); break; case -1: printf("#"); break; case -2: printf("*"); break; default: printf("o"); break; } } printf("\n"); } } void move() { int i, j; int x = snake[SNAKE_LEN - 1][0]; int y = snake[SNAKE_LEN - 1][1]; switch (dir) { case 0: x--; break; case 1: y++; break; case 2: x++; break; case 3: y--; break; } if (map[x][y] == -1 || map[x][y] >= 1) { game_over(); return; } update_snake(x, y); if (map[x][y] == -2) { score += FOOD_SCORE; update_food(); } else { map[snake[0][0]][snake[0][1]] = 0; for (i = 0; i < SNAKE_LEN - 1; i++) { snake[i][0] = snake[i + 1][0]; snake[i][1] = snake[i + 1][1]; } snake[SNAKE_LEN - 1][0] = x; snake[SNAKE_LEN - 1][1] = y; map[x][y] = SNAKE_LEN; } } void update_snake(int x, int y) { int i; map[snake[0][0]][snake[0][1]] = 0; for (i = 0; i < SNAKE_LEN - 1; i++) { snake[i][0] = snake[i + 1][0]; snake[i][1] = snake[i + 1][1]; } snake[SNAKE_LEN - 1][0] = x; snake[SNAKE_LEN - 1][1] = y; map[x][y] = SNAKE_LEN; } void update_food() { int i, j; rx = rand() % (HEIGHT - 2) + 1; ry = rand() % (WIDTH - 2) + 1; if (map[rx][ry] != 0) { update_food(); } else { map[rx][ry] = -2; } } void game_over() { over = 1; printf("Game Over!\n"); } void set_timer() { time_t t; t = time(NULL); while (1) { if (over == 1) { break; } if (time(NULL) - t >= SPEED / 1000) { move(); display(); t = time(NULL); } } } int main() { init_map(); init_snake(); init_food(); display(); HANDLE hTimer = NULL; LARGE_INTEGER liDueTime; liDueTime.QuadPart = 0; hTimer = CreateWaitableTimer(NULL, TRUE, NULL); SetWaitableTimer(hTimer, &liDueTime, SPEED, NULL, NULL, 0); HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)set_timer, NULL, 0, NULL); WaitForSingleObject(hThread, INFINITE); return 0; } ``` 该代码使用了线程定时器来控制蛇的移动速度,通过 CreateWaitableTimer 和 SetWaitableTimer 函数来创建和设置定时器,使用 CreateThread 函数来创建一个新线程并将 set_timer 函数作为入口点,通过 WaitForSingleObject 函数来等待线程执行完毕。 该代码实现了贪吃蛇的基本功能,包括蛇的移动、食物的随机生成、分数的计算、游戏结束等,可以在 Windows 环境下编译运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值