C语言中的定时器链表

发布于 2024-09-13 08:44:20 字数 3704 浏览 1 评论 0原文

我应该编写一个具有两个函数的程序:

一个函数负责构建一个数据结构,该数据结构由运行时(您获得一个函数指针)内的函数列表填充(启动给定程序运行后几秒钟) )。你应该知道如何操作功能,添加器官的数据结构,如何更有效地完成,保存测试,交叉检查等。 第二个函数按顺序运行所有函数。例如,如果程序的启动一旦有两个函数是 12 个 5 秒时间段中的一个,则第一个函数在 5 秒后运行,其他函数在第一个函数后运行 7 秒。 (自开始以来总共 12 个)。

再次必须注意如何做到这一点,即使是与第一个功能相关。 一切都在运行后关闭,即函数完成运行后不再是借口。所有重新运行的程序都会进入她的新功能。

附件是两个实现:一个是笨拙的并且没有通过主输出和用户的输出进行编译(至少对我来说),另一个是不麻烦但没有主输出和用户。 我需要使用繁琐的输出给用户(当然你还需要main)。有人可以帮我吗?为什么这里需要一个函数指针?任何解释或附加文档都可以帮助我更好地理解正在发生的事情。

预先非常感谢

    /*First implemntation: heavy and doesn't compile*/


#ifndef  __MY_TIMER_H__
#define  __MY_TIMER_H__

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef void(*fun_t)(int);
typedef struct timer
{
  int   time;
  fun_t func;
  struct timer *next;
}timers;

void add_timer(int sec, fun_t func,timers *head);

void run_timers(timers *head);

void timer_func(int);

#endif /* __MY_TIMER_H__ */

----------------------------------------------------------------------

#include "my_timer.h"

#undef  DEBUG
#define DEBUG 1

int main()
{
  int time = 1;
  timers *head = NULL;
  fun_t func = timer_func;

  while (time < 1000)
  {
    printf("\nCalling add_timer(time=%d, func=0x%x, head=0x%x)\n", time, func, &head);
    add_timer(time, func,&head);
    time *= 2;
  }

  run_timers(&head);
  return 0;
}

timers *head;

void timer_func(int time)
{
  printf("Called %s at time %d.\n", __FUNCTION__, time);
  return;
}

void add_timer(int sec, fun_t func, timers *head)
{
  timers *curr = head, *prev = NULL;
  timers *new_timer = NULL;

  new_timer = (timers*)malloc(sizeof(timers));
  new_timer->time = sec;
  new_timer->func = func;
  new_timer->next = NULL;

  if (curr == NULL) {
    head = new_timer;
    return;
  }

  while (curr != NULL && curr->time < sec) {
    prev = curr;
    curr = curr->next;
  }

  if (curr == head) {
    new_timer->next = curr;
    head = new_timer;
  }
  else {
    new_timer->next = prev->next;
    prev->next = new_timer;
  }    

  return;
}

void run_timers(timers *head)
{
  int elapsed_time = 0;
  timers *curr = head;
  timers *timer_p = NULL;

  while (curr != NULL) {
    printf("\nGoing to sleep for %d secs\n", curr->time - elapsed_time);
    printf("\ncurr->time = %d elapsed_time = %d\n", curr->time, elapsed_time);
    usleep(curr->time - elapsed_time);
    printf("\nWoke up after %d secs\n", curr->time - elapsed_time);
    elapsed_time = curr->time;
    curr->func(curr->time);
    timer_p = curr;
    curr = curr->next;
    free(timer_p);
    head = curr;
  }

  return;
} 

/*second implemntation: no main and no output for user*/ 

#include <stdio.h> 
#include<conio.h> 
#include <stdlib.h> 
#include <windows.h> 

typedef void(*fun_t)(void); 

typedef struct timer_s{ 
int time; 
fun_t fun; 
struct timer_s* next; 
}timer; 

timer* head=NULL; 

void add_timer(int sec, fun_t fun) 
{ 
    timer* curr,*new_timer; 
    new_timer=(timer*)malloc(sizeof(timer)); 
    new_timer->time=sec; 
    new_timer->fun=fun; 
    new_timer->next=NULL; 
    curr=head; 

    if(curr==NULL) 
    { 
        head=new_timer; 
        return; 
    } 

    while((curr->next!=NULL)&&(curr->next->time<sec)) 
        curr=curr->next; 

    new_timer->next=curr->next; 
    curr->next=new_timer; 

    return; 
} 

void run_timers() 
{ 
    int elapsed=0; 
    timer* tmp; 

    while(head) 
    { 
        Sleep((head->time-elapsed)*1000); 
        elapsed=head->time; 
        head->fun(); 
        tmp=head; 
        head=head->next; 
        free(tmp); 
    } 
    return; 
}

I'm supposed to write a program with two functions:

One function is responsible for building a data structure populated by a list of functions within a run (you get a function pointer) its run time (a few seconds after starting a given program running). You should know to operate the function, adding the data structure of organs, how to do it more efficiently, saving tests, cross checks, etc..
The second function runs all the functions in order. If for example the start of the program once you have two functions were one of the five second time period of 12, first ran after 5 seconds and the other seven seconds after the first. (Total 12 since the beginning).

Again have to pay attention to how to do it, even in relation to the first function.
Everything was closed after the run, ie after a function has finished running is no longer an excuse again. All re-run of the program will be entering her new functions.

Attached are two implentations: one awkward and not going through compiling (at least for me) with the main and an output for user, and the other is not cumbersome but without the main output and the user.
I need to use cumbersome with output to the user (and then of course you also need main). Can anyone help me please? Why do I need a function pointer here? Any explanation or additional documentation on help me better understand what's going on.

Many thanks in advance

    /*First implemntation: heavy and doesn't compile*/


#ifndef  __MY_TIMER_H__
#define  __MY_TIMER_H__

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

typedef void(*fun_t)(int);
typedef struct timer
{
  int   time;
  fun_t func;
  struct timer *next;
}timers;

void add_timer(int sec, fun_t func,timers *head);

void run_timers(timers *head);

void timer_func(int);

#endif /* __MY_TIMER_H__ */

----------------------------------------------------------------------

#include "my_timer.h"

#undef  DEBUG
#define DEBUG 1

int main()
{
  int time = 1;
  timers *head = NULL;
  fun_t func = timer_func;

  while (time < 1000)
  {
    printf("\nCalling add_timer(time=%d, func=0x%x, head=0x%x)\n", time, func, &head);
    add_timer(time, func,&head);
    time *= 2;
  }

  run_timers(&head);
  return 0;
}

timers *head;

void timer_func(int time)
{
  printf("Called %s at time %d.\n", __FUNCTION__, time);
  return;
}

void add_timer(int sec, fun_t func, timers *head)
{
  timers *curr = head, *prev = NULL;
  timers *new_timer = NULL;

  new_timer = (timers*)malloc(sizeof(timers));
  new_timer->time = sec;
  new_timer->func = func;
  new_timer->next = NULL;

  if (curr == NULL) {
    head = new_timer;
    return;
  }

  while (curr != NULL && curr->time < sec) {
    prev = curr;
    curr = curr->next;
  }

  if (curr == head) {
    new_timer->next = curr;
    head = new_timer;
  }
  else {
    new_timer->next = prev->next;
    prev->next = new_timer;
  }    

  return;
}

void run_timers(timers *head)
{
  int elapsed_time = 0;
  timers *curr = head;
  timers *timer_p = NULL;

  while (curr != NULL) {
    printf("\nGoing to sleep for %d secs\n", curr->time - elapsed_time);
    printf("\ncurr->time = %d elapsed_time = %d\n", curr->time, elapsed_time);
    usleep(curr->time - elapsed_time);
    printf("\nWoke up after %d secs\n", curr->time - elapsed_time);
    elapsed_time = curr->time;
    curr->func(curr->time);
    timer_p = curr;
    curr = curr->next;
    free(timer_p);
    head = curr;
  }

  return;
} 

/*second implemntation: no main and no output for user*/ 

#include <stdio.h> 
#include<conio.h> 
#include <stdlib.h> 
#include <windows.h> 

typedef void(*fun_t)(void); 

typedef struct timer_s{ 
int time; 
fun_t fun; 
struct timer_s* next; 
}timer; 

timer* head=NULL; 

void add_timer(int sec, fun_t fun) 
{ 
    timer* curr,*new_timer; 
    new_timer=(timer*)malloc(sizeof(timer)); 
    new_timer->time=sec; 
    new_timer->fun=fun; 
    new_timer->next=NULL; 
    curr=head; 

    if(curr==NULL) 
    { 
        head=new_timer; 
        return; 
    } 

    while((curr->next!=NULL)&&(curr->next->time<sec)) 
        curr=curr->next; 

    new_timer->next=curr->next; 
    curr->next=new_timer; 

    return; 
} 

void run_timers() 
{ 
    int elapsed=0; 
    timer* tmp; 

    while(head) 
    { 
        Sleep((head->time-elapsed)*1000); 
        elapsed=head->time; 
        head->fun(); 
        tmp=head; 
        head=head->next; 
        free(tmp); 
    } 
    return; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李白 2024-09-20 08:44:20

在第一个实现中,在 main() 的末尾,您必须更改此设置:

  run_timers(&head);
  return 0;
}

通过下面的代码!它将编译并运行。你能看出其中的区别吗?

  run_timers(head);
  return 0;
}

好吧,你可能对指针感到困惑,我建议你再研究一会儿。

因此,为了解释为什么您的代码无法编译,如果您回忆一下 run_timers() 函数签名,

void run_timers(timers *head);

您将看到它接收到一个指向 timers 结构的指针(或内存地址),在 main() 中,您按应有的方式声明它:

timers *head = NULL;

因此,当您需要调用 run_timers() 时,您必须按原样传递 head 变量:

run_timers(head); //right

发生的情况是您将其调用为:

run_timers(&head); //wrong

这意味着timers结构的指针的内存地址,而不是指针本身。

On the first implementation, at the end of main() you must change this:

  run_timers(&head);
  return 0;
}

By the code below! It will compile and run. Can you spot the difference?

  run_timers(head);
  return 0;
}

Ok, you might be confuse about pointers and I suggest you study it a little longer.

So, just to explain why your code wasn't compiling, if you recall the run_timers() function signature,

void run_timers(timers *head);

you'll see that it receives a pointer to (or memory address of) a timers structure, and inside main() you declare it as it should be:

timers *head = NULL;

Therefore, when you need to call run_timers(), you must pass the head variable as it is:

run_timers(head); //right

What happened is that you were calling it as:

run_timers(&head); //wrong

which means the memory address of the pointer of the timers structure, and not the pointer itself.

素年丶 2024-09-20 08:44:20

我想我做到了。任何优化都会受到欢迎。

#include <stdio.h> 
#include<conio.h> 
#include <stdlib.h> 
#include <windows.h> 

typedef void(*fun_t)(int);

typedef struct timer_s{ 
int time; 
fun_t fun; 
struct timer_s* next; 
}timer; 

timer* head=NULL; 
void timer_func(int time);
void run_timers();
void add_timer(int sec, fun_t fun) ;

int main()
{
  int time = 1;
  timer *head = NULL;
  fun_t func = timer_func;

  while (time < 20)
  {
    printf("\nCalling add_timer(time=%d, func=0x%x, head=0x%x)\n", time, func, &head);
    add_timer(time, func);
    time *= 2;
  }

  run_timers();
  return 0;
}

void timer_func(int time)
{
  printf("\nCalled %s before %d seconds.\n", __FUNCTION__, time);
  return;
}

void add_timer(int sec, fun_t fun) 
{ 
    timer* curr,*new_timer; 
    new_timer=(timer*)malloc(sizeof(timer)); 
    new_timer->time=sec; 
    new_timer->fun=fun; 
    new_timer->next=NULL; 
    curr=head; 

    if(curr==NULL) 
    { 
        head=new_timer; 
        return; 
    } 

    while((curr->next!=NULL)&&(curr->next->time<sec)) 
        curr=curr->next; 

    new_timer->next=curr->next; 
    curr->next=new_timer; 

    return; 
} 

void run_timers() 
{ 
    int elapsed=0; 
    timer* tmp; 

    while(head) 
    { 
        Sleep((head->time-elapsed)*1000); 
        elapsed=head->time; 
        head->fun(elapsed);
        tmp=head; 
        head=head->next; 
        free(tmp); 
    } 
    return; 
}

I Think I did it. Any optimizations will be gladly welcomed.

#include <stdio.h> 
#include<conio.h> 
#include <stdlib.h> 
#include <windows.h> 

typedef void(*fun_t)(int);

typedef struct timer_s{ 
int time; 
fun_t fun; 
struct timer_s* next; 
}timer; 

timer* head=NULL; 
void timer_func(int time);
void run_timers();
void add_timer(int sec, fun_t fun) ;

int main()
{
  int time = 1;
  timer *head = NULL;
  fun_t func = timer_func;

  while (time < 20)
  {
    printf("\nCalling add_timer(time=%d, func=0x%x, head=0x%x)\n", time, func, &head);
    add_timer(time, func);
    time *= 2;
  }

  run_timers();
  return 0;
}

void timer_func(int time)
{
  printf("\nCalled %s before %d seconds.\n", __FUNCTION__, time);
  return;
}

void add_timer(int sec, fun_t fun) 
{ 
    timer* curr,*new_timer; 
    new_timer=(timer*)malloc(sizeof(timer)); 
    new_timer->time=sec; 
    new_timer->fun=fun; 
    new_timer->next=NULL; 
    curr=head; 

    if(curr==NULL) 
    { 
        head=new_timer; 
        return; 
    } 

    while((curr->next!=NULL)&&(curr->next->time<sec)) 
        curr=curr->next; 

    new_timer->next=curr->next; 
    curr->next=new_timer; 

    return; 
} 

void run_timers() 
{ 
    int elapsed=0; 
    timer* tmp; 

    while(head) 
    { 
        Sleep((head->time-elapsed)*1000); 
        elapsed=head->time; 
        head->fun(elapsed);
        tmp=head; 
        head=head->next; 
        free(tmp); 
    } 
    return; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文