Quiz 2
Problem 1 (2pt)
Question
The command ln path/to/target.txt link1.txt
creates a hardlink link1.txt
to the file path/to/target.txt
. Similarly, ln -s path/to/target.txt link2.txt
creates a symbolic link link2.txt
. If we open link1.txt
and link2.txt
, how many inode accesses are required respectively? (hardlink 1pt, symlink 1pt)
Answer
First solution:
By calling open("link1.txt")
, you have to find its parent directory to look up the file’s inode number. That’s the first inode access. Then, we access the inode to link.txt
itself. It requires 2 in total.
The call open("link2.txt")
accesses the parent directory and the inode of symbolic link. To follow the symlink, we have to access two additional directories path
and to
, and finally the target.txt
inode. It sums to 5 in total.
Second solution:
Many students omit the parent directory access, whose answer should be 1 for hard link and 4 for symlink. Those answer 1, 4 or 2, 5 will get full points. Those answer 1, 5 or 2, 4 will get partial points.
Some answers count the access to link2.txt
twice. One is link2.txt
entry lookup and the other is path
entry lookup. In this case, you’ll get full points if the answer is 2, 6.
Problem 2 (3pt)
Question
Suppose two threads do lseek()
and write()
on the same file descriptor. Describe an example that offset racing occurs (1.5pt), and give a solution to this case (1.5pt).
Answer
Suppse two thread lseek at distinct absolute offsets, and respectively write three bytes. First thread does lseek(fd, 10, SEEK_SET)
, while the second does lseek(fd, 20, SEEK_SET)
. We expect first thread writes at 10th byte.
If the execution order is lseek (1st thread) -> lseek (2nd thread) -> write (2nd thread) -> write (1st thread)
. Because the offset is moved to 20 plus 3 bytes by second thread. The first thread won’t write at 10th byte.
To resolve this, We can replace lseek(), write()
pairs with pwrite()
, which is an atomic operation.
Some anwsers that two threads lseek to the end of file. The answer is incorrect because even if their executions are interleaved, the result is not affected. That is, overwriting does not occur.