unix concepts - bloomsburg university of...

41
UNIX Concepts COMPSCI 386

Upload: tranhuong

Post on 06-Feb-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

UNIX Concepts

COMPSCI 386

Page 2: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Topics

● History of C and UNIX

● The GNU Project

● Linux

● Command-Line Basics

● UNIX-Style File System

Page 3: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Multics Multiplexed Information and Computing Service

● Time-sharing system for mainframes (1965)

● virtual memory and other modern OS features● MIT, Bell Labs, and General Electric

● First (?) OS written in a high-level language (PL/1). Influential but not a commercial success.

● Ken Thompson and Dennis Ritchie worked on the project until Bell Labs dropped out. (Thompson at Google since 2006, where he co-invented Go.)

● They continued on their own. Kernighan and others at Bell Labs joined the effort. Designed UNIX for DEC PDP-7. Later reimplemented on the PDP-11.

Page 4: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

UNIX

● The goal was to create a time-sharing OS with a simple UI and simple components that can be arranged and composed in a variety of ways.

● Philosophy in a nutshell: modularity.

– Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface. (McIlroy)

– The 17 Rules

● Originally written in assembly, but a high-level language would be needed for its proper development.

Page 5: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

C

● First, Ken Thompson invented B.– a typeless, stripped-down version of BCPL

– compiler squeezed into 8K bytes of memory

● Then C was born as B-with-types.

● Easy to retarget C compiler:– made UNIX highly portable

– sparked its prominence in industry and academia

● C was standardized by ANSI in 1989.

Page 6: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

C

● Why are almost all operating systems written in C?● Speed

– Code maps efficiently to machine instructions.● no threads, templates, nested functions, function or operator

overloading, exception handling; limited support for OOP.– Not much runtime support is needed.

● no dynamic type checking● no bounds checking or garbage collection

● Low-level access to memory (and hardware) thanks to weakly typed pointers

Page 7: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

The GNU Project

● UNIX was proprietary, licenced to universities and corporations by A&T.

● In 1984, Richard Stallman (* * *) at the MIT AI Lab launched the GNU project to develop a free UNIX-like OS.

● He wrote gcc, emacs, and other GNU tools that are still in widespread use.

● Created the Free Software Foundation to sponsor GNU development and pursue free software ideals.

Page 8: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Open Source

● Free software: like free speech or free beer?– The freedom to view, copy, modify, and redistribute source

code without having to pay royalties or other fees.

● Open source movement– Best way to produce safe and reliable software?

– Enlist the cooperation of developers from around the world in a free exchange of ideas, opinions, and code.

● Stallman wrote the first copyleft licence (GNU GPL)

Page 9: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Linux

● GNU's kernel was never finished.● Linux released in 1991.

– The goal was to create a UNIX-like OS for PC.

– Just a kernel, but can be packaged with GNU tools and libraries for complete OS, called properly GNU/Linux.

● There are now hundreds of distributions.– Linux kernel with GNU tools and libraries, third-party

software, package manager, desktop environment.

Page 10: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

User Interfaces

● batch, shell (CLI), GUI● Two strategies for implementing a shell:

– Interpret and execute each command. (DOS) ● If user types rm myfile.txt, shell makes necessary system calls.

– Does not interpret the command. Just invokes a system program to perform the work. (UNIX)

● If user types rm myfile.txt, the shell invokes a system program named rm with myfile.txt as a parameter.

● Two major advantages to this approach.

Page 11: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Bourne Shell (1977)

● /bin/sh● expressive scripting language ● can use scripts as filters● I/O redirection and pipes● globbing and command substitution● scripts invoked as commands by their filename● synchronous/asynchonous execution of commands

Page 12: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

C Shell (1979)

● /bin/csh (or improved version: /bin/tcsh)

● scripting language with C-like syntax

● filename and command completion

● history, aliases, and other features for interactive use

Page 13: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Korn Shell (1983)

● /bin/ksh

● job control

● command aliasing

● command history

● better for interactive work than C shell, but had to be licensed from AT&T

Page 14: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Bash Shell (1989)

● /bin/bash

● Bourne-Again SHell

● Written for GNU Project

● Default shell for Linux and macOS

Page 15: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Command-Line Basics

● What shell am I using?

● Navigating the file system

● Viewing and finding files

● File permissions

● Copying and moving files

● Grepping

● Pipes and redirection

● Hard links and soft links

Page 16: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

BASH Variables

● Environment variables– inherited by child processes

– /etc/bash.bashrc (for interactive non-login shells)

– SHELL, USER, HOME, PATH, PWD

● Shell variables– not inherited by child processes

– ~/.bashrc (for interactive non-login shells)

– PS1, BASH_VERSION, HISTSIZE

Page 17: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

What Shell Am I Using?

> printenv SHELL/bin/bash

> echo $SHELL/bin/bash

> ps $$PID TTY STAT TIME COMMAND3877 pts/1 Ss 0:00 /bin/bash

> ps -p $$PID TTY TIME CMD3877 pts/1 00:00:00 bash

We can use ps to obtain info about all processes in the system.

Page 18: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Navigating the File System

● Listing directory contents with ls– options lapF

– man ls

– pwd

– cd

– ls .. ls ../.. ls / ls ~

● Change to parent, root, and home without specifying absolute paths.

Page 19: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Filesystem Hierarchy Standard

● Defines directory structure for UNIX-like systems.● Most Linux distributions are FHS-compliant.

Page 20: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Navigating the File System

● /bin (command binaries)

● /boot (files used by the kernel during start-up)

● /dev (device files)

● /etc (system configuration files)

● /home (home directories for individual users)

● /lib (shared libraries and kernel modules)

● /lost+found (recovered files, as after system crash)

● /media (mount point for removable media)

Page 21: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Navigating the File System

● /mnt (mount point for temporary file system)

● /opt (add-on software not part of default installation)

● /proc (virtual file system containing system info)

● /root (home directory for system administrator)

● /sbin (binaries for system maintenance and admin tasks)

● /tmp (temporary files)

● /usr (user-land programs and data)

● /tmp (temporary files, like lock files)

● /var (system log files)

Page 22: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Viewing Files

● cat /etc/shells

● cat bash_lab/episode4.txt

● more or less

Page 23: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Determining File Types

● file /etc/shells

● file /etc/p*

● file /bin/cat

Page 24: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Finding Files

Page 25: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Finding Files

Page 26: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

File Permissions

- rwx r-x --x

type user group other

Page 27: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

File Permissions

● Do a long listing after each of the following:– cd art

– chmod u-w 1*

– chmod g+w [2-4].jpg

– chmod u=rwx,go=r 5*

– chmod 751*

Page 28: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

File Permissions

Page 29: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

File Permissions

● Default file creation mask– umask

– umask -S

● Type umask 135 and touch a file.

● umask is a built-in command. – man umask

Page 30: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Copying and Moving Files

● cp /etc/shells .

● cp /etc/shells ~

● cp -r bash_lab/art ~

Try to remove art.

Page 31: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Grepping

● Using grep – the most common UNIX filter.

– grep womp episode4.txt

– grep “the droids” episode4.txt

– grep “ sand “ episode4.txt

– grep -c LUKE episode4.txt

– grep [A-Z]-[0-9] episode4.txt

– grep ^Eight episode4.txt

Page 32: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Pipes and Redirection

● A pipe connects one process to another.● Redirection connects a process to a file.● grep ^Luke episode4.txt | grep Ben

● grep droid episode4.txt | wc -l

● man less | less

● ls /etc > etc

● history > history

Page 33: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Pipes and Redirection

● Exercise

– Write a C program that prompts the user for two integers and displays the average. Compile and test it.

– Create a text file with input for the program.

– Run the program with input redirected to the file.

– Redirect the output to a file.

Page 34: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Hard Links

● Create directories tempdir1 and tempdir2.● Create a text file in tempdir1 and display contents.

Page 35: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Hard Links

● Make a hard link and display the link count.

● Move the link to tempdir2 and delete the text file.● Change to tempdir2.● cat indignation

● Surprised?

Page 36: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

UNIX File System

● Directories do not contain files. That's an illusion.● A directory is a file containing a list of (name, inum) pairs.

art 1409792

episode4.txt 1391495

episode5.txt 1391768

● Each inum is an index into a table of inodes.● Do a directory listing with i and ail options.

Page 37: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

UNIX File System

● The inode table contains inodes.

protection modeowner and group

file sizelink count

timestamps

direct blocks

indirect blocksdouble indirecttriple indirect

DATA

DATA

DATA

DATA

DATA

DATA

DATA

DATA

Note that file name not stored.

Page 38: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

UNIX File System

struct inode {

umode_t i_mode;unsigned short i_opflags;kuid_t i_uid;kgid_t i_gid;unsigned int i_flags;

}

● /usr/src/linux-headers-4.4.0-34/include/linux/fs.h

Page 39: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Hard Links

● ln episode4.txt e4

art 1409792episode4.txt 1391495episode5.txt 1391768

e4 1391495

● Move link to another directory.

● Now you can find the file with -inum option.

Page 40: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Soft Links

● Make a soft (symbolic) link.

● Move soft link to tempdir2.● Remove original file (yoda).

● Now in tempdir2, try: cat slink.

Page 41: UNIX Concepts - Bloomsburg University of Pennsylvaniafacstaff.bloomu.edu/dcoles/386/downloads/unix_concepts.pdf · UNIX Concepts COMPSCI 386. Topics ... episode5.txt 1391768 ... defined

Hard vs. Soft Links

● Which is like a Windows shortcut?● Hard links

– more space efficient

– faster access to link target

● Soft links– can link to directories

– can link across file systems