2013-04-25 41 views
1

我正在運行cygwin並使用pselect監視子進程的套接字和filedescriptors。pselect cygwin上的FD_ISSET問題

根據我在這裏找到的一些示例http://www.linuxprogrammingblog.com/code-examples/using-pselect-to-avoid-a-signal-race和手冊頁pselect應該返回在掩碼字段(http://linux.die.net/man/2/pselect)中設置的文件描述符的數量。

現在,當我連接到我的服務器pselect返回,這很好。但是,當我使用FD_ISSET測試的filedescriptors他們總是返回true:

FD_ZERO(&readers); 
FD_ZERO(&writers); 
FD_ZERO(&exceptions); 

FD_SET(fileno(stdin), &readers); 
FD_SET(socket, &readers); 
pret = pselect(FD_SETSIZE, &readers, &writers, &exceptions, NULL, &mSignalMask); 

,&讀者,作家&,&例外,NULL,& mSignalMask);

if(pret <= 0) 
{ 
    // ignore for now 
    continue; 
} 

if(FD_ISSET(fileno(stdin), &readers)) 
{ 
    string s; 
    cin >> s; 
    cout << "stdin: " << s << endl; // blocks because the code always gets here even when 
       // pselect returns because of a childsignal without any data. 
    continue; 
} 

if(FD_ISSET(socket, &readers)) 
{ 
    accept(); // blocks because the code always gets here even when 
       // pselect returns because of a childsignal without any data. 
    cout << "task created connection from " <<task->getClientHost() << endl; 
    continue; 
} 

回答

1

自己發現了問題。只有當pselect的結果大於0時才能使用FD_ISSET,否則FD_ISSET的返回值與調用前相同。所以最好把它當undefined返回< = 0;