concurrent access to file linux
c, concurrency, filesystems, linux, multithreading
Solution
Linux doesn't use any locking mechanism to protect multithread writing to a file.
You have to use your own mutex to protect your file.
Problem
I was looking at how a syscall read/write was done in linux, and i found this : ``` .... loff_t pos = file_pos_read(f.file); ret = vfs_read(f.file, buf, count, &pos); file_pos_write(f.file, pos); fdput(f); ...` ``` My questions are : Where did the locking go? I would have imaginated something like : ``` .... lock(f.file); // <-- lock file struct loff_t pos = file_pos_read(f.file); ret = vfs_read(f.file, buf, count, &pos); file_pos_write(f.file, pos); fdput(f); unlock(f.file); // <-- unlock file struct ... ``` If multiple threads try to read/write at the same time, they could read/write at the same offset ? If my understanding is correct, linux doesn't use any locking mechanism to protect the offset, is this POSIX compliant ? I did look at the POSIX specification, and found nothing about this case.