Transcript Unix

Lesson 1
PC vs. Multi-user System
Personal Computer – each user gets
his/her own processor (or multicore
processor).
 Multi-user system – The processor,
RAM, disk, and i/o devices are shared
among several users.
This can be accomplished through:

 multitasking
 timesharing
What is an Operating System?
An OS is software (i.e. a computer
program) that manages your computer.
Its main jobs are:
 Manage software
 Enable interaction with a user
 Control peripherals (e.g. disk, printer)
What is UNIX?
Unix is an operating system for multi-user
systems.
 Developed originally at Bell Labs in
1969 by Thompson and Ritchie.
 It is an Open System (no single
company owns it).
 It is portable – it can be installed on
different types of machines.
Main components of UNIX OS:
Kernel. Master control program of the
computer. In charge of managing
resources and multitasking.
 File System. Data is stored in files,
which are organized in directories.
 Shell. Accepts user commands and
passes them on to the Kernel.
 Utilities. These are the commands that
UNIX understands.

Review: Jobs of an OS
Manage software…. Kernel.
 Interact with User…. Shell (using
utilities).
 Control peripherals…. all peripherals are
part of the UNIX File System.
In summary: You type a “utility” into the
UNIX Shell, which gets passed to the
Kernel for processing.

Your UNIX Account
Since many users share one large disk,
each user is given a “piece” of the disk,
called an account.
 Each part of the disk has permissions
associated with it. You will have
permission to view only your own files.
 In order to work in your account, you will
login with a username and password.
(to be done before our next class.)

Shell Utilities
% date
% who
% whoami
% mail
% pine
(see chapter 20 of text for more mail
details)
% man
The UNIX File System (ch. 6)

The File System is used to organize
data. In UNIX, anything from which data
can be taken/sent is called a file.
 ordinary file on disk
 printer
 keyboard

Ordinary files are either of type:
 text – each character is stored as its ASCII
value.
 binary – to be read only by computer.
Directories
A directory in UNIX corresponds to a
Folder in Windows (a place on the disk
that can hold files and directories).
 When you are logged in to your account,
you are always in some directory. That is
called the current directory.
 Your “home” directory is the directory
that you are in by default each time you
login.

Directory Tree

Directories have a hierarchical structure,
represented by a Directory TREE.
root /
bin
…….
dev
etc home
tmp users1
sokol …...
root /
bin
dev
etc home
tmp users1
…….
sokol …...
courses
forms papers
CIS15 CIS23 CIS52
syllabus.htm hmwk1.htm
Pathnames
A pathname is the address of a file or
directory. It is the path of the file (or
directory) in the Directory Tree.
 An absolute pathname is the path from
the root to the specified file.
e.g.
/users1/sokol/courses/CIS15/syllabus.htm
/ represents the ROOT

Note: in Windows, the backslash \ is used to separate levels of
the tree. All web addressed, i.e. URL’s, use the forward slash
as in UNIX.
Relative Pathname
A relative pathname is the path in the
directory tree beginning with the “current
directory.”
e.g. Suppose I am working in my home
directory. I can refer to syllabus.htm as:
courses/CIS15/syllabus.htm
Or, if my current directory is CIS15, I can
simply say syllabus.htm

Naming Files and Directories
You do not have to use filename
extensions, although you can if you
wish.
e.g. notes.txt
myprogram.cpp
 UNIX is CASE SENSITIVE!
 Do NOT use spaces and special
characters in your filenames (although .
_ , are allowed).

Some Shell Commands
pwd prints working (or current) directory
 cd
change directory [without
arguments this brings you to home
directory]
 ls
lists files in current directory
Many commands take options. Example:
ls –la detailed list, and includes hidden
files, which are those whose name
begins with a . (e.g. .profile, .login)

How can you create a file?
Copy or move another file
2. Redirect standard output
3. Use a Text Editor
4. Computer program writes to a file
1.
cp pathname1 pathname2
mv pathname1 pathname2
2. Redirection
You can redirect the output of a
command:
e.g. ls > mylist
spell > sperrs

“funnels” the output to a file instead of
screen.
 > erases file if it exists
 >> concatenates to end of existing file
3. Text Editor
emacs
 vi
 pico

Choose one of these and learn it since you
have to be able to type up a text file in
UNIX.
4. Computer program
We will cover files in C++ in detail (Chapter
12 of text) so stay tuned…
Permissions
The permissions of each file and directory
can be viewed as a sequence of 10 bits.
d stands for directory
r read permission
w write permission
x execute permission
drwxrwxrwx
owner group public
Changing Permissions
Your web page has to grant read and
execute access to the public.
 How can you change permissions?
use the chmod command
examples:
chmod 111001001 public_html
chmod 711 public_html
chmod a=rx mypage.html
chmod a+r mp.html

More shell utilities for File
Management
cat
 more
 less
 pg
 lpr
 rm
 mkdir
 rmdir

Pipes
You can send the output of one utility to
the input of another utility using |
example:
cat pgm1 | more
who | sort
(note: redirection using > sends to a FILE,
piping sends to another command.)
Use the ; to group commands.
example:
ls; who; cd

The grep Command
grep will search for a word inside of files
or directories. It is extremely useful
example
grep December pgm1.cpp
[this will list every occurrence of December
in the file pgm1.cpp]
OR
grep December pgm*.cpp
[this searches all files that begin with pgm
and end with .cpp]
Wildcard character (*)
Other examples of using the *
ls *.cpp
// this lists all of your .cpp programs
that are in the current directory.
cp *.output outfiles // copies all files ending
with .output into a directory called outfiles
Processes (ch 11)
Since UNIX is a multitasking system, a
single user can also run several
processes at once.
 You can either open different windows or
you can run a process in the
“background.”
For example:
%netscape &
This will open netscape but give you the
shell prompt back in your window.

Processes
If you would like to see a list of all your
active processes, use the command.
Example:
% ps

PID
TTY
25157 pts/11
25177 pts/11
25206 pts/11
TIME
00:00:00
00:00:00
00:00:00
CMD
ksh
tcsh
ps
If you want to terminate a process:
% kill 25177
Processes (cont)
If a process is running in the foreground,
and you want to kill it:
^C
(Ctrl+C)
 To suspend a process, use:
^Z
(Ctrl+Z)
To resume the suspended process:
% fg
(for a foreground job)
% bg
(for a background job)

Symbols
~
/
.
..
&
*
> >>
|
Programming under UNIX (ch32)
1st level language = machine language (a
binary sequence of 0’s and 1’s)
 2nd level language = assembly language
Here, you can use English-like statements to
represent machine level instructions.
So, 1 assembly language instruction
corresponds to one machine instruction.
 3rd level or “High” level languages

 easier to program
 portable
Program Translation
Your high-level language program must be
translated into machine language in
order for it to be able to run. This is a 3
step process.
1.
2.
3.
Preprocessing
Compilation
Linkage
1. Preprocessing
A preprocessor reads through your
program and replaces all preprocessor
directives.
 A preprocessor directive is any line that
begins with a #
e.g. #include <stdlib>
 Notice that preprocessor directives are
not C++ statements, and therefore do
not terminate in a ;

2. Compile
Compiling a program consists of
translating a high level language to
machine language, called object code.
 Object code is NOT executable yet…
To compile your C++ program, type:
% g++ -c mypgm.cpp
or
% CC –c mypgm.cpp
NOTE: the –c option says to only compile.
The CC are capital (lowercase cc is for C).

Compiling in UNIX
When you compile your program, the
compilation errors will display on the
screen.
 You should open your program in a
different window to correct the mistakes.
 If there are no compilation errors, a file
called mypgm.o will be created. This is
the object code of your program.

3. Linking
There is still one more step to obtain your
executable. It is called “linking.” Linking
brings together all library functions that
you included, all of your own functions,
and your main function and an
executable file is created.
% g++ mypgm.o
The same command is used for linking.
[You can have linker errors, such as a function that is
not found, or no main function, or multiply defined
main’s.]
Running your program
The output of the linker is an executable file
called:
a.out
You can check that it has executable
permission by typing _________.
To run your program:
% a.out
Output will display on the screen.
% a.out > myoutputfile
Redirects your output to a file.
Compiling and Linking in 1 step
You can invoke g++ to compile and link
together, but this will be useful only for
small programs.
% g++ mypgm.cpp
This will first compile and then link. Errors
are labeled either as compiler or as
linker errors.