这里,我们首先要实现一种数据结构,将相应的任务,线程的fd,还有队列实现。
声明代码如下:
1 #ifndef _HEAD_H 2 #define _HEAD_H 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <unistd.h> 6 #include <string.h> 7 #include <sys/stat.h> 8 #include <sys/select.h> 9 #include <sys/types.h> 10 #include <fcntl.h> 11 #include <pthread.h> 12 #include <sys/time.h> 13 #include <signal.h> 14 #define MSG_LEN 1024 15 #define TASK_CNT 1024 16 extern pthread_mutex_t lock; 17 extern pthread_cond_t cond1; 18 extern pthread_cond_t cond2; 19 typedef struct tag_fd 20 { 21 int s_rfd; 22 int s_wfd; 23 struct tag_fd *next; 24 }FD,*pFD; 25 typedef struct tag_task 26 { 27 char s_msg[MSG_LEN]; 28 int s_fd; 29 }TASK,*pTASK; 30 typedef struct tag_que 31 { 32 TASK arr[TASK_CNT+1]; 33 int front; 34 int tail; 35 }QUEUE,*pQUEUE; 36 void fd_insert(pFD *phead,int rfd,int wfd); 37 void fd_init(pFD *phead); 38 int fd_find(pFD phead,int rfd); 39 void fd_del(pFD *phead,int rfd); 40 void add_task(pQUEUE pq,pTASK pt); 41 void get_task(pQUEUE pq,pTASK pt); 42 void excute_task(pTASK pt); 43 #endif |