CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux - - PowerPoint PPT Presentation

cpts 360 system programming unit 2 introduction to unix
SMART_READER_LITE
LIVE PREVIEW

CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux - - PowerPoint PPT Presentation

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)


slide-1
SLIDE 1

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)

slide-2
SLIDE 2

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)

slide-3
SLIDE 3

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)

slide-4
SLIDE 4

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)

slide-5
SLIDE 5

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)

slide-6
SLIDE 6

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, 2020)

slide-7
SLIDE 7

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)

slide-8
SLIDE 8

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)

slide-9
SLIDE 9

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)

slide-10
SLIDE 10

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, which we’ll study in an upcoming unit. What do these mean on the shell command line? (n and m are non-negative integers.) > >> < n> n>> n< | n>m n>&m

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-11
SLIDE 11

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)

slide-12
SLIDE 12

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)

slide-13
SLIDE 13

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, 2020)

slide-14
SLIDE 14

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)

slide-15
SLIDE 15

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)

slide-16
SLIDE 16

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)

slide-17
SLIDE 17

Unit 2: Introduction to UNIX and Linux

UNIX and UNIX-like OSes: A Detailed View

(see chart)

Bob Lewis WSU CptS 360 (Spring, 2020)

slide-18
SLIDE 18

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)

slide-19
SLIDE 19

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)