开发者

Is there way to make file descriptor non blocking in windows?

I want to port my code from linux to windows. It is something like this:

void SetNonBlocking( int filehandle )
{
    int fhFlags;

    fhFlags = fcntl(filehandle,F_GETFL);
    if (fhFlags < 0)
    {
        perror("fcntl(F_GETFL)");
        exit(1);
    }

    fhFlags |= O_NONBLOCK;
    if (fcntl(filehandle,F_SETFL,fhFlags) < 0)
    {   
        perror("fcntl(F_SETFL)");
        exit(1);
    } 

    return;
}

Now I want have same in windows. Any ideas? Actualy my fileh开发者_如何学Pythonandle is read side of pipe which is created via WinApi CreatePipe method.


The term for non-blocking / asynchronous I/O in Windows is 'overlapped' - that's what you should be looking at.

Basically, you identify a file handle as using overlapped i/o when you open it, and then pass an OVERLAPPED structure into all the read and write calls. The OVERLAPPED structure contains an event handle which can be signalled when the I/O completes.


Like this:

ulong arg = 1;
ioctlsocket(sock, FIONBIO, &arg);

FIONBIO sets the socket in non-blocking mode. Though you should also use OVERLAPPED io as Will suggests. But overlapping and non-blocking is not the same thing.


The Windows API function CreateNamedPipe has an option to make the handle non-blocking. (See MSDN). Also see the MSDN article on Synchronous and Overlapped I/O. BTW, you can directly compile POSIX compliant code on Windows using MinGW or Cygwin and thus avoid the headache of porting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜