Thread-Safe
Concept
所有 thread 都共用 file descriptor table,同時 read/write 可能會 race condition
Definition
A function is thread-safe if it can be safely called by multiple threads at the same time
Solution
使用 atomic I/O
Atomic I/O
pread
Function Prototype
ssize_t pread(int filedes, void *buf, size_t nbytes, off_t offset);Function
- 先
lseek到檔案頭然後read - 不會動到 open file table 的 offset
- atomic operation
pwrite
Function Prototype
ssize_t pwrite(int filedes, const void *buf, size_t nbytes, off_t offset);Function
- 先
lseek到檔案頭然後write - 不會動到 open file table 的 offset
- atomic operation