【发布时间】:2015-05-21 05:53:58
【问题描述】:
测试环境:Ubuntu 12.04 说明:我做了以下
# `sudo truncate -s 0 /var/log/syslog`
# logger "helloworld".
# `cat /var/log/syslog/`
May 21 11:02:10 setup-VirtualBox setup: `helloworld`
May 21 11:05:01 setup-VirtualBox CRON[3056]: `(root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)`
但是如果我通过下面的程序读取数据,我会重复得到这两行。
有什么问题?
data:May 21 11:02:10 setup-VirtualBox setup: `helloworld`
May 21 11:05:01 setup-VirtualBox CRON[3056]: `(root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)`
data:May 21 11:02:10 setup-VirtualBox setup: `helloworld`
May 21 11:05:01 setup-VirtualBox CRON[3056]: `(root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)`
data:May 21 11:02:10 setup-VirtualBox setup: `helloworld`
May 21 11:05:01 setup-VirtualBox CRON[3056]: `(root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1)`
代码
#include <stdio.h>
#include <sys/poll.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("/var/log/syslog", O_RDONLY);
if (fd == -1)
{
perror("open");
return 0;
}
int ret = 0;
struct pollfd p;
p.fd = fd;
p.events = POLLIN;
char dataBuff[1000];
memset(dataBuff, 0, 1000);
int i = 0;
int numEvents = 0;
/* wait for events */
while (1)
{
numEvents = poll(&p, 1, -1);
if (numEvents > 0)
{
if (0 != (POLLIN & p.revents))
{
ret = read(p.fd, dataBuff,1000);
if (ret > 0)
{
fprintf(stderr, "data:%s\n",dataBuff);
}
}
}
}
return 0;
}
【问题讨论】:
标签: linux system-calls epoll