Transcript UNIX-intro
Introduction to
Linux/UNIX
History
• UNIX beginnings in 1969 (Linus Torvalds is born!)
• AT & T Bell Laboratories (Ken Thompson & Dennis Richie)
• Working version (in 1970) on a DEC PDP 11/70
• UNIX and C Relationship
• June 81: Berkeley UNIX 4.1 BSD (enhanced with vi, csh, and
virtual memory management)
• 1983: Berkeley UNIX 4.2 BSD (added TCP/IP networking,
sockets and a new file system)
• Later UNIX/32V, SYSTEM III, SYSTEM V
• and Linux is introduced by Linus B. Torvalds in 1991
GNU
Project
• Launched in 1984 to develop a complete UNIX-like Operating System
that is free software
www.gnu.org
• Organized by free software foundation
Free GNU Software: http://directory.fsf.org/GNU/
• Usually C code, but some C++
• The GNU System provides a collection of applications, libraries, and
developer tools, plus a program to allocate resources and talk to the
hardware, known as a kernel
Basics
• Talking to Linux/UNIX The Shell
– displays the prompt for your next command
– parses your command input and executes it
– several different kinds: Bourne Shell or sh, ksh, csh, and tcsh
•
Setup your environment variables:
.bashrc
• Home directory
/user/local/home/YourLoginName
• Your files are at:
/user/local/home/YourLoginName/SDRIVE
• File/Directory structure A tree
Useful UNIX/Linux Commands
http://web.mst.edu/~ercal/284/UNIX-fork-exec/UNIX-commands.txt
• A subset of these commands are:
alias/unalias, bg, cat, cc, cd, chmod, cp, csh, diff, du
echo, emacs, fg, gcc, grep, history, jobs, kill, ln, logout
lpq, lpr, lprm, ls, man, mkdir, more, mv, printenv, ps
pwd, rlogin, rm, rmdir, rsh, script, set, setenv, source
tail, tar, telnet, touch, unsetenv, vi, wc, which, xterm
More Basics …
• Consulting the on-line manual man command
• Compiling with “gcc” and “g++”
• I/O Redirection
>
Redirect output
<
Redirect input
p & Run p in the background (try also jobs, ^Z, bg, fg, ps, and kill)
p1 | p2 Pipe output of p1 as input for p2
p >> f
Append output of p into file f
Command-line arguments
linux01 > myprog filename
int main( int argc, char* argv[])
{
argc = 2
argv
0
Myprog \0
1
filename
2
NULL
\0
}
Text Editing / data compression
• GNU emacs (/usr/bin/emacs)
• vi (/usr/bin/vim)
• /bin/gzip
/bin/gunzip
Writing Simple Shell Scripts
• Bourne Shell Programming (bash – standard Linux shell)
http://steve-parker.org/sh/sh.shtml
• Example: http://web.mst.edu/~ercal/284/ShellScripts/args
#!/usr/bin/env bash
echo name of script is $0
echo first argument is $1
echo second argument is $2
echo third argument is $3
echo eleventh argument is $11
echo number of arguments is $#
More examples…
http://web.mst.edu/~ercal/284/ShellScripts/SampleScripts.html
Example 1: Remove "Cntl-M" from a given file
Example 2: Keep track of users every 20 seconds
Example 3: List all files in all subdirectories recursively
Example 4: List and count all files in all subdirectories
Linux/UNIX Processes
• ps –acefl | more
• ps –acefl | grep “init”
• the first, primordial process init
PID=1 PPID=0 (kernel)
All other processes are descendants of init.
• Each process has only one parent but could have many children
• Child processes inherit the environment of the parent process
• All programs start with three files open: stdin, stdout, stderr
Linux/UNIX Process Model
Process Attributes
• A unique process identifier (PID) - getpid() call
• parent process (PPID) – getppid() call
• permission identifiers (UID, GID etc.)
• An address space
• A current working directory (.)
• A set of open files, directories
• environment variables – env or printenv (e.g. PATH= …;
HOME=…)
• scheduling priority
make facility
• used for maintaining component modules in a software system.
• The default file for make is called makefile or Makefile.
Otherwise, use
“make –f filename”
• A makefile consists of a series of sections each composed of target,
dependencies and commands.
Example: http://web.mst.edu/~ercal/284/MAKEFILE/makefile-1.txt
• Every command line must begin with a tab.
• make target returns without doing anything if no changes are made to
any of the files involved in making the target.
makefile
#A simple Makefile example
default: all
all: driver
driver: program.o \
scanner.o
@echo "Building $@"
g++ program.o scanner.o -o driver
program.o: parser.h symbol.h \
program.cpp
g++ -c program.cpp
scanner.o: scanner.h \
scanner.cpp
g++ -c scanner.cpp
clean :
rm driver program.o scanner.o