Originally Posted by
hamster_nz
Write does not have to write all the data requested, it can write just the first byte.
This is doubly important with pipes, where the pipe can only limited amount of data. It is possible that you might be dealing with this in higher-up layers... but you are most likely expecting the writes to be an atomic "all or nothing" deal.
read() and write() can fail with retriable/recoverable errors - e.g. SIGINT:
The way the code at the start of the thread was written exposed you to these behaviors. This is very different to how fread()/fwrite() handle things.
Ah so you meant that I wasn't checking the amount written/read, then yeah I get what you mean now, how 'bout this then?
Code:
memset( rd2, 0, sizeof(void*) );
for ( bytes = 0, rdbytes = 0; rdbytes < sizeof(void*); )
{
bytes = rdpipe( pipes[PIPE_RD], rd2, sizeof(void*) - rdbytes );
if ( bytes >= 0 )
{
for ( byte = 0; byte < bytes; ++byte, ++rdbytes )
ptr2ptr[rdbytes] = rd2[byte];
}
else break;
}
if ( bytes < 0 )
continue;
I temporarily re-added the size parameter, depending on whether this method is a good idea I'll just move it inside the functions and remove the size parameter again