推薦軟硬體、網站

Google Adsense

aNobii網路書櫃

2009年3月19日 星期四

讓vim記得檔案上次編輯的位置

keywords: mac, vim, restore cursor, remember last editing line, viminfo, file marks

這個問題困擾我很久了。我Mac (OSX 10.5.6) 上的vim開啟時,總是沒有辦法記憶該檔案上次離開時的游標,可能是關鍵字不好下,google好幾次都沒成功。今天用 mac vim "file marks" ,終於在某大陸論壇找到答案了!就這麼簡單:

在~/.vimrc裡加上這行:

au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif

如果想切成兩行,也可以在第二行首用\當作分隔記號寫成:

au BufReadPost * if line("'\"") > 0|if line("'\"")
\ <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif


That's it!

2009年3月9日 星期一

OpenOffice Writer 左右對齊 全形標點符號超出行尾邊界


Keywords: OpenOffice Writer, Chinese/Asian Punctuations, Punctuation Alignment, Justification.

Japanese and Korean users, this works for you, too!!

在OpenOffice Writer使用左右對齊(justification, left-right alignment)時,會發現剛好在行尾的全型標點符號,例如中文的 ',。、;?!' ,跑出左右對齊的邊界。其實一般對齊時,標點符號也會超出「頁面」的邊界,不過在編寫需要用到左右對齊的文件時,困擾比較嚴重。


*Solution:


選取修改範圍後(往往是全選) -> 格式 -> 段落 -> 亞洲語言排版式樣 -> 取消「行末附加標點符號」。
如果要允許標點符號出現在行首的話,可以再取消「顧及行首行末禁止使用字元的清單」。


Select the target text -> Format -> Paragraph -> Asian Typography -> Disable "Allow hanging punctuation".

You can optionally disable "Apply list of forbidden characters to the beginning and end of lines" to allow punctuation at line beginnings.




改好以後,就不會有標點符號跑到外面去的情況啦!輸出成PDF也更美觀了。
另外連結一篇龔維正網友詳細的文章,給大家參考。

2008年11月9日 星期日

[OS] Chapter3 -- Processes

http://www.csie.ntnu.edu.tw/~swanky/os/chap4.htm
http://www.csie.ntnu.edu.tw/~swanky/os/chap10.htm -> IPC
http://en.wikipedia.org/wiki/Process_(computing)
http://en.wikipedia.org/wiki/Scheduling_(computing):第一段


Process: the unit of work in most systems
包含:code section, data section, program counter, registers, stack, etc.
Program is a passive entity(是死的); Process is an active entity(是活的)
Processes of the same program: same text section, different data sections
Thread: a basic unit of CPU utilization
Threads of the same process: same code & data section, other resources.
<對照習題4.5, Fig.4.11,fork出child process時是複製data,
而產生新thread時是共享data。>

Process state: new, waiting, ready, running, terminated
Process control block (PCB) -- information representing a process:
1. Process state
2. Program counter
3. CPU registers
4. CPU-scheduling information
5. Memory-management information
6. Accounting information
7. I/O status information

Scheduling queues:
1. Job queue -- containing all processes in the system.
2. Ready queue -- those in memory that are ready to be executed.
3. Device queue -- processes waiting for that device.
<對照Fig.3.6看架構>

Schedulers:
1. Long-term scheduler (Job scheduler, Admission scheduler):
從Job Queue中挑選合適的Jobs,將其載入到Memory中(放進ready Q)準備執行。
a. 執行的頻率不高
b. 可控制degree of Multiprogramming
c. 可調和I/O Bound與CPU Bound Job比例混合
d. 適用於batch system,不適用於time-sharing, real-time systems。
2. Short-term scheduler:
從Ready Queue中挑選priority較高之process,使其獲得CPU的控制權。
a. 執行頻率很高
b. 必須非常快,否則浪費CPU time
c. 適用於所有系統
3. Medium-term scheduler:
當記憶體空間不足,且有高優先權之process需要memory時,
挑選某些process做"swapping"。
a. swap out: remove processes from memory to "reduce the
the degree of multiprogramming"
b. swap in: reintroduce the swapped-out processes
into memory
c. 可調和I/O bound與CPU bound Job比例
d. 適用Time sharing system

Context switch:
Save the state of old process and load the state of new process.

fork() system call: creates a new process
exec() system call: load a binary file into memory and execute
wait() system call: wait for child process to complete
exit() system call: process terminates
abort() system call: terminate execution of child process

Inter-Process Communication
Independent process -- can't affect or be affected by others
Cooperating process -- can affect or be affected by others
Advantages:
1. Information sharing
2. Computation speedup
3. Modularity
4. Convenience
Cooperating processes require an IPC mechanism:
1. Shared memory
2. Message passing
<對照study guide: ch10的補充>

Client-Server Communication
1. Remote procedure calls (RPC)
Client's Stub locates the server and packs parameters
Server's Skeleton unpacks parameters and performs procedures
<對照Fig3.28的流程>
2. Remote method invocation (RMI)
Java mechanism similar to RPC, except:
a. Invoking a method directly without a matchmaker.
b. Ordinary data structures without packaging(marshalling).

[OS] 證明:Shortest Job First [SJF] is the optimal scheduling algorithm


修改自 http://computing.dcu.ie/~humphrys/Notes/OS/processes.html

What is the optimal schedule?

In general, if in-order bursts are x1, x2, ..., xn, then:

For each dispatched process, waiting time = (好像計網概題目XD)
1st: 0
2nd: x1
3rd: x1 + x2
4th: x1 + x2 + x3
...
nth: x1 + x2 + ... + xn-1

=> Total waiting time
= (n-1) x1 + (n-2) x2 + ... + (2) xn-2 + (1) xn-1 + (0) xn

Obviously this sum is minimized if the xi's that are multiplied the most times are the smallest ones, i.e., x1 < x2 < ... < xn-1 < xn.
Thus, in non-preemptive scheduling, "Shortest Job First" (actually Shortest Next CPU burst) is optimal for the purpose of minimizing Average Waiting Time. #

算是個淺顯又不失嚴謹的證明法 希望有幫助囉^^
P.S.王家祥老師說他喜歡考這個。

[OS] Chapter5 -- CPU scheduling

http://www.csie.ntnu.edu.tw/~swanky/os/chap4.htm
http://en.wikipedia.org/wiki/CPU_Scheduling

Process execution consists of a "cycle" of CPU bursts and I/O bursts.
CPU-burst distribution follow a (hyper)exponential pattern.
=> I/O bound programs: many short CPU bursts(左半邊)
CPU bound programs: a few long CPU bursts(右半邊)

Recall process states:
new, waiting, ready, running, terminated

CPU scheduling decisions may take place when a process:
1. running -> waiting [I/O or event wait]
2. running -> ready [interrupt]
3. waiting -> ready [I/O or event completion]
4. running -> terminates
Scheduling under 1 and 4 is nonpreemptive.
Scheduling under 2 and 3 is preemptive.

Dispatcher transfers control of CPU to selected process, involving:
1. Context switching
2. Switching to user mode
3. Jumping to proper location in user program to 'restart' that program

Scheduling performance criteria/objectives:
[max]1. CPU utilization - CPU用在process執行的時間比率
[max]2. Throughput(產能) - 單位時間內完成的工作數量
[min]3. Turn around Time - submission ~ completion
[min]4. ★Waiting Time - total/average waiting time in "ready queue"
[min]5. Response Time - submission ~ 1st response
在time-sharing system及user-interactive system中特別強調
其他目標:
# Fair(排班時儘量要求公平)
# No Starvation(避免飢餓)
# Support "priority" scheduling
# Resource Utilization ↑

Scheduling algorithms: [建議study guide整段全看]
1. First-come, first-served (FCFS):
Long average waiting time
Convoy effect(護衛效應) -- lower CPU and I/O utilization

2. Shortest-job-first (SJF): [證明是optimum]
Actually "shortest next CPU burst"
Difficulty: next CPU burst難以估計 -> used in long-term scheduling
[變種1] Approximate SJF:
Exponentially Weighted Moving Average (EWMA)
τn+1 = (α)tn + (1-α)τn
= (α)tn + (1-α)(α)tn-1 + ... + (1-α)^n(α)t0 + (1-α)^(n+1)*τ0
{α=1/2} = 1/2tn + 1/4tn-1 + ... + 1/2^(n+1)t0 + 1/2^(n+1)τ0

Non-preemptive SJF -- SJF (Shortest Job First)
[變種2] Preemptive SJF -- SRTF (Shortest Remaining Time First)
若其剩餘的CPU burst time大於新到達的process之CPU burst time,
則此process會被迫放棄CPU,交由新process執行。

3. Priority:
也可分為non-preemptive和preemptive
關鍵在於priority value的定義方式: internal vs external
SJF是一種priority scheduling -- Priority is the "next CPU burst"
FCFS是一種priority scheduling -- Priority is the "arrival time"
Problem: Starvation -- Low priority processes may never execute.
(易發生在unfair,甚至加上preemptive的環境)
=> Solution: Aging -- 隨著時間經過逐漸提高該process的priority

4. Round-robin (RR):
Time quantum: q
若未能在q內完成工作,則此process會被preempt掉,排到ready queue的尾端。
Time-sharing system使用RR
q too large => 趨近FCFS
q too small => overhead of context switching
Typically, higher turnaround than SJF, but better response. (fair)
Time quantum愈多 => context switch愈多,但與turnaround time並無明顯相關。

5. Multi-level queue:
將單一ready queue分成許多不同優先權等級的ready queues
每個Queue中可有自己的排班法則,如:foreground:RR, background: FCFS。
不允許process在各個queue中移動(沒有feedback)
Queue與queue之間的scheduling: fixed priority, priority time slice.
Preemptive, possibility of starvation

6. Multi-level feedback scheduling
基本同[5],但Process可以在queue之間移動。
Still unfair, but prevent starvation
Design issues:
a. # of queues
b. Scheduling algorithms for each queue
c. 新Process進來該插到哪個queue
d. When to upgrade a process. e.g. Aging
e. When to downgrade a process. e.g. 在quantum內無法做完
設計上最為複雜

分類:
# Fair:
1. FCFS
2. RR
# No starvation:
1. FCFS
2. RR
3. Multilevel Feedback Queue
# Non-preemptive:
1. FCFS
2. SJF
3. Non-preemptive priority
# Preemptive:
1. SRTF
2. Preemptive priority
3. RR
4. Multi-level queue
5. Multi-level feedback queue

Multiple-processor scheduling: (assuming homogeneous systems)
1. Asymmetric multiprocessing: 有master-slave processors
2. Symmetric multiprocessing(SMP): self-scheduling
a. common ready queue; b. separate ready queues.
Process affinity -- attempt to keep running on the same processor
Load sharing: push migration -- process觀察後自主換排
pull migration -- idle processor拉waiting process
Symmetric multithreading[SMT] or Intel's hyper-threading[HT] --
One physical processor[PP], multiple logical processors[LP]

Real-time scheduling:
Hard real-time -- within a guaranteed amount of time
=> Resource reservation is needed
Soft real-time -- only receive higher priority

######################################
Algorithm evaluation methods:
1. Deterministic modeling
Input: a given algorithm, a predetermined workload
Output: Corresponding performance
優:simple and fast
缺:too specific
使用:舉例說明時;同樣程式可以一直跑;觀察趨勢再證明

2. Queuing models -- 排隊理論
Input: distribution of service time, process arrival time, etc.
Output: utilization, average queue, average waiting time, etc.
Little's formula:
n = λ * W
n:queue length; λ:arrival rate; W:waiting time (都是average)
優:compare algorithms
缺:只是理論估計,unrealistic,independent assumptions

3. Simulations -- Programming a model of the system
優:more accurate
缺:跑模擬費時,large storage,coding

4. Implementations -- Construct a read system
優:the most accurate way
缺:too costly, environment may change

OS參考資源 -- Operating System Study Guide


Operating System Study Guide

這裡有Operating Systems Concepts(恐龍本)6/e的詳細筆記

作者是師大資工博士生 蕭宇程 (才大我三歲@@")

只能說這位大大太神了!這資料根本就可以當OS開課講稿嘛@@"

強力推薦!


之後會貼上我自己整理的Operating Systems Principles/Concepts 7/e筆記,主要參考資料是課堂講義和他的文章。

建議第一次期中考,時間不夠的話倒著讀會比較順,前兩章東西太雜。

2008年10月11日 星期六

Octave on MacOSX

說到科學計算,一般人總是想到MathWorks公司的MATLAB。但這學期修數值方法,老師反而推薦另一套軟體:GNU/Octave。
其實這樣的態度是好的,老師也說,學生時期,應該要鼓勵我們用沒有版權爭議的Octave,而不是奇怪版本的MATLAB。況且MATLAB包含的領域太多了大部分也用不到,身為自由軟體的支持者,雖然MATLAB也能做到,但如果只是修課、寫作業的話,當然要試試看Octave這套軟體。


如果是Windows使用者那就直接跳過這篇吧XD。Windows上面的設定很簡單,只要抓回來安裝好就好了,而且還有內建編輯器SciTE,用edit指令就可直接呼叫。
Mac底下就沒有那麼容易了,自己也曾經碰到一些困難。以下說明Octave for MacOSX的一些主要設定步驟:

1. 下載編譯好的可執行檔
2. 解決編輯器的問題
3. 設定.octaverc
4. 解決繪圖的問題(gnuplot與AquaTerm)



1. 下載編譯好的可執行檔
這很簡單,官網Download的地方選MacOSX可以連到SourceForge下的下載頁面。選擇最新的版本並確定自己的機型就可以囉。Intel的選octave-x.x.x-i386.dmg,PowerPC的選octave-x.x.x-ppc.dmg。


接下來就是簡單的drag-and-drop,把disk image檔裡的Octave.app拖曳到Applications裡就可以啦。
不過檔案先別急著刪,Extras資料夾裡的gnuplot等等還會用到!
用一般方式開啟Octave,就可以得到如下的終端機畫面:


注意畫面上方的標題,與開terminal時不一樣,Octave使用的shell是sh而不是一般情形的bash,所以有些unix指令在octave裡是行不通的。


2. 解決編輯器的問題
Octave for Mac的一大問題就是沒有內建編輯器。雖然說其實有沒有不影響,開個慣用的editor來寫,再用Octave來跑就行了。不過當打edit指令沒有辦法出現自己習慣的編輯器畫面,爽度就是有差XD。
Octave預設的編輯器是emacs,不信可以打入EDITOR來查看環境變數,會出現"ans = emacs"。
如果要換成vim,可以打
> edit editor "vim %s" 來切換,雙引號和%s都要照打,>是命令提示字元不用打。

但我和同學用edit或是edit xxx來編輯檔案都會當掉,不知什麼原因,我們用的是Intel Mac + OSX 10.5.5,預設編輯器換成vim也是不行,看來只要是terminal下的編輯器都有問題。
感謝iPluto補充:也可以用edit editor "xterm -e vim %s"就不會當,但是這樣會多開X11環境多此一舉。

解決之道就是改用其他GUI的編輯器,不推薦TextMate,雖然號稱最貴的text editor,但中文支援有問題,而且想支援MATLAB語法還要裝外掛。
所以我推薦用Smultron,是自由軟體,支援數十種程式語言,也有一些不錯的特色。可惜對於MATLAB語言的支援還是不如MATLAB的編輯器來得好,keyword辨別、註解、縮排等還有進步空間,不過以一個支援這麼多語言的編輯器來說算很不錯了!


首先要注意,Smultron會把.m的副檔名,辨認成Mac開發者常用的Objective-C,所以除非讀者本身就是Cocoa API的視窗程式開發高手,否則可以考慮修改副檔名辨認。


在偏好設定->進階->語法定義,勾選「使用擴展名猜測」,並且在下面的程式語言列表中,把Matlab後面加上m,把Objective-C的m去掉,這樣以後用Smultron開.m時,就能辨識成Matlab語法了。

最後還有一步,既然我們希望在Octave底下打edit就能開Smultron,就必須讓它能從terminal開啟。所幸Smultron和TextMate都有這項設計。


從Smultron選單的說明->安裝命令行工具,或是TextMate選單的Help->Terminal Usage(TextMate第一次啟動似乎就會問你要不要裝),即可輕易安裝。過程會要求輸入管理者的密碼,因為要動到/usr/bin下的東西。剩餘步驟請見下一段。


3. 設定.octaverc
編輯器安裝設定完了,因為現在/usr/bin下已經有smultron, mate這些東西,只要到terminal下打
> smultron 或
> mate
就能開啟該GUI文字編輯器。(注意開terminal時,如果目前正在跑octave,要按cmd+N開一個新視窗才是bash shell的terminal喔)
再回到Octave畫面,打edit editor "smultron %s"就能將預設編輯器改成Smultron。
不過必須每次開Octave都要下edit editor "smultron %s"才能變更編輯器,很麻煩該怎麼辦呢?

可以設定.octaverc。點代表隱藏檔,rc是run commands的縮寫,在程式開始的時候就run一些commands,意義相當於偏好設定檔。其他unix-like系統常見的有.vimrc,.bashrc,.cshrc。
現在再回到bash terminal(不是octave喔),我們要在home底下建立.octaverc。
> cd ~ 切換到home,~代表家目錄的意思
> touch .octaverc 為了避免沒這個檔案,先摸(建立)一下。一定要加"點"喔!
> open .octaverc 用Mac的文字編輯開啟.octaverc。
會用vim的人也可以直接在第二步:
> vim .octaverc



先把預設編輯器改成自己喜歡的:
edit editor "smultron %s"
然後看還有沒有其他的東西要設定。例如預設的起始路徑是~/,但是我喜歡把程式都放在~/Octave目錄下,所以可以多加這行:
cd ~/Octave
讀者如果有自己習慣要先跑的指令也可以加在這裡。


4. 解決繪圖的問題(安裝gnuplot與AquaTerm)
現在再回到Octave下,舉一個簡單的繪圖範例:
> x = 1:100;
> y = sqrt(x);
> plot(x,y);
這樣會畫出 y=sqrt(x)從x=1~100的圖,可是畫面卻出現error:
error: you must have gnuplot installed to display graphics; if you have gnuplot installed in a non-standard location, see the 'gnuplot_binary' function 原來是還需要gnuplot這個元件才能畫圖。這時候回到1.抓下來的octave-x.x.x-xxxx.dmg檔,把Extras裡的gnuplot-x.x.x-xxxx.dmg裝起來,同樣也是drag-and-drop就可以了。(其實一開始就該裝了啦XD,只是故意寫在後面來說明。)

注意:使用gnuplot需要先安裝X11系統,OSX10.5 Leopard本身就附上X11了,但OSX10.4 Tiger的人需要到安裝光碟Disc 2找X11來裝,但下面有免X11的解決方法。

這時候再畫一次應該就能出現這樣的結果:


有點鋸齒狀,效果不是很好。因此在Mac上推薦使用AquaTerm這個套件。網路上說:

AquaTerm allows you to plot things in OS X without using an X client, such as X11. This has two advantages:
  • It requires less memory and hard drive space, if you don't have X installed or running already.
  • The graphs look better, and can be saved as Quartz PDFs.
不需要X11,省空間跟記憶體,畫出來的圖漂亮又能存成PDF和EPS,所以別懷疑快去下載吧。安裝方法也是簡單的drag-and-drop至/Applications裡。
不過裝完AquaTerm仍然會用X11來畫圖,還差最後一步。到bash terminal下:
> cd ~
> touch .profile
> open .profile 編輯.profile這個檔案,還是要加"點"。檔案裡打入這行:
export GNUTERM='aqua'

存檔離開,重新開啟Octave,再用剛剛的畫圖範例:
> x = 1:100;
> y = sqrt(x);
> plot(x,y);
現在用Octave畫圖就會開啟AquaTerm來顯示了,很細緻呢!




《本篇文章歡迎補充與指教》
主要參考資料:Octave Docs,Octave Wiki上的OctaveForMac。
Powered By Blogger

Google Analytics