COMP 3000 2012 Midterm Material

From Soma-notes

Sample Test Questions

  1. You can create a sparse file in UNIX by writing sequences of zeros to a file.
  2. Are environment variables ``global variables? Specifically, if you change the value of an environment variable X in one process, what happens to the value of X in other processes?
  3. In UNIX, there are three permissions associated with the user, the user's group, and everyone else. What are those three permissions?
  4. If you see a zombie process during normal system operation, how can you get rid of it?
  5. I can copy a file using I/O redirection as follows: cat
 /bin/ls > bar.  If I now type  ./bar, it won't run.  However, if I run another command before typing ./bar, I will get a file listing. What needed to be changed to get bar to run?
  1. System calls cannot normally be made using standard function calls. What CPU mechanism is used by regular processes to invoke kernel functionality?
  2. After a fork system call, what runs first, the child process runs or the parent process?
  3. Signal handlers can be called at almost any point in a process's execution. (T/F)
  4. System V init scripts run programs sequentially or in
   parallel?
  1. Without errors, what does the execve system call return? # Who calls a signal handler?
  2. What program is used to check the consistency of the data structures of a filesystem?
  3. What allocates the storage for environment variables and command-line arguments?
  4. A file that has a logical size of one gigabyte but only takes up 100K on disk is called what kind of file in UNIX?

Lab Questions

Questions from Lab 1

  1. What Linux distribution are you using? What version? You should look at the file /etc/issue generally to find out.
  2. What shell do you get by default when you log in? Check by running echo $SHELL. If it is not bash, then type exec bash to switch to it.
  3. Is your Linux environment running virtualized or on bare metal? How do you know? (If you aren't sure, say so, but explain why.)
  4. Who installed and configured your Linux environment? What level of access do you have to it?
  5. A shell command can be one of four things. What are those four things? Explain very briefly.
  6. On the machine you are on, in what directories are commands external to the shell stored? How can you get this information on the command line?
  7. Compare the commands ls /usr/bin | more and ls /usr/bin | less. How is the output similar? How are they different?
  8. How would you create an empty file with the filename empty file.txt? (Yes, that is a space in the filename.)
  9. How would you output the contents of this file?
  10. Some shell commands, such as pwd, are both built-in and are external. What is one reason why both versions might be present?
  11. What are the permissions on your Linux home directory? With those permissions and your knowledge of the other accounts on the system, who has access to your home directory, and what sort of access do they have?
  12. Give one or more command line strings that use the following operators: >, <, >>, <<, |. Explain briefly what each operator is doing in your examples, both concretely and in terms of STDIN and STDOUT.
  13. Are there operators that work specifically with STDERR? Explain briefly.
  14. How do you use these operators with other file descriptors? Explain briefly.
  15. Give an example of a bash for loop and explain what it does.
  16. Give an example of a bash if statement and explain what it does.
  17. With the & you can put processes in the "background". Given that external commands are always run as separate processes from the command shell, what is the key difference between foreground and background processes?
  18. What are the differences between shell and environment variables? Specifically, what processes have each of them, and to what extent are they shared? HINT: look at the execve system call.

Questions from Lab 2

  1. How can you get a list of all of the processes running on the system from the command line? Please give the command and required arguments, if any.
  2. What are five processes that are running on your system as non-regular users (i.e., as users other than the one you logged in as)? What does each do, briefly? Note: please exclude all processes enclosed in [], as those are built in to the kernel.
  3. How can I restart the graphical login screen on Ubuntu? (Hint: use virtual terminals and Ctrl-Alt-F? combinations to access a terminal that is independent of the GUI. Also, make sure you are aren't logged into a graphical session when you try this!)
  4. What happens when you send a kill -9 signal to one of the getty processes that are running by default? Why?
  5. What starts the upstart daemon? When is it started?
  6. How would you "change the system runlevel" to reboot? Shut down?
  7. Look at the output of ls -l /etc/init.d/. You will note -> for many of the entries. This arrow shows that those entries are symbolic links. Where do most of these symbolic links point to? Why?
  8. Install the openssh-server package in your virtual machine from the command line. What command(s) did you use?
  9. Note that sshd has separate entries in /etc/init and /etc/init.d differ. Why do both exist (when most services have either one or the other)?
  10. Log messages for the system are stored in /var/log. What are four log files that are present on your system? (Ignore the ones with numeric extensions, those are old versions.) What program wrote each of those files directly?
  11. In /etc/init.d/ssh there are two lines near the beginning that end with output || exit 0 (should be lines 16 and 17). What do each of these lines do exactly? And what is there purpose?
  12. What are the equivalent lines, if any, to these || exit 0 lines in /etc/init/ssh?
  13. What is plymouth?
  14. BONUS: Trace plymouth's behavior throughout the boot process.

Questions from Lab 3

  1. Compile the program hello.c (below) with gcc -O hello.c -o hello-dyn and then run it using the command ltrace ./hello-dyn . What dynamic functions does the program call?
  2. Compile the same program with gcc -O -static hello.c -o hello-static . Run this second binary with ltrace as before. What dynamic functions does the program now call?
  3. Run strace on the static and dynamically compiled versions of hello. How many system calls do they each produce?
  4. How can you make the output of hello.c go to the file "hello-output" by changing how it is invoked at the command line?
  5. How can you make the output of hello.c go to the file "hello-output" by changing its code?
  6. Compile run-program.c with the command gcc -g run-program.c -o run-program and use it to answer the following questions.
    1. Is the line "Is this line printed?" printed when you execute ./run-program /bin/ls -a /tmp? Why?
    2. Change the program to use execve instead of execvp. What is the difference between the two functions?
  1. Linux signals is a simple form of IPC that is used for a variety of purposes.
    1. What signal is generated when a program attempts to divide by zero? Give a simple program that generates such a signal.
    2. How would you send a signal to a process to have it pause execution? Resume?
  1. What command can lower the priority of an already running process?
  2. Why do we "open" files? Specifically, to what extent is it possible to do file I/O without open and close operations - assuming you could change the UNIX API?
  3. What is the relationship between function calls, system calls, and library calls?
  4. Create run-program-skips.c that is the same as run-program.c except that it ignores every other argument to the program to be run (starting with the first non-program-name argument). Hence, ./run-program-skip /bin/ls ignore -a ignore /tmp will do the same as ./run-program /bin/ls -a /tmp.
  5. Create sanitize-env.c that does the same as run-program.c except it exec's the program with all environment variables stripped except the PATH and TERM variables (assuming they are defined). Be sure to document how you tested your program.
  6. Create a simple program receive-term.c that uses sigaction in order to print "Ouch!" when you send the program the TERM signal and then exits. Note that your program should not consume much CPU time while waiting (i.e., do not poll for the signal). As part of your answer, give the command for sending the signal.
  7. Create a simple program nocrash.c that accesses an invalid pointer but then prints "Oops, my mistake" rather than crash.
  8. Create a program run-program-dots.c that works the same as run-program.c, except that it prints one dot every second while a given program runs. Note that it should stop printing dots once the exec'd program terminates. Your solution should use the fork(), sleep(), execve(), and sigaction() calls.
  9. What is the difference between the PR and NI columns in the top command? Explain in the context of running a CPU-intensive program. (Hint: you should try running a CPU intensive program or two and observe these columns...)

Program Listings

/* hello.c */
#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
       pid_t p;

       p = getpid();
      
       printf("Hello, world!  I am process %d.\n", (int) p);
       return 0;
}

/* run-program.c */

#include <unistd.h>
#include <stdio.h>

int main( int argc, char *argv[], char *envp[] ) {

   pid_t p;

   p = getpid();
      
   printf("Hello again.  I am process %d.\n", (int) p);

   if( argc < 2 ) {
       printf( "Insufficient arguments.\n" );
       return -1;
   }
   execvp( argv[1], argv + 1 );
   printf( "Is this line printed?\n" );
   return 0;
}

Questions from Lab 4

  1. Run the command truncate -s 1G foo. What is the logical size of foo, and how much space does it consume on disk?
  2. Run mkfs.ext4 foo. (Say "yes" to operating on a regular file.) What is the logical size of foo now, and how much space does it now consume on disk?
  3. What command do you run to check the filesystem in foo for errors?
  4. Run mount foo /mnt. What does this command do?
  5. Run df. What device is mounted on /mnt? What is this device?
  6. Run rsync -a -v /etc /mnt. What does this command do? Explain the arguments as well.
  7. Run umount /mnt. What files can you still access, and what have gone away?
  8. Run dd if=/dev/zero of=foo conv=notrunc count=10 bs=512. What does this command do?
  9. Run mount foo /mnt. What error do you get?
  10. What command can you run to make foo mountable again? What characteristic of the file system enables this command to work?
  11. Create a simple C program to generate a file consisting of the word "Hello", one thousand bytes of blank space (null bytes), and then "world\n", and then another thousand null bytes, such that the file produced consumes the maximum amount of space given its contents.
  12. Create a C program "mycopy-blocks.c" that takes three arguments: a block size and two filenames A and B as command line arguments. This program should copy the contents of A to B, making B a byte-for-byte copy of A, except that the length of B should be evenly divisible by the block size. The file should be padded with nulls as necessary. Zero bytes in A should consume little to no space in B, however, including the padding.
  13. How could loopback mounts be useful when working with virtual machines? What conditions must the VM software meet for this to work?

Answers

Answers from Lab 1

  1. should be: Ubuntu 12.04 LTS unless specifically stated reason otherwise
  2. /bin/bash *SCS gives csh as the default shell*
  3. bare metal (lambda) or virtualbox (virtualized)
  4. System administrators, level of access is restricted user
  5. An executable program like all those files we saw in /usr/bin. Within this category, programs can be compiled binaries such as programs written in C and C++, or programs written in scripting languages such as the shell, perl, python, ruby, etc. A command built into the shell itself. bash supports a number of commands internally called shell builtins. The cd command, for example, is a shell builtin. A shell function. These are miniature shell scripts incorporated into the environment. We will cover configuring the environment and writing shell functions in later chapters, but for now, just be aware that they exist. An alias. Commands that we can define ourselves, built from other commands.
  6. /usr/bin and /usr/sbin and other directories. Run "echo $PATH" to see list of directories. Also, which [external command] will tell you path for specific commands, e.g. "which ls"
  7. less allows backward movement within the file as well as forward movement. more is a rudimentary method for printing to crt terminals, but only allows movement through the file in a forward direction. You have to press 'q' to exit less, but more quits when it reaches EOF.
  8. touch "empty file.txt"
  9. cat empty\ file.txt or cat "empty file.txt" or cat 'empty file.txt' or > 'empty file.txt'
  10. Some require direct access to kernel functions and therefore must be built in. Also the built in functions operate faster than non built in functions. This is either for performance reasons -- builtins execute faster than external commands, which usually require forking off a separate process -- or because a particular builtin needs direct access to the shell internals.
  11. drwx------ d = a directory flag - since my home directory is a directory, there exists a d here rwx = read, write, execute permission for the User (owner of the directory) which is my username --- = read write execute permissions are lacking for a Group --- = read write execute permissions are lacking for other this means my work is private. Only myself and the root user can access my directory.
  12. cat file.txt | grep hello the | character joins the two commands cat and grep, it enables the output from one command to be passed directly to another. cat file.txt > error.txt the > character redirects the standard output to the specified filename that is included in the command after it. If the file is not created it will create it, if the file exists it will overwrite the current file cat file.txt > alreadyexistingerrorfile.txt the >> character appends to the already existing file the output of the first command. cat < file.txt is equivalent to cat file.txt the < character redirects the standard input to come from the file instead the last form of redirection is only useful within shell scripts to avoid having to code a bunch of echo statements, say for a particular menu. The document created is called a here document. It is a method of specifying a string literal in command line shells. (preserves line breaks and whitespaces / indentation within the text.) example:
  13. present a menu cat << alldone What would you like to do today? Play with shell scripts Write wonderful awk scripts Invent powerful sed scripts zippity do and dah alldone the above code will generate the following output: What would you like to do today? Play with shell scripts Write wonderful awk scripts Invent powerful sed scripts zippity do and dah otherwise you would have had to code echo statements for each line.
  14. STDERR does not have a dedicated redirection operator, instead to redirect stderr you have to refer to it's file descriptor. and call it using 2>
  15. the other file descriptors are 0 for standard in and 1 for standard out, the descriptor number is placed right before the redirection operator.
  16. for variable [in words]; do commands done variable is the name of a variable that will increment during the execution of the loop, words is an optional list of items that will be sequentially assigned to the variable, and commands are the list of commands to be executed on each iteration of the loop. for i in A B C D; do echo $i; done
  17. x=5 if [ $x = 5 ]; then echo "x equals 5." else echo "x does not equal 5." fi x=5 if [ $x = 5 ]; then echo "equals 5"; else echo "does not equal 5"; fi if commands; then commands [elif commands; then commands...] [else commands] fi where commands is a set of commands. Providing an exit status that indicates success or failure.
  18. Background processes usually interact with the system and do not interact with the shell or the user, foreground processes must interact with the command shell. In the shell, background and foreground processes are the same except that the shell waits for foreground processes to finish before giving a new prompt.
  19. shell variables apply only to the current shell. Environment variables apply to all possible shells running on the system. to list all env variables type printenv to list all shell variables use set a shell variable is local to a particular instance of the shell (such as a shell script), while environment variables are "inherited" by any program you start, including another shell That is, the new process gets its own copy of these variables, which it can read, modify, and pass on in turn to its own children. In fact, every UNIX process (not just the shell) passes its environment variables to its child processes.

Answers from Lab 2

  1. ps -aux
    ps -e
    Among others.
  2. cron - Cron is the time-based job scheduler.
    lightdm - Display manager. It also manages the X servers and facilitates remote logins using the XDMCP protocol.
    Network Manager - NetworkManager is a set of co-operative tools that make networking simple and straightforward. Whether WiFi, wired, 3G, or Bluetooth, NetworkManager allows you to quickly move from one network to another user/sbin/modemmanager.
    Upstart Socket Bridge - The upstart-socket-bridge queries the Upstart init(8) daemon for all job configurations which start on or stop on the socket event. It then waits for an incoming connection on each specified socket(7) and when detected emits the socket event (socket-event (7)), setting a number of environment variables for the job to query.
    acpid - The acpid process is designed to notify user-space programs of ACPI events.
    These are just some examples of acceptable answers. There are others as well.
  3. Make sure you are not logged into xsession, then restart lightdm with sudo service lightdm restart
  4. It will automatically respawn itself. There is a line in the tty script that instructs the process to automatically restart in the case of failure or termination of the process.
  5. The kernel starts the upstart daemon at system boot.
  6. To reboot use either telinit 6 or shutdown -r. To shutdown use either telinit 0 or shutdown tool with -h option and a time.
  7. The links point to /lib/init/upstart-job. Ubuntu is transitioning from SysVinit scripts to upstart scripts, these links are used to redirect to the upstart services.
  8. sudo apt-get install openssh-server.
  9. The init.d script is used only for chroot environments. See lines 14-26 in init.d/ssh script.
  10. Can check syslog process to see which logs it directly writes too. Some acceptable answers are:
    1. auth.log - CRON, lightdm, polkitd
    2. boot.log - modem-manager
    3. bootstrap.log - gpgv, dpkg
    4. dpkg.log - dpkg
    5. fontconfig.log - fontconfig
    6. kern.log - kernel
    7. pm-powersave.log - powerd
    8. pm-suspend.log - sleepd
    9. ufw.log - ufw
  11. test -x /usr/sbin/sshd || exit 0 - Check whether "/usr/sbin/sshd" exists and is executable and exit with status of 0 if not. Used to verify SSHD is installed.
    ( /usr/sbin/sshd -\? 2>&1 | grep -q OpenSSH ) 2>/dev/null || exit 0 - Runs SSH script with illegal option and pipes the output to grep to check for OpenSSH. Rest is piped to /dev/null essentially discarding it. This line checks to verify that the version of SSH installed is OpenSSH. If not will exit with status of 0.
  12. test -x /usr/sbin/sshd || { stop; exit 0; }
    Does not have a check for OpenSSH.
  13. Plymouth is a bootsplash. It provides a graphical boot animation while the boot process takes place in the background.
  14. not given.