日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > linux基礎復習(6)文件I/O操作

          linux基礎復習(6)文件I/O操作

          作者: 時間:2016-10-08 來源:網(wǎng)絡 收藏

          struct timeval

          {

          long tv_sec; /* seconds */

          long tv_usec; /* and microseconds */

          };

          select函數(shù)根據(jù)希望進行的文件操作對文件描述符進行分類處理,這里,對文件描述符的處理主要設計4個宏函數(shù):

          FD_ZERO(fd_set *set) 清除一個文件描述符集;

          FD_SET(int fd, fd_set *set) 將一個文件描述符加入文件描述符集中;

          FD_CLR(int fd, fd_set *set) 將一個文件描述符從文件描述符集中清除;

          FD_ISSET(int fd, fd_set *set) 測試該集中的一個給定位是否有變化;

          在使用select函數(shù)之前,首先使用FD_ZERO和FD_SET來初始化文件描述符集,并使用select函數(shù)時,可循環(huán)使用FD_ISSET測試描述符集, 在執(zhí)行完成對相關的文件描述符后, 使用FD_CLR來清除描述符集。

          實例

          /*select.c*/

          #i nclude fcntl.h>

          #i nclude stdio.h>

          #i nclude unistd.h>

          #i nclude stdlib.h>

          #i nclude sys/time.h>

          int main(void)

          {

          int fds[2];

          char buf[7];

          int i,rc,maxfd;

          fd_set inset1,inset2;

          struct timeval tv;

          if((fds[0] = open (hello1, O_RDWR|O_CREAT,0666))0)

          perror(open hello1);

          if((fds[1] = open (hello2, O_RDWR|O_CREAT,0666))0)

          perror(open hello2);

          if((rc = write(fds[0],Hello!n,7)))

          printf(rc=%dn,rc);

          lseek(fds[0],0,SEEK_SET);

          maxfd = fds[0]>fds[1] ? fds[0] : fds[1];

          //初始化讀集合 inset1,并在讀集合中加入相應的描述集

          FD_ZERO(inset1);

          FD_SET(fds[0],inset1);

          //初始化寫集合 inset2,并在寫集合中加入相應的描述集

          FD_ZERO(inset2);

          FD_SET(fds[1],inset2);

          tv.tv_sec=2;

          tv.tv_usec=0;

          // 循環(huán)測試該文件描述符是否準備就緒,并調(diào)用 select 函數(shù)對相關文件描述符做相應操作

          while(FD_ISSET(fds[0],inset1)||FD_ISSET(fds[1],inset2))

          {

          if(select(maxfd+1,inset1,inset2,NULL,tv)0)

          perror(select);

          else{

          if(FD_ISSET(fds[0],inset1))

          {

          rc = read(fds[0],buf,7);

          if(rc>0)

          {

          buf[rc]='