C
Using GNU C __attribute__
http://www.unixwiz.net/techtips/gnu-c-attributes.html
就像該網頁的範例一般,有個專門寫 log 的 function,會利用 format string 和 variadic parameter 以及 level 參數
來實作一個可以依不同 conf 設定,寫出不同詳細等級的 log file...
但,如果只是這樣宣告:
| extern void dprintf(int dlevel, const char *format, ...) |
其實 compiler 是不會像檢查 printf() 那般,幫忙檢查 format string 和 variadic parameter 是否相對應,
所以如果一時手滑,使用該 function 時少了一些 variadic parameter,是不會有 compile warning 的...
這時,就可以運用 __attribute__((format(printf,m,n))) 此一奇淫巧技
告訴 compiler 此一 funciotn 是與 printf() 類似的 function,請 compiler 幫忙檢查 format string 和 variadic parameter 的對應...
m 是 format string 所在的參數位置,n 是第一個 variadic parameter 所在的位置...
| /* printf only if debugging is at the desired level */ extern void dprintf(int dlevel, const char *format, ...) __attribute__((format(printf, 2, 3))); /* 2=format 3=params */ |
如果要考慮 compatibility with non-GNU compilers,
該網頁也有利用 macro 的解法:
| /* If we're not using GNU C, elide __attribute__ */ #ifndef __GNUC__ #define __attribute__(x) /*NOTHING*/ #endif |
另外需要注意的大概就是 __attribute__ applies to function declarations, not definitions
而 __attribute__ 除了 __attribute__ format 還有 __attribute__ noreturn 和 __attribute__ const,該網頁都有說明和範例
Reentrancy
最近我們改寫了那個寫 log 的 library,多記錄了時間,
卻發生了接收到某一 signal 切到該 signal handler 要寫 log 時卻 hang 住了...
經 w 小老闆和邁可哥明察,發現是與 reentrant 有關...
http://en.wikipedia.org/wiki/Reentrant
APUE Chap 10 Signals 10.6 Reentrant Functions
APUE Chap 12 Thread Control 12.5 Reentrancy
http://www.ibm.com/developerworks/linux/library/l-reent.html
| If a function is reentrant with respect to multiple threads, we say that it is thread-safe. This doesn't tell us, however, whether the function is reentrant with respect to signal handlers. We say that a function that is safe to be reentered from an asynchronous signal handler is async-signal safe. |
關於 reentrant,其實是有兩個面向: multiple threads 以及 signal handlers,一個稱為 thread-safe,一個稱為 async-signal safe,
兩者的 reentrant function 並不代表在另一情況下的 safe ...
http://www.ibm.com/developerworks/linux/library/l-reent.html#5
Practice 5
If the underlying function is in the middle of a critical section and a signal is generated and handled,
this can cause the function to be non-reentrant.
By using signal sets and a signal mask, the critical region of code can be protected from a specific set of signals,
as follows:
- Save the current set of signals.
- Mask the signal set with the unwanted signals.
- Let the critical section of code complete its job.
- Finally, reset the signal set.
| sigset_t newmask, oldmask, zeromask; ... /* Register the signal handler */ signal(SIGALRM, sig_handler); /* Initialize the signal sets */ sigemtyset(&newmask); sigemtyset(&zeromask); /* Add the signal to the set */ sigaddset(&newmask, SIGALRM); /* Block SIGALRM and save current signal mask in set variable 'oldmask' */ sigprocmask(SIG_BLOCK, &newmask, &oldmask); /* The protected code goes here ... ... */ /* Now allow all signals and pause */ sigsuspend(&zeromask); /* Resume to the original signal mask */ sigprocmask(SIG_SETMASK, &oldmask, NULL); /* Continue with other parts of the code */ |
Top
Shell
Keyboard Shortcuts for Bash
http://www.howtogeek.com/howto/ubuntu/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc/
| Ctrl + A | Go to the beginning of the line you are currently typing on |
| Ctrl + E | Go to the end of the line you are currently typing on |
| Ctrl + L | Clears the Screen, similar to the clear command |
| Ctrl + U | Clears the line before the cursor position. If you are at the end of the line, clears the entire line. |
| Ctrl + H | Same as backspace |
| Ctrl + R | Let's you search through previously used commands |
| Ctrl + C | Kill whatever you are running |
| Ctrl + D | Exit the current shell |
| Ctrl + Z | Puts whatever you are running into a suspended background process. fg restores it. |
| Ctrl + W | Delete the word before the cursor |
| Ctrl + K | Clear the line after the cursor |
| Ctrl + T | Swap the last two characters before the cursor |
| Esc + T | Swap the last two words before the cursor |
| Alt + F | Move cursor forward one word on the current line |
| Alt + B | Move cursor backward one word on the current line |
| Tab | Auto-complete files and folder names |
下面幾個是常用的:
| Ctrl + A | Go to the beginning of the line you are currently typing on |
| Ctrl + E | Go to the end of the line you are currently typing on |
| Ctrl + L | Clears the Screen, similar to the clear command |
| Ctrl + U | Clears the line before the cursor position. If you are at the end of the line, clears the entire line. |
| Ctrl + C | Kill whatever you are running |
| Ctrl + D | Exit the current shell |
| Ctrl + Z | Puts whatever you are running into a suspended background process. fg restores it. |
| Ctrl + W | Delete the word before the cursor |
| Ctrl + K | Clear the line after the cursor |
| Tab | Auto-complete files and folder names |
有時 home, end 沒設的機器,也只好用 ctrl + A、ctrl + E...
每次都會忘了 ctrl + L、ctrl + U...
ctrl + Z 常用來,忘了打 "&" 放背景跑時,先 ctrl + Z,再 bg,就可以放背景跑,要叫回再 fg...
cd to previous directory
cd 的 man page 這樣寫:
| cd [-L|-P] [dir] Change the current directory to dir. The variable HOME is the default dir. The variable CDPATH defines the search path for the directory containing dir. Alternative directory names in CDPATH are separated by a colon (:). A null directory name in CDPATH is the same as the current directory, i.e., ‘‘.’’. If dir begins with a slash (/), then CDPATH is not used. The -P option says to use the physical directory structure instead of following symbolic links (see also the -P option to the set builtin command); the -L option forces symbolic links to be followed. An argument of - is equivalent to $OLDPWD The return value is true if the directory was successfully changed; false otherwise. |
所以...
- "cd": 就是回到 HOME
- "cd ...": (not starting with slash): 從 CDPATH 設定的路徑開始找,否則就是 current directory 的相對路徑
- "cd ...": (starting with slash): 絕對路徑,不看 CDPATH
- "cd -": 用 $OLDPWD 回到上一次目錄
Top
SVN
- add: 新增的檔案、目錄一開始就 add,就不用擔心後來忘記
- resolved: 解決完 conflict 就得宣稱 resolved,然後才可以 ci
- merge: 把不同 release branch 的差異整合合併起來
- 遇到 confilct 解決步驟
- svn st | grep -v '^?' 可以把一些沒列進 svn 的 .o、binary file 忽略不顯示 status
- svn log -v 可以知道每個 revision 更動了哪些檔案 (verbose)
- 即使已經 ci 了,working copy 的 revision number、log 還不會是最新的,up 之後才會是
- blame / praise / ann 列出每一行 code 是誰在哪個 revision 加進來的,搭配 log,可以用來找出某行、某段 code 當初增加、修改的原因
Top
Misc
將目錄結構填進多欄的 word 表格
- 利用 find 導向 dump 目錄結構,並利用 IE 開啟複製 html 表格資料可以在 word 裡貼成表格
- 利用 find 導向 dump 目錄結構,並利用 Word 的「表格 → 轉換 → 文字轉表格」
Comments (0)
You don't have permission to comment on this page.