/**
* 该文件名为epoll.c
*
* 该测试代码是从http://blog.csdn.net/mote_li/archive/2004/12/08/209450.aspx修改来的. 只供学习使用.
*
* 我的测试环境as4u3
* [gan@localhost ~]$ uname -r
* 2.6.9-34.el
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <strings.h>
#include <pthread.h>
#define maxline 1024
#define open_max 100
#define listenq 20
#define inftim 1000
#define local_ip "192.168.1.101" /* 修改为自己本地机器就可以测试了 */
#define serv_port 5555
/**
* thread task link
*/
struct task
{
int fd; /* file descriptor */
struct task *next; /* next task */
};
struct user_data
{
int fd;
unsigned int n_size;
char line[maxline];
};
/* thread exec function */
void *readtask(void *args);
void *writetask(void *args);
/* declare epoll_event */
struct epoll_event ev, events[20];
int epfd;
pthread_mutex_t mutex; /* 线程安全使用 */
pthread_cond_t cond1; /* 线程条件等待使用 */
struct task *readhead = null,
*readtail = null,
*writehead = null;
void setnonblocking(int sock)
{
int opts;
opts = fcntl(sock, f_getfl);
if(opts<0)
{
perror("fcntl(sock,getfl)");
exit(1); /* 其实这样做不怎么好, 最好自己做好出错处理的工作, 不光是进程退出就可以了 */
}
if(fcntl(sock, f_setfl, opts | o_nonblock)<0)
{
perror("fcntl(sock,setfl,opts)");
exit(1);
}
}
int main()
{
int i, maxi, listenfd, connfd, sockfd, nfds;
socklen_t clilen;
pthread_t tid1,tid2;
struct task *new_task=null;
struct user_data *rdata=null;
/* initialize the thread pool */
pthread_mutex_init(&mutex, null);
pthread_cond_init(&cond1, null);
/* 创建线程, 最好做好错误处理工作, 自己也比较懒. 真正作东西千万别这样噢! */
pthread_create(&tid1, null, readtask, null);
pthread_create(&tid2, null, readtask, null);
/* 生成用于处理accept的epoll专用的文件描述符
* 以前从没用过
*
create a new epoll file descriptor by requesting the kernel allocate an event backing store dimensioned[n. 尺寸, 尺度, 维(数), 度(数), 元] for size descriptors. the size is not the maximum size of the backing store but just a hint to the kernel about how to dimension internal structures. the returned file descriptor will be used for all the subsequent calls to the epoll interface. the file descriptor returned by epoll_create must be closed by using posix::close.
when successful, epoll_create returns a positive integer identifying the descriptor. when an error occurs, epoll_create returns -1 and errno is set appropriately.
*
*/
epfd = epoll_create(256);
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
listenfd = socket(af_inet, sock_stream, 0);
//把socket设置为非阻塞方式
setnonblocking(listenfd);
//设置与要处理的事件相关的文件描述符
ev.data.fd = listenfd;
//设置要处理的事件类型
ev.events = epollin | epollet;
/*注册epoll事件
control an epoll descriptor, $epfd, by requesting the operation op be performed on the target file descriptor, fd.
$epfd is an epoll descriptor returned from epoll_create.
$op is one of epoll_ctl_add, epoll_ctl_mod or epoll_ctl_del.
$fd is the file desciptor to be watched.
$eventmask is a bitmask of events defined by epollin, epollout, etc.
when successful, epoll_ctl returns 0. when an error occurs, epoll_ctl returns -1 and errno is set appropriately.
*/
epoll_ctl(epfd, epoll_ctl_add, listenfd, &ev);
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = af_inet;
char *local_addr = local_ip;
inet_aton(local_addr, &(serveraddr.sin_addr));
serveraddr.sin_port = htons(serv_port);
bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
listen(listenfd, listenq);
maxi = 0;
for ( ; ; )
{
/*等待epoll事件的发生
wait for events on the epoll file descriptor $epfd.
$epfd is an epoll descriptor returned from epoll_create.
$maxevents is an integer specifying the maximum number of events to be returned.
$timeout is a timeout, in milliseconds
when successful, epoll_wait returns a reference to an array of events. each event is a two element array, the first element being the file descriptor which triggered the event, and the second is the mask of event types triggered. for example, if epoll_wait returned the following data structure:
*/
nfds = epoll_wait(epfd, events, 20, 500);
//处理所发生的所有事件
for(i = 0; i < nfds; i)
{
if(events[i].data.fd == listenfd)
{
connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clilen);
if(connfd < 0)
{
perror("connfd<0");
exit(1);
}
setnonblocking(connfd);
char *str = inet_ntoa(clientaddr.sin_addr);
printf("connect_from >> %s \n", str);
ev.data.fd = connfd; //设置用于读操作的文件描述符
ev.events = epollin | epollet; //设置用于注测的读操作事件
//注册ev
epoll_ctl(epfd, epoll_ctl_add, connfd, &ev);
}
else if (events[i].events & epollin)
{
printf("reading!\n");
if ((sockfd = events[i].data.fd) < 0)
continue;
new_task = (struct task *)malloc(sizeof(struct task));
new_task->fd = sockfd;
new_task->next = null;
pthread_mutex_lock(&mutex); //添加新的读任务
if(readhead == null)
{
readhead = new_task;
readtail = new_task;
}
else
{
readtail->next = new_task;
readtail = new_task;
}
//唤醒所有等待cond1条件的线程
pthread_cond_broadcast(&cond1);
pthread_mutex_unlock(&mutex);
}
else if (events[i].events & epollout)
{
rdata = (struct user_data *)events[i].data.ptr;
sockfd = rdata->fd;
printf("thread.%u write data fd.%d len.%d data.%s \n"
, (uint32_t)pthread_self(), sockfd, rdata->n_size, rdata->line);
write(sockfd, rdata->line, rdata->n_size);
close(sockfd);
free(rdata);
ev.data.fd = sockfd; //设置用于读操作的文件描述符
ev.events = epollin | epollet; //设置用于注测的读操作事件
//修改sockfd上要处理的事件为epolin
epoll_ctl(epfd, epoll_ctl_mod, sockfd, &ev);
}
}
}
}
/**
* thread exec function
*/
void *readtask(void *args)
{
int fd = -1;
unsigned int n;
//用于把读出来的数据传递出去
struct user_data *data = null;
while (1)
{
pthread_mutex_lock(&mutex);
//等待到任务队列不为空
while(readhead == null)
{
printf("thread.%u waiting, task is null ... \n", (uint32_t)pthread_self());
pthread_cond_wait(&cond1, &mutex);
}
fd = readhead->fd;
//从任务队列取出一个读任务
struct task *tmp = readhead;
readhead = readhead->next;
free(tmp);
pthread_mutex_unlock(&mutex);
data = (struct user_data *)malloc(sizeof(struct user_data));
data->fd = fd;
if ((n = read(fd, data->line, maxline)) < 0)
{
if (errno == econnreset)
{
close(fd);
}
else
printf("readline error \n");
if(data != null)
free(data);
}
else if (n == 0)
{
close(fd);
printf("client close connect!\n");
if(data != null)
free(data);
}
else
{
data->n_size = n;
printf("thread.%u read fd.%d len.%d data.%s \n"
, (uint32_t)pthread_self(), fd, n, data->line);
ev.data.ptr = data; //设置需要传递出去的数据
ev.events = epollout | epollet; //设置用于注测的写操作事件
//修改sockfd上要处理的事件为epollout
epoll_ctl(epfd, epoll_ctl_mod, fd, &ev);
}
}
}
/**
* 一个简单的makefile
*/
all: epoll.c
gcc -wall -g -o epoll epoll.c -lpthread
clean:
rm -f epoll
|