国产肉体XXXX裸体137大胆,国产成人久久精品流白浆,国产乱子伦视频在线观看,无码中文字幕免费一区二区三区 国产成人手机在线-午夜国产精品无套-swag国产精品-国产毛片久久国产

新聞中心

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

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

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

struct timeval

{

long tv_sec; /* seconds */

long tv_usec; /* and microseconds */

};

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

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

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

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

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

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

實(shí)例

/*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);

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

FD_ZERO(inset2);

FD_SET(fds[1],inset2);

tv.tv_sec=2;

tv.tv_usec=0;

// 循環(huán)測(cè)試該文件描述符是否準(zhǔn)備就緒,并調(diào)用 select 函數(shù)對(duì)相關(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]='