打印
[嵌入式linux]

求助: Linux下Timer编程

[复制链接]
2609|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
rockos|  楼主 | 2007-12-7 13:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
为了避免使用信号带来同步问题, 我使用了一个额外的辅助进程实现Timer算法, 用到了usleep和FIFO, 具体描述如下:

进程1(辅助进程):
1> 打开FIFO (用于写)
2> while (1) { usleep(500*1000); write FIFO; }

进程2(主进程):
1> 打开FIFO (用于读)
2> while (1) {
        poll fifo, 检查是否有数据可读
        if polling-success
        {
             读出FIFO数据
             产生Timeout消息
         }
    }
测试时发现, 如果进程1(辅助进程)在运行时, Timer机制可以正常使用. 但是, 一旦停下进程1(辅助进程), 进程2立马陷入无休止的"poll成功-读"过程.

请各位朋友指点一二, 如何在停下进程1后, 让进程2在poll fifo时失败. 谢谢!!

相关帖子

沙发
rockos|  楼主 | 2007-12-7 13:26 | 只看该作者

附测试源码, 辅助进程: fifo-w.c

fifo-w.c

#include <sys/time.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>

#define MYFIFO "timer.FIFO"
void sigalm_handler (int signum);

static int g_fifo;
int main()
{
    int ch;

    struct itimerval timer;

    // open fifo
    g_fifo = open(MYFIFO, O_NONBLOCK|O_WRONLY);
    if (g_fifo < 0)
    {
        perror("open FIFO");
        return -1;
    }
    
    ch = 0;
    while(1)
    {
        usleep(500 * 1000);
        
        write(g_fifo, &ch, sizeof(ch));
        ch++;
    }
    
    close(g_fifo);
    return 0;    
}

使用特权

评论回复
板凳
rockos|  楼主 | 2007-12-7 13:26 | 只看该作者

附测试源码: 主进程 fifo-r.c

#include <sys/time.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>

#define MYFIFO "timer.FIFO"

static int g_fifo;
int main()
{
    struct pollfd pollfds;
    int timer;

    int n;

    // open fifo
    g_fifo = open(MYFIFO, O_RDONLY);
    if (g_fifo < 0)
    {
        perror("open FIFO");
        return -1;
    }
    
    pollfds.fd = g_fifo;
    pollfds.events = POLLIN;
    
    n = 0;
    while(1)
    {
        if (poll(&pollfds, 1, -1) < 0)    // waiting for error.
        {
            perror("poll");
            continue;
        }
        
        if (read(g_fifo, &timer, sizeof(timer)) < 0)
        {
            perror("read");
            continue;
        }
        
        printf("Timer %d expires, No.%d ", timer, n);
        n++;
    }
    
    close(g_fifo);
    return 0;
}

使用特权

评论回复
地板
high| | 2007-12-8 16:42 | 只看该作者

FIF内容可以随意组织吗?

送一个EXIT行吗?

使用特权

评论回复
5
rockos|  楼主 | 2007-12-8 19:53 | 只看该作者

re

其实我并不关心FIFO里是什么内容, 只是希望能让poll read失败

使用特权

评论回复
6
LittleKen| | 2007-12-10 12:11 | 只看该作者

能提供g_fifo的read实现函数么?

能提供g_fifo的read实现函数么?
主进程看似是正确的。

使用特权

评论回复
7
rockos|  楼主 | 2007-12-10 14:13 | 只看该作者

.

FIFO是一种文件系统对象, 通常称为"有名管道"的

使用特权

评论回复
8
rockos|  楼主 | 2007-12-10 16:22 | 只看该作者

试了lseek到末尾, 也没有效果

将fifo-r.c的循环体修改为:

    while(1)
    {
        if (poll(&pollfds, 1, -1) < 0)    // waiting for error.
        {
            perror("poll");
            continue;
        }
        
        if (read(g_fifo, &timer, sizeof(timer)) < 0)
        {
            perror("read");
            continue;
        }

        lseek(g_fifo, SEEK_END, 0);
        printf("Timer %d expires, No.%d ", timer, n);
        n++;
    }

也无济于事...晕~~~

使用特权

评论回复
9
whereis| | 2007-12-10 19:17 | 只看该作者

.

不是很明白。

使用特权

评论回复
10
woyaya| | 2008-1-19 22:09 | 只看该作者

需要对poll的返回状态进行判断

printf("poll returns:0x%x ",(int)pollfds.revents);
看看输出什么结果

使用特权

评论回复
11
后学| | 2008-1-20 13:23 | 只看该作者

re

进程间通信有很多方法,没有听说用timer的,

使用特权

评论回复
12
rockos|  楼主 | 2008-1-23 01:09 | 只看该作者

楼上的,建议仔细分析一下这样做的优势


Timer目的并不是关键, 这样做的优势是消除了异步信号与生俱来的异步缺陷, 付出的代价仅仅是微弱的响应延时.

我只需要使用poll/select就可以在单进程环境中处理N多个异步事件, 而不需要考虑它们之间是否存在直接或间接的资源共享。

使用特权

评论回复
13
后学| | 2008-1-26 20:45 | 只看该作者

我很笨,理解不了,想不通,

比如你需要从串口和socket收发消息,你可以定义两个进程分别处理串口和socket接口,通过FIFO/消息队列等进程通信机制与主程序交互数据,怎么可能会有资源共享呢?
有OS的情况下,你非要搞个单进程,吃饱了撑得。。。

btw:当你的程序中间出现N个进程有资源共享,就说明你的程序架构有很大的缺陷,

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

172

帖子

0

粉丝