Thursday, November 11, 2010

fork() in c cgi

In programming, function invocation is designed to run in either synchronous or asynchronous mode. system() for instance, is a synchronous call. system(“something &”) is a ugly way to make system() kindda asynchronous because the execution of system command is put to background and thus system() returns immediately while return value is always 0.

To better control the system() process we’d better create another thread (pthread_create) or process (fork) to keep tracking the execution of that command, and in that thread/process we can do further process control over the command. For example, data validation, return value checking, sending notice to upper layer of the program…etc.

fork() is thus become very important to my c-cgi because certain operations are very time-consuming and without sophisticated adjustment, browser will be locked-up and wait till system command finishes. However, calling fork() is not enough. Let’s see how fork() is usually used in a program.

   1: if(!fork())
   2: { // start child process
   3:     setsid();
   4:   //do-something
   5:     exit(0);
   6: }

Basically if fork() successes, there will be a duplicated process that you can’t miss by watching `ps` output. This is also the case when doing fork() in a cgi program, only that even if parent process returns (cgi finished,) browser’s loading status bar is not finished. The reason is quite simple.

stdout and stderr of child process is duplicated from parent process, which is reserved for pipelining cgi output to browser webpage.

browser is actually waiting for that piece of memory (duplicated in child process) being released.

Realizing this fact, we can easily modify the fork() code as follows,

   1: if(!fork())
   2: { // start child process
   3:     fclose(stdout);
   4:     fclose(stderr);
   5:     setsid();
   6:   // do-something
   7:     exit(0);
   8: }

And thus your browser will no longer busy-waiting for the process. :)

Wednesday, July 7, 2010

加大 qemu image size

我預設拿來做 tool chain 的 qemu image 只給了 8G,沒想到這次客戶給的一整包 BSP 要 5G ,加上原有 host 裝 debian ,和 BSP 自己的壓縮檔,8G 一下子就爆掉了。

把原有的 disk image 加大很簡單,簡單點說就是幾個步驟就搞定。因為我是裝 ext3,似乎用 ntfs 的人得再多花點力氣…


還需要下載 gparted live cd 的 ISO檔:

1. 建立一個空的 qemu image,大小等於你要新增的空間,比如說10G

#qemu-img create –f raw empty.img

2. 如果原來的 disk image 不是 raw,就要 convert 成 raw

#qemu-img convert –f qcow2 –o raw disk.img disk_raw.img

3. 把它貼到原來的 image 後面

#cat empty.img >> disk_raw.img

4. 用 gparted live cd 開機 (指定 cdrom 給 qemu)

5. … 啊就 gparted ,resize 原有的 disk 。


Thursday, June 10, 2010

folks! stand out!

Corel TW Corp. is my former employer. I’m glad to be part of the big fat company and I’m also glad to be out of it. Now, thanks to my partner’s generous invitation, together we found a company which is actually burning out our last dime every moment.

But I’m happier.


最近很快地翻完一本書《happier》(中文書名:《更快樂》),號稱是哈佛最受歡迎的一堂課。有趣的是,書裡說的東西都是老生常談,同樣地重複出現在任何一本廉價二手勵智書裡。那些哈佛來的,也沒什麼了不起,不是嗎? =v=


在學校,在職場,我總是可以看到很有衝勁的年輕人,想學,想做,想看。但,他們也想要一個東西:錢。這無可厚非,錢帶來某種程度的安全感,於是經過一連串的面試後,這些小朋友看著 offer 上面的薪資選東家。 (oh my … poor little baby …) 然後被同化,然後變成下一個金錢和政治遊戲的受害者(或覺悟成為加害者)

stop doing that! 當走出校門,走進社會,你有初衷的,你有理想的。把它挖出來,放在枕頭邊,那是你失眠及安眠的理由。錢這種東西很有趣,它會跟著快樂走,所以古人很聰明,說:laugh, the world laugh with you, including money。但快樂這玩意兒,很妙,它不跟著錢走。古人也很聰明,說:weep, you weep alone, even money doesn’t give you a damn.

I’m not recruiting, though we’re in need of good hands. If you feel like to follow your ambition, your idealism, your ego! stop joining stupid companies and join smaller ones that share similar vision with you (/yell: we do embedded Linux ~ yo ho!!). And first you’ll be happy but poor, but years after you’ll be happy with fortune. And you will find out it’s not because the number in your account grows, but you’ll cherish whatever in your hands more. 如果 once in a while 你醒來,不斷地問自己 what am I doing?? this is sooooo “not” me. folks, stand out and be your own master.  :D

Thursday, May 27, 2010





到了周末好不容易見了面,所有累積的情緒一股腦變成…事後會讓人嘿嘿笑的思念炸藥用很長的 crescendo 來爆炸,倒也是種驚喜。然後在禮拜日的分手,爆炸處留下黑洞,吸走所有的甜:嗅覺的、味覺的、視覺的…種種感官的甜,留下又細又長的酸氣,從冰冷乾洌到悶熱溽溼,匯成下一個循還的低潮。







welcome home, babe.


Sunday, December 27, 2009

我聽到的 Hallelujah (Jeff Buckley)


I heard there was a secret chord
that david played and it pleased the lord
but you don't really care for music, do you
well it goes like this the fourth, the fifth
the minor fall and the major lift
the baffled king composing hallelujah


well your faith was strong but you needed proof
you saw her bathing on the roof
her beauty and the moonlight overthrew you
she tied you to her kitchen chair
she broke your throne and she cut your hair
and from your lips she drew the hallelujah


baby i've been here before
i've seen this room and i've walked this floor
i used to live alone before i knew you
i've seen your flag on the marble arch
but love is not a victory march
it's a cold and it's a broken hallelujah


well there was a time when you let me know
what's really going on below
but now you never show that to me do you
but remember when i moved in you
and the holy dove was moving too
and every breath we drew was hallelujah


well, maybe there's a god above
but all i've ever learned from love
was how to shoot somebody who outdrew you
it's not a cry that you hear at night
it's not somebody who's seen the light
it's a cold and it's a broken hallelujah


關於「這首歌到底在表達什麼意思」其實不只是非英文母語的我們東猜西猜,翻看國外的網站,老外也是對歌詞內含解釋來解釋去,沒有一個令所有人滿意的答案。既然如此,我也來分享一下我的心得好了。個人一直覺得這是一首很靈肉的歌,雖然看完其他人高尚的翻譯(ex. 覺得自己錯得離譜,但後來發現不少外國朋友對歌詞有和我一樣的解讀,到底對錯如何,體會如何,就讓大家自行判斷吧。

第一段在講大衛王搶人家老婆,拔示巴,的故事,這個一直沒什麼爭議。第二段說大衛王某日看見人家洗澡很美,就搞了她,沒想到搞下去才發現收拾不了。這段開始就有趣了。首先按聖經故事來讀大衛和拔示巴的關係,大衛搞大拔示巴的肚子後,想嫁禍給人家的綠帽子老公,烏利亞,卻失敗了。無計可施的情況下大衛用了爛招! 就是把他丟去戰死,然後收下拔示巴。耶和華看不下去,派人跟大衛說你亂搞人家就算了,還搞大肚子;你搞大人家肚子就算了,還想騙人說孩子的爸不是你;你的謊言沒辦法成功就算了,還把人家老公給弄死了,這樣對嗎?你明明有成群的嬪妃可以搞,還去搶一個小兵的老婆,還搞出兩條人命,實在很黃很下流! 大衛聽了痛哭流涕,痛改前非,但耶和華覺得這樣不夠力,還要弄死這綠油油的孩子才算數。所以這個通姦生下來的孩子夭折了,但故事還沒結束,大衛王的繼承人,所羅門王,就是他和拔示巴的孩子。而王位,卻是所羅門王殺死兄弟們奪來的。

故事說到這裡,大家有沒有什麼異樣的感覺?原來,色慾是一時的,逞一時偷情加中出,下場是到死都沒有人真的關心你呀…。大衛是信耶和華的,當他知道自己做錯事的時候,他深深地慚悔,這是對信仰的支持和虔誠 (your faith is strong) ,然而他也為自己的錯付出代價,這代價卻昂貴到令人難以再虔誠下去 (因為孩子出生就夭折了) (but you needed a proof) ,大衛的祈禱並不能免除他的罪,相反的,他受到十倍,甚至更淒慘的報應。這個報應就是,他和搶來的女人結婚 (she tied you to her kitchen chair) (這句英文有「通往男人的心經過胃」的意思,可以引申為他們有性交以外的關係,也就是婚姻),他們生下的孩子,得到了王位 (she broke your throne and she cut your hair) 。有很多解釋提到 cut your hair 跟參孫的拔掉頭毛就沒力有關,我覺得是合理的引申。但如果是講大衛的故事,我想意思也差不多,就是說大衛不僅丟了王位,也失去他的能力/權力/影響力。別忘了大衛可是曾經殺死巨人歌利亞,是被人擁戴的勇士。他還善於彈琴,趕走掃羅身上的邪惡。簡單講本來是個很神奇的人,沒想到搞了拔示巴以後,整個廢掉了。如果你是大衛,你的心情會是如何?你因為信仰,得到力量,得到王座,得到權柄,卻因為一時臣服女人的美貌,而接二連三失去你原來擁有的一切,你會不會想再祈禱一次「神呀,原諒我」?神可以給你的,也可以拿回去。大衛此時已入暮年,除了禱告「Hallelujah」還能怎辦?(and from your lips she drew the hallelujah)

我大膽猜測一下前述大衛王的心情。在他受到懲罰之前,他是一人之下萬人之上,想要的哪有得不到?於是,他開始覺得「寂寞」了 。這種心情就像外遇一樣,老婆整天顧小孩,不像以前能打扮的漂漂亮亮的,陪你玩玩性遊戲,於是想打打野味,重新實現男人的幻想,一有機會就三步拼兩步的推倒正妹,享受肉體帶來的愉悅。但當你開開心心的推倒正妹時,卻發現正妹其實心中另有所想時,你突然地跌入更深的空虛,更多的寂寞。我相信大衛對拔示巴心中總是會有個芥蒂,所以他從來不覺得在拔示巴的服侍中能感受兩人靈肉合一,常常是靈肉分離的狀態,讓他更「寂寞」了。我覺得這裡的「寂寞」,是整首歌的主軸,這首歌在唱的就是關於寂寞這回事。

第三段進來時,說故事時間結束了。歌詞開始把主詞受詞換成自己和另一個女人前三句說,寶貝你沒來之前,我習慣一個人在這裡。這是指和這女孩在一起之前「自我滿足」卻又「寂寞」的日子(baby i've been here before, i've seen this room and i've walked this floor, i used to live alone before i knew you)。然後因緣際會讓你遇到一個美到讓你看一眼就想射的女孩,你當然迫不及待的想推倒她,在她身上你得到了滿足,你以為這就是愛情,你以為從此你不再是一個人,你想像著愛情在肉體的結合中發芽,茁壯。但你錯了,因為,她沒那麼愛你,是的,她沒那麼愛你。當你看見她在你跨下扭動,在你的抽動中發出讚嘆的嬌喊聲,你享受前所未有「征服」,你覺得自己「勝利」了。然而事實卻非如此,她看似付出了她的肉體,實則擄獲了你的心呀! 當你發現她對你的示好不屑一顧的時候,你才真的了解到,你自始至終都是一個人,你的心被她踐踏,你不待她表現出征服者的姿態,心中已經覺得自己「輸」了。所以歌詞說到,我看見你插在拱門上的旗子 (就像插國旗呀,插在人家最大面的牌樓上就表示佔領) (暗示女方並沒有「愛回去」,像把旗子插在他的心上宣告勝利後就閃了) 但愛情不是勝利進行曲 (此時居然討饒了…這是男人的劣根性,把女人壓在跨下拍打屁股,自顧自的扭腰擺臀尋找自己爽快的節奏的時候,難道心中沒有勝利的快感?一但立場互換,又覺得自己很委屈,『愛情,不該是談誰贏誰輸的吧?』) (i've seen your flag on the marble arch, but love is not a victory march, it's a cold and it's a broken hallelujah) 愛情不是勝負,而是冰冷又支離破碎的 Hallelujah。(我覺得很能表達這種困境的圖,但18禁,請自己想辦法連…

第四段歌詞很簡單,他說,是妳讓我明白肉體的快樂 (well there was a time when you let me know, what's really going on below) (what's going on below 意思是下面如何?廢話,下面很爽呀~~) 但現在卻不給我了 (but now you never show that to me do you)。移情別戀的女人呀,連做愛都不像當初那麼投入。妳可記得當初,我在妳身體裡扭動時,妳是如何爽快地和我一起高潮?(but remember when i moved in you, and the holy dove was moving too, and every breath we drew was hallelujah) 很多解釋說 holy dove 是和平,我的看法是,把陽具稱為「鳥」的俗名是中外一致,因此 holy dove 會是什麼?那為什麼陽具也會動 (the holy dove was moving too)?因為射了呀~ 射精後陽具會反射性的自己抖動,所以意思是在高潮中的狂喜令人想叫 Hallelujah。

最後一段說,或許上帝存在,而我們都知道,上帝的愛是無私的,上帝給的愛,是不求回報的。然而,我唯一從愛中學到的 (but all i've ever learned from love) 是如何對著比你(指上帝)更吸引人的人(指正妹) 射精 (was how to shoot somebody who outdrew you)。shoot 就是發射,意思是說上帝教我們要愛,但我從愛裡學到的,只是推倒比你還要正很多的妹。(滿足肉慾優先啦~) 。然而,這樣的愛情,不是暗夜裡的哭泣(it's not a cry that you hear at night),也不是見證奇蹟的狂喜( it's not somebody who's seen the light),它是冷冰而且破碎的 Hallelujah 。


Saturday, October 17, 2009


這個時候我應該在寫 gstreamer plugin 的第四部份,但本來想用 twitter 發的文一直發不出去…乾脆寫一篇 blog 好了。








Thursday, October 8, 2009