Honesty

 

Tips

Page history last edited by Honesty 1 yr ago


C

Using GNU C __attribute__

http://www.unixwiz.net/techtips/gnu-c-attributes.html

就像該網頁的範例一般,有個專門寫 log 的 function,會利用 format stringvariadic parameter 以及 level 參數

來實作一個可以依不同 conf 設定,寫出不同詳細等級的 log file...

 

但,如果只是這樣宣告:

extern void dprintf(int dlevel, const char *format, ...)

其實 compiler 是不會像檢查 printf() 那般,幫忙檢查 format stringvariadic parameter 是否相對應,

所以如果一時手滑,使用該 function 時少了一些 variadic parameter,是不會有 compile warning 的...

 

這時,就可以運用 __attribute__((format(printf,m,n))) 此一奇淫巧技

告訴 compiler 此一 funciotn 是與 printf() 類似的 function,請 compiler 幫忙檢查 format stringvariadic parameter 的對應...

mformat 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 + AGo to the beginning of the line you are currently typing on
Ctrl + EGo to the end of the line you are currently typing on
Ctrl + LClears the Screen, similar to the clear command
Ctrl + UClears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + HSame as backspace
Ctrl + RLet's you search through previously used commands
Ctrl + CKill whatever you are running
Ctrl + DExit the current shell
Ctrl + ZPuts whatever you are running into a suspended background process. fg restores it.
Ctrl + WDelete the word before the cursor
Ctrl + KClear the line after the cursor
Ctrl + TSwap the last two characters before the cursor
Esc + TSwap the last two words before the cursor
Alt + FMove cursor forward one word on the current line
Alt + BMove cursor backward one word on the current line
TabAuto-complete files and folder names

下面幾個是常用的:

Ctrl + AGo to the beginning of the line you are currently typing on
Ctrl + EGo to the end of the line you are currently typing on
Ctrl + LClears the Screen, similar to the clear command
Ctrl + UClears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + CKill whatever you are running
Ctrl + DExit the current shell
Ctrl + ZPuts whatever you are running into a suspended background process. fg restores it.
Ctrl + WDelete the word before the cursor
Ctrl + KClear the line after the cursor
TabAuto-complete files and folder names

有時 home, end 沒設的機器,也只好用 ctrl + Actrl + E...

每次都會忘了 ctrl + Lctrl + 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 當初增加、修改的原因
    • 進一步應用: svn blame -r last_rev_decrease_by_one TARGET

      雖用 svn blame 找出最後一次改動,卻非元兇,往前指定一版號,繼續查找

 

Top


 

Misc

將目錄結構填進多欄的 word 表格

  • 利用 find 導向 dump 目錄結構,並利用 IE 開啟複製 html 表格資料可以在 word 裡貼成表格
  • 利用 find 導向 dump 目錄結構,並利用 Word 的「表格 → 轉換 → 文字轉表格

Comments (0)

You don't have permission to comment on this page.