CS360 Lecture notes -- Some Basic Terminology

  • James S. Plank
  • CS360
  • Directory: /home/plank/cs360/notes/Chap1
  • Lecture notes -- html: http://www.cs.utk.edu/~plank/plank/classes/cs360/360/notes/Chap1/lecture.html

    Filesystems

    Definition of a ``filesystem'': A hierarchical arrangement of directories.

    In Unix, the root file system starts with "/". However, there are other subfilesystems, that are part of the root one. To see the filesystems on your machine, type "df". You'll see something like:

    UNIX> df
    Filesystem            kbytes    used   avail capacity  Mounted on
    /dev/sd0a              11167    5952    4099    59%    /
    /dev/sd0g             140591   84016   42516    66%    /usr
    /dev/sd0h             171959  141421   13343    91%    /home
    -memory-               55720       0   55720     0%    /tmp
    plank:(pid130)             4       4       0   100%    /amd
    galoob:/export/sun4-sos4_1
                          335803  313356       0   100%    /a/galoob/export/sun4-sos4_1
    cs:/var/spool/mail    221351  122607   76609    62%    /a/cs/var/spool/mail
    alphard:/canary/homes
                          411687  293685   76833    79%    /a/alphard/canary/homes
    UNIX>
    
    The last entry on each line in this denotes a different filesystem. You'll note that all are subdirectories of "/". Each filesystem resides on a different disk or "disk partition".
  • Filename: The name of a file as it appears in a directory.
  • Pathname: A sequence of zero or more filenames, separated by slashes.

    When you say "ls -a", it lists all the filenames in the current directory:

    UNIX> cd ~plank/cs360/notes/Rbtree-1
    UNIX> ls -a
    .               jh.c            makefile        mysorti.c       mysortu2.c
    ..              lecture         mysort.c        mysortu0.c      mysortu3.c
    README          lecture.html    mysort2.c       mysortu1.c      randfile
    UNIX>
    
    (If you don't say "-a", it will not list filenames beginning with ".") Note there are two filenames that are in every directory: "." and ".."

    "." is the current directory. ".." is the parent of the current directory.

    The pwd command tells you the complete pathname of the current directory. The cd command moves you between directories:

    UNIX> cd ~plank/cs360/notes/Rbtree-1
    UNIX> pwd
    /home/plank/cs360/notes/Rbtree-1
    UNIX> cd .
    UNIX> pwd
    /home/plank/cs360/notes/Rbtree-1
    UNIX> cd ..
    UNIX> pwd
    /home/plank/cs360/notes
    UNIX> cd ../../..
    UNIX> pwd
    /home
    UNIX> cd ././././././.
    UNIX> pwd
    /home
    UNIX> cd ..
    UNIX> pwd
    /
    UNIX> cd ..
    UNIX> pwd
    /
    UNIX> cd ..
    UNIX> pwd
    /
    UNIX> 
    
    Note that the parent directory of "/" is itself.
  • Absolute Pathname: A pathname starting with a slash.
  • Relative Pathname: A pathname not starting with a slash.
  • Working Directory: The directory to which relative pathnames are relative.
  • Home Directory: The working directory of a user when he or she first logs in.

    I/O

    We'll go over I/O in detail later.

    Programs and processes:

  • Program: A file that can be executed, either directly, or through the aid of interpreters, compilers, and/or linkers.
  • Process: An executing instance of a program.
  • Process ID: The number given to a process by the operating system.

    Examples of programs:

  • /bin/ls -- this is a program that can be executed directly.
  • /usr/ucb/vi -- this is a program that can be executed directly.
  • /home/plank/cs360/notes/Rbtree-1/mysort.c -- this is a program that needs to be compiled in order to execute it.
  • /home/plank/bin/calc -- this is a shell script -- it is a program that needs to be interpreted by /bin/sh.

    When you run a program, its executing instance is called a process.

    For example, suppose you type "vi /home/plank/cs360/notes/Rbtree-1/mysort.c" in one window. This executes the vi program, found in /usr/ucb/vi. The executing instance is called a process. If you go to another window, and type "ps x", it will list all of the processes that you are currently executing:

    UNIX> ps x
     1394 p0 IW    0:00 xclock -geometry 100x100-0-0 -update 60
     1416 p0 S     0:01 twm
     1436 p1 IW    0:00 -sh (csh)
     1427 p2 IW    0:00 -sh (csh)
     1428 p3 IW    0:00 -sh (csh)
     1443 p5 S     0:01 -sh (csh)
     2328 p5 S     0:00 vi /home/plank/cs360/notes/Rbtree-1/mysort.c
     1444 p6 IW    0:00 -sh (csh)
     2329 p6 R     0:00 ps x
    UNIX>
    
    Note that we can run more than one vi process at the same time. Go to another window and type "vi /home/plank/cs360/notes/Rbtree-1/mysort2.c". Now when you type "ps x", you'll see the second process.
    UNIX> ps x
     1394 p0 IW    0:00 xclock -geometry 100x100-0-0 -update 60
     1416 p0 S     0:01 twm
     1436 p1 IW    0:00 -sh (csh)
     1427 p2 S     0:00 -sh (csh)
     2330 p2 S     0:00 vi /home/plank/cs360/notes/Rbtree-1/mysort2.c
     1428 p3 IW    0:00 -sh (csh)
     1443 p5 IW    0:01 -sh (csh)
     2328 p5 IW    0:00 vi /home/plank/cs360/notes/Rbtree-1/mysort.c
     1444 p6 S     0:00 -sh (csh)
     2331 p6 R     0:00 ps x
    UNIX>
    
    You should understand the distinction between process and program. The number in the first column of the ps command is the "process id".

    Error Handling

    Usually when an error occurs in a Unix system or library call, a special return value comes back, and a global variable "errno" is set to say what the error is. For example, suppose you try to open a file that does not exist: (This is in ch1a.c).

    #include <stdio.h>
    #include <errno.h>
    
    main()
    {
      int i;
      FILE *f;
    
      f = fopen("/home/plank/noexist", "r");
      if (f == NULL) {
        printf("f = null.  errno = %d\n", errno);
        perror("f1");
      }
    }
    
    

    ch1a.c tries to open the file /home/plank/noexist for reading. That file doesn't exist. Thus, fopen returns NULL (read the man page for fopen), and sets errno to flag the error. When you run the program, you'll see that errno was set to 2. To see what that means, you can do one of two things:

    Suppose I create /home/plank/noexist, and do chmod on it so that I can't open it for reading. Then ch1a will print a different error:
    UNIX> echo "" > /home/plank/noexist
    UNIX> chmod 0 /home/plank/noexist
    UNIX> ch1a
    f = null.  errno = 13
    /home/plank/noexist: Permission denied
    UNIX> rm -f /home/plank/noexist
    UNIX> ch1a
    f = null.  errno = 2
    /home/plank/noexist: No such file or directory
    UNIX> 
    
    This is the standard unix interface for errors. Learn to use perror.

    User Identification

  • User ID: The number given to each user by the system administrator.

    There are two ways that the system deals with identification: User Id's and group id's. We won't talk much about group id's here. Your user id can be gotten by the system call "getuid()" -- read the man page.

    To print out your UID, try ch1b.c: ch1b.c

    #include <stdio.h>
    
    main()
    {
      printf("%d\n", getuid());
    }
    
    


    Signals

  • Signal: An interruption of the program.
  • Signal Handler: The mechanism by which the program may gracefully deal with signals.

    Signals are a way for asynchronous events to occur in a program. For example, compile and run the program ch1c.c:

    #include <stdio.h>
    
    main()
    {
      int i;
    
      i = 0;
      while (1) {
        i++;
        printf("%d\n", i);
        fflush(stdout);
        sleep(1);
      }
    }
    
    

    This program implements a counter that increments itself every 5 seconds. Let it run for a few seconds, and then type < CNTL-Z >. This sends the "STOP" signal to the program, which stops it. You'll be back at the shell now. If you type "ps", you'll see something like:

      2483 p5 T 0:00 ch1c
    
    The "T" means that the process is not running -- it is stopped. To start it, you can type "fg", which will send it the "START" signal.

    Now, while it's running, type < CNTL-C > to kill the program -- this sends it the "INT" signal, which teminates it. Segmentation faults are also signals. You can write your programs to handle signals any way you please. This is a more advanced programming technique, which we will deal with later in the semester.