cpts 360 (system programming) unit 2: introduction to …bobl/cpts360/u02_introunixlinux/...2....
TRANSCRIPT
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 SciencesWashington State University
Spring, 2018
Bob Lewis WSU CptS 360 (Spring, 2018)
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, 2018)
Unit 2: Introduction to UNIX and Linux
References
� Stevens & Rago, Ch. 1 & 2
� http://www.levenez.com/unix
Bob Lewis WSU CptS 360 (Spring, 2018)
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, 2018)
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, 2018)
Unit 2: Introduction to UNIX and Linux
UNIX Shells
classic shells:� sh
� [Steven]Bourne shell
� Bell Labs
� csh� [Bill Joy] C
shell� UC Berkeley
contemporary shells:� ksh
� [David] Korn shell� AT&T Bell Labs
� bash� “Bourne-again” shell� net-developed
� zsh� ”z shell”� ”...”� huge
� ash� [Kenneth] Almquist shell� teensy� used for diagnostics & small systems
Bob Lewis WSU CptS 360 (Spring, 2018)
aka "dash"
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 directorynames. (aka “current directory” or “current workingdirectory”)
home directory The working directory to which you log in.
root directory The uppermost directory in the directory tree. It isalways named “/”.
Bob Lewis WSU CptS 360 (Spring, 2018)
Unit 2: Introduction to UNIX and Linux
Filenames and Paths
filename a sequence of characters describing a file or directorywithin a directory.Filenames may have restrictions on name lengths andpermittable 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 adirectory).
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, 2018)
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, 2018)
Unit 2: Introduction to UNIX and Linux
Standard Input/Output
UNIX supports:
� standard input (n = 0)(aka stdin)
� standard output (n = 1)(aka stdout)
� standard error (n = 2)(aka stderr)
These are all part of the“standard I/O” package, whichwe’ll study in an upcoming unit.
What do these mean on the shellcommand line? (n and m arenon-negative integers.)
>
>>
<
n>
n>>
n<
|
n>m
n>&m
Bob Lewis WSU CptS 360 (Spring, 2018)
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, 2018)
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, 2018)
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 structs and unions
Bob Lewis WSU CptS 360 (Spring, 2018)
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
Bob Lewis WSU CptS 360 (Spring, 2018)
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, 2018)
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, 2018)
Unit 2: Introduction to UNIX and Linux
UNIX and UNIX-like OSes: A Detailed View
(see chart)
Bob Lewis WSU CptS 360 (Spring, 2018)
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 Windows7)
� 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, 2018)
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, 2018)
RESUME
Unit 2: Introduction to UNIX and Linux
Limits
� Q: How large should I declare an array that will hold a path?Take a look at /usr/include/limits.h.
� Other useful quantities:
CHAR_MIN
CHAR_MAX
INT_MIN
INT_MAX
UINT_MIN
UINT_MAX
LONG_MIN
LONG_MAX
ULONG_MIN
ULONG_MAX
Bob Lewis WSU CptS 360 (Spring, 2018)
Unit 2: Introduction to UNIX and Linux
cpp Feature Test Macros
� _GNU_SOURCE
to enable non-POSIX GNU features:
#define _GNU_SOURCE
#include <whatever>
� _POSIX_SOURCE
to force POSIX compliance:
#define _POSIX_SOURCE
#include <whatever>
� __STDC__
(mainly) to create macros to avoid function prototyping,which you shouldn’t do. You probably won’t use it, but youmight come across it.
Bob Lewis WSU CptS 360 (Spring, 2018)
Unit 2: Introduction to UNIX and Linux
Primitive System Data Types
There are a number of system-defined typedefs (usually) that thePOSIX API uses.
� Example: pid_t� Where is it defined?� (/usr/include is your friend.)� It used to be 16 bits long.� How many processes did that permit?� Why did it change?
� Others:
caddr_t
clock_t
time_t
size_t
off_t
Use these to declare API arguments and return values.
Bob Lewis WSU CptS 360 (Spring, 2018)