CS360 Lecture notes -- Chapter 1 in the book

  • Jim Plank, modified by Jian Huang
  • CS360
  • Directory: ~huangj/cs360/notes/Chap1
  • Lecture notes -- html: http://www.cs.utk.edu/~huangj/cs360/360/notes/Chap1/lecture.html
    This lecture elaborates on some of the stuff in chapter 1 of the book.

    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%    /blugreen/homes
    -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 ~huangj/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 ~huangj/cs360/notes/Rbtree-1
    UNIX> pwd
    /home/huangj/cs360/notes/Rbtree-1
    UNIX> cd .
    UNIX> pwd
    /home/huangj/cs360/notes/Rbtree-1
    UNIX> cd ..
    UNIX> pwd
    /home/huangj/cs360/notes
    UNIX> cd ../../..
    UNIX> pwd
    /home
    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.
  • ~huangj/cs360/notes/Rbtree-1/mysort.c -- this is a program that needs to be compiled in order to execute it.
  • /blugreen/homes/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 ~huangj/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/huangj/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/huangj/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/huangj/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/huangj/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".

    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:

    #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);
        sleep(5);
      }
    }
    
    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.

    Yes, signal is a part of the ANSI C standard, it should perform the same (or, very similarly) on all systems claiming to support ANSI'87. The primary differences stem from which additional signals, beyond the basic set of signals, are supported on different systems. The basic set specified by ANSI C standard is rather small.