; char line[MAXLINE];};
// 线程的任务函数
void * readtask(void *args);
void * writetask(void *args);
// 声明 epoll_event 结构体的变量 ,ev 用于注册事件 , 数组用于回传要处理的事件
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);
}
opts = opts | O_NONBLOCK;
if (fcntl(sock, F_SETFL, opts) < 0)
{
perror("fcntl(sock,SETFL,opts)");
exit(1);
}
}
int main()
{
int i, maxi, listenfd, connfd, sockfd, nfds;
pthread_t tid1, tid2;
struct task *new_task = NULL;
struct user_data *rdata = NULL;
socklen_t clilen;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond1, NULL);
// 初始化用于读线程池的线程
pthread_create(&tid1, NULL, readtask, NULL);
pthread_create(&tid2, NULL, readtask, NULL);
// 生成用于处理 accept 的 epoll 专用的文件描述符
epfd = epoll_create(256);
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
// 把 socket 设置为非阻塞方式
setnonblocking(listenfd