CSE4001 Fall 2005 Exam #1, open book, open notes. Name ____________________ 1. Write UNIX commands to complete the tasks below. You may use any of the common shells (sh, csh, tcsh, ksh, bash). (5 pts each). ANSWERS a. List all running processes and sort the output. ps | sort b. Create a directory listing and save the result to ls > dir.txt a file named dir.txt. c. Set the permissions of a.out so that anyone may chmod 711 a.out execute it but only the owner can read or modify it. d. Show the value of the PATH variable. echo $PATH e. Create a directory tmp (under your home directory) cd and set the permissions so that anyone may list the mkdir tmp contents and modify the files it contains, but only you chmod 755 tmp can create new files or delete them. f. Compile foo.c in gcc in the background with standard (csh, tcsh) gcc foo.c >& /dev/null & output and standard error discarded. (sh, bash) gcc foo.c > /dev/null 2>&1 & 2. Write a bash script "link" that takes 2 file names as arguments and creates a symbolic link from the first to the second, e.g. (10 pts). link foo bar ls -las foo bar lrwxrwxrwx 1 user group 123 Sep 28 1400 foo -> bar -rw-rw-rw- 1 user group 123 Sep 28 1400 bar ANSWER: #!/bin/bash ln -s $2 $1 ANSWERS 3. What character will kill the currently running process? (5 pts). Ctrl-C or ^C 4. How do you suspend the currently running process and ^Z run it in the background? (5 pts). bg 5. How do you kill a process running in the background? (10 pts). ANSWER: find the process id with ps (say, 1234), then: kill 1234 6. Write a program in C that copies standard input to standard error using UNIX system calls for I/O and a block size of 4096 (40 pts). /* ANSWER (with no error checking or header files) */ main() { char buf[4096]; while (1) { int n = read(0, buf, 4096); /* n = number of bytes read */ if (n <= 0) /* EOF or error? */ break; write(2, buf, n); } }