libevent default hehaviour on linux_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > libevent default hehaviour on linux

libevent default hehaviour on linux

 2010/11/19 9:18:40  yaojingguo  http://yaojingguo.javaeye.com  我要评论(0)
  • 摘要:libeventisapopularnotificationlibrary.TheAPIiselegant.Andtheperformanceisgood.Thebadthingisthatthereisnomuchinformationaboutitsimplementation.Afterfailingtofindsuchinformationontheweb,Idecidetotakealookatthesourcecodeoflibevent
  • 标签:

libevent is a popular notification library. The API is elegant. And the performance is good. The bad thing is that there is no much information about its implementation. After failing to find such information on the web, I decide to take a look at the source code of libevent. I have analyzed?its default behavior of the following libevent sample code on linux. I am using ubuntu.

?

?

#include <sys/stat.h>
#include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "event.h"

void fifo_read(int fd, short event, void *arg) {
  char buf[255];
  int len;
  struct event *ev = arg;

  fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
      fd, event, arg);
  len = read(fd, buf, sizeof(buf) - 1);
  if (len == -1) {
    perror("read");
    return;
  } else if (len == 0) {
    fprintf(stderr, "Connectionless closed\n");
    return;
  }
  buf[len] = '\0';
  fprintf(stdout, "Read: %s\n", buf);
  /*event_add(ev, NULL);*/
}

int main(int argc, const char *argv[]) {
  struct event evfifo;
  struct stat st;
  char *fifo = "event.fifo";
  int socket;

  if (lstat(fifo, &st) == 0) {
    if ((st.st_mode & S_IFMT) == S_IFREG) {
      errno = EEXIST;
      perror("lstat");
      exit(1);
    }
  }

  unlink(fifo);
  if (mkfifo(fifo, 0600) == -1) {
    perror("mkfifo");
    exit(1);
  }

  socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
  if (socket == -1) {
    perror("open");
    exit(1);
  }

  fprintf(stderr, "Write data to %s\n", fifo);

  event_init();
  event_set(&evfifo, socket, EV_READ | EV_PERSIST, fifo_read, &evfifo);
  event_add(&evfifo, NULL);
  fprintf(stderr, "begin dispatch\n");
  event_loop(0);
  fprintf(stderr, "end dispatch\n");
  return 0;
}
?

On ubuntu, epoll is used as?the internal event mechanism. event_loop is a wrapper of?event_base_loop. For my case, here is the pseudo code of?event_base_loop.

?

event_base_loop

?? loop

?? ? ?epoll_wait // timeout is set to -1

?

You can see that event_base_loop will bock on epoll_wait indefinitely if no IO happens.

?

?

?

  • 相关文章
发表评论
用户名: 匿名