UNIX and C Programming

advertisement
DT089/2
SOFT 2289 Software Development 3
Chapter 1: Introduction to the UNIX Environment
1.
Topics






Brief History of UNIX
Overview of the UNIX Operating System
UNIX File System structure
Fundamental UNIX commands, facilities and UNIX tools
UNIX shells
Basic system administration functions
2.
UNIX Introduction
2.1
Brief history of UNIX
UNIX is an operating system, that is, a suite of programs that make the computer work. The
operating system controls the resources of a computer and allocates these resources among
its users. It lets the users run their programs, it controls the peripheral devices such as
printers and discs, and it provides a file system that manages the long-term storage of
information such as programs and documents.
UNIX was created at Bell Labs by Ken Thompson, Dennis Ritchie and others. In 1969 the
first version was written in assembler for a DEC PDP-7 as a small general-purpose timesharing operating system. This was rewritten in 1973, by Thompson and Ritchie, using the
C programming language, making the operating system portable. This Fifth Edition of
UNIX was made available to Universities at a low cost.
The Seventh Edition, released in 1978, marked a split in UNIX development into two main
branches:
 BSD (Berkeley Software Distribution) from the University of California at Berkeley.
 SYSV (System 5) developed by AT&T and other commercial companies.
UNIX flavours based on SYSV have traditionally been more conservative, but better
supported than BSD-based flavours. Today SYSV and BSD UNIX are very similar, with
some minor differences are to be found in file system structure, system utility names and
options and system call libraries.
Linux is a free open source UNIX OS for PCs that was originally developed in 1991 by
Linus Torvalds, a Finnish undergraduate student. Linux is neither pure SYSV or pure BSD.
Instead, incorporates some features from each.
106757748
Page 1
DT089/2
SOFT 2289 Software Development 3
Figure 1
UNIX History
Because UNIX is written almost entirely in the C programming language, UNIX is one of
the most portable operating systems ever developed. It has been implemented on computers
ranging from desktop microcomputers to Cray supercomputers.
3.
Overview of the UNIX operating system
UNIX is a multi-user, multitasking operating system. This means that it supports more than
one user at a time, and can have more than one task, or job, running at one time. Each task
gets a certain percentage of CPU time, delegated by the kernel. UNIX systems are preemptively multitasking, which means that the operating system is in control of when a task
will get CPU time, allowing all the tasks to be running simultaneously, each taking turns.
The UNIX operating system is made
up of three parts:
 kernel,
 shell
 programs.
Figure 2
106757748
Page 2
UNIX Operating System
DT089/2
3.1
SOFT 2289 Software Development 3
The kernel
The kernel of UNIX is the hub of the operating system: it allocates time and memory to
programs and handles the filestore and communications in response to system calls.
3.2
The shell
The shell sits between you and the operating system, acting as a command interpreter (CLI).
The user interacts with the kernel through the shell. It reads terminal input and translates the
commands into actions taken by the system. The commands are themselves programs: when
they terminate, the shell gives the user another prompt ($ on shell we shall use). The shell is
analogous to command.com in DOS.
When you log into the system you are given a default shell. When the shell starts up it reads
its startup files and may set environment variables, command search paths, and command
aliases, and executes any commands specified in these files.
Bourne shell (sh)
This is the original Unix shell written by Steve Bourne of Bell Labs. It is available on all
Unix systems. However, this shell does not have the interactive facilites provided by
modern shells such as the C shell and Korn shell. The default prompt for the Bourne shell is
$ (or #, for the root user).
C shell (csh)
This shell was written at the University of California, Berkley. It provides a C-like language
with which to write shell scripts, hence its name. The C shell was popularised by BSDderived versions of UNIX, such as SunOS. It provides more features than the Bourne shell,
but is already being phased out on many commercial UNIX platforms in favor of more
powerful shells such as the Korn shell. The default prompt for the C shell is%.
Korn shell (ksh)
This shell was written by David Korn of Bell labs. It is now provided as the standard shell
on UNIX systems. It provides all the features of the C shell together with a shell
programming language similar to that of the original Bourne shell. The Korn shell is the
most efficient shell in that it consumes fewer system resources than the Bourne and C shells.
Numerous other shells are available from the network. Almost all of them are based on
either sh or csh with extensions to provide job control to sh, allow in-line editing of
commands, page through previously executed commands, provide command name
completion and custom prompt, etc. Some of the more well known are the Bourne Again
SHell, bash, from the Free Software Foundations GNU project, based on sh, the T-C shell,
tcsh, and the extended C shell, cshe, both based on csh.
3.3
UNIX Programs
A program, or command, interacts with the kernel to provide the environment and perform
the functions called for by the user. A program can be: an executable shell file, known as a
shell script; a built-in shell command; or a source compiled, object code file. System
programs are usually binary, having been compiled from C source code. These are located in
places like /bin, /usr/bin, /usr/local/bin, etc.
106757748
Page 3
DT089/2
SOFT 2289 Software Development 3
4.
UNIX File System structure
4.1
Introduction
Ordinary files can be ASCII or binary or a combination, and may contain, for example,
text data, a shell script or compiled object code for a program.
Directories are containers or folders that hold files, and other directories.
Note: unlike other operating systems, UNIX filenames are not broken into a name part and
an extension part (although extensions are still frequently used as a means to classify files).
Instead they can contain any keyboard character except for '/' and be up to 256 characters
long (note however that characters such as *,?,# and & have special meaning in most shells
and should not therefore be used in filenames). Putting spaces in filenames also makes them
difficult to manipulate - rather use the underscore '_'.
A process is an executing program identified by a unique PID (process identifier).
4.1.1
The Directory Structure
All the files are grouped together in the directory structure. The file-system is arranged in a
hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called
root, denoted by /.
Figure 3
Directory Structure
In the diagram above, we see that the directory students contains the subdirectory dt289-2
Each node is either a file or a directory of files, where the latter can contain other files and
directories. You specify a file or directory by its path name, for example:
/export/home/students/dt289-2/jbloggs/clabs/clab1/prog1.c
106757748
Page 4
DT089/2
SOFT 2289 Software Development 3
5.
Getting Started with UNIX
5.1
Login from Workstation
After connecting with a Unix system, a user is prompted for a login username, then a
password. The login username is the user's unique name on the system. The password is a
changeable code known only to the user. At the login prompt, the user should enter the
username; at the password prompt, the current password should be typed.
Note: Unix is case sensitive. Therefore, the login and password should be typed exactly as
issued. When your account is issued, you will be requested to change your password for
system and personal security.
When setting your password:
 use at least 6 characters, at least two of which should be numeric
 use a mixture of character types (alphabetic, numeric, special)
 use a mixture of upper case and lower case
 choose a password you can remember
5.2
Login to UNIX from PC LAB using Exceed
Exceed is an application that transforms PC into a fully functional X Window terminal. It
lets you run and display UNIX applications (X clients) from the familiar Microsoft
Windows environment. X Clients interact with Exceed to appear on your computer screen
as they would on a remote UNIX or VMS terminal/workstation.
Exceed is based on the client-server model. This means that it works on a series of userlevel programs known as clients and servers. Servers are software that control the user’s
display. Clients are application programs, which operate independently of the server.
Clients send requests to the server to perform graphics or window operations. Servers
respond to these requests. Exceed facilitates this communication between the server and the
clients.
5.3
Desktop Environment
The Common Desktop Environment (CDE) for the SolarisTM Operating Environment
provides workstation users with powerful client/server computing resources through an
industry standard, easy to use interface. CDE provides users with customisable workspaces,
graphical system monitoring, and enhanced productivity tools.
CDE is the result of Sun, HP, IBM, and USL cooperating to form a single Motif-based
standard for Unix system interfaces. Most major Unix vendors now provide the CDE as
standard.
106757748
Page 5
DT089/2
SOFT 2289 Software Development 3
Upon login, the user is presented with the CDE Desktop. The desktop includes a front panel,
virtual workspaces, and window management. CDE supports the running of applications
from a file manager, an application manager and from the front panel.
Figure 4
CDE
Click on the right mouse button and display the Workspace menu as shown in the
figure above.
5.3.1
Front Panel
The front panel contains a set of icons and popup menus that appear at the bottom of the
screen. The front panel contains the most regularly used applications and tools for
managing the workspace. A user can configure several virtual workspaces -- each with
different backgrounds and colours if desired.
Figure 5
Clients, Servers and Xlib
5.3.2
File Manager
CDE includes a standard file manager. The functionality is similar to that of the Microsoft
Windows, Macintosh, or Sun Open Look file manager. Users can directly manipulate icons
associated with UNIX files, drag-and-drop them, and launch associated applications.
5.3.3
The Application Manager
The user interaction with the application manager is similar to the file manager except that is
intended to be a list of executable modules available to a particular user. The user launches
the application manager from an icon in the front panel. Users are notified when a new
application is available on a server by additions (or deletions) to the list of icons in the
application manager window.
106757748
Page 6
DT089/2
5.4
SOFT 2289 Software Development 3
Starting a Terminal session
A terminal emulator is an application that displays a window where you can use UNIX
commands, and cut and paste text within or between terminal emulation windows.
Figure 6
Terminal Session
The command-line prompt is a special character that is displayed in your Terminal window
after you press [Return]. It can be a %, :, <, $, or other special character. A small box or bar,
called a cursor, shows where characters appear in the window when you type something on
the keyboard.
There are several ways you can start a terminal emulator:
 From the Front Panel
 From File Manager
 From a command in an existing terminal window
 From Application Manager
 From New in the Terminal Window pull-down menu
5.4.1
To Close Terminal
1. Type exit at the command line and press Return.
2. Or, Choose Close from the Window menu.
3. Or, choose Close from the window menu pull-down menu, which can be accessed
from the button at the upper left corner of the window manager frame.
Note: typing exit at the command line is the preferred method of closing Terminal. Closing
it through the menus does not terminate any background processes you started from
Terminal, which can sometimes cause problems.
106757748
Page 7
DT089/2
SOFT 2289 Software Development 3
6.
Fundamental UNIX commands, facilities and UNIX tools
6.1
Introduction
A UNIX command line consists of the name of a UNIX command (actually the "command"
is the name of a built-in shell command, a system utility or an application program)
followed by its "arguments" (options and the target filenames and/or expressions). The
general syntax for a UNIX command is
command -options targets
where an option modifies the command, changing the way it performs. A target indicates on
what the command is to perform its action, usually a file or series of files.
Commands are case sensitive. Options are generally preceded by a hyphen (-), and for most
commands, more than one option can be strung together, in the form:
command -[option][option][option]
Summary of useful UNIX commands
106757748
Command
Description
cat File
concatenate (list) File
cd
change directory
cp file1 file2
copy file1 to file2
date
report current date and time
diff file1 file2
compares two text files and displays difference
ls
List directories/files
mkdir myDir
make directory
mv file1 file2
move file1 to file2
pwd
print working (current) directory
rm file
remove file
rmdir myDir
remove directory
Page 8
DT089/2
6.2
SOFT 2289 Software Development 3
Fundamental Directory Commands
Fundamental Directory Commands
•
•
•
•
•
6.2.1
pwd
cd
ls
mkdir
rmdir
> print working directory
> change working directory
> list files and directories
> make adirectory
> remove (empty) directory
Print working directory
To get the path to your root directory, after you log, type the command:
pwd
This will respond with your root path. Pathnames allow you to work out where you are in
the relation to the whole file-system.
6.2.2
List command
To list the files available in the current directory, type the command:
ls
The list command ls has a number of options that can be used, for example:
6.2.3
ls –l
this gives a long listing with much of the file information
ls –a
this lists all files in the current directory including hidden files (files beginning
with a dot (.) usually containing important configuration information)
Make new directory
To create a new subdirectory the mkdir command is used, for example:
mkdir MyDir
6.2.4
Changing to a different directory
To change the current working directory, the cd command is used. The current working
directory may be thought of as the directory you are in, i.e. your current position in the filesystem. For example:
106757748
cd MyDir
to change to the directory you have just made
cd
To return to your own root path from any directory,
cd ..
To go one directory up the hierarchy, i.e. the parent directory of the current
directory
Page 9
DT089/2
6.2.5
SOFT 2289 Software Development 3
Remove an empty directory
To remove an empty directory, i.e. a directory that contains no files, the rmdir command is
used, for example:
rmdir MyDir
6.3
Text File Display Commands
Text File Display Commands
• clear
• cat file
> clear screen
> display contents of file on
screen
> display content of file one page
• less file
at a time
• head -n file > prints first 10 or -n lines
• tail -n file > prints last 10 or -n lines
To clear the terminal window of the previous commands, type:
clear
The command cat can be used to display the contents of a file on the screen, for example:
cat MyFile.txt
If the file is longer than the size of the window, it scrolls past making it unreadable. The
less command can be used to display such files one page at a time, for example:
less MyFile.txt
To see another page, press the scroll-bar key. To quit reading, type q.
The head command writes the first ten lines of a file to the screen, or if the –n parameter is
used, the first n lines are displayed, for example:
head –8 MyFile.txt
display the first eight lines of the file MyFile.txt
The tail command writes the last ten lines of a file to the screen, or if the –n parameter is
used, the last n lines are displayed, for example:
tail –8 MyFile.txt
display the last eight lines of the file MyFile.txt
106757748
Page 10
DT089/2
6.4
SOFT 2289 Software Development 3
File Manipulation Commands
File Manipulation Commands
•
•
•
•
6.4.1
cp file1 file2
mv file1 file2
rm file
grep “keyword” file
> copy file
> move file
> remove file
> search file for
keyword
Copying Files
The cp command is used for copying one file to another. For example, to make a copy of
File1.txt in the current directory and call the copy File2.txt, type
cp File1.txt File2.txt
6.4.2
Moving Files
The mv command is used to move a file from one place to another or to rename a file in the
current directory. For example:
mv File1.txt File2.txt
this renames the File1.txt file to File2.txt
mv File1.txt MyDir/.
this move File1.txt from the current directory to a subdirectory called MyDir
6.4.3
Removing Files
The rm command is used to remove files from a directory, for example:
rm MyFile.txt
removes the file MyFile.txt from the current directory
6.4.4
Searching Files
grep is a UNIX utility which can be used to search a file for specified word or patterns, for
example:
grep Hello MyFile.txt
each line containing the word “Hello” is printed out. The grep command is cased sentitive.
To search ignore upper/lower case, use the –i option, for example:
grep –i Hello MyFile.txt
To search for a phrase or pattern, enclose it in single quotes, for example:
grep –i “Hello World” MyFile.txt
Other options of grep are:
-v
displays those line that do not match
-n
precede each matching line with a line number
-c
print only the total count of matching lines
106757748
Page 11
DT089/2
6.5
SOFT 2289 Software Development 3
Information Commands
Information Commands
• who
• who am I
• whatis
• man
• history
> Display information about current
users
> displays information about this
connection
> brief description of a command
> manual help pages for most commands
> displays all commands you have
entered
To view users currently on the system, type who
There are online manuals which give information about most commands. To view these use
the man command or for a one-line description of a command use the whatis command.
man ls
whatis ls
To view your command history list, type history
To recall the last command type !! (C shell)
To recall the second most recent command, type !-2
6.6
Exiting
^D
^C
logout
exit
106757748
indicates end of data stream; can log a user off.
interrupt
leave the system
leave the shell
Page 12
DT089/2
SOFT 2289 Software Development 3
APPENDIX A:
Feature
kernel name
boot init
mounted FS /
default shell
FS block size
print subsystem
echo command
(no new line)
ps command
multiple wait
syscalls
memory access
syscalls
SYSV vs BSD
Typical SYSV
/unix
/etc/rc.d directories
etc/mnttab
sh, ksh
512 bytes->2K
lp, lpstat, cancel
echo "\c"
Typical BSD
/vmunix
/etc/rc.* files
/etc/mtab
csh, tcsh
4K->8K
lpr, lpq, lprm
echo -n
ps -fae
poll
ps -aux
select
memset, memcpy
bzero, bcopy
Differences between SYSV and BSD
APPENDIX B:
Examples of platforms:
– Sun Microsystems (SunOS/Solaris)
– Silicon Graphics (Irix)
– Digital Equipment Corporation
• MIPS (Ultrix)
• Alpha
– IBM PC/ Intel Architectures
• Linux
• SCO UNIX
• UnixWare
• FreeBsd
– Hewlett-Packard
– NeXT (Mach)
– IBM Workstations (AIX)
Appendix C: EXCEED
Starting Exceed
From the Startup menu, click on HummingBird, then Exceed and then XDMCP-broadcast
Startup mode.
The XDMCP-broadcast Startup Mode broadcasts an X connection request to the network
hosts. You can broadcast to hosts in the Host List File and/or those referenced by the
Broadcast Address. This Startup mode assumes that the host supports the X Display
Manager Control Protocol and contains a session file (script) to start clients.
The XDMCP Display Manager Chooser dialog box will appear. This dialog box displays a
list of potential XDMCP display managers. Select the snowwhite host name and click OK.
106757748
Page 13
DT089/2
SOFT 2289 Software Development 3
A Login dialog box will request you to enter your user name and then your password.
Appendix D: Ways to start a terminal emulator
To Start Terminal from the Front Panel
The Terminal control is located in the Hosts sub-panel.
Click the This Host control. The activity indicator spins, indicating that the terminal is being
activated. The Terminal window appears after a short time.
To Start Terminal from File Manager
Choose Open Terminal from the File menu.
This step opens a Terminal with the same current directory as the File Manager view.
To Start a Terminal Emulator Other Than dtterm
To use a terminal emulator other than dtterm, start it from a command line in an existing
terminal emulator window. At the command-line prompt, type the name of the terminal
emulator and any options you want. For example, to start xterm, enter:
xterm
options
[options] &
represent optional items to customise the terminal emulator.
&
specifies that the terminal emulator runs in the background, so you can continue
working in your original window while the terminal emulator is running.
The terminal emulator starts in the current workspace unless directed otherwise byoptions.
To Start Terminal from the Window Menu
Choose New from the Window menu in an existing Terminal window. A duplicate Terminal
window appears.
106757748
Page 14
Download