免费注册 查看新帖 |

Chinaunix

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

[C] 我的程序阻塞在一个非常令人困惑的地方!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-07-15 12:31 |只看该作者 |倒序浏览
今天写了一个测试程序,本想测试PREEMPT RT LINUX的时间精度,可程序总是阻塞在第30行。
第29行的printf("\nMSG:1.The second is:%d ,the nanosecond is :%d", t.tv_sec, t.tv_nsec);可以输出,
但到第31行的printf("\nMSG:2 就没有输出了,光标一直在那里闪动。
按道理来说,程序不应该在第30行或31行那里阻塞啊!!
请教高手!谢谢!

1#include <stdlib.h>
2#include <stdio.h>
3#include <time.h>
4#include <sched.h>
5#include <sys/mman.h>
6#include <string.h>
7#define MY_PRIORITY (49) /* we use 49 as the PRREMPT_RT use 50
8                           as the priority of kernel tasklets
9                            and interrupt handler by default */
10
11 int main(int argc, char* argv[])
12 {
13       struct timespec t;
14       struct sched_param param;
15        int interval = 10; /* 50us*/
16       /* Declare ourself as a real time task */
17        param.sched_priority = MY_PRIORITY;
18        if(sched_setscheduler(getpid() , SCHED_FIFO, &param) == -1) {
19               perror("sched_setscheduler failed");
20                exit(-1);
21       }
22        /* Lock memory */
23       if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
24                perror("mlockall failed");25
26                exit(-2);
27       }
28        clock_gettime(CLOCK_MONOTONIC ,&t);
29        printf("\nMSG:1.The second is:%d ,the nanosecond is :%d", t.tv_sec, t.tv_nsec);
30        clock_gettime(CLOCK_MONOTONIC ,&t);
31        printf("\nMSG:2.The second is:%d ,the nanosecond is :%d", t.tv_sec, t.tv_nsec);
32        clock_nanosleep(2, TIMER_ABSTIME, &t, NULL);
33        clock_gettime(CLOCK_MONOTONIC ,&t);
34        printf("\nMSG:3.The second is:%d ,the nanosecond is :%d,it is after nanosleep\n", t.tv_sec, t.tv_nsec);
35
36  return 0;
37}

论坛徽章:
5
技术图书徽章
日期:2013-11-07 13:21:58技术图书徽章
日期:2013-12-07 10:34:46技术图书徽章
日期:2014-04-23 08:50:31双鱼座
日期:2014-09-16 09:12:34亥猪
日期:2015-01-23 13:37:49
2 [报告]
发表于 2011-07-15 12:42 |只看该作者
链接 -lrt了吗?

论坛徽章:
0
3 [报告]
发表于 2011-07-15 12:50 |只看该作者
肯定连接了,要不都编译不了了!

论坛徽章:
5
技术图书徽章
日期:2013-11-07 13:21:58技术图书徽章
日期:2013-12-07 10:34:46技术图书徽章
日期:2014-04-23 08:50:31双鱼座
日期:2014-09-16 09:12:34亥猪
日期:2015-01-23 13:37:49
4 [报告]
发表于 2011-07-15 13:06 |只看该作者
吧printf换成 fprintf(stderr, xxxxx) 看看吧,或者把 \n 放到字符串最后。

论坛徽章:
0
5 [报告]
发表于 2011-07-15 14:51 |只看该作者
吧printf换成 fprintf(stderr, xxxxx) 看看吧,或者把 \n 放到字符串最后。
nketc 发表于 2011-07-15 13:06


按你的方法,问题解决了,谢谢!
请问你有没有搞过Ingo molnar preempt RT linux,即RED HAT 公司出的那个RT补丁。
现在这方面的参考很少,又没有API!

论坛徽章:
0
6 [报告]
发表于 2011-07-15 14:59 |只看该作者
晕,原来LZ卡在这个地方

标准输入输出的缓冲区,不是实时flush的

论坛徽章:
0
7 [报告]
发表于 2011-07-15 15:07 |只看该作者
晕,原来LZ卡在这个地方

标准输入输出的缓冲区,不是实时flush的
net_robber 发表于 2011-07-15 14:59


请问要实时flush该如何解决???

论坛徽章:
0
8 [报告]
发表于 2011-07-15 15:17 |只看该作者
手动flush
  1. NAME
  2.        fflush - flush a stream

  3. SYNOPSIS
  4.        #include <stdio.h>

  5.        int fflush(FILE *stream);

  6. DESCRIPTION
  7.        For output streams, fflush() forces a write of all user-space buffered data for the given output or update stream via
  8.        the stream's underlying write function.  For input streams, fflush() discards any buffered data that has been fetched
  9.        from the underlying file, but has not been by the application.  The open status of the stream is unaffected.

  10.        If the stream argument is NULL, fflush() flushes all open output streams.
复制代码

论坛徽章:
5
技术图书徽章
日期:2013-11-07 13:21:58技术图书徽章
日期:2013-12-07 10:34:46技术图书徽章
日期:2014-04-23 08:50:31双鱼座
日期:2014-09-16 09:12:34亥猪
日期:2015-01-23 13:37:49
9 [报告]
发表于 2011-07-15 17:27 |只看该作者
不搞内核
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP