Sunday, November 2, 2014

Combine READ/WRITE with SEEK

pread() and pwrite() is like the normal read/write call with addition parameter on position.  The p calls ignore the current file position and perform IO at the position passed.  The call also does not update the current file position.  Thus, mixing read/write with pread/pwrite may cause data corruption.

The advantage p call is that it elimnates the race condition in a mutithread environment that shared the file table.  lseek and the following read/write call is not atomic.  Therefore, after a lseek and before the next read/write call made by one thread, the current file position can be altered by another thread.  Using pread/pwrite avoids this racy situation.

No comments: