Transcript document

What an operating system does?
Without an Operating System (1)

What do you do with the bare hardware?
– you write a special program for the specific hardware that
takes care of everything.
 managing all aspects of the hardware
 Input, output
– Once your program ends, the hardware hangs up.

In the early days, that was the way it worked.
Without an Operating System (2)

What about all of those peripherals?
– Peripherals got more and more complex. Getting them to
work became more difficult
– It became necessary to have pre-written llittle programs
that looked after the hardware.

What if someone else wants to share the
hardware?
– He/She had to wait until your job was done.
– This was even worse than a Hammer – two people can
share a hammer during their jobs.
– It became necessary to have a program in charge of
manage the resources and the tasks.
What is an operating system

An operating system is a program that controls the
resources of a computer and tasks of the users.
– CPU time, Memory, Disk space, Network, Printers and other
peripherals.

Applications access the resources using “system
calls”. The operating system does the rest.
– Therefore, you can program much easier.
– Programs became portable.

Applications request the resources and the
operating system allocate the resources.
– Therefore, applications can share the resources.
How they share
 Memory
-- use different region
 Disk space -- use different sectors
 Network -- use different bandwidth
 Printers -- line up at the printing queue
 Other peripherals -- line up
 CPU -- ??
Time sharing

Different tasks are run for some milliseconds in
turns.
A
B
C
– Each task believes that he has the whole machine! – Slower,
but quick enough. Really they were just getting “time-slices”.

Interactive tasks were given higher priorities than
background tasks.
Not really three times slow
The other guys seem
very polite today …
I am waiting for the
user’s input. Pass
by me.
A
B
I hate the
slow
network…
C
Parts of a Unix OS (1)
kernel
shell
utilities
Parts of a Unix OS (2)

Kernel controls the computer.
– Kernel hides the hardware.
– Kernel manages the tasks and resources.

Shell stands as an interface between users and the
kernel.
– The shell is a command line interpreter (CLI). It
interprets the commands the user types in and
arranges for them to be carried out.

Utilities are standard tools/applications of Unix.
– “mail” is a utility to send emails.
– They are used so often that they become a part of Unix.
UNIX history (1)
 In
1969, Ken Thompson at AT&T Bell Lab.
began developing Unix.
– In assembly language.
– Ran on DEC PDP-7 with 8K words of memory.
 In
1974, Unix was rewritten in C.
– By writing C compilers, Unix can be ported to other
computers.
Unix history (2)

BSD UNIX
– BSD: Berkeley Software Distribution
– UCB took the Internet Protocols (IP) and put them into UNIX
– 4.4BSD (Berkeley): 1994

System V.
– AT&T System V: 1984
– Current version is SVR4: System V Release 4.

POSIX: Portable Operating System Interface for
Computer Environments. (1984)
– All Unix systems conform POSIX.

This course is based on SVR4. But most of the things
you will learn in this course work in different Unix
systems with small differences.
Linux


In 1991, Linus Torvalds wrote the Linux kernel as a
class project while a student at the University of
Helsinki in Finland
Numerous of programmers have worked on it and
made it a popular Unix-like operating system.
– recently estimated at 20% of PC server market

It is free. (free is the free in “free speech”)
– You can copy, modify, distribute, and even sell it as you wish.
Go to http://www.gnu.org/ to see more details.
– Companies packed the system to sell “service”.
– Download it at http://www.download.com, search red hat
Linux.
Unix shells and basic commands
UNIX Shell (1)

Command Line Interpreter
– Once logged in, login gives control to a "shell"
 it prompts for input, then parses, interprets, finds and
executes the commands you type
 much like COMMAND.COM program in DOS
– Shells are much more sophisticated
– Newer ones are quite user friendly

A High-Level Programming Language
– By writing a shell “script”, you can let the system perform a
series of commands by typing the file name of the script.
– like but more powerful than BAT file in DOS.
UNIX Shell (2)

Not as user-friendly as a menu or a GUI
Graphical
User
Interface
– shells offer no lists/display of what's available
 Recall v.s. recognition.
– GUI's are often better for things which you do not do that often
 they lead you by the hand

Shells tend to be faster and more flexible for experienced
users.
– You need not find the command in a list, just “say” it.
– You need not follow the tedious steps that a GUI was designed.

Many modern UNIX systems offer both a GUI and a Shell
interface
– Gnome (from Free Software Foundation: GNU) is a good example
of this
UNIX Shell (3)

There are a number of shells for UNIX. The choice
is yours:
– sh: The Bourne Shell
 the original UNIX shell
 named for S.R. Bourne who designed it at Bell Labs
 not very "user friendly"
 good at programming
– csh: The C-shell
 a shell whose syntax is more "C"-like
 command history and job control
– make it very popular as a CLI.
 not good at programming.
UNIX Shells (4)
– tcsh: The T C-Shell
 updated C-shell with better "line-editing"
– Ksh: Korn Shell
 was written by David Korn of Bell labs.
 It provides all the features of the TC shells.
 good at programming.
– bash: The Bourne Again Shell
 implemented as a part of GNU project by public efforts
– http://www.gnu.org/software/bash/bash.html
 an sh-compatible shell that incorporates useful features
from the Korn shell (ksh) and C shell (csh)
– ...
Unix shell (5)
 Default
shell is the shell after you login.
 Change your shell
– with the "chsh" command
 change your default shell.
– by typing “sh”, “csh”, “bash” etc.
 run another type of shell as a “subshell”
 When you exit from the subshell, you will come
back to the current shell.
Unix shell (6)
 Which
shell do we use in cs211b?
– For the CLI aspect of the shell, we teach csh.
– For programming language aspect, we teach
Bourne shell.
– Many Unix users use shells in this way.
– Many features of sh or csh are shared by other
shells.
 You will be able to use other shells.
Issuing Commands (1)

Prompts
– UNIX shell issues a prompt for input:
– Usually something like
algernon%
seperator
Machine name

Editing Input:
– Using BackSpace or DEL or ctrl-H to erase a letter
– Using ctrl-U to erase the whole line.
Issuing Commands (2)
 Running
a Program
– Just type the name of the program, press <return>
– If the shell can find it, it will run it
– Most commands take command line arguments
 some of these are things like file names, etc.
obelix[2]% cat myfile
list file "myfile"
 others
are options, like -h, -x, -F, ...
obelix[3]% cat -n myfile
 A running
program is a process.
list file "myfile"
with line numbers
Issuing Commands (3)
 Stopping
and pausing processes
– Control-c : terminate current process now
 Send SIGINT signal to the process.
 Default setting of receiving a SIGINT is to quit.
– Control-\ : terminate current process now
 Send SIGQUIT signal to the process.
 A core file is generated.
– Control-z : Pause command now
 can be restarted using the "fg %1" command
 more on this useful keystroke later.
– kill: Kill a process.
 more on this useful command later.
Common Commands (1)

Some Commands
– cp for CoPy
Use:
obelix[4]% cp file1 file2
Action: copy file1 into file2
– rm for ReMove
Use:
obelix[5]% rm file2
Action: removes file2
– mv for MoVe
Use:
obelix[6]% mv file1 file3
Action: renames file1 as file3
Compare: cp file1 file3; rm file1
Common Commands (2)
– cat for...listing the contents of a file
 Use:
obelix[7]% cat file1
 Results:
display the contents of file1
 Why “cat”?
– originally “short” for concatenate
– can use: obelix[8]% cat file1 file2
– prints file1 followed by file2
– more for listing the contents of a file by pages.
 Use:
obelix[7]% more file1
 Results: display the contents of file1 for a page and
pause. Press space bar to see other pages. Press q to
quit.
Common Commands (3)
– date: what time is it?
Use:
obelix[10]% date
Result: print the time and date
– hostname: what machine am I on?
Use:
obelix[11]% hostname
Result: print the machine's name
– who: who else is logged onto the computer you’re
on?
Use:
obelix[12]% who
Result: a list of users and some info about them
– uptime: how long has the machine been up?
Use:
obelix[13]% uptime
Result: one line with all sorts of neat stuff
Common Commands (4)
– passwd: change your password
Use:
obelix[14]% passwd
Result: this program will prompt you for input
Old Password:
New Password:
Verify:
– For security, it will not echo anything.
– netscape: surf the net
Use:
obelix[15]% netscape
Result: web surfing software
works only under X-windows (and NT and ...)
– lynx: surf the net.
Common Commands (5)
– echo: print some text
Use:
obelix[16]% echo Unix is easy!
Result:
Unix is easy!
printed on your screen
– clear for Clear Screen
Use: obelix[9]% clear
Action: clears the screen
Electronic Mail (1)


We assume you are familiar with email
Email addresses:
– E.g. [email protected], [email protected]
– An email address is made up of two parts
 username@domainname

Your email address in CS
– Your user name is your login name on gaul.
– Your domain name is gaul.csd.uwo.ca
 gual.csd.uwo.ca is the "domain name“ for all of the
machines in the Undergrad lab
– Your email address is [email protected]
Electronic Mail (2)

There’s a very wide choice of mail programs
– which one you use is totally up to you

mail
– the "default" mailer
– has a command-line interface with lots of commands
– You need some time to become familiar with it.

elm
– this is a visual, partially menu-driven mailer
– complete, flexible and easy to use
– requires that you know the “vi” or “emacs” editor

pine
–
–
–
–
the simplest mailer
“noisy” menu-driven interface, but quite complete
use it if you are unsure of yourself...it does the job
Type pine, and follow the prompts.
Electronic Mail (3)
E-mail doesn't always get through
– you may get messages bounced back
– read what you get and determine what went wrong
– usually it is because the email address was misspelled.
Spam
– mass e-mail sent without permission to people who you do
not know
– it is
EVIL
– should your account be implicated in anything like this you
will be asked to explain why we should not revoke your
access privileges
– we get people's attention by turning accounts off...
Unix Editors
Unix Editors

Editors in UNIX (and other O/S's) come in two general
flavors: modal and modeless
– modal editors have "modes"
 generally input mode and command mode
– input mode allows entry of text
– command mode allows file operations and
sophisticated text modification.
 primary UNIX examples: ed and vi
– modeless editors
 positioning and text manipulation are done by special key
sequences (like arrow keys and function keys)
– could also be done by mouse actions and menus
 primary UNIX example: emacs
The ED editor
 ed
is the original line editor
 very difficult to use.
 Still available but out of date.
The VI Editor (1)



vi was developed by UCB and is a standard utility of SVR4.
vi is very fast for experienced users.
vi is a modal editor
– insert mode (get out with Escape)
– command mode
– colon command mode
Start vi
i
input mode
Command mode
:
Insert text
Esc
editing
Colon command mode
q
File operation
quit
The VI Editor (2)

Starting vi
obelix[23]% vi filename

Commands
–
–
–
–
–
–


esc Go to command mode.
Colon commands
–
–
–
–

arrow keys move the cursor
x
delete the current character
dd
delete the current line
u
undo the change
/
search for the text following /
i
change to input mode.
:w
:wq
:q
:q!
write the file.
save and quit
quit (if no change after the last saving)
exit without save
The above 11 commands are enough to do editing.
The VI Editor (2)
Cursor Movement
–
–
–
–
h
j
k
l
move one char left
move one line down
move one line up
move one char right
– w move to next word
– e move to end of current
word
– b move to beginning of previous
word
– 0 move to the beginning of
the current line
– $ move to the end of the
current line
Screen Movement
–
–
–
–
–
–
H move to top of screen
L move to bottom of screen
^F scroll down one page
^B scroll up one page
^U scroll up one half page
^D scroll down one half page
Search
– / search for something
– ? search backwards for
something
The VI Editor (3)
Deletion Commands







x
D
dd
d$
d0
dw
db
delete character under cursor
delete to end of line
delete entire line
delete to end of line
delete to beginning of line
delete the next word
delete the previous word
Other Commands
 . redo last modification



Change Commands






command
u undo the last command
:w file write the buffer to this file
:r file read this file into the buffer

s substitute a string for current
char (end with ESC)
r replace current char with another
R overwrite text (end with ESC)
C replace to end of current line
(end with ESC)
c0 replace to beginning of current
line (end with ESC)
cw replace the current word (end
with ESC)
cb relace the previous word (end
with ESC)
Emacs (1)

Emacs: (Editor MACros)
– modeless,
– Has versions for Windows and Linux.
 Downloadable from net.
– menu-driven and mouse-driven under X-windows.

Emacs uses special keys (ESC and CTRL) to perform
editor functions other than input
– Notation:
 ESC: The Escape key.
 CTRL: The Control key.
 SPC: The Spacebar.
 RET: The Return or Enter key.
 DEL: The Delete key.
Emacs (2)

Key combination: a sequence of (special) keys
– C-x "Control X"
 Hold down Control key while typing x.
– C-x C-c
 Hold down Control key while typing x and c.
 Or hold down control key while typing x, then release,
then hold down control while typing c.
– C-x u
 Hold down the Control key, keep it down while typing x.
Release the Control key and type u.
– ESC x "Escape x" or "Meta x"
 What always works:
– Type the Escape key. Release. Type x.
 What sometimes works (and is convenient):
– hold down the Alt key and x key at the same time
Emacs (3)
 Starting
Emacs on a file:
obelix[23]% emacs myfile
 myfile is either a new or existing filename.
 Emacs
is a full-screen editor,
– if the filename you typed was an existing file, you
will see the first page of the file on your screen.
– If you typed a new filename, you will be faced with a
blank screen, and you may type the file.
– The file name will appear at the bottom of the
screen.
Emacs (4)

Basic operations:
– Adding text to cursor
 You can start to add text right away.
– Moving cursor
 Arrow keys
 Page up, page down, home, end
 Mouse
– Delete characters
 Ctrl-d delete the character at the cursor
 Delete delete the character left to the cursor
– Undo
 Ctrl-_
control+shift+’-’
 Ctrl-x u
Emacs (5)

Basic file operations:
– Ctrl-x ctrl-s
save the file
– Ctrl-x ctrl-c
quit emacs.
– Ctrl-x ctrl-w write the file to (another) file
 you will be prompted to enter a filename

Cancel a command
– If you get to a mode which you don't want
 for example, your typed ctrl-x and emacs expects more
– Type C-g
 will usually back you out.
Emacs (6)

Cut and Paste
– To move or copy a block of text
Move cursor to start of block
C-@
Set mark
Move cursor to end of block.
C-w
Wipe out (Cut) or
ESC w Copy.
Move cursor to new location
C-y Yank back last thing killed
(Paste).
– The CTRL-y may be repeated for multiple copies.
Emacs (7)
 Cut
and Paste a few of lines
– C-k Kill line
 Deletes to end of line
 Use home first to kill the whole line.
– Use C-k for many times to kill many lines
– Move cursor to the correct position
– Use C-y to yank the cut lines to the new position
Emacs (8)
Searching



Search allows you to search for a
string
Search from the cursor position to
the end of file.
To search for a string, type
Search and Replace

Replace all occurrences of one
string with another
– ESC x replace-string
 you are prompted for the
replacement text

C-s string
 C-s again repeats
 C-g to quit
Query-replace asks before
replacing each occurrence.
– Type: ESC %
– you are prompted for search &
replace strings.

At each occurrence, respond:
–
–
–
–
y/n to replace/not replace.
!
to replace all remaining
ESC to exit
? for lots more options
Emacs (9)
Arrow keys, editing keys do not always work
Cursor Movement




C-f Forward a character
(Right)
C-b Back a character (Left)
C-n Next line (Down)
C-p Previous line (Up)
Other movements:

C-a Beginning of line.
C-e End of line.
C-v View next screen.
ESC v View previous screen.
ESC < Start of file.
ESC > End of file.
ESC f Forward a word.

ESC b






Back a word.
Files and Directories
Files and Directories (1)


What is a File?
– a container for ordered data
– persistent (stays around) and accessibly by name
UNIX files
– regular UNIX files are pretty simple
 essentially a sequence of bytes
 can access these bytes in order, or access a
particular offset from the front
– A UNIX file is identified by a name in a directory
 this name is actually used to resolve the
– hard disk name/number, the cylinder number,
the track number, the sector number
– you see none of this
 it allows the file to be accessed
Files and Directories (2)

UNIX files come in other flavors as well
– Directories
 a file containing pointers to other files
 equivalent of a folder on a Mac or Windows
– Links
 a pointer to another file
 used like the file it points to
– Devices
 access a device (like a soundcard, or mouse, or ...) like it
is a file
Directories (1)
More about directories
 The Current Working Directory
– the shell remembers the Current Working Directory
 the directory you are looking at right now
– To determine the Current Working Directory use the
command pwd (Print Working Directory)
Use: obelix[18]% pwd
Result: print the current working directory
E.g. /csd/faculty/bma/bin
– When you first log in, you are in your HOME directory
 to see what this is:
obelix[23]% pwd
/csd/faculty/bma
Directories (2)

Moving about the filesystem
– use the “cd” (Change Directory) command to move between
directories
Use:
obelix[19]% cd bin
Result: makes the current directory bin

Listing the contents of a directory
– use the “ls” (LiSt directory) command to list the contents of a
directory
Use:
obelix[20]% ls
Result: lists contents of current directory
E.g. 826/ code/ isup* smit.script
glom.c cs402@
Link
Directories
Executable
Directories (3)

The Upside-down tree
– the UNIX filesystem is organized like an (upside-down) tree
 at the top of the filesystem is the root

– write this as a lone slash: /
– this is NOT a backslash!
For example, you can change to the root directory:
obelix[21]%
obelix[22]%
TT_DB/
bin@
cdrom/
core
courses@
cd /
ls
dev/
devices/
etc/
export/
gaul/
home/
mnt/
sbin/
kernel/
net/
tmp/
lib@
opt/
usr/
local/
platform/ var/
lost+found/ proc/
vol/
xfn/
Directories (4)
Pathnames (1)

The UNIX filesystem spans many disks
– as a user you don’t know or need to know which physical disk things are
on
 in fact, you don’t even know which machine they are attached to:
disks can be “remote”

The Absolute Path
– specify the logical location of a file in the file system.
– Starting with slash, followed by the name of the subdirectories,
separating names with slashes:
– E.g. /csd/faculty/bma/bin/readme.txt
– This is the absolute path for my a readme.txt file under my home
directory.
– lists everything from the root down to the directory you want to specify;
accurate not convenient.
Pathnames (2)

Some shorthand
– in some shells (including tcsh, csh, and bash), your home
directory can be abbreviated as ~ (tilde)
– Example: obelix[26]% cd ~/bin
 change to the bin directory under your home directory
 this is where you usually store your own commands or
“executables”
– To quickly go home:
obelix[27]% cd
with no parameters, cd changes to your home directory.
– You can use cd ~ as well.
Pathnames (3)

Relative pathnames
– you can also specify pathnames relative to the current working directory
 this is called a relative pathname
– For example
obelix[28]% pwd
/csd/faculty/bma
obelix[29]% ls
bin/ code/ tmp/
obelix[30]% ls bin
readme.txt
obelix[31]% cat bin/readme.txt
Unix is easy!
notice: you don’t need to know your absolute pathname
– The above example is same as
 cat /csd/faculty/bma/bin/readme.txt


For most commands which require a file name, you can specify
a pathname (relative or absolute)
Pathnames (4)

Every directory contains two “special” directories:
. and ..
. : another name for the current directory
– this will prove useful later
.. : another name for the immediate parent
directory of the current directory
– use this to cd to your parent:
obelix[32]% pwd
/csd/faculty/bma
obelix[33]% cd ..; pwd
/csd/faculty
obelix[34]% cd ..; pwd
/csd
– it can be used in a relative pathname
obelix[35]% cd ../csd
– You did nothing!
Pathnames (5)
 You
can locate a file or directory by this way:
– look at the first character of the pathname
/
start from the root
.
start from the current directory
 ..
start from the parent directory
~
start from your home directory
 else start from the current directory
– going down to the subdirectories in the pathname,
until you complete the whole pathname.
– for example:
 /csd/faculty/bma/cs211/readme.txt
 ~/cs211/readme.txt
 cs211/readme.txt