CPS 250, 346, & 444/544 Lecture notes: Introduction to UNIX



Coverage: [UPE] Chapter 1, and [USP] Chapter 1 (pp. 3-20) and Appendix A.1 (pp. 797-800)


What is UNIX?

an operating system

what is an
operating system?


Hallmarks of UNIX

  • time-shared,
  • multi-user,
  • portable (written in C),
  • accessible (nohup, dump process table),
  • interactive,
  • text-based,
  • terse,
  • efficient,
  • silent, and
  • free!


Historical perspective

  • originally systems programs were written in assembly language
  • research in the 1960's lead to BCPL and then C
  • UNIX developed in the late 1960's (Ken Thompson, 1969, Bell Labs, successor to MIT's Multics)
  • UNIX rewritten in C in the early 1970's
  • C is a `low' high-level programming language; WYSIWYG (What You See Is What You Get)
  • the marriage of UNIX in C provided an ideal environment for systems programming
  • the majority of systems programming today is still done in UNIX and C


The UNIX philosophy

    (communication)
  • model: compose a solution to a problem by combining several small, atomic programs in creative ways through interprocess communication and interoperability mechanisms, such as pipes
    • atomic programs are the building blocks
    • communication mechanisms are the glue
    • such program are easier to develop, debug, and maintain than large, all-encompassing, monolithic systems
    • `If you give me the right kind of Tinker Toys, I can imagine the building. I can sit there and see primitives and recognize their power to build structures a half mile high, if only I had just one more to make it functionally complete' -- Ken Thompson, creator of UNIX and the 1983 ACM A.M. Turing Award Recipient, quoted in IEEE Computer 32(5), 1999.
  • (concurrency)
  • processes can clone themselves (through fork)
    • why would you want to do this?
    • think of programs you use everyday
    • turns out to be an incredibly powerful and useful primitive
  • uniform style of I/O

  • we will see these themes recur throughout the course


History of UNIX and C


  • 1967
    • Martin Richards develops BCPL as a language for writing operating systems and compilers
    • Ken Thompson develops B, which B evolved from BCPL, at AT&T Bell Laboratories in Murray Hill, NJ
    • both B and BCPL were typeless languages (i.e., every data item occupied one word in memory)
  • 1969
    • Ken Thompson used B to develop early version of the UNIX operating system on a DEC PDP-7 computer at Bell Labs in Murray Hill, NJ.
    • UNIX evolved from Multics, also at Bell Labs
    • B became widely known as the development language of the UNIX OS
  • 1972
    • Dennis Ritchie wrote a C compiler at Bell Labs
    • evolved from B
    • originally implemented on a DEC PDP-11 computer
    • considered a hybrid between a low-level language and a high-level language; gives programmer facilities to allocate and manipulate memory
    • excellent for writing systems programs (e.g., compilers), but for other programs C is not the best choice
      • it does not babysit the programmer with several automatic checks
      • no training wheels (no undelete)
  • 1973: Dennis Ritchie helped Thompson port UNIX to a DEC PDP-11; they rewrote the UNIX kernel in C
  • 1974
    • they licensed UNIX to colleges and universities for educational purposes
      • major role in the development of UNIX and C
      • `four-year effect'
    • later UNIX become available for commercial use
    • Computer "Systems" Research Group @ UC Berkeley (UCB) made significant additions and changes
    • UNIX developers split into two camps
      • UCB camp (west coast)
        • resulted in BSD (Berkeley Software Dist), 4.xBSD Berkeley UNIX, Ultrix (DEC's UNIX, based on BSD 4.2), SunOS, FreeBSD (based on 4.4BSD-Lite)
        • vi editor
      • AT&T Bell Labs & UNIX Systems Laboratories (USL) camp (east coast): resulted in SVR3, SVR4 (developed jointly with USL ans Sun Microsystems), USL's SVR5, Solaris 2.0
      • trying to merge today, want a more standard version, ongoing work on POSIX
  • C evolved into C++ (the ++ creates a cute pun!)
  • today virtually all new major OS's are written in C/C++
  • UNIX is not an acronym, but a weak pun on Multics -- the OS Thompson and Ritchie worked on before UNIX


UNIX architecture



  • hardware
  • kernel
  • shell (e.g., sh)
  • compilers
    • gcc - provides a virtual C computer
    • g++ - provides a virtual C++ computer
  • programs and applications (e.g., cat, wc, sed, awk)
  • X-windows system


Login/logout

  • UNIX®: a multiuser, multitasking, hierarchical file structure operating system (ref. [PGUS])
  • hallmarks of UNIX: time-shared, multi-user, portable (written in C), accessible, interactive, text-based, terse, efficient, silent, free!
  • logging on
    • enter your login name (echoed to screen), hit <enter, enter your password (not echoed), hit <enter
      (some UNIX systems will initially require the user to establish a password for a new account)
    • the password are changed by execution of the passwd utility program.
  • logging out: hit <ctrl-d> or enter exit


Accessing your UNIX account

  • ssh'ing
  • login process
    • login name echo'ed
    • password not echo'ed
    • if you enter an invalid string for either, the system will not indicate which was invalid
  • concept of the shell: your interface to the system
  • ls'ing, clear, and banner
  • some system status commands: date, hostname, whoami (or logname), who, w, uptime (when was the system last rebooted), uname, and uname -a


General syntax of UNIX commands

    <command> [<options>] [<argument(s)>]
    
  • examples:
       $ ls
       $ ls -l (total # of blocks; block = 1/2k (512 bytes))
       $ ls -l myfile
       $ ls -ld mydir
       $ ls -F mydir
       $ ls -al
       $ ls -a -l (POSIX)
       $ ls -l -d mydir
    
  • command names (like filenames) are case sensitive


Getting help on the UNIX system

  • for a help on a particular command, use man <command>
    • man retrieves the manpage (manual page) for any command (and functions in C libraries)
    • for instance, man wc, man -s 3C printf, or man fgetc
    • man man (eerie self-referential command)
  • for all commands on a general topic, use apropos
    • apropos <keyword/topic> (e.g., apropos copy)
    • apropos = man -k
  • whatis = man -f <title>
  • manpage can be searched with /<keyword/topic>
  • man printf (which section?)
  • use man -a printf (all)
  • man -s 2 fork
  • man -s 3 intro


UNIX manual

  • Ch1: Commands
  • Ch2: System Calls
  • Ch3: Libraries (portable, meet a standard C specification)
  • Ch4: File Formats
  • Ch5: Misc Facilities, macros
  • Ch6: Games
  • Ch7: Devices and Networking
  • Ch8: System Maintenance
  • Ch9: Device Drivers


UNIX standards

  • POSIX (Portable Operating System Interface)
  • IEEE standard for UNIX libraries to promote the development of reliable software
  • Solaris, Mac OS X, and many other flavors of UNIX are moving toward POSIX standards; POSIX threads


Introduction to the vi editor


vi philosophy

  • editors like vi and emacs are editors for programmers and power-users; they were designed for people who want to be extremely efficient and productive in their work
  • why do the h,j,k,l keys correspond to left, down, up, right, resp.? because it is quicker for the typist to reach the h,j,k,l keys than the arrow keys
  • we study vi since it is the only editor guaranteed to exist on all UNIX systems
  • there is a steep learning curve, but the increase in productively is worth the investment
  • vi is a moded-editor; there are two modes: insert (or editing) mode and command mode; you can enter insert mode in one of multiple ways (e.g., by hitting the 'i' key or the 'o' key), but there is only one way enter command mode (i.e., hit the <esc> key)
  • vi is built on top of ex and ex is built on top of ed (the original UNIX line editor); hitting : while in command mode permits the user to enter ex commands
  • general syntax for commands:
    vi -- [n]operator [m] object
    ex -- :[address] command [options]
    


vi editor

  • quick reference guide
  • approaches to studying: memorize commands or learn/know general syntax
  • ed ⊂ ex ⊂ vi
  • general syntax of ex commands: :[address]command[options] (e.g., :g/^$/d (deletes all blank lines))
  • general syntax of vi commands: [n]operator[m]object
    • 3x (delete 3 characters)
    • d^ (delete back to beginning of line)
  • view <file(s)> (open file(s) as read-only)
  • vi +n <file(s)>
  • syntax form: ... ?
  • :set showmode
  • :set noshowmode
  • :set number (or :set nu for short)
  • :set nonumber (or :set nonu for short)
  • :801 (go to line 801)
  • :set ruler showmode showmatch
  • :set ts=3 expandtab
  • :set wrapscan wrapmargin=1 (for wordwrap)
  • :set list (display each TAB as ^Is and EOLs as $)
  • :set nolist
  • :r <file> (reads file with filename <file> into buffer)
  • :r !<command> (reads standard output of <command> into buffer)
  • search and replace: :%s/search_text/replacement_text/g (same as 1,$s/RE/replacement_text/g); examples:
    • :%s/Alice/Lucy/g (the g makes it global, i.e., replace all occurrences, not just the first, on each line)

    • :%s/fprintf/FPRINTF/g (replaces all occurrences of fprintf with FPRINTF)

  • notice how the following commands follow the general syntax above:
    • x key deletes a single character
    • dd (delete 1 line)
    • 3dd (delete 3 lines)
    • yy (yank)
    • p (paste)
    • 3p (paste 3 lines)
  • <crtl-u> (pageup)
  • <crtl-d> (pagedown)
  • <crtl-l> (repaints the screen)
  • we will go a lot deeper into vi as the course progresses


Text editing: vi

  • to enter vi, vi <filename(s)> (if file does not exist, vi creates it)
  • vi can be in one of the following three modes:
    • vi command mode (for cursor movement and text deletion)
    • vi input mode (for entering text)
    • vi ex mode (all commands starting with a colon)
  • when vi is first loaded, it is in command mode (i.e., all keystrokes entered are interpreted as commands)
  • to return to command mode from input mode, hit <esc>
  • see the vi notes for more commands and details
  • the following commands put vi in input mode; table gives the position where the text is inserted

      description insert text
      before cursor i
      at beginning of line I
      after cursor a
      at end of line A
      after current line o
      before current line O

  • vi reference table (in command mode)

      description command code
      move one space to the right space, l, or right arrow
      move one space to the left h, or left arrow
      move down one line j, or down arrow
      move up one line k, or up arrow
      move one word to the right w, or W
      move one word to the left b, or B
      move to beginning of line 0
      move to end of line $
      move to top of screen H
      move to middle of screen M
      move to bottom of screen L
      save contents to file :w
      quit file :q
      quit vi, saving file only if changes were made :x
      save file and quit vi :wq
      save contents to file and quit vi ZZ
      toggle between uppercase and lowercase ~
      delete back one character X
      delete character under cursor x
      delete line dd
      delete word dw


References

    [CPL] B.W. Kernighan and D.M. Ritchie. The C Programming Language. Prentice Hall, Upper Saddle River, NJ, Second edition, 1988.
    [PGUS] M.G. Sobell. A Practical Guide to the UNIX System. Addison-Wesley, Reading, MA, Third edition, 1995.
    [UPE] B.W. Kernighan and R. Pike. The UNIX Programming Environment. Prentice Hall, Upper Saddle River, NJ, Second edition, 1984.
    [USP] K.A. Robbins and S. Robbins. UNIX Systems Programming: Concurrency, Communication, and Threads. Prentice Hall, Upper Saddle River, NJ, Second edition, 2003

Return Home