Upload
mae-clarke
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
Lecture 2Lecture 2
UNIX
1999 Addison Wesley Longman 4.2
Basics of Operating Systems
1999 Addison Wesley Longman 4.3
A Fast, Stupid Machine
• Computers:– Hardware– Have limited capabilities– Can only do basic
mathematics and logical comparisons
– Must be instructed with programs what to do
1999 Addison Wesley Longman 4.4
User vs. Computer
• Computer only knows zeros and ones
• User want to edit text for example
• How they interract?
1999 Addison Wesley Longman 4.5
Chapter Outline
• Software contains instructions that tell a computer whatto do
• Operating System (OS)
• Application• User interface
1999 Addison Wesley Longman 4.6
What the Operating System Does
• The operating system controls:– Communication with peripherals– Coordination of concurrent processing– Memory management– Monitoring of resources and security– Management of programs and data– Coordinating network communications
1999 Addison Wesley Longman 4.7
The User Interface: The Human-Machine Connection• The user interface is what the user sees
on the screen and interacts with
• Two major user interface types:– Character-based interface– Graphical user interface
1999 Addison Wesley Longman 4.8
A Character-Based User Interface: MS-DOS
• This is a disk operating system in which the user interacts with characters– letters– numbers– symbols
1999 Addison Wesley Longman 4.9
UNIX
• Slides of Dave Hollinger, Department of Computer Science, Rensselaer Polytechnic Institute
• http://www.cs.rpi.edu/~hollingd/introunix
1999 Addison Wesley Longman 4.10
Unix History and Motivation
• The first version of Unix came from AT&T in the early 1970s (Unix is old!).
• Why to study UNIX?– to understand Internet better– Linux becomes widely used in industry– to understand command line interface in general
(present also in Windows OSes)– to comment on hacker movies to your friends
1999 Addison Wesley Longman 4.11
Flavors of Unix
• There are many versions of Unix that are used by lots of people:– SysV (from AT&T)– BSD (from Berkeley)– Solaris (Sun)– IRIX (SGI)– AIX (IBM)– LINUX (free software)
1999 Addison Wesley Longman 4.12
Unix Help
• There is online help available on any Unix system.
• The help system is call the "Unix man pages" – set of help files and a command to view them.– the book has some of the same information,
but you might need to check the man pages for your specific system for details.
1999 Addison Wesley Longman 4.13
Single vs. Multitasking
• Some old operating systems could only do one thing at a time (DOS).
• Most modern systems can support multiple applications (tasks) and some can support multiple users (at the same time).
• Supporting multiple tasks/users means the OS must manage memory, CPU time, network interfaces, ...
1999 Addison Wesley Longman 4.14
Single vs. Multiuser
• In Windows only one user can be on the computer
• In UNIX, many users can log in from the console (that is keyboard and monitor attached to computer) or log in remotely (using telnet or ssh)
1999 Addison Wesley Longman 4.15
Unix Accounts
• To access a Unix system you need to have an account.
• Unix account includes:– username– password– home directory
• Supervisor ”root” account
1999 Addison Wesley Longman 4.16
Home Directory
• A home directory is a place in the file system where the account files are stored.
• A directory is like a Windows folder (more on this later).
• Many unix commands and applications make use of the account home directory (as a place to look for customization files).
1999 Addison Wesley Longman 4.17
Shell
• A Shell is a unix program that provides an interactive session - a text-based user interface.
• When you log in to a Unix system the program you initially interact with is your shell.
• There are a number of popular shells that are available.
1999 Addison Wesley Longman 4.18
Some Simple Commands
• Here are some simple commands to get you started:– ls lists file names (like DOS dir
command).– who lists users currently logged in.– date shows the current time and date.– pwd print working directory
1999 Addison Wesley Longman 4.19
Files and File Names
• A file is a basic unit of storage (usually storage on a disk).
• Every file has a name.
• Unix file names can contain any characters (although some make it difficult to access the file).
• Unix file names can be long
1999 Addison Wesley Longman 4.20
File Contents
• Each file can hold some raw data.• Unix does not impose any structure on
files– files can hold any sequence of bytes.
• Many programs interpret the contents of a file as having some special structure– text file, sequence of integers, database
records, etc.
1999 Addison Wesley Longman 4.21
The Filesystem
/
bin etc users tmp usr
hollid2 scully bin etc
netprog unix X ls who
1999 Addison Wesley Longman 4.22
Pathnames
• The pathname of a file includes the file name and the name of the directory that holds the file, and the name of the directory that holds the directory that holds the file, and the name of the … up to the root
• The pathname of every file in a Unix filesystem is unique.
1999 Addison Wesley Longman 4.23
Pathnames (cont.)
• To create a pathname you start at the root (so you start with "/"), then follow the path down the hierarchy (including each directory name) and you end with the filename.
• In between every directory name you put a "/".
1999 Addison Wesley Longman 4.24
Pathname Examples
/
bin etc users tmp usr
hollid2 scully bin etc
netprog unix X ls who
/usr/bin/lsSyllabus
/users/hollid2/unix/Syllabus
1999 Addison Wesley Longman 4.25
Absolute Pathnames
• The pathnames described in the previous slides start at the root.
• These pathnames are called "absolute pathnames".
• We can also talk about the pathname of a file relative to a directory.
1999 Addison Wesley Longman 4.26
Relative Pathnames
• If we are in the directory /users/hollid2, the relative pathname of the file Syllabus is:
unix/Syllabus
• Most unix commands deal with pathnames!• We will usually use relative pathnames when
specifying files.
1999 Addison Wesley Longman 4.27
Disk vs. Filesystem• The entire hierarchy can actually include
many disk drives.– some directories can be on other computers
/
bin etc users tmp usr
hollid2 scully
1999 Addison Wesley Longman 4.28
The current directory and parent directory
• There is a special relative pathname for the current directory:
.
• There is a special relative pathname for the parent directory:
..
1999 Addison Wesley Longman 4.29
The ls command
• The ls command displays the names of some files.
• If you give it the name of a directory as a command line parameter it will list all the files in the named directory.
1999 Addison Wesley Longman 4.30
Some things to try
ls list files in current directory
ls / list files in the root directory
ls . list files in the current directory
ls .. list files in the parent directory
ls /usr list files in the directory /usr
1999 Addison Wesley Longman 4.31
ls command line options
• To use a command line option precede the option letter with a minus:
ls -a or ls -l• You can use 2 or more options at the
same time like this:
ls -al
1999 Addison Wesley Longman 4.32
General ls command line
• The general form for the ls command is:
ls [options] [names]
• The options must come first!• You can mix any options with any names.• An example:
ls -al /usr/bin
1999 Addison Wesley Longman 4.33
Moving Around in the Filesystem
• There cd command can change the current working directory:
• cd change directory
• The general form is:
cd [directoryname]
1999 Addison Wesley Longman 4.34
cd
• With no parameter, the cd command changes the current directory to your home directory.
• You can also give cd a relative or absolute pathname:
cd /usr
cd ..
1999 Addison Wesley Longman 4.35
Some more commands and command line options
• ls -R will list everything in a directory and in all the subdirectories recursively (the entire hierarchy).– you might want to know that Ctrl-C will
cancel a command (stop the command)!
• pwd: print working directory• df: shows what disk holds a directory.
1999 Addison Wesley Longman 4.36
Copying Files
• The cp command copies files:
cp [options] source dest• The source is the name of the file you
want to copy.
• dest is the name of the new file.
• source and dest can be relative or absolute.
1999 Addison Wesley Longman 4.37
Deleting (removing) Files
• The rm command deletes files:
rm [options] names...• rm stands for "remove".
• You can remove many files at once:
rm foo /tmp/blah /users/clinton/intern
1999 Addison Wesley Longman 4.38
File Owners
• Each file is owned by a user.
• You can find out the username of the file's owner with the "-l" option to ls,
• Each file is also owned by a Unix group.• ls -l also shows the group that owns
the file.
1999 Addison Wesley Longman 4.39
File Permissions• Each file has a set of permissions that
control who can mess with the file.
• There are three kinds of permissions:– read abbreviated r– write abbreviated w– execute abbreviated x
• There are separate permissions for
• the file owner, group owner and everyone else.
1999 Addison Wesley Longman 4.40
ls -l
> ls -l foo
-rw-rw---- 1 hollingd grads 13 Jan 10 23:05 foo
permissionsowner group
size
time
name
1999 Addison Wesley Longman 4.41
ls -l and permissions
-rwxrwxrwx Owner Group Others
Type of file:- means plain filed means directory
1999 Addison Wesley Longman 4.42
Changing Permissions
• The chmod command changes the permissions associated with a file or directory.
• There are a number of forms of chmod, this is the simplest:
chmod mode file
1999 Addison Wesley Longman 4.43
chmod mode file
• Mode has the following form*:[ugoa][+-=][rwx]
u=user g=group o=other a=all+ add permission - remove permission = set
permission
*The form is really more complicated, but this simple version will do enough for now.
1999 Addison Wesley Longman 4.44
chmod examples
> ls -al foo
rwxrwx--x 1 hollingd grads …
> chmod g-wx foo
> ls -al foo
-rwxrw---- 1 hollingd grads
>chmod u-r .
>ls -al foo
ls: .: Permission denied
1999 Addison Wesley Longman 4.45
Other filesystem and file commands
• mkdir make directory• rmdir remove directory• less print out to terminal.
1999 Addison Wesley Longman 4.46
Startup files DOS: autoexec.bat & config.sys
bash:~/.bash_profile~/.bashrc
~/.bash_logoutcsh:~/.cshrc~/.login~/.logout
1999 Addison Wesley Longman 4.47
Wildcards
• “a*b” matches all files in the current directory that start with a and end with b.
• “a?b” matches all 3-letter files in the current directory that start with a and end with b.
• Things to try:ls *ls –al *ls a*ls ?b
1999 Addison Wesley Longman 4.48
Programs and Standard I/O
ProgramProgramStandard Input
(STDIN)Standard Output
(STDOUT)
Standard Error(STDERR)
1999 Addison Wesley Longman 4.49
Input Redirection
• The shell can attach things other than your keyboard to standard input.– A file (the contents of the file are fed to a
program as if you typed it).– A pipe (the output of another program is
fed as input as if you typed it).
1999 Addison Wesley Longman 4.50
Output Redirection
• The shell can attach things other than your screen to standard output (or stderr).– A file (the output of a program is stored in
file).– A pipe (the output of a program is fed as
input to another program).
1999 Addison Wesley Longman 4.51
How to tell the shell to redirect things
• To tell the shell to store the output of your program in a file, follow the command line for the program with the “>” character followed by the filename:
ls > lsoutthe command above will create a file
named lsout and put the output of the ls command in the file.
1999 Addison Wesley Longman 4.52
Input redirection
• To tell the shell to get standard input from a file, use the “<“ character:
sort < nums• The command above would sort the
lines in the file nums and send the result to stdout.
1999 Addison Wesley Longman 4.53
Output and Output Append
• The command ls > foo will create a new file named foo (deleting any existing file named foo).
• If you use >> the output will be appended to foo:
ls /etc >> foo
ls /usr >> foo
1999 Addison Wesley Longman 4.54
Pipes
• A pipe is a holder for a stream of data.• A pipe can be used to hold the output of
one program and feed it to the input of another.
prog1prog1 prog2prog2STDOUT STDIN
1999 Addison Wesley Longman 4.55
Asking for a pipe
• Separate 2 commands with the “|” character.
• The shell does all the work!
ls | sort
ls | sort > sortedls
1999 Addison Wesley Longman 4.56
Job Control
• The shell allows you to manage jobs– place jobs in the background– move a job to the foreground– suspend a job– kill a job
1999 Addison Wesley Longman 4.57
Background jobs
• If you follow a command line with "&", the shell will run the job in the background.– you don't need to wait for the job to complete, you
can type in a new command right away.– you can have a bunch of jobs running at once.– you can do all this with a single terminal (window).
ls -lR > saved_ls &
1999 Addison Wesley Longman 4.58
Listing jobs
• The command jobs will list all background jobs:
> jobs
[1] Running ls -lR > saved_ls &
>
• The shell assigns a number to each job (this one is job number 1).
1999 Addison Wesley Longman 4.59
Suspending and Killing the Foreground Job
• You can suspend the foreground job by pressing ^Z (Ctrl-Z). – Suspend means the job is stopped, but not
dead.– The job will show up in the jobs output.
• You can kill the forground job by pressing ^C (Ctrl-C). – It's gone...
1999 Addison Wesley Longman 4.60
Moving a job back to the foreground
• The fg command will move a job to the foreground.– You give fg a job number (as reported by
the jobs command) preceeded by a %.> jobs
[1] Stopped ls -lR > saved_ls &
> fg %1
ls -lR > saved_ls