免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: piginthetree
打印 上一主题 下一主题

如何实现这样的功能? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2009-08-04 12:59 |只看该作者
crontab吧

论坛徽章:
0
12 [报告]
发表于 2009-08-04 14:37 |只看该作者
找到一个例子
  编译方式:   
    gcc   example.c   -lrt   -lpthread   
   
  #include   <stdio.h>   
  #include   <time.h>   
  #include   <signal.h>   
  void   
  handle   (sigval_t   v)   
  {   
      time_t   t;   
      char   p[32];   
      time   (&t);   
      strftime   (p,   sizeof   (p),   "%T",   localtime   (&t));   
      printf   ("%s   thread   %d,   val   =   %d,   signal   captured.\n",   p,   pthread_self   (),   
                      v.sival_int);   
      return;   
  }   
   
  int   
  create   (int   seconds,   int   id)   
  {   
      timer_t   tid;   
      struct   sigevent   se;   
      struct   itimerspec   ts,   ots;   
      memset   (&se,   0,   sizeof   (se));   
      se.sigev_notify   =   SIGEV_THREAD;   
      se.sigev_notify_function   =   handle;   
      se.sigev_value.sival_int   =   id;   
      if   (timer_create   (CLOCK_REALTIME,   &se,   &tid)   <   0)   
          {   
              perror   ("timer_creat";   
              return   -1;   
          }   
      puts   ("timer_create   successfully.";   
      ts.it_value.tv_sec   =   3;   
      ts.it_value.tv_nsec   =   0;   
      ts.it_interval.tv_sec   =   seconds;   
      ts.it_interval.tv_nsec   =   0;   
      if   (timer_settime   (tid,   TIMER_ABSTIME,   &ts,   &ots)   <   0)   
          {   
              perror   ("timer_settime";   
              return   -1;   
          }   
      return   0;   
  }   
   
  int   
  main   (void)   
  {   
      create   (3,   1);   
      create   (5,   2);   
      for   (;   
          {   
              sleep   (10);   
          }   
  }

论坛徽章:
0
13 [报告]
发表于 2009-08-04 15:45 |只看该作者
用 crontab 是最方便的

写代码还可以这样


  1. #include <stdio.h>
  2. #include <pthread.h>

  3. void* fun1(void* arg)
  4. {
  5.         printf("fun1\n");
  6.         return 0;        
  7. }

  8. void* fun2(void* arg)
  9. {
  10.         printf("fun2\n");
  11.         return 0;
  12. }

  13. int gcd(int m,int n)
  14. {
  15.         int t;
  16.         while(t = m%n)
  17.         {
  18.                 m = n;
  19.                 n = t;
  20.         }
  21.         
  22.         return n;
  23. }

  24. int main()
  25. {
  26.         int ts1 = 60;
  27.         int ts2 = 10;
  28.         
  29.         int ts_gcd = gcd(ts1,ts2);
  30.         
  31.         int n = 0;
  32.         while(1)
  33.         {
  34.                 if(n*ts_gcd%ts1 == 0)
  35.                 {
  36.                         pthread_t thd1;
  37.                         pthread_create(&thd1, 0, fun1, 0);
  38.                 }
  39.                
  40.                 if(n*ts_gcd%ts2 == 0)
  41.                 {
  42.                         pthread_t thd2;
  43.                         pthread_create(&thd2, 0, fun2, 0);
  44.                 }
  45.                
  46.                 n++;
  47.                 sleep(ts_gcd);
  48.         }
  49.         
  50.         return 0;
  51. }
复制代码

  1. gcc -g test.c -lpthread
复制代码

[ 本帖最后由 xujg 于 2009-8-4 15:47 编辑 ]

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
14 [报告]
发表于 2009-08-04 15:55 |只看该作者
原帖由 piginthetree 于 2009-8-4 11:09 发表
程序每隔一分钟,做一个操作,每隔10分钟,做另一个操作,linux平台下面咋搞?用一个进程能够实现么?还是要并发?

试试crontab。

论坛徽章:
0
15 [报告]
发表于 2009-08-04 23:10 |只看该作者
原帖由 piginthetree 于 2009-8-4 11:09 发表
程序每隔一分钟,做一个操作,每隔10分钟,做另一个操作,linux平台下面咋搞?用一个进程能够实现么?还是要并发?


是否要并发,要取决于lz的具体需求:
每隔一分钟做的一个操作,每一次做的操作是否能在下一次1分钟到来之前完成, 或者是否允许各个没有完成的一分钟任务并行?

每隔10分钟,做另一个操作,同上问...

论坛徽章:
0
16 [报告]
发表于 2009-08-05 10:36 |只看该作者
看了下crontab,如果要取消这个功能的话必须重新设置crontab么?这样好像又添加了别的麻烦

论坛徽章:
0
17 [报告]
发表于 2009-08-05 11:01 |只看该作者
原帖由 piginthetree 于 2009-8-5 10:36 发表
看了下crontab,如果要取消这个功能的话必须重新设置crontab么?这样好像又添加了别的麻烦


自己写,也无非是重新实现下crontab
而且多进程可靠性要好点?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP