5

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 filehandle is read side of pipe which is created via WinApi CreatePipe method.

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
Mihran Hovsepyan
  • 10,810
  • 14
  • 61
  • 111

3 Answers3

2

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.

Will Dean
  • 39,055
  • 11
  • 90
  • 118
1

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.

Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122
0

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.

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
  • 2
    1. The link to msdn is useful, but doesn't solve all my problems. There explaining way how read or write to HANDLE of file in non blocking mode, but not how make HANDLE of file non blocking. If the HANDLE is non blocking I can make from it FILE* and then read via fgets, fscanf... and this operations wouldn't block program, but if only read and write are non blocking then it is less flexible, and I have to use WinApi read write methods for non blocking operations. 2.Cygwin and MinGW doesn't implement full functionality of posix. And my question is from that part which is not implemented. – Mihran Hovsepyan Mar 16 '11 at 11:28
  • MinGW does not attempt to provide any decent POSIX compatibility. – rubenvb Feb 10 '13 at 11:26