What is an Operating System? - define OS: "control program for computer" - OS functions: - allocate resources to users of system: - storage: memory, disk, tape file system printers terminals - schedule process tasks - control access to CPU (another resource) - allow timesharing: single CPU handles multiple task apparently concurrently - CPU only does one thing at a time Evolution of Operating Systems - load and run: - batch: if multiple users, each waits for turn multiple jobs wait in queue for execution - single user interactive, single tasking - single user interactive, multitasking: - multi-user interactive: Why Unix? - portable: written in C, non-proprietary - scalable: runs on PCs through supercomputers - powerful and flexible user interface: shell, GUI, "turnkey" - simple interface for multiple processes from user commands - flexible heirarchichal file system: - strong support for networking and communications - simplicity maintained through I/O redirection and pipes: - chain simple, single-purpose tools to perform complex tasks - large library of user supported and contributed software, all in source form History of Unix: - developed at Bell Labs in 1969 by Ken Thompson - rewritten in C for port to PDP-11 (with help from Dennis Ritchie) - lineage: Version 7 --> System V and BSD Unix in the real world - runs on most computer systems Unix at UNF: System V: 1 4 1 1 12 AT&T 3B15 (unf5) AT&T 3B2/310 (unf1 - unf4) AT&T 3B2/500 (unf7) Sequent Balance (unf6) Sun 386i BSD: 12 Sun 386i 1 Sequent Balance (unf6) Networking Under Unix - TCP/IP: the "standard" Unix network - developed by DARPA - runs on 10-megabit/sec Ethernet - allows: - transparent access to remote files (files on other computers on the network appear "local") - file transfer - electronic mail - remote login - remote command execution - allows network of different machines from different vendors to share resources in a transparent and consistent manner - even IBM The Unix environment: - telling Unix your terminal type - the shell: Bourne shell, Korn shell, C Shell, bash, tcsh - Unix is case sensitive Unix files: - file names - max 14 characters (SysV), 256 characters (BSD) - any characters (even non-printing ones) are legal - all files are just a stream of ASCII characters (bytes) - if text file, lines terminated by NL (ascii 10) - no end-of-file character stored in file Commands to manipulate files: - cat, pg (SysV), more (BSD, less (both), cp, mv, ln, rm, vi The Unix File System - Unix has a heirarchical file system (tree-structured) - subdirectories are just files containing information about other files - current working directory - home directory - specifying files (relative, absolute paths) - . and .. - each file has a unique inode (index node) number - inode contains pointers to data on disk - since inodes are unique, file can have multiple name (names are just mapped to inode - directory entries just contain names and inode numbers - special files: devices (in /dev directory) - character, block devices, fifos Commands to manipulate directories: - ls, cd, pwd, mkdir, rmdir, mv File permissions: - 3 levels: read, write, execute user, group, other - chmod Electronic mail: mail The Shell - the Unix command interpreter: - Command-line syntax: $ command [-options] [arg1 [arg2 [...]]] <return> - command: name of an executable file - each command executing is a process: "Standard" I/O: output I/O Redirection: standard input, standard output, standard error you can "redirect" standard output: $ ls -l >dirfile puts the output of the 'ls' command into the file "dirfile" - if dirfile did not exist, it is created - if dirfile did exist, its previous contents are replaced by the output of the 'ls' command $ ls -l >>dirfile puts the output of the 'ls' command into the file "dirfile" - if dirfile did not exist, it is created - if dirfile did exist, the output of the 'ls' command is appended to it - you can redirect standard input: < > >> Pipes - you can tell one command to use as its input the output from another command: | Filename generation: "wild cards": * ? [...] Unix commands: - where are files: /bin /usr/bin /usr/lbin /usr/local/bin - shell PATH variable: - others: touch, tail, file, chown, chgrp, who, w, lpr, lpq, lprm, date, echo, diff, du, ps, kill, nice, nohup, grep, telnet, rlogin, rsh, rcp, man Background Processing: & - stdout and stderr unchanged, process is "unlinked" from stdin - jobs: list background jobs for this terminal session with brief ID # Miscellaneous shell stuff - Redirecting stderr: 2>, 2>&1 - command separation: ';' separates commands on a line - command grouping - (command-list): runs command-list in subshell - line continuation - '\' at end of line continues the current line - command history and editing Shell Scripts - file containing a list of shell commands - have the same effect as typing the commands in to the shell - sh scriptfile, make executable, . scriptfile - Shell Variables: - all are string variables - unless exported, not passed to subprocesses - all are created when first assigned a value - all are referenced by preceding name with '$' - command substitution: - exporting variables: - environment: $ DATEVAR=`date` the environment A string storage space, a copy of which is given to every process started by the shell - to place a variable in the environment, export it - programs (shell scripts and otherwise) can look at the env. and modify their behavior based on it's contents - many programs use environment variable to "learn" about what the user expects them to do Readonly shell variables: $0, $1 - $9, $*, $#, $$, $? - shift, set Flow control in shell scripts: -test or [ ... ]: More shell stuff: if, while, for, case -f,-d,-r,-w,-x,-n,-z,=,!=,-eq,-ne,etc. echo, read, exit -"here" documents, << - Shell Functions Trapping signals - prevent user from breaking out of shells: trap Word processing under Unix: WYSIWYG vs. embedded command: - runoff, roff, nroff/troff, TeX, LaTeX - Documenter's Workbench - nroff/troff macros: mm, ms, man - preprocessors: tbl, grap, eqn, pic - Writer's Workbench: spelling, style, grammar, readability Writer's Workbench: diction, gram, punct, spellwwb, proofr, wwb Introduction to C programming: First systems-programming language that wasn't machine specific (assemb) A First C program: #include <stdio.h> - include or header files contain info for - compiler about functions used in this - program /* this is a comment */ - standard form for comments, may cross lines main() function { - every program has exactly one main - and this is its definition -- it's the - entry point for the program - body of function enclosed in {} printf("Hello world.\n"); - call to puts function with string as arg } - end of function, end of program - Compiling: $ cc -o exec-name source-name Data in C: char, short, int, long, float, double, void - scalar variables - modifiers: signed, unsigned - sizeof() operator: returns size of object in bytes - statements, expressions, operators - expressions: constants, strings, function calls - expressions have a single value of the appropriate type - lvalue is a special type of expression which can be assigned a value - operators act on expressions - compound statements are enclosed in {} - the null statement is ";" - Constants: constants are expressions, have a value, can be operated on by operators - constants have a type - string constants: any characters in double quotes - "special" characters: \n, \t, \b, \r, \f, \\, \?, \', \", \0 \ooo, \xhh Functions in C: take arguments and return values: - functions have type: default is int - type is the type of the value returned by function - functions must be declared like variables - system and library functions are declared in appropriate header (include) files - function arguments must be declared in "formal argument list" - arguments passed are assumed to match declared type - not checked The printf() function - produces formatted output on stdout - also scanf(), putchar(), getchar() Operators: - arithmetic: + - * / % - assignment: = ++ -- += -= *= /= %= - comparison: == != < > <= >= - boolean: ! || && - bitwise: ~ | & ^ >> << if and while for cast Pointers: & * - NULL - pointer declarations Parameter passing: call by value