Exam 2, Question 2 Solution

Written by Sanghwan Lee

Question 2. Files and Abstraction. (20 pts.)

A. When we use the POSIX open function (system call) to create a new file, we can specify the access permission mode for the file. When we use the ANSI C fopen function to create a file, however, there is no way to specify this. Why?

POSIX is a specific operating system interface which has its own access permission mode.
ANSI C standard library is supposed to operate on any plateform with an ANSI C-compliant compiler.(horizontal abstraction)
And there are many platforms that don't have POSIX like permission mode.
So ANSI C fopen() cannot have a mechanism to specify the access permisson mode.





The POSIX close function (system call) and the ANSI C fclose function seem to be doing approximately the same things.

B. Would it be possible to safely implement fclose using close (that is, by having fclose call close)? Explain your answer.

YES.

fclose() is a standard library function that closes an opened file.
close() is a POSIX system call that closes an opened file.
Since ANSI C standard library is platform independent, at some point the standard library functions should make use of the system call interface of the specific platform.
And close() is the system call doing the same thing what fclose() does.
So on a POSIX system, it is possible for the fclose() to use close().
And actually fclose() uses close() on a POSIX system.




C. Would it be possible to safely implement close using fclose (that is, by having close call fclose)? Explain your answer.

NO

From the vertical abstraction's view, close() is a lower abstraction than fclose().
close() does a platform specific file closing operation.
If close() uses fclose() in its body, then fclose should do some platform specific file closing operation or fclose() should call close() again since there's no other system call that closes a file on a POSIX platform.
Since fclose() is a platform independent library function, fclose() shouldn't do anything specific to the POSIX platform.
If fclose() calls close() again, then it makes an infinite loop, which is very unsafe.
It is a violation of the abstraction behavior.