Skip to content

Commit

Permalink
12.1 select调用
Browse files Browse the repository at this point in the history
  • Loading branch information
NaZixia committed Feb 29, 2024
1 parent fce068c commit 96a5e77
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Binary file added 12IO复用/1select调用事例/1select
Binary file not shown.
42 changes: 42 additions & 0 deletions 12IO复用/1select调用事例/1select.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>

#define BUF_SIZE 30

int main(int argc,char *argv[])
{
char buf[BUF_SIZE];
fd_set reads,temps;
int result,str_len;
struct timeval timeout;
FD_ZERO(&reads);
FD_SET(0,&reads);

while(1){
temps=reads;
timeout.tv_sec=5;
timeout.tv_usec=0;
result=select(1,&temps,0,0,&timeout);
if(result==-1){
fputs("error select\n",stdout);
break;
}
else if(result==0){
fputs("time out\n",stdout);
break;
}
else{
if(FD_ISSET(0,&temps)){
str_len=read(0,buf,BUF_SIZE);
buf[str_len]=0;
printf("message: %s",buf);
}
}
}

return 0;
}


0 comments on commit 96a5e77

Please sign in to comment.