Grand Central Dispatch 試玩

[華語, cmn-Hant-TW]

因為開有多執行緒的需求,就研究了一下 mac 的 framework 有提供哪些東西可以用。初步看來就是傳統 pthread 跟 NSThread 可以用,不過 Thread 程式寫起來一向就很煩,所以就想找找有啥替代品可以用,後來就注意到 Mac OS X 10.6 (iOS 4)之後新加的 Grand Central Dispatch。

基本概念很簡單,就是把你要做的東西加進 queue 就好了。把要做的事情放在同一個 queue 的話他就會照順序一個一個執行,當然你可以開很多個 queue 同時跑,那就可以一次做很多事情,直到把系統榨乾為止。

首先要開一個 queue
dispatch_queue_t queue = dispatch_queue_create("cc.pighead.queue", NULL);

第一個參數可以隨便打,第二個參數在 10.6 沒用,10.7 以上才有用途。

然後把要執行的程式碼放進 queue 裡面
dispatch_async(queue, ^{
  printf("this is a block!\n");
});

裡面包住的東西叫 block,至於 block 是啥這邊先不管他,反正就是包在裡面的程式碼會被另外開一個執行緒來跑就是。

然後把他 sync 回來
dispatch_sync(queue, ^{});
最後 release 掉
dispatch_release(queue);

要同時跑很多的話就多開幾個就可以了,想開幾個就開幾個,在 sync 之前所有的 queue 都可以同時跑,如果有變數會共用的話記得用 lock 或是 @sychronized 這些的東西來控管,大致上就是這樣。另外還有 queue 群組跟 for loop 拆解成平行處理的用法,這次沒用到所以就沒特別去注意了。

話說 FreeBSD 上也是可以用 Grand Central Dispatch 的,用法也差不多,需要的是新一點的 kernel、llvm/clang 編譯器以及 devel/libdispatch 這個 port。

參考文件

A Simple Job Queue With Grand Central Dispatch

Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial

Nullified Construction

Mac OS X (Cocoa) NSLock

GCD on FreeBSD

 

bash 的 <<<

[華語, cmn-Hant-TW]

有時候會看到用 bash 寫的 script 裡面有 <<< 這種用法,啊這個語法 sh 是吃不進去的,我們這種 sh 基本教義派當然會想要把他消滅掉。 消滅的方法也不難,這個語法其實就是把 <<< 後面字串變數的內容丟給前面的東西 (程式,或是另外一個 script) 吃而已,所以改用 pipeline 就可以了。

bash:
run.sh <<< $var
sh:
echo "$var" | run.sh

要注意的是 echo 的變數最好要用雙引號包起來,避免字串內的換行字元之類的東西(如果有的話)被吃掉 。

Posted in Development. Tags: , . No Comments »

群組化 $_POST

[華語, cmn-Hant-TW]

HTML的表單項目太多的話,丟給 PHP 處理的時候就會有點煩,所以最好是在填表的時候就先分好群組。
分的方式其實還蠻簡單的,只要用中括號([])當元件的名稱即可。

<input name="項目[]" type="text" value="a" />
<input name="項目[]" type="text" value="a" />

然後輸入的內容就會變成放到 “項目[]” 這個陣列裡了,依序會是項目[0]、項目[1],當然要直接指定 key 也是可以的,細節可以看這篇

Posted in Development. Tags: . No Comments »

去掉字串中非數字的部份

[華語, cmn-Hant-TW]

主要是用在去掉電話號碼上那堆括號或是連字號之類的東西。用正規表示式來處理並不困難,其中 ‘\D’ 代表非數字(注意大小寫),把非數字的字元通通取代成空字串即可。

Perl

$string =~ s/\D//g;

PHP

$string = preg_replace( '/\D/', '',  $string );
Posted in Development. Tags: , . No Comments »

在 perl script 底肚用 open 拿到系統執行的結果

[客家話, hak-Hant-TW]

Perl script 可以用 system(); 來執行外部的程式,偷懶時當好用。 毋過 system(); 有一隻麻煩: 執行結果會直接丟到 stdout,script 自家拿不到結果,是講 script 會拿到執行結果做下一步動作,就要用其他方法了,用 open 加 pipe 就可以做到,像要用 find 尋到全部的 .txt 檔,一隻一隻撈拒打開來,就可以按仔寫:

open FILES, "/usr/bin/find *.txt |"
while (<FILES>) {
  open(FILE, $_);
}

第一隻 open 那邊會存下結果放到 FILES,記得盡後背要加一隻 ‘|’ (pipe) 來導向,用 while 一行一行的撈他印出來。第二隻 open 就是正經來打開檔案了,按仔寫毋會太麻煩,毋夠跨平台的能力較差就是。

Posted in Development. Tags: . 2 Comments »

clang 及 gcc 的編譯訊息

[華語, cmn-Hant-TW]

因為 GPLv3 的問題,FreeBSD 將來的預設編譯器會從 gcc 往 clang 移動。clang 的賣點之一就是編譯的訊息較易懂,一個簡單的例子

1
2
3
4
5
6
#include &lt;stdio.h&gt;
 
main () {
  int a
  int b;
}
gcc version 4.2.1 20070719 [FreeBSD]
ft.c:3: warning: return type defaults to 'int'
ft.c: In function 'main':
ft.c:5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'int'
ft.c:5: warning: unused variable 'b'
ft.c:6: warning: control reaches end of non-void function
clang version 2.8 (trunk)
ft.c:3:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
main () {
^
ft.c:4:8: error: expected ';' at end of declaration
  int a
       ^
       ;
1 warning and 1 error generated.

gcc 有加 -Wall 參數把 warning 印出來,預設是只會印出第 5 行有 error 的訊息。很明顯 clang 的輸出較具有可讀性,他明確指出第 4 行忘了加分號 (;) 而不是說第 5 行前面漏了什麼東西,另外若 Terminal 有支援的話,他的一些關鍵字及標示位置的符號(^)還會用不同的顏色表示,可以很清楚的看出問題出在哪裡。

希望 clang 能愈來愈普及!

Posted in Development. Tags: , . No Comments »

ACL in CakePHP

[華語, cmn-Hant-TW]

cakephp 下開 acl 的簡單疏失心得,當然資料庫連結之類的設定要先弄好,然後至少要有一個寫好的 controller 。

用 console 工具來弄會比較方便,首先要切到 app 的目錄下

../cake/console/cake schema create DbAcl

這是 1.3 的語法,如果是 1.2 的話請用 schema run create,接著在 controller 裡面加上

var $components = array('Acl');

一定要加,不然就有可能會吃到像是這樣的錯誤訊息:
Error: Missing database table ‘aros’ for model ‘Aro’

然後就可以加 ACO 跟 ARO 了。

../cake/console/cake acl create aro 0 Users

就可以開一個叫 Users 的 ARO了,ACO 也是一樣的情況。

再加上 login 的機制後,整個 ACL 的架構應該就算是完整了,再來就要去定義哪些 ARO 可以用哪些 ACO 之類的,把他補齊就差不多可以用了。

Posted in Development. Tags: , . No Comments »

壓縮檔案小測試

[華語, cmn-Hant-TW]

要把一些舊東西壓一壓,正好可以來測一下哪種格式壓起來效果最好。
原始檔案是事先打包好的 tar 檔,大小為 1,089,927 KB。

格式 參數 大小
zip 預設值 464,597 KB
7z 預設值 286,850KB
gzip 預設值 473,692KB
bz2 預設值 453,056KB
lzma (lzma1) 預設值 314,828KB
xz (lzma2) 預設值 314,199KB
xz (lzma2) -9, –extreme 217,252KB

其中 7z 和 zip 是在 windows 7/x64 下壓的結果,而 gzip, bzip2, lzma 及 xz 是在 FreeBSD 8.0/amd64 得到的結果,
因為測試的機器及平台相差有大到,所以也就沒有特別去算壓縮解壓縮的時間了。不過下 -9 –extreme 這個極限壓縮參數花的時間很明顯比用預設值長很多,雖然說壓縮比的差異也很明顯就是。

Posted in Development. Tags: , , , , , , . No Comments »

Conditional Expressions

[華語, cmn-Hant-TW]

每次都會忘記,記錄一下省得每次都要用 Google 找半天

BASH

-a file ~ True if file exists.

-b file ~ True if file exists and is a block special file.

-c file ~ True if file exists and is a character special file.

-d file ~ True if file exists and is a directory.

-e file ~ True if file exists.

-f file ~ True if file exists and is a regular file.

-g file ~ True if file exists and its set-group-id bit is set.

-h file ~ True if file exists and is a symbolic link.

-k file ~ True if file exists and its “sticky” bit is set.

-p file ~ True if file exists and is a named pipe (FIFO).

-r file ~ True if file exists and is readable.

-s file ~ True if file exists and has a size greater than zero.

-t fd ~ True if file descriptor fd is open and refers to a terminal.

-u file ~ True if file exists and its set-user-id bit is set.

-w file ~ True if file exists and is writable.

-x file ~ True if file exists and is executable.

-O file ~ True if file exists and is owned by the effective user id.

-G file ~ True if file exists and is owned by the effective group id.

-L file ~ True if file exists and is a symbolic link.

-S file ~ True if file exists and is a socket.

-N file ~ True if file exists and has been modified since it was last read.

file1 -nt file2 ~ True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.

file1 -ot file2 ~ True if file1 is older than file2, or if file2 exists and file1 does not.

file1 -ef file2 ~ True if file1 and file2 refer to the same device and inode numbers.

-o optname ~ True if shell option optname is enabled. The list of options appears in the description of the -ooption to the set builtin (see The Set Builtin).

-z string ~ True if the length of string is zero.

-n string ~ True if the length of string is non-zero.

string1 == string2 ~ True if the strings are equal. ‘=’ may be used in place of ‘==’ for strict posix compliance.

string1 != string2 ~ True if the strings are not equal.

string1 < string2 ~ True if string1 sorts before string2 lexicographically in the current locale.

string1 > string2 ~ True if string1 sorts after string2 lexicographically in the current locale.

arg1 OP arg2 ~ OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.

資料來源: http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions

TCSH

-r   Read access
-w   Write access
-x   Execute access
-X   Executable in the path or shell builtin, e.g., `-X ls' and  `-X
	       ls-F' are generally true, but `-X /bin/ls' is not (+)
-e   Existence
-o   Ownership
-z   Zero size
-s   Non-zero size (+)
-f   Plain file
-d   Directory
-l   Symbolic link (+) *
-b   Block special file (+)
-c   Character special file (+)
-p   Named pipe (fifo) (+) *
-S   Socket special file (+) *
-u   Set-user-ID bit is set (+)
-g   Set-group-ID bit is set (+)
-k   Sticky bit is set (+)
-t   file  (which  must be a digit) is an open file descriptor for a
	       terminal device (+)
-R   Has been migrated (convex only) (+)
-L   Applies subsequent operators in a multiple-operator test  to  a
	       symbolic  link rather than to the file to which the link points
	       (+) *
資料來源 FreeBSD tcsh manual pages
Posted in Development. Tags: , , , , . 2 Comments »

傳參數給 File::Find::find 的 wanted 函數

[華語, cmn-Hant-TW]

有用到順便記一下,參考這個網頁的討論,應該這樣寫即可

File::Find::find( {wanted =&gt; sub { wanted($arg) }}, $dir);
sub wanted {
  my ( $arg ) = $_;
  ...
}

$arg 即為要傳的參數,$dir 是搜尋目錄。當然參數要傳 reference 也是可以的,像是傳陣列就把參數改成 \@array 之類的即可,丟 reference 的原因是 wanted 函數不會回傳東西,要把處理後東西挖出來就得用這個方法。

Posted in Development. Tags: . No Comments »