introduction to *nix systems
DESCRIPTION
Introduction to *nix Systems. HKOI Training Team Sidney Fong. Overview of this presentation. What to expect Brief introduction to UNIX-like systems Brief history of Linux Introduction to basic Linux commands and environment Linux as competition environment What not to expect - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/1.jpg)
Introduction to *nix Systems
HKOI Training Team
Sidney Fong
![Page 2: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/2.jpg)
Overview of this presentation
What to expect– Brief introduction to UNIX-like systems– Brief history of Linux– Introduction to basic Linux commands and
environment– Linux as competition environment
What not to expect– Detailed instructions on how to install Linux– Setting up Linux as a server
![Page 3: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/3.jpg)
What is *nix?
“*nix” refers to a class of UNIX-like operating systems
The term “UNIX” is sometimes used to refer to *nix systems
Examples of *nix systems:– Linux (or GNU/Linux)– BSD (FreeBSD, NetBSD, OpenBSD, etc)– Solaris– Mac OS X (Darwin)– GNU Hurd
![Page 4: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/4.jpg)
Typical Features of *nix Systems
Geared towards being a “server” / “networking” OS– Multi-user– Multi-tasking– Tends to focus more on command line interface
The Unix Philosophy– Write programs that do one thing and do it well– Write programs to work together– Write programs to handle text streams, because that is a
universal interface
![Page 5: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/5.jpg)
Linux - Overview
For the rest of the session, we will focus on Linux
![Page 6: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/6.jpg)
Important Figures
Linus Torvalds– Creator of Linux Kernel
Richard M. Stallman (RMS)
– Founder of GNU and the Free Software Movement
![Page 7: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/7.jpg)
Names
Pronunciation of “Linux”– “lee-nix” ( /linəks/ )
Pronunciation of “GNU”– “guh-noo” ( /gnu/ )
GNU– recursive acronym for “GNU’s not UNIX”
“Linux” vs “GNU/Linux” debate
![Page 8: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/8.jpg)
The GNU Project
Started by RMS to create a complete UNIX-like system in 1983
– Kernel, system tools, compilers, documentation, editors, etc. Ideology of Free Software Movement
– Free: “Free” as in Freedom– “Copyleft” Software: GNU General Public License (GPL)– Related: Open Source Movement
Most GNU tools were ready by the early 90’s. … Except the kernel: “GNU Hurd”
– Hurd was supposed to be a microkernel– Over-design? Wrong approach?
![Page 9: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/9.jpg)
BSD
At the same time, another open source UNIX distribution, the BSD, was being sued by UNIX System Labs. (USL v. BSDi)
The lawsuit cast doubt upon the legal status of the BSD’s. The case was finally settled in 1994, with both parties agreeing not to litigate further.
This, together with the lack of a kernel in the GNU project, created a demand for a free UNIX-like kernel, which was fulfilled by Linux
![Page 10: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/10.jpg)
Linux
Started by Linus Torvalds as a hobby project Inspired by Minix, an OS used as an example
in teaching principles of operating systems First release of Linux in September 1991 Free of the problems of GNU Hurd and BSD Eventually joined by other developers More:
– Just for Fun: The Story of an Accidental Revolutionary
![Page 11: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/11.jpg)
A Typical Linux System
Linux Kernel User space programs (many are GNU software)
– Shell (command line): bash– Utilities: ls, cp, mv, rm, cat, grep, ...– Compiler: gcc– … and more …
X Window System (for desktops/workstations) Various daemons (a.k.a. services)
– Eg.: syslogd, httpd, cron, inetd
![Page 12: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/12.jpg)
User space programs
Shell
A Rough Graphical Representation of a Linux System
Kernel
Hardware
X Client
Daemons
X Server
![Page 13: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/13.jpg)
Linux “Distributions”
Since most software on Linux can be freely modified and re-distributed, people began packaging these software into “distributions”
A “distribution” is responsible for most of the software installed on the Linux system, including the base “operating” system and any other applications the user installs
– rpm, deb/apt, tgz Each distribution has it’s own “character”, differing in
– Purpose : Server, Workstation, Firewall, etc…– Installation / upgrade method– Choice of software– Way of packaging software (tgz, rpm, deb)– Default configuration– Aims and Ideology – Commercial, Promote Free Software, etc
![Page 14: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/14.jpg)
Linux Distributions – Redhat / Fedora
The “standard” Linux Has a relatively high level of support from third party
vendors Geared towards commercial / corporate
environments Initial set up is generally easy Rather expensive if you buy the enterprise edition
(hundreds of USD!) Maintenance is a nightmare! ;-p
www.redhat.com
![Page 15: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/15.jpg)
Mandriva (was “Mandrake”)
Originally based on Redhat, therefore somewhat similar to Redhat
Geared towards desktop and newbies
www.mandrivalinux.com
![Page 16: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/16.jpg)
Slackware
Stable, simplistic One man distribution Zipslack, which works without having separat
e Linux partition(s) Relatively UNIX-like compared to other distri
butions
www.slackware.com
![Page 17: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/17.jpg)
Gentoo
Source based installation!– The standard way of installing new software on th
e system is to compile it from source (using the “portage” system)
– This is equivalent to the BSD ports system– Takes LoOOong time to install / upgrade
www.gentoo.org
![Page 18: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/18.jpg)
Debian
Made by a worldwide non-profit organization, with thousands of volunteers helping to maintain packages
Quality and quantity of packages are generally quite high
Slow release cycle – the last “stable” release was in July 2002!!
www.debian.org
![Page 19: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/19.jpg)
Linux - Basics
An introduction to using Linux as a normal user
![Page 20: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/20.jpg)
Post-Login Screen
“Message of the day”
Usually contains important notices
Username Hostname Current directoryHint: You may press
<Ctrl+Alt+F1> to <Ctrl+Alt+F6>for multiple terminals if you are
using Linux on the local machine(instead of over network)
Why is the username and hostname displayed in the command prompt?
![Page 21: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/21.jpg)
Basic Commands
“ls” – List directory contents “cd” – Change directory “cp” – Copy file(s) “rm” – Remove file(s) “mv” – Move file(s) “echo” – Display a line (or lines) of text “mkdir” – Make (i.e. Create) Directory “cat” – Concatenate files and print to stdout “exit” / “logout” – Exit the shell
![Page 22: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/22.jpg)
Example I
Try the following commands– ls # list directory– echo HKOI Cows # print a message on screen– ls -l # list directory (long format)– mkdir hello # create directory “hello”– cd hello # change directory to hello– cp /etc/motd . # copy file to current dir– ls -la # list directory– cat motd # display contents of file– rm /etc/motd # try to remove file (fails ;-p)– mv motd .. # move file to parent dir– cd .. # change to parent directory
![Page 23: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/23.jpg)
Linux File System Hierarchy
In Linux, the file system is a (single) tree Everything begins from the root directory: / Non-exclusive listing of commonly used directories
– /home – User home directories– /bin, /usr/bin – Binaries (executables, programs)– /lib, /usr/lib – Libraries (similar to “DLL”s in Windows)– /etc – System configuration files– /var, /usr/var – Stores files that are often modified (eg. log files, dat
abase files, mail spool, print spool)– /dev – special device files– /tmp – Temporary files
In general, a normal user cannot modify the files outside his home directory (except /tmp)
![Page 24: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/24.jpg)
Exploring the system
Try to navigate your way around the system with the above commands
You may view the contents of a file with the “cat” command
You can return to your home directory with the “cd” command (no arguments)
Make good use of tab completion
![Page 25: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/25.jpg)
Text Editors
Vi (VIM)– Visual Editor– VIM: Vi Improved.
Emacs– Editor Macros– Nickname: Esc-Meta-Alt-Control-Shift
Nano– “Notepad”-like– Easy to use but less powerful than Vi / Emacs
![Page 26: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/26.jpg)
Text Editors (How to quit)
Some programs may launch text editors that you may not know how to use -- even quitting is difficult!
VIM– <Esc> :q! (Quit without saving)
Emacs– <Ctrl-X> <Ctrl-C> (Quit)
Although not required, it is strongly recommended that you learn one of the above editors since they can increase your productivity quite significantly
![Page 27: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/27.jpg)
Man(ual) pages
Example: man cp
Purpose of commandDifferent
usages
Detailed description of command and
its options
Hint:
Press ‘q’ to quit
![Page 28: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/28.jpg)
*nix Quirks (compared to MSDOS)
Path separator is a slash “/” instead of backslash “\” Files are case sensitive “File extensions” are “meaningless”
– The “.” character in a file has no special meaning unless it is in the beginning of a filename
“.file” – hidden file “.” – current directory “..” – parent directory
– Executable files have an “executable bit” set, instead of a “.exe” extension
<Ctrl-D> is End of File (<Ctrl-Z> in DOS)
![Page 29: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/29.jpg)
*nix Quirks II
There must be a space between the command and subsequent arguments
– Eg. “cd /” works, “cd/” does not Command line arguments start with “-” or “--” instead of “/”
– DOS: dir /OD– Linux: ls -t
Unix text format vs MSDOS text format– A line in *NIX systems ends with a ‘\n’ (ASCII 10)– A line in MSDOS ends with ‘\r\n’ (ASCII 13, 10)– The relevant functions of the C library in DOS/Windows converts ‘\
n’ to ‘\r\n’ automatically
![Page 30: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/30.jpg)
*nix Quirks III
Unless the path the command is specified, the shell will only run commands located in the $PATH environment variable (why?)– Question: how to run a program on the current
directory? ./command
– To see what $PATH contains, you may issue the command: echo $PATH
– To edit the value of $PATH: PATH=(new value)
![Page 31: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/31.jpg)
Write a program in Pascal that– Reads an integer n from standard input– Writes n random integers to standard output
Save it as numbers.pas Compile the program Execute the program Keep a backup copy in ~/backup/numbers.pa
s
Task I
![Page 32: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/32.jpg)
I/O Redirection, Pipes
“A > B” redirects standard output from program A to file B. This operation will overwrite original contents of B (if any)
“A >> B” redirects standard output from program A and appends it to the end of file B.
“A < B“ redirects standard input from file B to program A “A | B” redirects standard output of program A to standard input
of program B Try the following commands:
– echo 100 | ./numbers– echo 10 > ten.txt– ./numbers < ten.txt– echo 20 | ./numbers | sort –n
Hint: Also works in MSDOS and
cmd.exe
![Page 33: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/33.jpg)
Task II
Download http://www.hkoi.org/~sydneyfong/public/openq.zip to home directory and unzip the contents (use wget / w3m)
Change working directory to ~/prog0 Write a program to solve the HKOI “Open Question” (see HKOI
2005 website) (You may try to write a “wrong” one (i.e. “incorrect solution 4”) f
or checking answers) Save the program as “program0.pas” Run the program using inputs from the files input1.txt to input5.t
xt, redirect the output to output1.txt to output5.txt Check your answers by using `diff`
![Page 34: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/34.jpg)
More on bash shell
alias – substitute a string as a command– Eg. alias ls=‘ls --color’ – You may list current aliases by simply issuing the command “alias”
without arguments The script ~/.bash_profile is run during login The script ~/.bashrc is run in non-login shells
– You may set aliases, environment variables and run commands in these files to customize your shell
– You may want to make the two files identical– Aliases that you may want to have by default:
alias cp=‘cp -i’ # asks for confirmation before overwriting a file alias mv=‘mv -i’ # asks for confirmation before overwriting a file alias rm=‘rm -i’ # asks for confirmation before deleting a file
![Page 35: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/35.jpg)
More on man pages
There are different sections of man pages
– Section 1: commands– Section 2: system calls– Section 3: C library calls– Section 4: special files (in /dev)– Section 5: (config) file formats– Section 6: “games”– Section 7: Misc– Section 8: System administrati
on commands
If there are conflicts in names, the section of lower value is displayed. For a specific section:
– man <section> <page> Note that you may find secti
on 3 very useful as a reference to standard C functions (eg. printf, fopen, string functions, etc)
![Page 36: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/36.jpg)
Special files (in /dev)
/dev/zero – returns an endless stream of ‘\0’ characters– To create an file of size 1MB:
dd if=/dev/zero of=myfile.img bs=1k count=1024
/dev/null– Always return EOF when read
cat /dev/null > abc – creates empty file of size zero
– All data written to /dev/null is discarded ls > /dev/null – mutes output of ls
![Page 37: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/37.jpg)
Tips in competitions…
Practical advice for competition environments
![Page 38: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/38.jpg)
Useful Commands in Competitions
bc – Arbitrary precision calculator sort – Sort lines of text files (note the “-n” option) ps – list running processes on the machine kill – Sends a signal to running programs (terminates it by default) date – Print system date/time time – reports execution time of a command diff – compares files head, tail – prints the first/last N lines of the input wc – word count (also counts characters and lines) more – pager (allows you to scroll the input for easy viewing) less – better version of more grep – print lines matching a pattern find – search for files factor – factorize numbers tsort – perform topological sort seq – print a sequence of numbers file – determine file type (sometimes displays other useful information) tar – Archive files (“glue” multiple files into one, and vice versa)
![Page 39: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/39.jpg)
Compilers - fpc
In most cases, fpc <file> would do Useful options
– -Co (check for integer overflow)– -Cr (range check)– -O1, -O2, -O3…
In some systems the man page is outdated
![Page 40: Introduction to *nix Systems](https://reader036.vdocuments.mx/reader036/viewer/2022082216/5681499e550346895db6df9e/html5/thumbnails/40.jpg)
Compilers – gcc/g++
use -o <output> to specify output file, otherwise the default is “a.out”
Most gcc options apply to g++ Normally you would do:
– gcc -O2 -o program1 program1.c– g++ -O2 -o program1 program1.cc
Remember to compile your program using the flags given by the judge/competition. (In particular, the optimization flags sometimes causes trouble)
If you want, you may also run `make <prog>`