免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2115 | 回复: 2
打印 上一主题 下一主题

一个setitimer问题(linux programming by exmaple) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-05-14 18:03 |只看该作者 |倒序浏览
看了linux programming by examples的第14章,课后习题有个问题想请教一下.
下面是程序
/* ch14-timers.c ---- demonstrate interval timers */

#include <stdio.h>
#include <assert.h>
#include <signal.h>
#include <sys/time.h>

/* handler --- handle SIGALRM */

void handler(int signo)
{
        static const char msg[] = "\n*** Timer expired, you lose ***\n";

        assert(signo == SIGALRM);

        write(2, msg, sizeof(msg) - 1);
        exit(1);
}

/* main --- set up timer, read data with timeout */

int main(void)
{
        struct itimerval tval;
        char string[BUFSIZ];

        timerclear(& tval.it_interval);        /* zero interval means no reset of timer */
        timerclear(& tval.it_value);

        tval.it_value.tv_sec = 10;        /* 10 second timeout */

        (void) signal(SIGALRM, handler);

        printf("You have ten seconds to enter\nyour name, rank, and serial number: ");

        (void) setitimer(ITIMER_REAL, & tval, NULL);
        if (fgets(string, sizeof string, stdin) != NULL) {
                (void) setitimer(ITIMER_REAL, NULL, NULL);        /* turn off timer */
                /* process rest of data, diagnostic print for illustration */
                printf("I'm glad you are being cooperative.\n");
        } else
                printf("\nEOF, eh?  We won't give up so easily!\n");

        exit(0);
}
问题是:
    if (fgets(string, sizeof string, stdin) != NULL) {
(void) setitimer(ITIMER_REAL, NULL, NULL); /* turn off timer */
printf("I'm glad you are being cooperative. \n");
}
else
printf("\nEOF, eh? We won't give up so easily!\n");

上面的setitimer有race condition,假设用户输入在规定时间内输入了字符,如果这时程序被suspend,那么定时器将不能被关闭,因而产生了race condition,我的问题是该怎么做呢?来尽量消除这个race condition???

论坛徽章:
0
2 [报告]
发表于 2012-05-16 15:04 |只看该作者
怎么没人回答啊????难道是太难了???

论坛徽章:
0
3 [报告]
发表于 2012-05-16 15:40 |只看该作者
  1. /* ch14-timers.c ---- demonstrate interval timers */

  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <signal.h>
  5. #include <sys/time.h>

  6. /* handler --- handle SIGALRM */

  7. void handler(int signo)
  8. {
  9.         static const char msg[] = "\n*** Timer expired, you lose ***\n";

  10.         assert(signo == SIGALRM);

  11.         write(2, msg, sizeof(msg) - 1);
  12.         exit(1);
  13. }

  14. /* main --- set up timer, read data with timeout */

  15. int main(void)
  16. {
  17.         struct itimerval tval;
  18.         char string[BUFSIZ];

  19.         timerclear(& tval.it_interval);        /* zero interval means no reset of timer */
  20.         timerclear(& tval.it_value);

  21.         tval.it_value.tv_sec = 10;        /* 10 second timeout */

  22.         (void) signal(SIGALRM, handler);

  23.         printf("You have ten seconds to enter\nyour name, rank, and serial number: ");

  24.         (void) setitimer(ITIMER_REAL, & tval, NULL);
  25.         if (fgets(string, sizeof string, stdin) != NULL) {
  26.                 (void) setitimer(ITIMER_REAL, NULL, NULL);        /* turn off timer */
  27.                 /* process rest of data, diagnostic print for illustration */
  28.                 printf("I'm glad you are being cooperative.\n");
  29.         } else
  30.                 printf("\nEOF, eh?  We won't give up so easily!\n");

  31.         exit(0);
  32. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP