CSC322 C Programming and UNIX Stephan Schulz
Total Page:16
File Type:pdf, Size:1020Kb
CSC322 C Programming and UNIX Stephan Schulz Department of Computer Science University of Miami [email protected] http://www.cs.miami.edu/~schulz/CSC322.html Prerequisites: CSC220 or EEN218 Hackers! Hacker [originally, someone who makes furniture with an axe] 1. A person who enjoys exploring the details of programmable systems and how to stretch their capabilities, as opposed to most users, who prefer to learn only the minimum necessary. 2. One who programs enthusiastically (even obsessively) or who enjoys programming rather than just theorizing about programming. 3. A person capable of appreciating hack value. 4. A person who is good at programming quickly. 5. An expert at a particular program, or one who frequently does work using it or on it; as in ‘a Unix hacker’. (Definitions 1 through 5 are correlated, and people who fit them congregate.) 6. An expert or enthusiast of any kind. One might be an astronomy hacker, for example. 7. One who enjoys the intellectual challenge of creatively overcoming or circumventing limitations. 8. [deprecated] A malicious meddler who tries to discover sensitive information by poking around. Hence ‘password hacker’, ‘network hacker’. The correct term for this sense is cracker. The New Hacker’s Dictionary (aka the Jargon File) Stephan Schulz 2 UNIX and You The UNIX Operating System Stephan Schulz 3 UNIX and You The UNIX Operating System Stephan Schulz 4 UNIX and You The UNIX Operating System C Stephan Schulz 5 Our AIM / etc home usr dev bin joe jackjill ls man cat hdaThe mouse mta I n UNIX t e PID: PID: r Operating System n 182 512 e t C Stephan Schulz 6 The Myth UNIX is a big-iron operating system UNIX is complicated UNIX is hard to use UNIX has been created by SUN, IBM, HP, and other large companies UNIX is monolithic Stephan Schulz 7 Counterpoint UNIX was developed on small machines and became popular on the “killer micros”. UNIX dialects now run on everything from a PDA to CRAY supercom- puters UNIX is based on simple and elegant principles (but has added a some cruft over the years) UNIX is not particularly hard to use (compared to the power it gives to the user), but has a reasonably steep learning curve. It’s not a “show-me” operating system, but a “tell me” operating system, UNIX has been created in a research environment, and much of it has been developed in informal settings by hackers. Much of the impetus for UNIX comes from free versions (Linux, Net-, Open-, FreeBSD), although many companies contribute to it’s development Many UNIX kernels are monolithic, but the UNIX system is extremly modular. Stephan Schulz 8 UNIX First portable operating system (NetBSD: 18 processor architecures, ≈ 50 com- puter architecures) Written in a “high-level” language (C) Small (for what it does): – Recent LINUX kernel: 2.4 million LOC (1.4 million for driver, 0.4 million architecture-dependent stuff (16 ports) – Windows 2000: Estimates range from 29 million to 65 million LOC, supports just 1.5 architecures Modular (though often on a monolithic kernel) – Separate windowing system (X) and window managers – Various Desktop-Solutions (CDE, KDE, Gnome) – Toolbox-philosphy: Combine (lot’s of) simple tools – Underneath: Strong and simple abstraction (“Everything is a file”) Stephan Schulz 9 C “Pragmatic” high level language: – Handles characters, numbers, adresses as implemented by most computers – Small core language, much functionality provided by libraries (mostly in C!) – Compilers are easy to write – Compilers are easy to port – Even naive compilers produce reasonably efficent code Hacker-friendly – Straightforward compilation (nothing is hidden) – Compact source code (fewer keystrokes, fast to read) – Typed, but no bondage-and-discipline language Adequate support for building abstractions – Structures (composing objects), unions, enumerations – Arrays and pointer – Support for defining new types Stephan Schulz 10 UNIX history tree (simplified) For a fuller tree see http://www.levenez.com/unix/ Stephan Schulz 11 A Short History of UNIX and C 1969 Ken Thompson wrote the first UNIX (in assembler) on a PDP7 at AT&T Bell Labs, allegedly to play Space Travel 1970 Brian Kernighan coins the name UNIX. The UNIX project gets a PDP11 and a task: Writing a text processing system 1971-72 Creation of C (Dennis Ritchie), UNIX rewritten in C 1972 Pipes arrive, UNIX installed on 10 (!) systems 1975 AT&T UNIX “Version 6” distributed with sources under academic licenses 1976 Ken Thompson in Berkely, leading to BSD UNIX 1977 1BSD release 1978 UNIX “Version 7”, leading to System V (AT&T) Stephan Schulz 12 A Short History of UNIX and C 1978 3BSD, adding virtual memory 1980 Microsoft XENIX brand of UNIX 1982 4.2BSD, adding TCP/IP 1982 SGI IRIX 1983 Bjarne Stroustrup creates C++ (at AT&T Bell labs) 1983 GNU Project announced (Aim: Free UNIX-like system) 1983-1984 Berkeley Internet Name Demon (BIND) created 1984 SUN introduces NFS (Network File System) 1985 Free Software Foundation (Stallman), GNU manifesto, GNU Emacs Stephan Schulz 13 A Short History of UNIX and C 1986 HP-UX, SunOS3.2 (from BSD Unix), “attack of the killer micros” 1986 MIT Project Athena creates X11 (Network window system) 1986 POSIX.1 (Portable operating system interface standard) 1988 GNU GPL 1988 System VR4 “One UNIX to rule them all” (AT&T+SUN) 1988 NeXTCUBE with NeXTSTEP operating system 1989 ANSI-C Standard “C89”(adds prototypes, standard library) 1889 SunOS 4.0x 1990 Net/1 Release (free BSD UNIX) 1990 IBM AIX Stephan Schulz 14 A Short History of UNIX and C 1991 Linux 0.01, “attack of the killer PCs” (continuing till this day) 1991 World Wide Web born 1991–1992 Lawsuits around BSD UNIX Net/1 and Net/2 releases 1992 SunOS 5 aka Solaris-2 (from System VR4) 1993 FreeBSD 1.0 1994 Linux 1.0 1994 NetBSD 1.0, 4.4BSD Lite (unencumbered by AT&T copyrights, becomes new base for all non-commercial BSD flavours) 1995 “UNIX wars” are over 1996 Tux the Penguin becomes Linux mascot Stephan Schulz 15 A Short History of UNIX and C 1998 UNIX-98 branding (Single UNIX specification) 2000 New ANSI “C99” 2001 IBM runs prime time TV ads for Linux 2001 UNIX-based MacOS X 2002 Linux is at version 2.4, Emacs is version 21.2, SunOS is at 5.9 (aka Solaris 9), BIND is version 9.2.1 Stephan Schulz 16 Another Opinion UNIX is not an operating system. but is the collected folklore of the hacker community! Stephan Schulz 17 Spot the Even Ones Stephan Schulz 18 Upshot You don’t have to grow a beard to become a world-class UNIX hacker. but it does seem to help! Stephan Schulz 19 CSC322 C Programming and UNIX UNIX from a User’s Perspective Stephan Schulz Department of Computer Science University of Miami [email protected] http://www.cs.miami.edu/~schulz/CSC322.html UNIX Architecture Application Shell Libraries UNIX Kernel Hardware Stephan Schulz 21 UNIX Architecture Application Shell Libraries UNIX Kernel Hardware Stephan Schulz 22 Some Concepts UNIX is a multi-user system. Each user has: – User name (mine is schulz on most machines) – Numerical user id (e.g. 500) – Home directory: A place where (most of) his or her files are stored UNIX is a multi-tasking system, i.e. it can run multiple programs at once. A running program (with its data) is called a process. Each process has: – Owner (a user) – Working directory (a place in the file system) – Various resources A shell is a command interpreter, i.e. a process accepting and executing commands from a user. – A shell is typically owned by the user using it – The initial working directory of a shell is typically the users home directory (but can be changed by commands) Stephan Schulz 23 More on Users There are two kinds of users: – Normal users – Super users (“root”) Super-users: – Have unlimited access to all files and resources – Always have numerical user id 0 – Normally have user name “root” (but there can be more than one user name associated with UID 0) – Can seriously damage the system! Normal users – Can only access files if they have the appropriate permissions – Can belong to one or more groups. Users within a group can share files – Usually cannot damage the system or other users files! Stephan Schulz 24 The User Interface UNIX: Provide Tools, Not Policy – Most tools operate on all (ASCII) file formats – Extremely configurable environment – different users have different user expe- riences – No restrictions ⇔ Little consistency – We will assume the default environment on the lab machines for examples X Window System: Provide Mechanisms, Not Policy – Windowing system offers (networked) drawing primitives – Different GUIs built on top of this – GUI conventions may even differ from one application to the other! – Modern desktop environments (GNOME/KDE) try to change this, but you are bound to use many legacy applications anyways! Stephan Schulz 25 My Graphical Desktop Stephan Schulz 26 Default KDE Desktop (SuSE Linux) Stephan Schulz 27 Desktop Discussion My Desktop – Uses windowing mostly to provide a better text-based interface (compared to pure text terminals) – Text editor and shell (command line) windows – (Can also run graphical applications) KDE Desktop – Graphical, mouse-based user experience – Mostly a launcher for GUI-based programs ∗ Office prgrams ∗ Graphics programs ∗ Web browser – Can also run shell windows! Stephan Schulz 28 KDE Desktop with Terminal Application Stephan Schulz 29 Exploring the Text Interface Convention: System output is shown in typewriter font, user input is written in bold face, and comments (not to be entered) are written in italics. whoami will print the user name of the current user (more exactly: It will print the first user name associated with the effective user id) [schulz@gettysburg ∼]$ whoami schulz pwd prints the current working directory (more later): [schulz@gettysburg ∼]$ pwd /lee/home/graph/schulz Non-standard setup! ls lists the files in the current working directory: [schulz@gettysburg ∼]$ ls core Desktop Not much there at the moment Stephan Schulz 30 Text Interface Example (contd.) Most UNIX programs accept options to modify they behavior.