CPS 250, 356, & 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

  • multiuser,
  • preemptive multitasking (time-shared),
  • interactive,
  • portable (written in C),
  • accessible (nohup, dump process table),
  • 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 programs 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 Distribution), 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
  • 1983: Ken Thompson and Dennis Ritchie are given the ACM A.M. Turing Award for contributions to OS theory and the implementation of UNIX
  • 1987: AT&T Bell Labs and Sun Microsystems wanted to merge BSD and System V which resulted in SVR4 (developed jointly by USL and Sun)
  • Sun developed Solaris 2.0
  • trying to merge today, want a more standard version, ongoing work on POSIX
  • UNIX history and timetime
  • C evolved into C++ (the ++ creates a 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


Conceptual 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, preemptive multitasking (time-shared), hierarchical file structure operating system
  • 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
  • bc -l (an arbitrary precision calculator)
  • some system status commands: date, hostname, whoami (or logname), who, w, uptime (when was the system last rebooted), uname and uname -a, ulimit and ulimit -a (ulimit is a shell builtin), ps and ps -a, and top and htop


General syntax of UNIX commands

    <command> [<options>] [<argument(s)>]
    
  • examples:
       $ ls
       $ ls -l # displays total # of blocks; block = 1/2k (512 bytes)
       $ ls -l myfile
       $ ls -ld mydir
       $ ls -F mydir
       $ ls -al
       $ ls -a -l # POSIX compliant
       $ 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, C library function, or system call
    • for instance, man wc, man -s 3C printf, man fgetc, man fork, or man man (a 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
$ man man
           The table below shows the section numbers of the manual followed by the
           types of pages they contain.
    
    
           0   Header files (usually found in /usr/include)
           1   Executable programs or shell commands
           2   System calls (functions provided by the kernel)
           3   Library calls (functions within program libraries)
           4   Special files (usually found in /dev)
           5   File formats and conventions eg /etc/passwd
           6   Games
           7   Miscellaneous (including macro  packages  and  conven-
               tions), e.g. man(7), groff(7)
           8   System administration commands (usually only for root)
           9   Kernel routines [Non standard]
    


POSIX: UNIX standards

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


Introduction to the vi editor and philosophy


vi editor examples

  • 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)
    • rc (replace char with c)
  • :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)

    • :%s/fprintf/FPRINTF/gc (replaces all occurrences of fprintf with FPRINTF, requires confirmation)

  • we will go a lot deeper into vi as the course progresses


vi editor reference


References

    [CPL] B.W. Kernighan and D.M. Ritchie. The C Programming Language. Prentice Hall, Upper Saddle River, NJ, Second edition, 1988.
    [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