Linux Envrironment
Download
Report
Transcript Linux Envrironment
Chapter 1
LINUX Environment
Heejune AHN
Topics:
Linux History
Linux User Environment
Linux Development Environment
What is Operating Systems ?
Application Program vs System Program
Provides uniform resource (hardware) access mechanism to
Application Program/programmer
same access mechanism to different hardware: Resources
include CPU, memory, disk, tape, printers, terminals, modems,
etc.
Resource Efficiency and Protection: multi-tasking, multiprocessing, multiuser
Provide HW resources to multiple task, and protect mis- or
over-use of one task.
–2–
BASIC-OS, MSDOS, Unix OS, Linux OS, Windows OS, Apple
OS
SNUT, Heejune AHN
History of Linux
Unix
developed at Bell Labs (AT&T, Kerningham ) in 1969 for
minicomputer
coded over 90% in C, not assembler: easy port to different
machines
Xnix family
UC Berkely BSD, At&T SVR4 version
SUN Solaris, IBM AIX, HP UNIX, SCO-Xenix etc
GNU(Gnu's not Unix) project
–3–
Richard Stallman
started in early 80's to create and promote free software
SNUT, Heejune AHN
Linux’ Birth
Linus Torvalds in the 1990's, released to the Internet in 1994
about 1/3 of Linux is GNU code from the Free Software
Foundation - a Linux distribution consists of Linux kernel +
GNU compilers/tools/utilities + other free software
Popularity
Linux uses the Open Source model for development - code
is placed on the Internet, users download and test it,
programmers improve it and place it back on the web there
is competition among programmers to fix bugs and improve
Linux
Packages : Redhat (now Fedora), Suse Linux, IBM etc
Latest version : Release 2.6.x
–4–
SNUT, Heejune AHN
Linux Internal
Linux (wide-sense) = Linux Kernel + Utilities + alpha
Utilities + Applications
Shell
Linux Kernel + Drivers
HW (CPU + MEM + Peripherals)
–5–
SNUT, Heejune AHN
Linux Internal
Hardware : CPU, Memory, Disk, Peripherals, surrounded by Kernel
UNIX Kernel (basic OS)
Process Management : CPU scheduling
Memory Management : Memory allocation, protection, Paging etc.
File system : secondary/permanent data management
Device Drivers : peripherals control
IPC (inter-process communication) : signal, pipe, fifo, semaphore,
message queue, share memory etc
Network : TCP/IP, IPX, UNIX protocols etc
Utilities/applications (or commands)
mozila, telnet, apache, mincom, gcc, make etc.
Shell (user interface, command interpreter, and some built-in
commands)
Bourne shell (sh), C shell (csh), Korn shell (ksh), Bourne again shell
(bash), TC shell (tcsh), Z shell (zsh)
–6–
SNUT, Heejune AHN
–7–
SNUT, Heejune AHN
Yes, I am a Linuxer!
Entering Commands, Logging In & Out
To login, use a telnet program, and provide userid and
password
DOS>telnet 10.2.32.210
login:yourId
Password:
–8–
To logout, use logout, exit, or control-d
Use passwd to change password
SNUT, Heejune AHN
Unix File system
Linux File Structure
Everything in Linux is file : file, directory, devices …
hierarchical (tree, directory within directory)
root directory is / - ancestor of all files and directories on
system
directories contain files and/or other directories to be safe, for file and
directory names use alphabetics, numerics, underscore, period, or
comma some UNIX systems allow up to 255 characters in a filename,
others only allow up to 14
Filename
absolute pathname : starting with ‘/’, I.e. root )
relative pathname : not starting with /, relative to your current
directory
. (current), .. (parent directory)
–9–
SNUT, Heejune AHN
Access Permission
Linux is multi-user minded OS
User (me), Group (my team), and Others
Permission
Read
Write
Execute
chmod (explain later)
used to alter access permissions to a file or directory
read/write/execute for user(owner)/group/other
chmod xxx filename (xxx is 3 octal digits representing the binary
string rwxrwxrwx where the first three characters are
read/write/execute permission for the user, the next three for the
user's group, and the last three for all others)
chmod 640 file1
chmod u+r filename
chmod u+x file1
– 10 –
SNUT, Heejune AHN
Unix Journey
standard directories & files
– 11 –
/home typically contains the home directories of all users
/usr/bin or /bin contains standard utility programs
/sbin and /usr/sbin contain utilities for system administration
/etc contains admin & configuration files, such as
/etc/passwd
/var contains files that vary as the system is running, such
as temp, log, spool, and mailbox files
/dev contains files representing peripheral devices (device
drivers)
/tmp is used by some programs for temporary files
SNUT, Heejune AHN
Basic Linux commands: 1
cd [dirPath]
change current directory , cd (to hone directory)
ls
- lists information about files and directories
Ls –al - "a"ll files (including hidden), "l"ong form
ls -F - indicates file type, / for directory, * for executable
pwd : show your current directory
mkdir - to create directories
mkdir -p - to create any parent directories not already existing
mv - move (rename) filet/directory
rmdir - to delete empty directories, eg. rmdir directory-list
rm - to delete files, eg. rm file-list
rm -r - to delete directories including files and subdirectories
rm -ir - to delete directories including files and subdirectories, with
prompt to confirm removal
– 12 –
SNUT, Heejune AHN
Basic Linux commands: 2
head -n filename - displays first n lines, 10 is default
tail -n filename - displays last n lines, 10 is default
sort filename - displays in sorted order
more or less - displays file contents one page at a time
<space bar> - will go to next page
b - will go to previous page
<enter> - will go down one line
/string - will search for string within document being viewed
q - will quit
touch - creates a new file, or updates stats on an existing file
diff file1 file2 - compares 2 files
indicates lines to add, delete, or change to convert file1 to file2
wc - displays various counts of the contents of a file
wc –l | c | w filename - number of lines / character/ words
– 13 –
SNUT, Heejune AHN
Basic Linux commands: 3
cp source-file destination-file
to copy files (careful, overwrites destination with no warning)
mv existing-file new-file - to rename files & directories
file filename
gives info about the contents of the file
whereis utility
lists all libraries in your path containing a utility of this name
which utility
lists specific library in your path that would be used to access this
utility
ln (link) command gives a file an additional name, or pointer, from
the same or a different directory
ln file1 file2 - gives file1 the additional name file2 (hard link)
ln -s file1 file2 - gives file1 the additional name file2, as an indirect
pointer (symbolic link)
– 14 –
grep 'string' file - print lines in file that contain the string
SNUT, Heejune AHN
Basic Linux command : 4
Chmod
Permission
x permission : execution for normal file, pass-through for a directory
r permission for a directory allows viewing of file names in the directory,
but no access to the files themselves (regardless of the files'
permission settings)
x and r permissions for a directory allow viewing of file names, and
access to any files which have appropriate permissions set
permissions can only be changed by file owner or superuser (system
administrator)
Usage
eg. chmod g-w file1 - would take away write permission from the user's
group if they had it before
eg. chmod o=r file1 - would set all others' permission to read only
regardless of what they had before
chmod -r ( Recursive)
– 15 –
SNUT, Heejune AHN
umask
– 16 –
defines default permissions for newly created files, doesn't
change permissions on existing files
default permissions will be 777 minus umask arg for
directories, 666 minus umask arg for files
eg. umask - by itself, shows current umask setting
eg. umask 077 - new files will be 600, new directories will be
700
eg. umask 023 - new files will be 644, new directories will be
754
SNUT, Heejune AHN
Basic Linux commands: 5
echo
text or $variable (eg. $USER)
printf
similar to echo but can use C-style formatting
allows escapes: eg. \n (newline) \t (tab)
eg. printf "This is a menu\n\t1. Item 1\n\t2. Item2\n\t3. Item3\nEnter
choice: "
date - gives date and time
allows extensive formatting
eg.: date +"Today is %A %B %d, %Y"
Print command
lpr filenames - to print files, lp on some systems
lpq - to list print queues, lpstat on some systems
lprm - to remove files from print queue
– 17 –
SNUT, Heejune AHN
Basic Linux commands: 6
Script
different from each shells
variables
String : e.g. var1=“hello heejune!”
environment variable : $HOME, $PATH, $n (argument)
flow control
if, elif, for, while, until, case
comparison
» string
» numeric
=, !=, -n, –z
-eq, -ne, -gt, -ge, -lt, -le, !, -d, -d/e/f/g/r/s/u/w/x
list
process successive command, &&, ||
function
user defined function e.g. Fun1(){ echo “this is function1”}
Shell-Internal function e.g. Echo, eval, exec, exit, export ….etc
– 18 –
SNUT, Heejune AHN
Configure your environment
configuration file
Make your life easy / customization
Configuration file is often start with “.”
Look after ls –a in your HOME
.bash_profile, .bashrc, .bash_login, .bash_logout, .exrc etc
.bash_profile
Started when you log-in/new terminal
Put your preference there
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
– 19 –
SNUT, Heejune AHN
Communication with Others : Optional
Who
w - information about users logged on to system
who am i, whoami - information about your session
who -T - displays additional info, such as message acceptance
finger - info about users on system
finger username [@remotemachine]
» info about a specific user on system - looked up in /etc/passwd file, can
also use first or last name instead of userid
talk user : character by character conversation with another user,
end with delete
write user : line by line conversation with another user, end with
control-d
mesg n, mesg y - deny or accept messages (or mesg -n, mesg -y)
mail users - mails a message to users, end with control-d on line by
itself
– 20 –
mail [email protected] < filename - mails a file to a user
pine - another mail program
SNUT, Heejune AHN
Help me !!
Online Documentation
man command
online help for command, if online documentation installed by
– 21 –
Unix administrator
man -k keyword - eg. man -k sort - searches through all man
sections
man uses more to display information:
<space bar> - will go to next page
b - will go to previous page
<enter> - will go down one line
/string - will search for string within document being viewed
q - will quit
info - is replacing man on Linux
SNUT, Heejune AHN
Software development tool flow
Gnu tool-chain
Editor (vi, emacs)
Source (*.c, *.cc,
*.h)
preprocessor
compiler (front/backend)
Object files (*.o, *.obj)
archiver
linker
Library
*.a
*.so
Executable (a.out, *)
loader
– 22 –
SNUT, Heejune AHN
vi editor 사용법
Vi Editor
Most popular, simple, but sill powerful editor in Linux
Provides visual env. Without window system
No menu scheme
사용법
vi 파일명
view 파일명
: vi in read-only mode
PC 버전 (freeware) 도 있음
Mode
3 modes
input (editing) mode : 내용 입력, 타이핑한 내용이 화면에 나옴
command mode
last line mode
– 23 –
: 간단한 editor 명령지원, 화면에 타이핑이 나오지 않음.
: 복합 명령 지원
SNUT, Heejune AHN
Mode change
to input mode:
insert - i, I (before cursor, beginning of line)
append - a, A (after cursor, end of line)
open - o, O (below, above)
to command mode
<escape>
to last-line mode
(from command mode)
“:”
– 24 –
SNUT, Heejune AHN
Symbols in VI
w (word) , c (character)
$ (endof line, end of file), 0 (begin of line, file),
(, ) ( beginning or end of sentence
{, } - beginning or end of paragraph
Cursor 이동
return, space, arrow keys, or h, j, k, l (left, down, up, right)
repetition factor, eg. 12j will move down 12 lines
H, M, L - move cursor to top, middle, bottom of screen
^d, ^u, ^f, ^b (^는 CTRL key)
scroll forward 1/2 screen, backward 1/2 screen, forward full
screen, backward full screen
– 25 –
nG - move to line n, or last line if n not specified
SNUT, Heejune AHN
Delete
x - delete character under cursor ( repetition factor, eg. 3x )
dw, dd - delete text (word to the right, line) and copy to buffer
can use repetition factor, eg. 5x, 7dw or d7w, 12dd or d12d
D - delete to end of line
Copy & Past
yw, yy - yank - copy text (word, line) to buffer
can use repetition factor eg. 5yw or y5w, 12yy or y12y
– 26 –
Y - copy to end of line
p, P - paste buffer after or before cursor if buffer contains
words, and below or above current line if buffer contains
lines
SNUT, Heejune AHN
Linux Internal
Replace
r - replace character under cursor
ns - substitute number of characters specified by n
cw, cc - change - delete text (word, line), copy text to buffer,
and leave in input mode (repetition factor, eg. 5cw or c5w,
12cc or c12c)
Undo & repeat
– 27 –
u - multiple levels on some systems, one level on others
. - repeat most recent command that made a change
C - change to end of line
J - join current and following line
SNUT, Heejune AHN
last-line mode
/pattern - searches for the next occurrence of pattern after
the current cursor position
?pattern - searches backwards through the file
n - repeat last search, N will repeat in opposite direction
/ - repeat last search forward, ? to repeat backward
Finish vi
“ZZ” or “:wq” (write and quit)
“:q!” (quit without writing), “:q” (quit, only allowed if not
changed since last save)
– 28 –
SNUT, Heejune AHN
GNU C Compiler
GCC ( Gnu C Compiler /Gnu compiler collection)
Global command, ie. it deos not do work itself
gcc call C preprocessor, compiler, assembler and linker
Cf ) g++ for compiling cpp code
{cpu}-{loader}-gcc e.g.) arm-linux-gcc
사용 법: gcc [options] file
E.g. gcc -o test test.c
For full description, read Manual
--help Display this information
Output Options
-c Compile and assemble, but do not link
-o Place the output into
-E Preprocess only; do not compile, assemble or link
-S Compile only; do not assemble or link
– 29 –
SNUT, Heejune AHN
Code generation
-g Include Debugging Information
-On Optimization (n = 1,2,3), speed, memory, but longer compile time
-Wn Warning (e.g. -Wall, ...)
-m Machine dependent handling (e.g. -msoft-float -m)
-fxxx code generation (e.g. PIC: relocatable code)
Directory options
-llibraryname -static, -shared library to link
-Llibpath non standard library Location
-Iheaerpath non standard header path
ETC
– 30 –
-D Preprocessor Macro-definition
-x Specify the language of the following input files
-Wa, Pass comma-separated on to the assembler
-Wp, Pass comma-separated on to the preprocessor
-Wl, Pass comma-separated on to the linker
SNUT, Heejune AHN
GNU Make utility
Project and Make Utility
Compile process automation required
most modern project has more than 100 source files
Batch file (script) is ok for it.
Intelligent compilation
Many c files -> long Compile time
Make utility checks the dependency and compile only the
required to compile.
.
– 31 –
SNUT, Heejune AHN
How to make "Makefile"
1.
2.
write definition section : define Macros, eg. CC, CFLAGS etc
write dependency
target and its sources
3.
write commands
Notes
# is for comment
Variable: $variable, $@ is target, $< is source
commands must start with "tab" not space or something
How to Run it
– 32 –
make <-f file> target . . . (eg. make -f mymakefile target1
target2)
make (for gnuMakefile, makefile, Makefile and "all" target)
SNUT, Heejune AHN
Sample (most primitive)
#definition section
CC=gcc
CFLAGS=-O3 –g
# command section
OBJS = main.o first_func.o second_func.o
all : main
main : $(OBJS)
$(CC) -o $@ $(OBJS)
main.o : main.c func.h
first_func.o : first_func.c
second_func.o : second_func.c
.c.o : $(CC) -c $<
clean :
rm -f main *.o
– 33 –
SNUT, Heejune AHN
How it works
read the makefile, parse it (rule check), build the
dependence info
check the dates of each files
run the commands (explicit or implicit)
Details
– 34 –
Real world is more dirty, see Manual
SNUT, Heejune AHN
Preprocessor
Preprocessor
Before compiler process the source file, the preprocessor expands
or substitutes MACROs.
For Conditional compile, in-line function (faster than real function),
include files
Rules
#include <file> | "file"
include file from IMPLICIT ( or EXPLICT )
#define MARCO [DEFINITION] , #undef
#define DEBUG
#define ADD(a,b) ((a) +(b))
#define EOF (-1)
#undef DEBUG
#error, #warning
#warning "this is warning" (error stops the compile process, while
warning does not stops on "most good" tool-chains)
– 35 –
SNUT, Heejune AHN
Conditional Processing
#if #ifdef #ifndef #else #elif #endif defined()
#ifdef DEBUG
printf(" data : %0x\n", data);
#endif
#if CPU == MIPS
....
#elif CPU == x86
....
#endif
Predefined MACROs
– 36 –
__DATE__
__FILE__
__LINE__
__STDC__
__TIME__
(%s): Sep 5 2004
(%s): main.c
(%d): 23
(%d): 1
(%s): 02:11:54
SNUT, Heejune AHN
# : variable name not the variable itself
#define INTVAR(var) printf("%s:%d \t %s = %d\n", __FILE__,
__LINE__, #var, var);
## : Concatenate (연결) the variable names
#define x(i)
x##i
int x(i), x(2), x(3); x(1) = 100; x(2) = 200; x(3) = 300;
int x1,x2, x3 ; x1 = 100; x2 =200; x3 = 300;
– 37 –
은
과 같음.
SNUT, Heejune AHN
주의 사항
use the "(" parenthesis not to be mis-expanded
#define ADD(a +b) (a) + (b)
ADD(1 + 2) * 3 =====> not 9 but 7 !!
MARCO does not check the Syntax
#define MOD(a,b) ((a) % (b))
MOD(7.0, 5.0) -> ok for preprocessor, but error in compiler!!
MARCO is always good for performance (speed).
Abusing Macros make the code lengthy and need more code
memory than Instruction Cache can do.
Trouble shooting
“gcc -E src.c” and investigate the bug!
– 38 –
SNUT, Heejune AHN