select 함수 관련 질문입니다..

jungjury의 이미지

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
 
#include <stdio.h>
 
int main()
{
        fd_set read_fds;
        fd_set write_fds;
        int ret;
 
        FD_ZERO( &read_fds );
        FD_SET( 0, &read_fds );
        FD_SET( 1, &read_fds );
 
        FD_ZERO( &write_fds );
        FD_SET( 0, &write_fds );
        FD_SET( 1, &write_fds );
 
        ret = select ( 2, NULL, &write_fds, NULL, NULL );
 
 
        printf( "ret =  %d \n", ret );
 
        if ( ret > 0 )
        {
                if ( FD_ISSET( 0, &read_fds ) )
                        printf( "read from 0\n" );
                if ( FD_ISSET( 1, &read_fds ) )
                        printf( "read from 1\n" );
                if ( FD_ISSET( 0, &write_fds ) )
                        printf( "write to 0\n" );
                if ( FD_ISSET( 1, &write_fds ) )
                        printf( "write to 1\n" );
 
        }
}

실행시키면
ret = 2
write to 0
write to 1

아래로 바꾸어 실행하면
ret = select ( 2, &read_fds, NULL, NULL, NULL );

hi <--hi입력
ret = 4
read from 0
read from 1
write to 0
write to 1

STDIN과 STDOUT이 버퍼를 같이 써서 이런 상황이 발생하는 건가요?

park7275의 이미지

일단은 select 에 parameter 로 넣지 않은 값은.. 무의미할듯한데요
해당값은 call by reference로 처리하는 값입니다.

그니깐..

ret = select ( 2, NULL, &write_fds, NULL, NULL );
할때 read_fds 값의 FD_ISSET은 무의미 하지 않나요?

ret = select ( 2, &read_fds, NULL, NULL, NULL );
할때 write_fds도 마찬가지고요.

select 에 parameter로 넣지 않은것을 FD_ISSET 을 검사하는 것은 무의미 합니다.
어떤 반응이 나올지는 kernel에 따라 다를수 있지 않을까 싶네요.
원래 FD_SET으로 한값이 나올것 같은데..

근데..나온 결과로는...모르겠네요.

밑에 결과가 4라는 것은 read_fds와 write_fds가 2개씩 등록되어있으니
select에서 parameter 로 2개 다 사용했을때 나오지 않을까 싶은데...
넣지도 않은 값이 나왔다는게..이상..--;

FD_SET이 다른건가?? 좀 애매하네요..ㅜㅜ

[KILL] 죽을각오로.........

[KILL] 죽을각오로.........