ANSWERS TO QUIZ FOR UNIX GURUS 1) What does it mean if a file called "xaa" appears in your home directory? You have used the "split" command. 2) What are the "lost+found" directories for? When "fsck" (file system check) is run at reboot, any files it finds which are on the disk but not mentioned in any directory are put in the lost+found directories, of which there is one for each mounted file system. 3) What do BSS, SCCS and UUCP stand for? Block Starting with Symbol (mnemonic from Honeywell 516 series, equivalent to rmb or dc.b) Source Code Control System Unix to Unix Copy 4) What access permissions do you need to remove a file? You need write permission on the directory the file is in and execute permission (ie search permission) on all directories leading to the file. 5) Why should you not strip /vmunix? Because ps needs the symbol table to find where things are in the kernel memory. 6) What is a clist? A C-list is a small structure (record to Pascal programmers) which is used to hold keyboard type-ahead and character output. Each clist contains a few characters and a pointer to the next clist in the linked list. It stands for character-list. 7) Why doesn't it matter too much if someone removes your editor's temporary file from /tmp? Because the disk space will not actually be freed for re-use by the kernel while there is a living process that has the file open. It would affect recovery of the file if the system crashed but would not affect a running editor. 8) How do you turn the lines in a file upside-down with one em command? g/^/m0 9) What is the unix command to turn each of the lines in a file back-to-front? rev 10) Under what circumstances does a process which has called _exit() not die immediately, but remain a zombie? A process whose parent has not waited for it will not die when it calls _exit. If the parent is still alive but is not currently waiting, all of the child's resources are freed except for the information which will have to be returned to the parent if it waits. If the parent is already dead, the child is inherited by process 1 (init), one of whose jobs is to wait for dead orphans. 11) How is it possible to create a 500K file without using 500K of disk space? fd = creat("big", 0666); lseek(fd, 512*1024-1L, 0); write(fd, "X", 1); close(fd); This extends the file to 500K in length, but the first 496K (should this be 499K for 4.2? Or something else?) or so does not have disk blocks allocated to it; the disk block pointers are null. man 2 lseek. 12) Name one bug in nroff or troff. If the first line of the input file is a .tl, the ditroff output is not valid and blows up most ditroff device driving programs. If you try to get three evenly spaced x's using "x \f(POx\fR x" (roman x, roman space, print-out x, roman space, roman x), the first space is far width appropriate to a print-out space, which is wider than a roman space. This seems to be a space-flushing problem. Heaven only knows the difference between \e, \\ and \ as there seems to be little or no rhyme or reason in the way they behave. 13) How can you implement file locking on 4.1BSD Or, indeed, any unix system without an add-on file locking system call. Use creat("lockfile", 0444) to create a read-only file in an agreed place. If the lockfile was absent, the creat will succeed. If the lockfile was already present and read-only, the creat fails. To unlock, the locking process should remove (unlink()) the lockfile. This needs weeding if the system crashes while the lockfile exists, or the locking process exits or dies horribly without removing the lockfile. There is a hairier method which relies upon being unable to open for writing a demand-paged executable file which is executing, and vice versa. This has the advantage that however the locking process dies, either the file descriptor is closed automatically, or the dead child gets inherited by process 1 (you can play the lock two ways), but a secondary locking mechanism such as creatting a read-only file is required around the critical code as the test-and-lock operation is not atomic. I've got some code to do this if you're *really* interested. 14) Your machine has crashed and the automatic reboot has failed. You go to the console and say "ls /usr" only to find that it is utterly empty! "Don't panic!" calls your guru. Why not? The /usr disk partition has not been mounted on the /usr directory, so all you see is the enpty directory on top of which it would normally reside. This actually happened! 15) Why is the following C code *wrong*? It seems to work. #include main() { char ch; while ((ch=getchar()) != EOF) putchar(ch); } The "char ch" should be "int ch" as getc needs to return 257 different values, 256 possible 8-bit characters and EOF as well. On a Vax, where characters are signed, this would mean that the above loop would terminate if it met a real character 255 in a file, and on an Orion, where characters are unsigned, the -1 returned by getchar would become 255 in c, which != -1, so the program would loop forever. 16) What sequence of unix commands could you use to convert your C program in the file "prog.c" into an executable file? Oh, I almost forgot...someone has removed /bin/cc and you haven't a backup copy anywhere. # these comments are unnecessary, of course # preprocessor /lib/cpp prog.c temp1 # compile /lib/ccom temp1 temp2 # assemble as -o prog.o temp2 # link editor. crt0.o is C run-time startup routine which calls main(), # and -lc instructs ld to look for library functions in /lib/libc.a ld /lib/crt0.o prog.o -lc 17) Name two ways to keep files on disk in a form which uses less disk space than the way files are normally kept. a) use "compact" to perform data compression the file b) use "ar" to bundle lots of small files into one large one. This works because small files use an entire disk block. 18) At UKC, how is it found what sort of terminal you have logged in on? The kernel reads what PAD line you logged on to out of the "calling address" of the transport service packet which the PAD sent when you told what host to connect to. The login program does an ioctl on the file descriptor open to the tty and the kernel returns this info as a pair of numbers. It then looks up the location in an encoded version of /etc/locations/ttys and sets the environment variable TERM. 19) How can you produce an executable file that doesn't contain the stdio library? 1) You have to use system calls to do I/O and... 2) crt0.o causes some problems. This is linked in at compilation, and calls your main() and then exit(). exit() is part of stdio, so rakes in malloc, flsbuf and friends. One way to get round this is to define a function called exit() which just calls _exit(), to prevent the real exit() being sought in /lib/libc.a. Another way which seems to work on our 4.2 systems but is heavily system-dependent is to leap straight into your code without using the standard start-up code. If write.c is: main() { write(1, "Hello, World!\n", 14); _exit(); } then cc -c write.c ld write.o -lc would produce a minimal binary, 132 bytes on an Orion. 20) What program promises to "execute cmd in a subshell"? more, when you ask for help from the [--More--] prompt. 21) How can you get an ls -l of all the setgid programs on the system? find / -perm -2000 -exec ls -ld {} \; Strictly speaking, there should be a "-type f" in there as well as you can setgid directories (to no effect!)