operation system: unix 簡介 國立暨南國際大學 計算機中心 研究助理 張克寧...
Post on 21-Dec-2015
267 views
TRANSCRIPT
Operating system (OS)• The program that supervises the execution of
other programs– Program loading: control is returned to OS when
program terminates– Control of peripherals: device drivers– Data management: file system– Concurrency or multi-user– User friendly interface– System calls– Security
disknetwork
cpuprinter
.
.
systemresources
OS
shell
smtp
init
inetd
..systemcall
Operating System
process
loginusernamepassword
/etc/passwd
user:passwd:uid:gid:gcos:home:shell
telnetor
login 檢查 password執行 shell更換工作目錄
encoded
shell
• 命令編譯器– 內部命令 : cd, set, unset.....( 與 shell 種類相
關 )– 外部命令 : 外部程式
• 環境變數 ( 可用 env 顯示 )– PATH: 外部執行程式搜尋順序
/usr/bin:/usr/sbin:/usr/ucb:/usr/ccs/bin:.
. 表現在目錄 , 不建議使用或放在最後Ex. xyz -> /usr/bin/xyz-> /usr/sbin/xyz -> ....-> ./xyz
線上求助• man: 手冊 (manual) ,ex: man wc
• man -k keyword, 尋找與 keyword 相關的指令 ( 系統需建索引檔 :windex or whatis, 以 catman 建立 )
• 分冊 : Vol 1:command, Vol2: system call...cat(1): 表示 cat 在手冊的第一冊
• 新手可試 man man
• 環境變數 : MANPATH, 內定 /usr/man
file system• 階層式的檔案架構 , 樹狀結構• 所有 device 都是一個 file (special file)
/
usr var home etc
mail passwdbin
ls ..... ..........
...............
pathname: /etc/passwordroot
ncnu
mount point
.....
目錄相關指令• ls: 查看目錄 , ls [-al.....] [dir]
• cd: 更換目錄 , cd [dir]
• pwd: 顯示現在目錄• mkdir: 新造目錄 , mkdir dir
• rmdir: 刪除目錄 rmdir dir (dir 需為空的 )
$ lsMail bin fonts image mail misc slides tmpNews doc gwm kernel mbox nsmail src util$ pwd/home/staff/cc/klim$ cd News$ lscomp.lang.forth.001 comp.lang.forth.003 comp.lang.forth.005comp.lang.forth.002 comp.lang.forth.004 comp.lang.forth.006$ pwd/home/staff/cc/klim/News$ cd ..$ mkdir newdir$ ls newdir$ rmdir newdir$
目錄相關指令
Mail News .......
/home/staff/cc/klim
.......
檔案相關指令• cat: 顯示檔案內容 , cat [file1 file2..] • more: 一頁一頁顯示檔案內容 , more file• 編輯器 : ed, ex, vi, emacs, joe• cp: 複製 , cp a b, cp a b c dir• rm: 刪除 , rm a b c .....• mv: 更換檔名或移動檔案
mv a b, mv a b c dir
• ln: 檔案鏈結 , ln [-s] a b, a--> b
rm
• Please set an alias in .cshrc
• Add a line
alias rm ‘rm –i’
it will prompt a question for confirmation
檔案相關指令$ cat mboxFrom [email protected] Mon Mar 16 09:01 CST 1998Received: from dns.tcmds.com.tw ([210.66.177.61]) by^C$ more mboxFrom [email protected] Mon Mar 16 09:01 CST 1998..--More--(15%)$ joe mbox$ cp mbox sun$ cp mbox sun tmp$ rm sun tmp/sun$ mv mbox sun$ mv sun tmp
mbox tmp .......
/home/staff/cc/klim
sun .......
檔案屬性
$ ls -altotal 201drwxr-xr-x 24 klim sys 1536 Mar 17 16:09 .drwxr-xr-x 12 root other 512 Mar 10 19:49 ..drwxr-xr-x 2 klim sys 512 Mar 6 08:49 Maildrwxr-xr-x 2 klim sys 512 Mar 5 09:14 News-rw-r--r-- 1 klim sys 49259 Mar 14 20:28 kernel-rw------- 1 klim sys 4820 Mar 16 17:35 mbox
owner group
d rwx rwx rwx
type d: directory-: file
owner group otherr: readw: writex: execute
size date
檔案屬性• chmod: 更改屬性 , chmod mode file1 file2 ..
chmod 444 mbox
chmod a+r,u+wx,g+x,o-wx mbox – mode: octal number, augo[+-]rwx
• chown: 更改 owner, chown user file ....superuser 方能執行
• chgrp: 更改 group, chgrp group file ....
421 421 421
檔案相關指令• head: 列出檔案的前十行 , head f1 f2 ....
• tail: 列出檔案的後十行 , tail f1 f2 .....
• grep pattern f1 f2...: 列出檔案中包含 pattern 的那些行
• find: 在目錄中尋找符合條件的檔• du: 統計目錄所用空間• df: 列出磁碟機使用情形
檔案相關指令$ head -3 postedFrom klim Fri Mar 6 08:59:26 1998Subject: test , ignore itNewsgroups: tw.test$ tail -3 postedthis is a test. just ignore it.
$ grep From postedFrom klim Fri Mar 6 08:59:26 1998$
檔案相關指令
$ find . -name duplex.ps -print./misc/duplex.ps$ du misc6 misc$ dfFilesystem 1024-blocks Used Available Capacity Mounted on/dev/dsk/c0t0d0s0 96031 16540 69891 19% //dev/dsk/c0t0d0s6 674471 402207 204824 66% /usr/dev/dsk/c0t0d0s3 240055 160943 55112 74% /var/dev/dsk/c0t0d0s5 288855 145740 114235 56% /optstaff:/export/staff 2064704 810248 1244136 39% /home/staff$
從目前目錄開始尋找檔名為 duplex.ps , 並且列印出來
NFS
輸出入轉向與 pipe• cat f1 f2 > f3 • mail user@address < letter• ls | wc -w
stdin
stdout
pipe
applicationprogram
stdin stdout
(keyboard) (screen)
>導入 > 導出
|
導管
另一個 AP的 input
$ cat duplex.ps%%BeginFeature<< /Duplex true >> setpagedevice<< /Tumble false >> setpagedevice%%EndFeature$ wc duplex.ps 4 12 99 duplex.ps$ lscourse duplex.ps refcard.format rivest warn$ ls | wc -w 5
線上交談• talk user, talk user@hostname
• write user: 線上送訊• mesg n: 切斷通訊 , mesg y 打開通訊• who: 查看線上使用者• w: 查看線上使用情形
程序 (process)
• ps: 印出程序相關資訊– /bin/ps -ef, /usr/ucb/ps aux 列出所有程序
• kill: 送訊號給程序– kill -HUP pid, kill -1 pid : 經常用來使某個程
序重讀裝置檔 (configutation file)– kill -TERM pid: 結束某個程序– kill -9 pid: 強迫結束
• nice, renice: 設定程序的優先權
程序和子程序
OS
Shell
systemcall
Shell
fork
execl (...program..)
program
離線作業• command &: 將 command 置入背景執行
– 若程式需讀入 stdin 或寫出 stdout stderr 時 , 需加以轉向 , 否則會停住
• 有 job control 的 shell (csh, tcsh, bash):– 可用 ctrl-Z 停住正在跑的程式– bg [%n] : 置入背景 , fg [%n]: 移至前景– jobs: 查看目前的程序
• nohup command args &: 置入背景且 logout 後不會因 SIGHUP 停住
離線作業• at: 定某個時間執行程式• atq, atrm: 查詢及移除排定時間的程式• crontab: 定時執行 , /usr/spool/cron/crontabs
– crontab [filename], crontab [-elr] usrname0,30 * * * * /usr/news/bin/nntpsend分 時 日 月 星期 命令
$ at midnightat> mail [email protected] < announceat> <EOT>job 890236800.a at Thu Mar 19 00:00:00 1998$ atq Rank Execution Date Owner Job Queue Job Name 1st Mar 19, 1998 00:00 klim 890236800.a a stdin$ atrm 890236800.a890236800.a: removed$
其它• date: 顯示日期• cal: 月曆 , cal [[month] year], ex: 1997• 列印 :
– lpr [-Pprinter] f1 f2 ....., 列印檔案 [ 至 printer]
– lpq [-Pprinter], 顯示 [printer] 的佇列– lprm [-Pprinter] queue#, 刪除 [printer] 佇列中的 jobs
• gcc, g++: C C++ compiler• telnet, finger.... • FAQ, 各大 BBS 中 UNIX 版的精華區