Freitag, 29. Februar 2008

Better than usleep ...

What do you do when a socket read or write returns EAGAIN/EWOULDBLOCK?

Tests showed that an immediate attempt to read again also returns the same error code.
So, what I did was to implement a usleep() for 10 milliseconds (shorter times are not possible on Solaris systems) and then try again. This works.
But nowadays 10 milliseconds is far too much: A process that can handle 5000 msgs/s would get through 50 messages in this time...

Now I learned about the function yield() (sched_yield() on Linux). This function tells the scheduler to look for another process to execute (in simple words).
I replaced the usleep() call with a call to yield(), of course hoping that the calling process would be activated by the scheduler earlier than when usleep() was called.
And test showed that it works: A process that handled 2'500-3'000 msgs/s before managed more than 7'500 messages afterwards!

So, if you have a process that is blocked somehow and should wait for a change of state, specially when this change of state is caused by another process, call yield()!

Keine Kommentare: