Week 08 Tutorial Questions

  1. We say that the Unix filesystem is tree-structured, with the directory called / as the root of the tree, e.g.,

    Answer the following based on the above diagram:

    1. What is the full pathname of COMP1521's web directory?

    2. Which directory is ~jas/../..?

    3. Links to the children of a given directory are stored as entries in the directory structure. Where is the link to the parent directory stored?

    4. What kind of filesystem object is cat?

    5. What kind of filesystem object is home?

    6. What kind of filesystem object is tty0?

    7. What kind of filesystem object is a symbolic link? What value does it contain?

    8. Symbolic links change the filesystem from a tree structure to a graph structure. How do they do this?

  2. The stat() and lstat() functions both take an argument which is a pointer to a struct stat object, and fill it with the meta-data for a named file.

    On Linux, a struct stat contains the following fields (among others, which have omitted for simplicity):

    struct stat {
    	ino_t st_ino;         /* inode number */
    	mode_t st_mode;       /* protection */
    	uid_t st_uid;         /* user ID of owner */
    	gid_t st_gid;         /* group ID of owner */
    	off_t st_size;        /* total size, in bytes */
    	blksize_t st_blksize; /* blocksize for filesystem I/O */
    	blkcnt_t st_blocks;   /* number of 512B blocks allocated */
    	time_t st_atime;      /* time of last access */
    	time_t st_mtime;      /* time of last modification */
    	time_t st_ctime;      /* time of last status change */
    };
    

    Explain what each of the fields represents (in more detail than given in the comment!) and give a typical value for a regular file which appears as follows:

    ls -ls stat.c
    8 -rw-r--r--  1 jas  cs1521  1855  Sep  9 14:24 stat.c
    

    Assume that jas has user id 516, and the cs1521 group has group id 36820.

  3. Consider the following (edited) output from the command ls -l ~cs1521:

    drwxr-x--- 11 cs1521 cs1521 4096 Aug 27 11:59 17s2.work
    drwxr-xr-x  2 cs1521 cs1521 4096 Aug 20 13:20 bin
    -rw-r-----  1 cs1521 cs1521   38 Jul 20 14:28 give.spec
    drwxr-xr-x  3 cs1521 cs1521 4096 Aug 20 13:20 lib
    drwxr-x--x  3 cs1521 cs1521 4096 Jul 20 10:58 public_html
    drwxr-xr-x 12 cs1521 cs1521 4096 Aug 13 17:31 spim
    drwxr-x---  2 cs1521 cs1521 4096 Sep  4 15:18 tmp
    lrwxrwxrwx  1 cs1521 cs1521   11 Jul 16 18:33 web -> public_html
    
    1. Who can access the 17s2.work directory?

    2. What operations can a typical user perform on the public_html directory?

    3. What is the file web?

    4. What is the difference between stat("web", &info) and lstat("web", &info)?
      (where info is an object of type (struct stat))

  4. Write a C program, chmod_if_public_write.c, which is given 1+ command-line arguments which are the pathnames of files or directories

    If the file or directory is publically-writeable, it should change it to be not publically-writeable, leaving other permissions unchanged.

    It also should print a line to stdout as in the example below

    dcc chmod_if_public_write.c -o chmod_if_public_write
    ls -ld file_modes.c file_modes file_sizes.c file_sizes
    -rwxr-xrwx 1 z5555555 z5555555 116744 Nov  2 13:00 file_sizes
    -rw-r--r-- 1 z5555555 z5555555    604 Nov  2 12:58 file_sizes.c
    -rwxr-xr-x 1 z5555555 z5555555 222672 Nov  2 13:00 file_modes
    -rw-r--rw- 1 z5555555 z5555555   2934 Nov  2 12:59 file_modes.c
    ./file_modes file_modes file_modes.c file_sizes file_sizes.c
    removing public write from file_sizes
    file_sizes.c is not publically writable
    file_modes is not publically writable
    removing public write from file_modes.c
    ls -ld file_modes.c file_modes file_sizes.c file_sizes
    -rwxr-xr-x 1 z5555555 z5555555 116744 Nov  2 13:00 file_sizes
    -rw-r--r-- 1 z5555555 z5555555    604 Nov  2 12:58 file_sizes.c
    -rwxr-xr-x 1 z5555555 z5555555 222672 Nov  2 13:00 file_modes
    -rw-r--r-- 1 z5555555 z5555555   2934 Nov  2 12:59 file_modes.c
    
    Make sure you handle errors.
  5. Write a C program, compile.c, which is given 1+ command-line arguments which are the pathname of single file C programs.

    It should compile each program with dcc.

    It also should print the compile command to stdout.

    dcc compile.c -o compile
    ./compile file_sizes.c file_modes.c
    /usr/local/bin/dcc file_modes.c -o file_modes
    /usr/local/bin/dcc file_sizes.c -o file_sizes
    
    Make sure you handle errors, for example, you should stop if any compile fails.
  6. Write a C program, fgrep.c, which is given 1+ command-line arguments which is a string to search for.

    If there is only 1 command-line argument it should read lines from stdin and print them to stdout iff they contain the string specified as the first command line argumenbt.

    If there are 2 or more command line arguments, it should treat arguments after the first as fiilenames and print any lines they contain which contain the string specified as the first command line arguments.

    When printing lines your program should prefix them with a line number.

    It should print suitable error messages if given an incorrect number of arguments or if there is an error opening a file.

  7. Consider the following edited output from the ps(1) command running on one of the CSE servers:

      PID    VSZ   RSS TTY      STAT START   TIME COMMAND
        1   3316  1848 ?        Ss   Jul08   1:36 init
      321   6580  3256 pts/52   Ss+  Aug26   0:00 -bash
      334  41668 11384 pts/44   Sl+  Aug02   0:00 vim timing_result.txt
      835   6584  3252 pts/124  Ss+  Aug27   0:00 -bash
      857  41120 10740 pts/7    Sl+  Aug22   0:00 vi echon.pl
      924   6524  3188 pts/184  Ss   15:52   0:00 -bash
      938   3664    96 pts/184  S    15:52   0:00 /usr/local/bin/checkmail
     1199   6400  3004 pts/142  Ss   Oct05   0:00 -bash
     1381  41504 11436 pts/142  Sl+  Oct05   0:00 vim PageTable.h
     2558   3664    96 pts/120  S    13:47   0:00 /usr/local/bin/checkmail
     2912  41512 11260 pts/46   Sl+  Aug02   0:00 vim IntList.c
     3483  14880  5168 pts/149  S+   Sep20   0:00 gnuplot Window.plot
     3693  41208 11240 pts/120  Tl   13:50   0:00 vim trace4
     3742   6580  3320 pts/116  Ss+  Sep07   0:00 -bash
     5531   6092  2068 pts/158  R+   16:04   0:00 ps au
     5532   4624   684 pts/158  S+   16:04   0:00 cut -c10-15,26-
     5538   3664    92 pts/137  S    15:05   0:00 /usr/local/bin/checkmail
     6620   5696  3028 pts/89   S+   Aug13   0:00 nano PingClient.java
     7132  41516 11196 pts/132  Sl+  Sep08   0:00 vim board1.s
    12256 335316 10436 ?        Sl   Aug14  15:01 java PingServer 3331
    12272   4260  2816 ?        Ss   Aug02  10:34 tmux
    12323  10276  4564 ?        S    Sep09   0:02 /usr/lib/i386-linux-gnu/gconf/gconfd-2
    12461   4260  2808 ?        Ss   Sep02   5:42 tmux
    13051  43448 13320 pts/110  Sl+  Sep05   0:02 vim frequency.pl
    13200  47772 21928 ?        Ssl  15:19   0:02 gvim browser.cgi
    13203  41756 11560 pts/26   Sl+  Aug12   0:02 vim DLList.h
    13936  11872  6856 ?        S    Sep19   0:06 /usr/lib/gvfs/gvfs-gdu-volume-monitor
    30383   7624  3828 pts/77   S+   Aug23 336:28 top
    
    1. Where might you look to find out the answers to the following questions?

    2. What does each of the columns represent?

    3. What do the first characters in the STAT column mean?

    4. Which process has consumed the most CPU time?

    5. Why do some processes have no TTY?

    6. When was this machine last re-booted?

  8. The Unix/Linux shell is a text-oriented program that runs other programs. It behaves more-or-less as follows:

    print a prompt
    while (read another command line) {
    	break the command line into an array of words (args[])
    	// args[0] is the name of the command, a[1],... are the command-line args
    	if (args[0] starts with '.' or '/')
    		check whether args[0] is executable
    	else
    		search the command PATH for an executable file called args[0]
    	if (no executable called args[0])
    		print "Command not found"
    	else
    		execute the command
    	print a prompt
    }
    
    1. How can you find what directories are in the PATH?

    2. Describe the search the command PATH process in more detail. What the kinds of system calls would be needed to determine whether there was an executable file in one of the path directories?