select 함수

    반응형

    write set 인 wfds를 출력으로하는 select 문 예제

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/time.h>
    #include <sys/select.h>
    
    #define BUF_SIZE 30
    
    int main(int argc, char *argv[])
    {
    	fd_set reads, temps, wset; //fd선언
    	int result, str_len;
    	char buf[BUF_SIZE];
    	struct timeval timeout;
    
    	FD_ZERO(&wset); // fd배열을모두 0값으로 초기화
    	//FD_SET(0, &reads); // 0 is standard input(console)
      FD_SET(1, &wset);
    
      
    	/*
    	timeout.tv_sec=5;
    	timeout.tv_usec=5000;
    	*/
    
    	while(1)
    	{
    		temps=wset;
    		timeout.tv_sec=5;
    		timeout.tv_usec=0;
        //printf("select 직전 \n");
    		result=select(9, 0, &wset, 0, &timeout);
    		if(result==-1)
    		{
    			puts("select() error!");
    			break;
    		}
    		else if(result==0)
    		{
    			puts("Time-out!");
    		}
    		else 
    		{
    			if(FD_ISSET(1, &wset)) 
    			{
    				//str_len=read(0, buf, BUF_SIZE);
    				//buf[str_len]=0;
    				//printf("message from console:");
            sleep(5);
    			}
    		}
        printf("끝\n");
    	}
      //printf("끝\n");
    	return 0;
    }
    반응형

    댓글