<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > linux基礎(chǔ)復(fù)習(xí)(6)文件I/O操作

          linux基礎(chǔ)復(fù)習(xí)(6)文件I/O操作

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

          struct timeval

          {

          long tv_sec; /* seconds */

          long tv_usec; /* and microseconds */

          };

          select函數(shù)根據(jù)希望進行的文件操作對文件描述符進行分類處理,這里,對文件描述符的處理主要設(shè)計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í)行完成對相關(guān)的文件描述符后, 使用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,并在讀集合中加入相應(yīng)的描述集

          FD_ZERO(inset1);

          FD_SET(fds[0],inset1);

          //初始化寫集合 inset2,并在寫集合中加入相應(yīng)的描述集

          FD_ZERO(inset2);

          FD_SET(fds[1],inset2);

          tv.tv_sec=2;

          tv.tv_usec=0;

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

          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]='看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();