linux 下阻塞睡眠等待selec函数简单实现

/* int select(int nfds, fd_set *readfds, fd_set *writefds,
   fd_set *exceptfds, struct timeval *timeout); 后两个参数可用NULL代替*/

/*
   void FD_CLR(int fd, fd_set *set);  //清除某一文件描述符
   int FD_ISSET(int fd, fd_set *set); //测试文件描述符是否准备就绪(可写还是可读)
   void FD_SET(int fd, fd_set *set);  //将一个文件描述符加入文件描述富集
   void FD_ZERO(fd_set *set);         //清空文件描述符集
   */


001 #include <sys/types.h>
002 #include <sys/stat.h>
003 #include <fcntl.h>
004  
005 #include <stdio.h>
006 /* According to POSIX.1-2001 */
007 #include <sys/select.h>
008  
009 /* According to earlier standards */
010 #include <sys/time.h>
011 #include <sys/types.h>
012 #include <unistd.h>
013  
014 /* int select(int nfds, fd_set *readfds, fd_set *writefds,
015    fd_set *exceptfds, struct timeval *timeout); 后两个参数可用NULL代替*/
016  
017 /*
018    void FD_CLR(int fd, fd_set *set);  //清除某一文件描述符
019    int FD_ISSET(int fd, fd_set *set); //测试文件描述符是否准备就绪(可写还是可读)
020    void FD_SET(int fd, fd_set *set);  //将一个文件描述符加入文件描述富集
021    void FD_ZERO(fd_set *set);         //清空文件描述符集
022    */
023  
024 #define OPEN_FLAG  O_RDWR|O_CREAT
025 #define OPEN_MODE  00777
026 #define CONTENT    "howaylee"
027  
028 int main(int argc, char* argv[])
029 {
030     int fd1 = -1;
031     int fd2 = -1;
032     int ret = -1;
033  
034     fd_set r_fds ;
035     fd_set w_fds ;
036     int max_fds = 0;
037  
038     char buf[12] = {0};
039  
040     //open file1
041     fd1 = open(argv[1], OPEN_FLAG, OPEN_MODE);
042     if(-1 == fd1 )
043     {
044         perror("open fd1 failed: ");
045         return -1;
046     }
047  
048     //write
049     ret = write(fd1, CONTENT, sizeof(CONTENT));
050     if(-1 == ret )
051     {
052         perror("write fd1 failed: ");
053         return -1;
054     }
055  
056     //lseek head
057     ret = lseek(fd1, 0, SEEK_SET);
058     if(-1 == ret )
059     {
060         perror("lseek fd1 failed: ");
061         return -1;
062     }
063     //open file2
064     fd2 = open(argv[2], OPEN_FLAG, OPEN_MODE);
065     if(-1 == fd2 )
066     {
067         perror("open fd2 failed: ");
068         return -1;
069     }
070  
071     /*判断文件是否就绪*/
072     //获取max_fds,为最大fd值加一
073     max_fds = (fd1 > fd2)?(fd1+1):(fd2+1);
074     //清空读写文件集
075     FD_ZERO(&r_fds);
076     FD_ZERO(&w_fds);
077     //设置读写文件集
078     FD_SET(fd1, &r_fds);
079     FD_SET(fd2, &w_fds);
080     while(1)
081     {
082         //lseek head
083         ret = lseek(fd1, 0, SEEK_SET);
084         if(-1 == ret )
085         {
086             perror("lseek fd1 failed: ");
087             return -1;
088         }
089         //文件阻塞,监测谁有数据
090         ret = select(max_fds, &r_fds, &w_fds, NULL, NULL);
091         if(0 > ret )
092         {
093             perror("select failed: ");
094             return -1;
095         }
096         //文件1是否就绪,可读
097         if(FD_ISSET(fd1, &r_fds))
098         {
099             //read file1
100             ret = read(fd1, buf, sizeof(buf));
101             if(-1 == ret )
102             {
103                 perror("read to buf failed: ");
104                 return -1;
105             }
106         }
107         //文件2是否就绪,可写
108         if(FD_ISSET(fd2, &w_fds))
109         {
110             //write file2
111             ret = write(fd2, buf, sizeof(buf));
112             if(-1 == ret )
113             {
114                 perror("write to fd2 failed: ");
115                 return -1;
116             }
117         }
118     }
119     //close file1 file2
120     close(fd1);
121     close(fd2);
122  
123     return 0;
124 }

你可能感兴趣的:(linux 下阻塞睡眠等待selec函数简单实现)