- [3 points] The following code is used to open a file. Describe the purpose of each of the components in this command.
fd = open("myFile", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)
fd: the return value is the id of a file desriptor which has been opened or -1 on error
"myFile": is the name of the file to be opened
O_RDWR: open the file read/write for the process
O_CREAT : create the file if it does not exist
O_TRUNC: truncate (or erase) any data in the file
S_IRUSR: give the user read permission on the file
S_IWUSR: give the user write permission on the file
This code is directly from page 73 of the book.
- [5 points] the following code is used to write to a file.
- Give code to detect and report an error due to executing this code.
- What situation, other than errors, might the programmer need to handle as a result of this code executing?
for part i
if (-1 == bytesWritten) {
perror("Write failed");
}
for part 2
If bytesWritten is less than bufferSize, the the write was incomplete and the rest of the data should be written.
bytesWritten = write(fd, buffer, bufferSize);
- [4 points] Describe the result of seeking 1000 bytes past the end of a file and writing data. Does the program crash due to this action and is the data written lost?
The data will be written 1000 bytes past the end of the file and there will be a hole in the file. No data will be lost and the program will not crash.
- [2 points] What happens if the last reference to a file removed (unlinked), while a process still has an open file descriptor referencing that file?
The file will remain until the last open file descriptor is closed, at which point the file will be removed.
I understand that it will be removed when the program exits, but that is an upper bound, it has potential to be removed MUCH sooner.