Schedule
Labs
Assignments
TA office hours
Topic videos
Some course notes
Extra problems
Lecture recordings
Make sure you have all 11 pages (including this page).
(Don't panic about the page count -- there's lots of extra space for answers.)
Answer all questions. Answer questions in the space provided. Answers not in the correct space will not be graded unless a note in the correct space says "see page ..." and the answer on that page is clearly labelled with the question number.
In general, in the C programming questions you can omit the #includes, and you can omit comments unless you need to explain something unclear about the functioning of your code.
1. [15 marks] For each of the following pairs of sh commands, is the behaviour the same or different? If the behaviour is the same, explain why the difference in the command has no effect on the behaviour. If the behaviour is different, state the behaviour of each of the two commands.
a)
echo hello, worldas opposed to
echo 'hello, world'
b)
x=hello echo '$x, world'as opposed to
x=hello echo "$x, world"
c)
cat fileas opposed to
cat <file
d)
sort file | tr x yas opposed to
sort file | tr x y | sort
e)
cat file >file2as opposed to
cat file >file2 2>&1
2. [6 marks] The "Fibonacci" sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... After the initial 0 and 1, each number is the sum of the two previous numbers, e.g. 13=5+8. (The traditional formulation begins with 1, but sometimes in Computer Science we like to begin with 0; anyway, please use the above version for this exam question.)
Write a loop in the sh programming language to output the Fibonacci sequence up to the last number less than 1000 (i.e. do not output numbers greater than (or equal to) 1000).
3. [9 marks] Write a shell script (in the sh programming language) which takes a grep pattern and a file name, and outputs the single line in the specified file which matches that pattern.
If other than exactly one line matches, this is an error. If no lines match, there should be no output (to stderr or to stdout), but the shell script should exit with exit status 1. If more than one line matches, output a suitable error message to stderr, and nothing to stdout.
Your shell script takes just the two specified arguments, and should output a usage error if run with other than the two specified arguments.
4. [7 marks] Consider the following portion of a unix filesystem:
inode number | is directory? | contents |
---|---|---|
2 | D | "." 2, ".." 2, "bin" 3, "usr" 4, "vmunix" 5 |
3 | D | "." 3, ".." 2, "sh" 6 |
4 | D | "." 4, ".." 2, "ajr" 7 |
5 | unix kernel machine code | |
6 | machine code for sh | |
7 | D | "." 7, ".." 4, "hello.c" 8 |
8 | #include <stdio.h> int main(){print("Hello\n");return(0);} | |
9 |
a) What is an absolute path name for inode 8?
b) If we execute the command mkdir /usr/sally , and the newly created directory is inode 9, what is changed in the table above? Make the changes in place in the table, by striking out any removed information and writing in added information.
c) [1 mark] What is another absolute path name for inode 8, different from your part 'a' answer?
5. [3 marks] What is the purpose of the "link count" in the inode in the unix filesystem? It is incremented when the file is linked into the filesystem somewhere, and decremented when the file is unlinked; but why does the unix operating system have to track this information?
6. [20 marks] Write a simplified version of cmp in C. cmp takes two filename arguments. It reads the files in parallel, one byte (character) at a time, until it reaches a difference. If the files are identical, it exits with no output.
If one file ends before the other, but the files are identical up to that point, the message is of the format "EOF on filename" (specifying the appropriate file name). If the files' characters differ at a particular point, it outputs a message of the format "file1 file2 differ: char 123" (using the two file names, and the byte position in the file (one-origin) of the difference).
The exit status is 0 if the files match; 1 if the files differ; and 2 if there is an error of any kind.
You needn't call getopt(). You do, however, have to output all appropriate error messages for a standard unix tool.
You can omit the #includes.
sample solution which is less terse but becomes more complicated
7. [20 marks] Write a simplified version of man in C.
Your man command will take zero or more arguments, and loop through them. For each argument, it searches all eight possible 'man' directories, in order, for a file name beginning with the supplied name and a dot. For the first one it finds, it executes /usr/bin/less (using fork() and execl()), specifying the absolute path name to the appropriate file as argv[1].
The man directories are /usr/share/catman/man1, /usr/share/catman/man2, and so on through /usr/share/catman/man8 (inclusive).
A sample possible file name matching "man cat" is /usr/share/catman/man1/cat.1 Another sample possible file name matching "man cat" is /usr/share/catman/man1/cat.1n However, the file name /usr/share/catman/man1/catch.1 must not be deemed to match "man cat" -- you need to check for the dot as well.
8. [20 marks] Write a C program which listens on port number 1234 and tells each caller how many people have connected so far. You could connect to it by typing "telnet hostname 1234" (for the appropriate hostname), and it would output "1" and close the connection. The next person would be told "2", etc. The program does not exit (until it is killed).
You do not have to handle multiple simultaneous incoming connections. Remember to use the network newline convention -- end the output with a newline. You can omit the #includes, and you can cite course web pages rather than copying out bits of code if you can do so unambiguously.
End of exam. Total marks: 100. Total pages: 11.