#include <poll.h>
extern int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout);
/* Data structure describing a polling request. */
struct pollfd
{
int fd; /* poll 的文件描述符. */
short int events; /* fd 上感兴趣的事件(等待的事件). */
short int revents; /* fd 上实际发生的事件. */
};
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <poll.h>
int main (int argc, char **argv)
{
int sfd1, sfd2, sfd3;
struct pollfd Events [3];
int retval;
char buff [256];
int readcnt;
sfd1 = open ("/dev/ttyS1", O_RDWR | O_NOCTTY);
sfd2 = open ("/dev/ttyS2", O_RDWR | O_NOCTTY);
sfd3 = open ("/dev/ttyS3", O_RDWR | O_NOCTTY);
...
/*各个串行环境设定程序*/
...
memset (Event, 0, sizeof(Events));
Event[0].fd = sfd1;
Event[0].events = POLLIN | POLLERR; /*关心读取和出错事件*/
Event[1].fd = sfd2;
Event[1].events = POLLIN | POLLERR; /*关心读取和出错事件*/
Event[2].fd = sfd3;
Event[2].events = POLLIN | POLLERR; /*关心读取和出错事件*/
while (1) {
/*等待事件*/
retval = poll ((struct pollfd *)&Events, 3, 5000);
if (retval < 0) {
perror ("poll");
exit (EXIT_FAILURE);
}
if (retval == 0) {
prinntf ("no data in 5 seconds.\\n");
continue;
}
for (i = 0; i < 3; i++) {
/*检查错误*/
if (Events[i].revents & POLLERR) {
printf ("device error!\\n");
exit (EXIT_FAILURE);
}
/*检查是否存在传递的数据(可读)*/
if (Events[i].revents & POLLIN) {
readcnt = read (Events[i].fd, buff, 256);
write (Events[i].fd, buff, readcnt);
}
}
}
close (sfd1);
close (sfd2);
colse (sfd3);
}
[beyes@localhost poll]$ cat errors2 |grep "errno = 11" | wc -l
2191
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <syslog.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define BUFFER_SIZE 1000000
#define LINE_LEN 256
typedef enum {B_FALSE, B_TRUE} boolean_t; /*定义布尔变量*/
static char buf [BUFFER_SIZE];
int daemon_proc = 0;
int set_fsflag (int fd, int new_flags)
{
int flags;
if ((flags = fcntl (fd, F_GETFL)) == -1) /*得到 fd 上的状态*/
return (-1);
flags |= new_flags; /*对 fd 添加新的属性*/
if ((flags = fcntl (fd, F_SETFL, flags)) == -1) /*设置新属性*/
return (-1);
return (0);
}
int clear_fsflag (int fd, int new_flags)
{
int flags;
if ((flags = fcntl (fd, F_SETFL, flags)) == -1)
return (-1);
return (0);
}
static void err_common (boolean_t flag, int level, const char *text, va_list args)
{
int old_errno;
int n;
char buf [LINE_LEN];
old_errno = errno; /*获得错误号*/
#ifdef NEED_SNPRINTF
n = vsprintf (buf, text, args); /*n 为写入到 buf 中的字节数,不包括'\\0'*/
#else
n = vsnprintf (buf, sizeof (buf), text, args);
#endif
if (flag)
snprintf (buf + n, sizeof (buf) - n, ": %s", strerror (old_errno)); /*附加出错具体提示*/
strcat (buf, "\\n");
if (daemon_proc)
syslog (level, buf); /*产生日志消息*/
else {
fflush (stdout);
fprintf (stderr, "%s", buf);
fflush (stderr);
}
}
void log_msg (const char *text, ...)
{
va_list arg;
va_start (arg, text);
err_common (B_FALSE, LOG_INFO, text, arg);
va_end (arg);
}
void err_msg (const char *text, ...)
{
va_list arg;
va_start (arg, text);
err_common (B_TRUE, LOG_ERR, text, arg);
va_end (arg);
exit (1);
}
void err_set (const char *text, ...)
{
va_list arg;
va_start (arg, text);
err_common (B_TRUE, LOG_INFO, text, arg);
va_end (arg);
}
int main (void)
{
ssize_t n;
ssize_t res;
char *ptr;
int errs;
struct pollfd fds;
errs = 0;
n = read (STDIN_FILENO, buf, BUFFER_SIZE);
log_msg ("Read %d bytes", n);
set_fsflag (STDOUT_FILENO, O_NONBLOCK);
fds.fd = STDOUT_FILENO;
fds.events = POLLOUT;
fds.revents = 0;
ptr = buf;
while (n > 0) {
if (poll (&fds, 1, -1) == -1)
err_msg ("Can't poll");
while ((n > 0) && ((res = write (STDOUT_FILENO, ptr, n)) > 0)) {
if (errs > 0) {
err_set ("write failed %d times\\n", errs);
errs = 0;
}
log_msg ("Wrote %d bytes", res);
ptr += res;
n -= res;
}
}
clear_fsflag (STDOUT_FILENO, O_NONBLOCK);
return (0);
}
[beyes@localhost poll]$ ./poll.exe < zypper.log 2> errors
[beyes@localhost poll]$ cat errors
Read 1000000 bytes
Wrote 4059 bytes
Wrote 4061 bytes
Wrote 4061 bytes
Wrote 4062 bytes
Wrote 4064 bytes
Wrote 4071 bytes
... ...
[beyes@localhost poll]$ wc -l errors
247 errors
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |