Pages

Wednesday, July 27, 2011

diff OUTPUT FORMAT

Showing Differences Without Context

The "normal" diff output format shows each hunk of differences without any surrounding context. Sometimes such output is the clearest way to see how lines have changed, without the clutter of nearby unchanged lines (although you can get similar results with the context or unified formats by using 0 lines of context). However, this format is no longer widely used for sending out patches; for that purpose, the context format (see section Context Format) and the unified format (see section Unified Format) are superior. Normal format is the default for compatibility with older versions of diff and the Posix standard.

Detailed Description of Normal Format

The normal output format consists of one or more hunks of differences; each hunk shows one area where the files differ. Normal format hunks look like this:

change-command
< from-file-line < from-file-line... --- > to-file-line
> to-file-line...
There are three types of change commands. Each consists of a line number or comma-separated range of lines in the first file, a single character indicating the kind of change to make, and a line number or comma-separated range of lines in the second file. All line numbers are the original line numbers in each file. The types of change commands are:

`lar'

  • Add the lines in range r of the second file after line l of the first file. 
  • For example, `8a12,15' means append lines 12--15 of file 2 after line 8 of file 1; or, if changing file 2 into file 1, delete lines 12--15 of file 2.

`fct'

  • Replace the lines in range f of the first file with lines in range t of the second file. This is like a combined add and delete, but more compact. 
  • For example, `5,7c8,10' means change lines 5--7 of file 1 to read as lines 8--10 of file 2; or, if changing file 2 into file 1, change lines 8--10 of file 2 to read as lines 5--7 of file 1.

`rdl'

  • Delete the lines in range r from the first file; line l is where they would have appeared in the second file had they not been deleted. 
  • For example, `5,7d3' means delete lines 5--7 of file 1; or, if changing file 2 into file 1, append lines 5--7 of file 1 after line 3 of file 2.

Comparison & Merge files

http://www.chemie.fu-berlin.de/chemnet/use/info/diff/diff_toc.html

Saturday, July 23, 2011

textmate 1.5.9 破解

TextMate 在 Mac OS X 下被奉为编辑器中的首选。

安装破解步骤:

1. 软件下载:
TextMate 1.5.9 官方下载地址 http://macromates.com/
0xED 官方下载地址 http://www.suavetech.com/0xed/0xed.html

2. 打开terminal,找到 Applications/TextMate.app/Contents/MacOS/TextMate文件。将该文件复制到documents下面以便下一步修改。命令如下:

cp /applications/textmate.app/contents/macos/textmate users/XXX/documents
(XXX: 代表你的用户名)

3. 启动 0xED Mac 十六进制的编辑器,然后用 0xED 打开 documents 下的文件 textmate,找到里面所有的年份日期并修改,然后保存。(我使用的是最新版TextMate1.5.9, 故将文件里所有的2009改为了2099, 里边有4个2009)

4. 将原本的Applications/TextMate.app/Contents/MacOS/TextMate文件删除,命令如下:

rm /applications/textmate.app/contents/macos/textmate
然后将你修改了的documents下的TextMate文件复制回原来的地方,命令如下:

cp /users/XXX/documents/textmate /applications/textmate.app/contents/macos
关闭 terminal(终端)。



5. 打开 TextMate 程序,输入如下注册信息:

user: handholder crakced you

code: DKFTCCXCMWOX35TZKPRN5YNR2NYUTJJAY52VHWKX2H5URTUB72KW- RCRTQJCC2ZZV5BTHSKCNQXTAOSGSLN46V3E7NIJKDBLRDY37NRVD- IXQWZ5SVPHBN67JZDZTTAQ6MS4ROVXRCGDZGKGE2VGOGHEYMPRGY- O5Y243GTBKPZLPP55QSBIHR6MDEUBMVQT4Q3SESPWETRG6PJM

之后再打开 TextMate -> Registration,可以看到已经成功注册。





转自:http://zd5.org/2010/09/23/textmate-1-5-9-%E7%A0%B4%E8%A7%A3%E6%B3%A8%E5%86%8C%E6%96%B9%E6%B3%95/

Thursday, July 14, 2011

Introduction to Dynamic Programming

Dynamic programming is a method for efficiently solving a broad range of search and optimization problems which exhibit the characteristics of overlappling subproblems and optimal substructure. I’ll try to illustrate these characteristics through some simple examples and end with an exercise. Happy coding!

Contents

  1. Overlapping Subproblems
  2. Optimal Substructure
  3. The Knapsack Problem
  4. Everyday Dynamic Programming

Wednesday, July 13, 2011

Error: [: = unary operator expected

# Remove all shared memory segment
ipcs -m |\
while read key shmid owner perms bytes nattch status
do
if [ $owner = $USER ]
then
echo -e "\t removing shared memory segment \"$shmid\"" 1>&2
ipcrm -m $shmid
fi
done

Error:

rmShm.sh: line 13: [: =: unary operator expected
removing shared memory segment "55443496"
rmShm.sh: line 13: [: =: unary operator expected


Fix:
if [ "$owner" = "$USER" ]

Thursday, July 7, 2011

Deadlock

====== Deadlock  =====

Conditions for Deadlock
1. deadlock prevention
2. deadlock avoidance

Prevention
- made some one of the conditions necessary for deadlock don't hold

Avoidance
- OS gets info about what resources a process may require during its lifetime anbased on 
this makes decisions about whether immediately grant request or to make a process wait.



Prevention
----------
1. Mutual Exclution
- some resources sharable e.g. reading files.
- some resources intringsically not sharable.
* CAN NOT prevent deadlock by preventing the mutual exclusive condition.
2. Hold and wait
- make process request all the resources it needs at the start.
- cons:
- resource utilization.
- let a process request a resources only if it holds none.
- cons: starvation
3. No Preemption (of already allocated resoruces)
- protocal 1
P holds R1.....Rn, requests Rm;
Rm not available
resources held (R1...Rn) are implicitely released.
P now waiting for R1...Rn,Rm, resumes when they are available
- protocal 2
P requests R1,...Rn
if available, 
P gets them
else if they are allocated to another process P2 that is waiting for other resources,
then they are taken from P2 and gien to P
else not availabe & not allocated to processes waiting on something
P waits.
while P waits some of its resources may be preempted.
4. Circular Wait
- Define a total order > on the resource types
F:R -> N
- Protocal 1
Process request resources in increasing order
- request instances of Ri (all needed instances at once)
- later request Rj only if F(Rj) > F(Ri)
- Protocal 2
Before Rj requested process must release all instances of Ri s.t. F(Ri) >= F(Rj)
NEVER holding resources numbered higher than the ones  ???? requesting
then no circular wait.


Deadlock Avoidance
-------------------

- use information about a process's possible future resource use.
- OS gets info about max # of (instances of each) resource-type that a process needs.
Algorithm looks at resource allocation stat to avoid circular conditions.
- resource allocation state = f(# of availabe resources, 
# of allocated resources, 
max possible demands of the processes)


Safe State
-----------
- if the system can allocate resources to each process (up to its maximum) in some order and  still avoid a deadlock,
i.e. if there exists a safe sequence (of processes)

sequence <P1,P2,Pi,..Pn> is a safe sequence for the currenct allocation state, 
if for each Pi the resources that Pi can still request 
can be satisfied by 
- currently available resources and 
- resources held by all Pj, j<i
(P1 can request all availabe resources because no resources is held.
P2 can request all avilable resources + resources held by P1.
etc......)


Example.(P.257)
12 units of resources
Processes P0, P1, P2

Process Max Needs Cuurent Allocated
====== ========= =================
P0 10 5
P1 4 2
P2 9 2
--------
3 available
System IS in a safe state
safe sequence: <P1, P0,P2>
P1, : needs 2 more resources (that can be get from available resources)
P0: needs 5, = 3 free + 2 held by P1
P2: needs 7, = 3 free + 2 from P1 + 5 from P0

Go from safe to unsafe:
P2 request and is allocated 1 more unit
Process Max Needs Cuurent Allocated
====== ========= =================
P0 10 5
P1 4 2
P2 9 3
--------
2 available

any sequence must start with : P1 needs 2 more resources
<P1, P0  
P1: needs 2 = 2 free 
P0: needs 5 > 2 free + 2 from P1

 OR <P1, P2
P1: needs 2 = 2 free 
P2: needs 6 > 2 free + 2 from P1

So system won't immediately grant P2's request


Resource Allocation from Graph algorithm  (See notes on paper)
- for 1 instance per resource
- resource use (max) known in advance
- reqeust edge 



Banker's Algorithm
------------------

extension of safe state idea
- n processes in resources
- data structure used

available   
  0 m-1
-------------------------------------
| | | | | | | | | |  # of units of each resource still unallocated.
-------------------------------------

max
  ------------------------------------
i |___________________________________|
  |___________________________________|
n |___________________________________|

max(i,j) max # of units of resource j needed by process i

alloc 
m
  ------------------------------------
  |___________________________________|
  |___________________________________|
n |___________________________________|

alloc(i,j) process i currently holding this many units of resource j


Need : "could still need"
m
  ------------------------------------
  |___________________________________|
  |___________________________________|
n |___________________________________|

need(i,j): # of units of resources j that may still be needed by process i
need = max - alloc


ResourceTotal
  0 m-1
-------------------------------------
| | | | | | | | | |  # of units of each resource still unallocated.
-------------------------------------

avail[j] = resourceTotal[j] - Sum(alloc[ij])




Safety Algorithm
----------------

See if there is an order of processes(safe sequence) Pi0, Pi1.... where we can do the following:
1. satifsy the outstanding need of process Pi0 with unused resources which is available only.
2. consider Pi0 finished and rturn its resources.
3. repeat for Pi1, Pi2, ... etc.

Notes: if there is more than one candidate for Pi0, it does NOT matter which one you choose.

Example:
4 resources R0,R1,R2,R3
5 processes: P0,P1,P2,P3,P4

resourceTotal  
R0 R1 R2 R3
---------------------------------
| 8 | 5 | 9 | 1 |
---------------------------------

Max:
-------------------------------------
| R0 | R1 | R2 | R3 |
-------------------------------------
P0 | 3 | 2 | 1 | 4 |
-------------------------------------
P1 | 0 | 2 | 5 | 2 |
-------------------------------------
P2 | 5 | 1 | 0 | 5 |
-------------------------------------
P3 | 1 | 5 | 3 | 0 |
-------------------------------------
P4 | 3 | 0 | 3 | 3 |
-------------------------------------

Alloc
-------------------------------------
| R0 | R1 | R2 | R3 |
-------------------------------------
P0 | 2 | 0 | 1 | 1 |
-------------------------------------
P1 | 0 | 1 | 2 | 1 |
-------------------------------------
P2 | 4 | 0 | 0 | 3 |
-------------------------------------
P3 | 0 | 2 | 1 | 0 |
-------------------------------------
P4 | 1 | 0 | 3 | 0 |
-------------------------------------
Total| 7 | 3 | 7 | 5 |
-------------------------------------

NEED:

-------------------------------------
| R0 | R1 | R2 | R3 |
-------------------------------------
P0 | 1 | 2 | 0 | 3 |
-------------------------------------
P1 | 0 | 1 | 3 | 1 |
-------------------------------------
P2 | 1 | 1 | 0 | 2 |
-------------------------------------
P3 | 1 | 3 | 2 | 0 |
-------------------------------------
P4 | 2 | 0 | 0 | 3 |
-------------------------------------

NEED = MAX - ALLOC


---------------------------------
| R0 | R1 | R2 | R3 |
---------------------------------
ResourceTotal | 8 | 5 | 9 | 7 |
---------------------------------
TotalAlloc | 7 | 3 | 7 | 5 |
---------------------------------
Avail | 1 | 2 | 2 | 2 |
---------------------------------

1. Find need[i] such that avail >= need[i]
- only P2
2. consider P2 done return its resources
change Avail: = Avial + alloc
(alloc, NOT need)
---------------------------------
Avail | 5 | 2 | 2 | 5 |
---------------------------------
P2 DONE.
3. back to 1. Find another satisfiable Process
- P0 or P4
4. consider P0 (arbitrarily) done + its resources returnd.
- change avail
---------------------------------
Avail | 7 | 2 | 3 | 6 |
---------------------------------
P0 done
5. find next satisfiable process.
- P1 or P4
6. Consider P1 done, return its resources
---------------------------------
Avail | 7 | 3 | 5 | 7 |
---------------------------------
7. remaining processes P3, P4 satisfiable.
State IS SAFE.

State safe. Now comes a request from P3 for 1 unit of R0.
i.e.  P3 requests <1,0,0,0>
Consider request by temprorily changing the allocation to reflect granting request.

Alloc
-------------------------------------
| R0 | R1 | R2 | R3 |
-------------------------------------
P0 | 2 | 0 | 1 | 1 |
-------------------------------------
P1 | 0 | 1 | 2 | 1 |
-------------------------------------
P2 | 4 | 0 | 0 | 3 |
-------------------------------------
P3 | 1 | 2 | 1 | 0 |
-------------------------------------
P4 | 1 | 0 | 3 | 0 |
-------------------------------------
Total| 8 | 3 | 7 | 5 |
-------------------------------------

NEED:

-------------------------------------
| R0 | R1 | R2 | R3 |
-------------------------------------
P0 | 1 | 2 | 0 | 3 |
-------------------------------------
P1 | 0 | 1 | 3 | 1 |
-------------------------------------
P2 | 1 | 1 | 0 | 2 |
-------------------------------------
P3 | 0 | 3 | 2 | 0 |
-------------------------------------
P4 | 2 | 0 | 0 | 3 |
-------------------------------------
---------------------------------
| R0 | R1 | R2 | R3 |
---------------------------------
ResourceTotal | 8 | 5 | 9 | 7 |
---------------------------------
TotalAlloc | 8 | 3 | 7 | 5 |
---------------------------------
Avail | 0 | 2 | 2 | 2 |
---------------------------------
So avail is 0, 2 ,2 ,2
What can be first process in safe sequence??
- NO process 
- So NO Safe Sequence
------------------------------
This means granting P3's request would put the system into an unsafe state,
Must wait to satisfy request.
------------------------------
Aside. A request by P3 for 2 units of R0 would exceeds system resources.

Monday, July 4, 2011

How to transfer Mac mail accounts to another mac

1.  Copy the contents of ~/Library/Mail/ and the mail preferences file ~/<USER>/Library/Preferences/com.apple.mail to the same locations on the other Mac.


2. Copy all the contents of ~/<User>/Library/Mail to the same location on the other mac.