unix intro

53
Uni x

Upload: cong-thanh-nguyen

Post on 19-Jan-2017

418 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Unix Intro

Unix

Page 2: Unix Intro

Unix Intro.

• We use the Unix labs for CSCI213 • Your backgrounds and knowledge vary, some of

you are:– already Unix proficient, :-)– just starting Unix in CSCI204 where you will study it

in some detail, :-|– only using Unix for CSCI213, :-(

• So, a minimal intro to Unix Operating System

Page 3: Unix Intro

• Unix– origins– organization

• kernel, libraries, tools and utilities, shell– shell

• command interpreter, shell scripting language, commands start other programs, some standard program libraries, extensibility

– time-share system• controls on users

Page 4: Unix Intro

Unix origins

• 1960s “time shared” computer systems– pioneered at MIT, 1960-1962 first useful system

developed,– multiple users logged on via terminals

• systems offers facilities for editing files, compiling code, and running small programs

• system allocates small amount of time (0.1 second to each user); when time is up, user’s code and data “swapped out” of memory onto fast disk storage allowing another user to run

Page 5: Unix Intro

Unix origins

• By late 1960s, much more elaborate systems were under construction - with features like shareable code and data segments, multiple levels of security and access control, …

• Multics project involved work by MIT, GE-Honeywell, ATT labs, ...

Page 6: Unix Intro

Unix origins• Multics - too costly, too slow, and basically

around 1969/1970 it still didn’t work.• ATT group split off from Multics• They (Thompson & Ritchie) wanted a system

– run on readily available small machines– specifically intended for small groups of software

developers working together on interrelated projects

Page 7: Unix Intro

Unix origins

• ATT guys idea:– a small core to Operating System (OS) that

provides essential facilities– additional functionality to be provided by libraries, – users (all accomplished professional programmers)

to provide additional components and tools– want a means to build complex programs by

combining simple components

Page 8: Unix Intro

Unix origins

• ATT approach was novel at the time– write all the OS in a high level language– small, well defined core of essential facilities as

“system calls” – make things uniform - same code to work for

  I/O to disk file, terminal, …–

Page 9: Unix Intro

Unix: first implementations

• Around 1969/1971, ATT guys did some prototyping on a PDP7 (8K words - approx. 20 Kbytes - main memory, small disk ~64Kbytes, Dectapes, single terminal) using BCPL, B, and early dialects of C.

• First real implementation for DEC PDP11 computer using C; published around 1973.

Page 10: Unix Intro

• Unix– origins– organization

• kernel, libraries, tools and utilities, shell– shell

• command interpreter, shell scripting language, commands start other programs, some standard programs, extensibility

– time-share system• controls on users

Page 11: Unix Intro

Unix: core (kernel)

• Twenty or so “system calls”– read, write, create, open, link “file” access– fork, exec, exit, brk process (program) control– signal, kill, pipe, dup interprocess communication– …

• Rather more than 20 now.• “chapter 2 of ‘man’ (manual) pages”

Page 12: Unix Intro

Unix libraries

• stdio– C’s I/O library (can be used from C++, but iostreams

preferred)• makes use of read, write, open, create etc systems

calls• provides programmer with much higher level

facilities - formatted I/O of numbers, strings etc; efficient buffering of I/O, ...

Page 13: Unix Intro

Unix libraries

• stdlib– an assortment!

• random numbers, • malloc and free (the C free storage, “heap”, management

functions, can involve OS system calls like brk - request to expand heap)

• math• string• See “chapter 3 of ‘man’ pages”

Page 14: Unix Intro

Unix: tools and utilities

• Little programs– fgrep and grep - programs that search files for

occurrences of particular words or patterns of characters

– diff - a program to report difference in text files– wc - a program to count number of characters,

words, and lines in a text file – In “chapter 1 of ‘man’ pages” along with details

of ‘shell’ facilities

Page 15: Unix Intro

Unix shell

• It is the “shell” that accepts and acts on the commands of a Unix user.

• When people say they “know how to use Unix” what they usually mean is that they know how to make simple use of a Unix shell (there are several “shells” that differ slightly, each is someone’s favorite - you will use “sh” by default, many students change this to “bash”, other quite popular shells are “ksh” and “csh”)

Page 16: Unix Intro

Unix shell

• Shell?

• Well, it went around the lower levels of Unix – shielding users from low level kernel– shielding low level kernel from users

Page 17: Unix Intro

• Unix– origins– organization

• kernel, libraries, tools and utilities, shell– shell

• command interpreter, shell scripting language, commands start other programs, some standard program libraries, extensibility

– time-share system• controls on users

Page 18: Unix Intro

Unix ‘shell’

• When working interactively, you have to be able to tell the operating system what program you want to run, and what data it is to use.

• Original (1960s) time-share systems had simple “command line interpreters”– commands defined by keyword (e.g. EDIT, COMPILE,

PRINT, RUN) and took arguments• COMPILE CODE=MYPROG OPTIMIZE=3• RUN TIME=20 INPUT=MYDATA OUTPUT=PRINT

Page 19: Unix Intro

Unix ‘shell’

• Unix developers wanted something much more sophisticated– handle repetitive tasks (e.g. run program XXX for

each data file in a given directory)– use output from one program as input to next– run program X1, if successful continue with

program Y1, otherwise print message and run program Z1,

– ...

Page 20: Unix Intro

Unix shell• Unix guys decided that what they basically needed was an

interpreter for a slightly simplified “programming language” (based on C)

• variables (“environment variables”)– loop and test constructs– mechanisms for starting programs and passing “command line

arguments” (this part much the same as commands in earlier operating systems)– mechanism for putting programs together so output from a program

could go to terminal, or to a file, or be used as input for another program

Page 21: Unix Intro

Unix shell

• The “shell” is an interpreter program.– prompts the user for input– reads the inputs that the user types at a terminal – identifies commands, sorts out arguments and

starts appropriate programs– deals with any of the more complex looping

and selection constructs available in the shell language

Page 22: Unix Intro

Unix shell• Typical use is:

– enter a single command (with arguments as needed)– wait for shell to process it

• But:– can set environment variables– can type in a little “shell code” and then have the shell

interpret it immediately– write “shell scripts” (programs in shell language) and store these

in files, these scripts can then be used as extra commands

Page 23: Unix Intro

Unix shell

• A small number of commands are actually built into the shell interpreter program itself.

• Most require “shell” to start another program– the programs that you can start with shell

commands are mainly kept in a few standard file directories (folders)

Page 24: Unix Intro

Unix shell

• Command repertoire is easily extended.• All those little utilities - grep, wc, … - become

commands by simply moving the programs into the standard directories used by shell.

• You can add your own programs as commands (compiled C, C++, Java, or ‘shell scripts’) - (you have to note, in an environment variable, details of where you store your extra programs so shell knows where to find them)

Page 25: Unix Intro

Unix shell

• You can build up special purpose “commands” by using the shell’s facilities to combine simpler commands – e.g. you need to count the number of calls to some

function Foo() in a file• so you use grep to find the lines with calls to Foo()• pass the output from grep to wc which will count the

number of lines

Page 26: Unix Intro

• Unix– origins– organization

• kernel, libraries, tools and utilities, shell– shell

• command interpreter, shell scripting language, commands start other programs, some standard program libraries, extensibility

– time-share system• controls on users

Page 27: Unix Intro

Unix as a time-share system

• Unix is intended to be a multi-user system– multiple terminals can be connected– computers have sufficient memory to keep

many users’ programs in main memory, (but some code and data are still “swapped” to/from disk)

– time-slicing applied• your program may get up to 0.1 seconds of CPU

time, then CPU switches to another user

Page 28: Unix Intro

Unix as a time-shared system

• All time-shared systems have to have controls on users– users identify themselves (password controlled login)

– system keeps track of user-id• determines which files can be accessed• determines how many programs can be run (you can

have several running at same time)• determines limits on number of files and total storage

used

Page 29: Unix Intro

Unix users

• Two levels of user:– “root” the system’s administrator (or

“guru”) who controls all aspects of Unix use (including users’ passwords, files, programs, ..)

– everyone else subject to restrictions

Page 30: Unix Intro

Unix users and “groups”• Unix also defines “groups” (remember, Unix meant to help

groups of cooperating programmers)– Individual users can be members of more than one group

(a command changes your effective group)– Groups members can be given shared access to certain

files.– You have a default group (all undergraduates in same group).– You may be allocated to extra groups for subjects like

CSCI205, CSCI311 and CSCI321,

Page 31: Unix Intro

Unix users

• Controls on users (and groups) are tied in with the way Unix organizes files.

• Recorded with every file are details of– owner (user)– group

• Controls on access to files distinguish user, group, and other (“root” has unrestricted access to all files)

Page 32: Unix Intro

• Unix– time-share system

• controls on users – file system

Page 33: Unix Intro

File system

• The Unix file system is a little more elaborate than the systems that you are used to on Macs and PCs.

• There you have:– disk

• folders on disks– files in folders– folders in folders etc

Page 34: Unix Intro

Unix file system

• The Unix file system has a similar hierarchical arrangement;– rather than files and folders on disks, its described in

terms of a tree structured hierarchy

• Differences?– Unix file system integrates everything!

• all mounted disks form part of single hierarchy• all devices represented by files, ...

Page 35: Unix Intro

/

/tmp /dev /bin /packages /usr /pub

Some directoriestmp a place for temporary files

dev “files” for devices like terminals, disks, etc bin directory where many of simple command programs are kept (‘binary’) packages directory for Sun’s compilers etc pub public files (some general info. etc) usr /usr/bin more shell commands /usr/include “header” files for libraries

Page 36: Unix Intro

Unix file system• Each individual user has own directory (traditionally,

individual directories were subtrees of /usr, but more likely your directories will be located in another part of the file system).

• Files and directory access– read (for directory this means you can get a list of files it contains)– write (change contents of file, add/remove files from directory)– execute (access subdirectories, run a file if it’s a program)

Page 37: Unix Intro

Access to files and directories

• Directory access settings will keep you out of those parts of the file system that you shouldn’t see - most of file hierarchy is actually accessible as Unix traditionally let users explore.

• Your own directory will have access controls keeping others from seeing your files; files that you create will be created so that only you can read and change them.

Page 38: Unix Intro

• Unix– time-share system

• controls on users – file system– using Unix

Page 39: Unix Intro

You and Unix• On Unix, you are represented by

– your “home directory” and its subdirectory– entries in a number of system files

• /etc/password– your user id (e.g. ds01), a unique identifier number used by the

system (e.g. 1745), your default group number, your actual name, your “shell”, (but not your password)

– When you login, Unix checks your password, notes details of user and group, starts a shell with current directory set to your home directory

Page 40: Unix Intro

Customisation of Environment

• You can “customise” your Unix environment,– setting things like prompt string– changing directories where shell will look for programs

(will have to make some change here relating to use of Java)

– …• You do this by defining any special features that you

want, the definitions go in a file in your home directory named .profile

Page 41: Unix Intro

Communicating with Unix

• Traditionally, Unix is “text oriented” – all communication in terms of typed commands

and textual responses• Desktop environments for graphics terminals?

– Available, similar to Mac/Windows but usually less complete and less integrated

– Generally, have to revert to command line operation for some tasks anyway

Page 42: Unix Intro

Default Unix environment• Unix lab has graphics X-terminals

– can use “desktop” style environments– default is “FVM2” windowing environment

• multiple windows– command window (“xterm” window)– textedit (or dtpad) simple unsophisticated editor– ….

• Popup menu in background, lets you create more of these windows

• Each xterm window is associated with a running “shell” program accepting commands

Page 43: Unix Intro

xterm

xterm$ pwd/home/cs_ug/ds01$ lsA1 note test.cc$ mkdir JavaStuff$ cd JavaStuff

Page 44: Unix Intro

Not an Xterminal?

• If you use anything other than a graphics Xterminal (Mac, PC, old text-only terminal, dial-in line, …)– much the same as working with a single

“xterm” session on an Xterminal– no access to other programs like textedit

Page 45: Unix Intro

Unix commands

• You will have to learn at least:– some basic commands

• commands for moving around file system, copying and moving files

• commands to display or print files• …

– use of one (or more) editor programs– use of (limited) on line help facilities on Unix

Page 46: Unix Intro

File system commandsdirectories

• pwd “print working directory”• cd change directory• ls list names of files and subdirectories

• option -l details of size and access controls for files• option -a all files (files starting with “.” not normally

shown)• …

• mkdir “new folder”

Page 47: Unix Intro

File system commandsfiles

• cp copy

• mv move

• rm remove (delete)

• pg page (display contents of text file one “page” at a time)

• cat can be used to display contents of file (like pg) or copy files (like cp) or copy several files into a single output file

• lp send (text) file to line printer

Page 48: Unix Intro

Information

• man display manual page for specified topic

• xmananother version of man, may not have information on as many topics, uses a separate window to display details of topics that it knows about

• netscape (some of Sun documentation as files that you can display using netscape, there is a link from departmental home page to these Sun documents; most of Java documentation is in files best read via netscape)

Page 49: Unix Intro

Editors

• textedit easy to use, similar to Mac/PC-Windows editor, mouse actions, cut and paste (only works on Xterminals)

• ed, vi, sed standards for Unix environment, available everywhere (ed and sed are for “hackers”, too cryptic for average user)

• jove popular editor• pico ?

• Learn jove or textedit for use in labs; if log in remotely, you will need to learn vi, pico, or ed as well

Page 50: Unix Intro

Starting Unix?

• Login• Use commands like pwd, cd (specify source as one of

directories with subject information, e.g. /share/cs-pub/121), ls, …

• Create subdirectories in own home directory (cd, mkdir)

• Copy some example files into own directory (cp)• Print a file (lp)

Page 51: Unix Intro

Starting Unix

• Look up info on a simple command (e.g. diff, fgrep, time, date, du, …) using man (some programs don’t have entries in the manual)

• Try “elm” (Unix mail system)– “w” and “finger” give information about other

users so that you can mail them• Try “nn” (news reader)• Try “netscape”

Page 52: Unix Intro

Starting Unix

• Editors– textedit should have no difficulties– jove there is a special program “teachjove”

that takes you through a tutorial on the use of this editor– others? Buy a book on Unix!

Page 53: Unix Intro

Working with Unix

• Plan on a hierarchy of subdirectories– one for each subject, 204, 212, 213, etc– within each subject directory have a subdirectory for each

assignment

• In lab, use multiple xterm sessions– run netscape (viewing Java documentation)– textedit and an xterm window for compiles of Java code– you can close (iconise) windows to clear up screen space