I'm doing MIT labs for RISK-V xv6 and was working with files in the kernel/file.c
. I was wondering about how I should arrange spinlocks to safely concurrently modify file struct (see kernel/file.h
for it). I decided to check how file locking was already implemented in xv6, but then I discovered that there is no locks when modifying shared file struct e.g. filewrite
and fileread
.
// there is no lock when modifying file flags
int
fileread(struct file *f, uint64 addr, int n)
{
int r = 0;
if(f->readable == 0)
return -1;
...
return r;
}
Is there really a race condition because this is toy educational OS or I am overlooking why this is safe to do?
I have checked all references and calls to this function, and still there is no locks. And, of course, this file struct in no way is private to the process modifying it