Unit 2: Introduction to UNIX and Linux CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux Bob Lewis School of Engineering and Applied Sciences Washington State University Spring, 2020 Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Motivation ◮ APIs have a history: Learn from it. ◮ What do OSes really do? ◮ What happens when... ◮ a system boots? ◮ you log in? ◮ you log out? ◮ a system shuts down? ◮ What general facilities does the OS provide the programmer? ◮ Note in passing: What influenced OS design decisions? Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux References ◮ Stevens & Rago, Ch. 1 & 2 ◮ http://www.levenez.com/unix Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Logging In and Out What happens when you log in on a console? 1. init(1) prompts for your login, passing it to... 2. login(1) prompts for your password ◮ if unsuccessful, login(1) exits and control returns to (1) ◮ if successful, login(1) ... ◮ cd’s to your new directory ◮ starts up your shell What happens when you log in on a display? ... and when you log out? Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Contents of /etc/passwd ◮ name ◮ password (encrypted – why?) ◮ UID ◮ GID ◮ comment (usually full name, phone, etc.) ◮ home directory ◮ login shell Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux UNIX Shells classic shells: contemporary shells: ◮ sh ◮ ksh ◮ [Steven] ◮ [David] Korn shell ◮ AT&T Bell Labs Bourne shell ◮ Bell Labs ◮ bash ◮ csh ◮ “Bourne-again” shell ◮ [Bill Joy] C ◮ net-developed shell ◮ zsh ◮ UC Berkeley ◮ ”z shell” ◮ ”...” ◮ huge ◮ ash ◮ [Kenneth] Almquist shell ◮ teensy ◮ used for diagnostics & small systems Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Files and Directories file A named collection of bytes residing in a directory. Under UNIX, it’s always byte-, not record-oriented. directory A collection of files and other directories. working directory The directory used to interpret relative directory names. (aka “current directory” or “current working directory”) home directory The working directory to which you log in. root directory The uppermost directory in the directory tree. It is always named “ / ”. Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Filenames and Paths filename a sequence of characters describing a file or directory within a directory. Filenames may have restrictions on name lengths and permittable characters. Q: What two characters cannot appear in a filename? (With one exception.) path a sequence of one or more filenames joined by “ / ”s. absolute path a path that begins with “ / ” (which is the name of a directory). relative path a path that does not begin with “ / ”. Q: What’s “ . ”? Is it absolute or relative? Q: What’s “ .. ”? Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Example: A Small Directory Tree / home bin usr etc bobl bash ls cat lib passwd Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Standard Input/Output UNIX supports: What do these mean on the shell command line? ( n and m are ◮ standard input ( n = 0) non-negative integers.) (aka stdin ) > ◮ standard output ( n = 1) (aka stdout ) >> ◮ standard error ( n = 2) < (aka stderr ) n > These are all part of the n >> “standard I/O” package, which n < we’ll study in an upcoming unit. | n > m n >& m Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Programs and Processes program an executable file process a running program Big deal in UNIX: process control Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Oh, Say, Can You C? You can’t talk about UNIX without mentioning C. ◮ A C compiler is a given for all UNIX OSes. ◮ Original C was by Brian Kernighan and Dennis Ritchie (hence “K & R”) at Bell Labs from 1969 to 1973. ◮ ANSI (in 1989, hence“C89”) and ISO (in 1990, hence “C90”) standards are identical. “ANSI C” is common usage. ◮ ANSI C New Features: ◮ function prototypes (borrowed from C++) ◮ generic pointers (void *) ◮ international character sets (ISO 8859) ◮ very portable (arguably more so than C++) ◮ ISO/IEC 9899:1999 (hence “C99”) added: ◮ inline functions ◮ new types ( long long int , complex ) ◮ vararg macros ◮ // comments and finally ... Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux C11 ◮ alignment control ◮ _Generic() : selects expressions based on type ◮ multi-threading support ◮ better Unicode support ◮ gets() goes away (and it’s about time!) ◮ bounds-checking ◮ static assertions that know about types at compile time ◮ anonymous struct s and union s Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux So What’s C Good For? ◮ not just UNIX ◮ OS kernels ◮ drivers ◮ debuggers ◮ embedded systems ◮ compilers (e.g. Haskell, C++ (originally)) ◮ interpreters (e.g. Perl, Python) ◮ practically anything else, if the design is good Take a look at ◮ http://www.tiobe.com/tiobe-index ◮ http: //www.toptal.com/c/after-all-these-years-the-world-is-still-powered-by-c-programming Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux Other UNIX Entities ◮ user/group ID’s ◮ positive integers ◮ uniquely identify a user or group ◮ signals ◮ notify process that some error has occurred ◮ may have “handlers” ◮ time values ◮ time-of-day ◮ CPU usage ◮ elapsed time (time interval) Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux UNIX vs. UNIX-like OSes ◮ Major UNIX implementations: ◮ SVr4 ◮ 4.4BSD ◮ FreeBSD (for Intel) ◮ NetBSD (for all platforms) ◮ OpenBSD (secure) ◮ Major UNIX-like implementations: ◮ Linux ◮ MacOS X Darwin = FreeBSD running on Mach microkernel (Which is?) ◮ Solaris But how many UNIX and UNIX-like OSes do you think there are? Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux UNIX and UNIX-like OSes: A Detailed View (see chart) Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux UNIX System Calls ◮ two kinds of (system) library: ◮ static ◮ dynamic (most system libraries are this) ◮ POSIX Standard ◮ IEEE Std. 1001.1-1989 (a.k.a. ISO/IEC 9945) ◮ includes commands and the API (threads, real-time, IPC, etc.) ◮ supported just about everywhere, except Windows, ◮ Interix environment subsystem (up to and including Windows 7) ◮ deprecated in Windows 8 ◮ alternatives: Cygwin (separate library), MinGW (built-in) ◮ this class mostly concerns the API (POSIX.1[abc]) Some common POSIX programming features... Bob Lewis WSU CptS 360 (Spring, 2020)
Unit 2: Introduction to UNIX and Linux POSIX Error Handling If a system call returns a negative value (usually, but not always, -1), something went wrong. Look in the global errno for details. ◮ To get errno : #include <errno.h> ◮ To get the string associated with errno : #include <string.h> char *strerror(int ernnum); ◮ To print an error string: #include <errno.h> void perror(const char *msg); prints “ msg : error string ” on stderr . Remember: errno is set when an error occurs, but never cleared. Bob Lewis WSU CptS 360 (Spring, 2020)
Recommend
More recommend