[cs631apue] final exam grades
jschauma at stevens.edu
Sun Dec 18 18:50:03 EST 2011
I will be sending out your final exam grades in a little while. Here
are some notes and correct answers for the final (the final itself is
available on the course website):
1) What is the effect of lseek(fd, 10, SEEK_END) where fd is a valid
file descriptor for the process calling lseek?
B. The file offset pointer for the descriptor fd is set 10 bytes beyond
the current end of file. That is, a subsequent write will write data
10 bytes past the current end, creating a so-called sparse file.
2) Consider the following file:
-r--r--r-- 3 jans wheel 0 Dec 1 14:00 file
Calling 'unlink("./file")' as user 'jans' from within this directory
will have the following effect:
A. The directory entry "file" will be removed from the current
Well, technically it's possible that the unlink call will fail if the
current directory does not have write permissions for user 'jans', but
since you do not have that information you'd either have to answer 'none
of the above' or guess that A is correct.
Recall that write permissions on the file itself are irrelevant to the
act of unlinking a file.
3) What is the difference between _exit(2) and exit(3)?
A. _exit(2) starts with an underscore, while exit(3) does not
B. _exit(2) is a system call, while exit(3) is a library function
4) Name 4 reasons for open(2) to fail (with an example).
I was looking for any of the things listed in the manual page under
5) fork(2) returns
A. the pid of the child to the parent, because a parent may have
multiple children, and there is no way to say "what's the pid of child
B. 0 to the child, because a process can always get its own pid via
getpid(2) and its parent's pid via getppid(2)
6) Every process has a process ID and a parent process ID. Name 4
other properties of a process.
Things of interest: signal mask, umask, ulimit, session id, process
group, environment, resource utilization, ...
7) If process A calls fork, creating process B, and process B then calls
fork again, creating process C, and process C calls exec to overlay
itself with the new program image, in what order do the processes run
after the first fork call?
D. There is no fixed order.
8) Suppose you wish to update the ctime on a given file. The
correct function to use is:
D. stat() is not correct -- stat does not change anything about the
file, nor does it actually access the file itself (if it did, it would
update the atime, and thus the ctime), but only the metadata of the
9) Name 4 system calls that may block. For each, give an example of how
to avoid blocking.
read, write, send, recv, open, accept, wait, ...
avoid blocking by setting non-blocking via fcntl, in open flags etc,
use select to identify fds ready for IO etc.
10) 12 points
Consider the files "file1" "file2" "dir/file3" and "dir/file4" with the
permissions as indicated below. For each file, list all users
(jschauma, root, all users in group wheel, all users in group sys, all
users in group users, all other users) who may
(a) read the file
(b) write the file
(c) remove the file
If the answer is non-obvious, explain.
$ groups jschauma
$ ls -lad .
drwxrwxrwt 4 root wheel 512 Jul 20 17:35 .
$ ls -la file1 file2 dir
----rw--w- 1 jschauma wheel 0 Jul 20 17:35 file1
-rw-r--r-- 1 root sys 0 Jul 20 17:33 file2
drwxrwx--- 2 root wheel 512 Jul 20 17:34 .
drwxrwxrwt 4 root wheel 512 Jul 20 17:35 ..
-rw------- 1 jschauma wheel 0 Jul 20 17:33 file3
-r--rw-r-- 1 root wheel 0 Jul 20 17:33 file4
(a) file1: root (owner jschauma does not have read permissions;
technically, owner jschauma could change permissions and then read,
file3: root, jschauma
file4: root, group wheel, jschauma (b/c jschauma is in group wheel, which 'dir'
is restricted to; others can't read since they can't access 'dir')
(b) file1: everybody but jschauma, effectively; jschauma owns the file
so could change permissions and then write, though
file3: root, jschauma
file4: root, users in group wheel, jschauma (by virtue of being in group wheel)
(c) file1: root, jschauma (note sticky bit set on the directory)
file3: root, all users in group wheel (including jschauma)
file4: root, all users in group wheel
11) Write a program 'revline' that, given an arbitrarily large text
file, reverses the contents line by line (but does not reverse the words
in the file).
No comment since the extra-credit assignment is still open.
12) Implement the lock(1) command.
Things I was looking for:
- turn of echo on terminal before reading password
- compare passwords, obviously
- install signal handlers for SIGINT and SIGALRM:
- SIGALRM handler simply exits the program
- SIGINT handler prints diagnostic message and time remaining
(ie keep track of time)
- set alarm
- loop forever and read
And that is all. I will keep your blue books until next semester. If
you'd like to get it back, please let me know at the beginning of the
spring semester and meet me prior to my class (I won't be on campus
until then, I'm afraid).
More information about the cs631apue