Wait for new data in a file - winapi

I wanna read file, while another process is writing it.
So my process reads a file till the end, but what is the correct and efficient way to wait for new data ?
Is there some specialized API for this ?
Found similar question but for posix

I/O Completion Ports are designed for this. PostQueuedCompletionStatus can be used to notify worker threads of external events after associating the Io completion port with a specified file handle(CreateIoCompletionPort).
I/O Completion Ports also apply to Socket. There is a sample for socket.

Related

Client to server connection only sending not receiving

This is my case, I have a server listening for connections, and a client that I'm programming now. The client has nothing to receive from the server, yet it has to be sending status updates every 3 minutes.
I have the following at the moment:
WSAStartup(0x101,&ws);
sock = socket(AF_INET,SOCK_STREAM,0);
sa.sin_family = AF_INET;
sa.sin_port = htons(PORT_NET);
sa.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(sock,(SOCKADDR*)&sa,sizeof(sa));
send(sock,(const char*)buffer,128,NULL);
How should my approach be? Can I avoid looping recv?
That's rather dependant on what behaviour you want and your program structure.
By default a socket will block on any read or write operations, which means that if your try and have your server's main thread poll the connection, you're going to end up with it 'freezing' for 3 minutes or until the client closes the connection.
The absolute simplest functional solution (no multithreadding) is to set the socket to non-blocking, and poll in in the main thread. It sounds like you want to avoid doing that though.
The most obvious way around that is to make a dedicated thread for every connection, plus the main listener socket. Your server listens for incoming connections and spawns a thread for each stream socket it creates. Then each connection thread blocks on it's socket until it receives data, and either handles it itself or shunts it onto a shared queue.
That's a bulky and complex solution - multiple threads which need opening and closing, shared resources which need protecting.
Another option is to set the socket to non-blocking (Under win32 use setsockopt so set a timeout, under *nix pass it the O_NONBLOCK flag). That way it will return control if there's no data available to read. However that means you need to poll the socket at reasonable intervals ("reasonable" being entirely up to you, and how quickly you need the server to act on new data.)
Personally, for the lightweight use you're describing I'd use a combination of the above: A single dedicated thread which polls a socket (or an array of nonblocking sockets) every few seconds, sleeping in between, and simply pushed the data onto a queue for the main thread to act upon during it's main loop.
There are a lot of ways to get into a mess with asynchronous programs, so it's probably best to keep it simple and get it working, until you're comfortable with the control flow.

How to interrupt select call on windows?

I know it's probably a basic question, but I'd like to hear the best way to realize it.
So to the problem. I have a driver thread using a call to select, and I have a GUI thread that needs sometimes to interrupt select by writing to some file descriptor within the same process (GUI FD or something). I used pipe in UNIX, but I'm not experienced in sockets for Windows, so I'm not sure what kind of FD I should use. Example is greatly appreciated but not required ).
Thanks.
select() is not the best to implement asynchronous I/O under Windows. unfortunately, the select() call on windows only works with socket handles and not with pipe or fie handles.
you should have a look at overlapped I/O.
by using an event in your overlapped structure, you can have a behaviour close to select(). any event on a socket will trigger an event, which you can wait on by using WaitForMultipleObjects(). now, your GUI thread can signal the I/O thread by setting a specific (separate) event, which you create using the CreateEvent() call.
You could set the timeout shorter on the select and then loop round if it was a timeout. Or you could send a simple packet to the socket being selected causing it to wake up.

Windows: Child Process with Redirected Input and Output

I'm trying to create a Child Process with Redirected Input and Output (as described here - http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx).
For the people that don't want to bother reading the source code on that page, the author is using anonymous pipes to redirect the child's input and output. The parent process writes to the child process's input and reads from the child process's output.
In that code however, the program is closing the pipes after reading and writing (in WriteToPipe and ReadFromPipe), so actually the program just reads a file, dumps it on the child process input stream and then reads the child process response.
Now, what I'm looking for is a code where we will not close the pipes, but we will continuously post requests and read the child process response (in contrast to making just 1 request).
I've tried several modifications to the source code given on the link posted above, but no matter what I try, the program always hangs when calling ReadFile() in the ReadFromPipe() function (it probably waits for the child to quit - but as I said I like to get the child response, and then send other requests to it).
Any ideas on how I can get over this?
Update:
Can anyone at least tell me whether using the .NET Process class with RedirectStandardInput and RedirectStandardOutput is a good option?
Had exactly the same problem, and solved it by using PeekNamedPipe (which according to MSDN is also fine for anonymous read pipes) to check for available data before each call to ReadFile. That removed the blocking issues I was getting, and allowed my GetExitCodeProcess() to see that the process had exited and cleanup the pipes.
Yes - the .Net Process class redirects the standard input / output of the child process with anonymous pipes in a very similar way to the linked sample if you use RedirectStandardInput and RedirectStandardOutput, so this is probably a fairly good option.
As for why ReadFile is hanging - it sounds like this function is waiting for the child process to either send some data back, or to close the pipe. If you want to continuously post requests to the child process then you need to either:
Know exactly when it is appropriate to read so that you are not left waiting / blocked for the child process (so for example you only read immediately after a request has been sent). This strategy is very risky as there is always a chance that the child process doesn't behave as expected and you are left waiting on the child process indefinitely.
Have a dedicated thread for reading - if you have a dedicated thread for reading then it doesn't matter that the thread may wait indefinitely for the child process as your other threads are still able to send requests and operate as normal. This method is more complex than the first option, however when done properly is far more robust. The only other drawback to this approach is that it requires you have an additional read thread for each child process, meaning that it doesn't scale very well if you need to communicate with a large number of child processes.
Use asynchronous IO - It is possible to call the ReadFile function in a way such that it always immediately returns, however notifies you when a read has completed (I'm a little fuzzy on the exact details on how this works as I'm more used to C#). This is know as Asynchronous IO and is the most versatile of these 3 methods as it allows you to communicate with many child processes without needing a dedicated thread for each one. The tradeoff however is that it is also the most complex to do correctly (at least in my opinion).

Is is possible to use IOCP (or other API) in Reactor-style operations?

Is there any scalable Win32 API (like IOCP not like select) that gives you reactor style
operations on sockets? AFAIK IOCP allows you to receive notification on completed operations
like data read or written (proactor) but I'm looking for reactor style of operations: I
need to get notification when the socket is readable or writable (reactor).
Something similar to epoll, kqueue, /dev/poll ?
Is there such API in Win32? If so where can I find a manual on it?
** Clarification:** I need select like api for sockets that is as scalable as IOCP, or I'm looking for a way to use IOCP in reactor like operations.
Even more clarification: IOCP allows you to receive an notification on completion of given operation. For example:
WSARecv(buffer,...); // start reading
WSAWaitForMultipleEvents(...); // wait when read is done
So I get notication after operation is done -- proctor style of operations.
What I need is something like that:
WSARecv( NOTHING ); // start waiting for readability (not actual read)
WSAWaitForMultipleEvents(...); // wait until read would not block
// Now WSARecv would not block
WSARecv(buffer,...); // now actual non-blocking read
How can I do this?
You want to look at the WSAAsyncSelect API. It uses a Windows message queue to signal that a handle is read for read/write/whatever, so it doesn't have the concurrency benefits of IOCP, but it allows you to implement a standard reactor pattern without having a limit to the number of handles (like WSAWaitForMultipleEvents).
I'm confused, isn't Reactor pattern where the thread blocks waiting on multiple event sources? That would be select(), which windows supports. The Proactor pattern is where there is a single callback per call, that you can do via ReadFileEx/WriteFileEx.
Not possible.
I've checked Boost.Asio sources that do have reactor style operations and use IOCP. For all reactor style operations separate thread with select is used instead of IOCP.
Have you tried passing zero nNumberOfBytesToRead to, for example ReadFile(socket_fd, ..)?
Maybe it will help to get the "read ready" event.

How does a non-forking web server work?

Non-forking (aka single-threaded or select()-based) webservers like lighttpd or nginx are
gaining in popularity more and more.
While there is a multitude of documents explaining forking servers (at
various levels of detail), documentation for non-forking servers is sparse.
I am looking for a bird eyes view of how a non-forking web server works.
(Pseudo-)code or a state machine diagram, stripped down to the bare
minimum, would be great.
I am aware of the following resources and found them helpful.
The
World of SELECT()
thttpd
source code
Lighttpd
internal states
However, I am interested in the principles, not implementation details.
Specifically:
Why is this type of server sometimes called non-blocking, when select() essentially blocks?
Processing of a request can take some time. What happens with new requests during this time when there is no specific listener thread or process? Is the request processing somehow interrupted or time sliced?
Edit:
As I understand it, while a request is processed (e.g file read or CGI script run) the
server cannot accept new connections. Wouldn't this mean that such a server could miss a lot
of new connections if a CGI script runs for, let's say, 2 seconds or so?
Basic pseudocode:
setup
while true
select/poll/kqueue
with fd needing action do
read/write fd
if fd was read and well formed request in buffer
service request
other stuff
Though select() & friends block, socket I/O is not blocking. You're only blocked until you have something fun to do.
Processing individual requests normally involved reading a file descriptor from a file (static resource) or process (dynamic resource) and then writing to the socket. This can be done handily without keeping much state.
So service request above typically means opening a file, adding it to the list for select, and noting that stuff read from there goes out to a certain socket. Substitute FastCGI for file when appropriate.
EDIT:
Not sure about the others, but nginx has 2 processes: a master and a worker. The master does the listening and then feeds the accepted connection to the worker for processing.
select() PLUS nonblocking I/O essentially allows you to manage/respond to multiple connections as they come in a single thread (multiplexing), versus having multiple threads/processes handle one socket each. The goal is to minimize the ratio of server footprint to number of connections.
It is efficient because this single thread takes advantage of the high level of active socket connections required to reach saturation (since we can do nonblocking I/O to multiple file descriptors).
The rationale is that it takes very little time to acknowledge bytes are available, interpret them, then decide on the appropriate bytes to put on the output stream. The actual I/O work is handled without blocking this server thread.
This type of server is always waiting for a connection, by blocking on select(). Once it gets one, it handles the connection, then revisits the select() in an infinite loop. In the simplest case, this server thread does NOT block any other time besides when it is setting up the I/O.
If there is a second connection that comes in, it will be handled the next time the server gets to select(). At this point, the first connection could still be receiving, and we can start sending to the second connection, from the very same server thread. This is the goal.
Search for "multiplexing network sockets" for additional resources.
Or try Unix Network Programming by Stevens, Fenner, Rudoff

Resources