2011年6月29日 星期三

[Linux] driver's log

dmesg 如果有開syslogd & klogd的話, 會把printk印出的message 寫到/var/log/kern.log之下
關機的方式有好幾種
如果是shutdown -h的話 那會照順序執行/etc/rc0.d/ 下面的script, 簡單而言如果要找關機時發生的錯誤, 應該是可以到 /var/log/kern.loug去找

2011年6月10日 星期五

[Linux] vfork & waitpid

在Linux底下有vfork & fork 兩種方式可以讓你create child process去作事情,  vfork & fork的差別請參照 http://csie-tw.blogspot.com/2009/04/vforkuclinux.html

最近遇到很奇怪的情形, 就是通常parent process fork child process之後, 要不parent process必須waitpid 去等待child process結束, 不然就是你必須在parent process上面註冊SIGCHLD 的signal handler (這個SIGCHLD會在child process terminated的時候發送給parent process), 結果我在parent process當中居然waitpid return -1, 訊息是no child process!  所以猜測parent process在還沒有執行到waitpid的時候, child process就結束了

所以稍微寫了小程式測試


  • version1: vfork + execv with no signal handler (parent process 在執行waitpid前先sleep)
    • waitpid return 0, WEXITSTATUS(status)可以正常拿到child process exit code
  • version1: vfork + execv with SIGCHLD signal handler (parent process 在執行waitpid前先sleep)
    • waitpid return -1, no child process, WEXITSTATUS(status)拿不到child process exit code
  • version1: vfork + execv with ignore SIGCHLD signal(parent process 在執行waitpid前先sleep)
    • waitpid return -1, no child process, WEXITSTATUS(status)拿不到child process exit code

所以最後問題在於只要你有註冊SIGCHILD相關處理, 就無法拿到child process exit code!

另外還有一種情形, 在linux system call的時候, 有甚饃會被系統interrupt, 這時候通常的解法就是retry~
http://book.chinaunix.net/special/ebook/addisonWesley/APUE2/0201433079/ch10lev1sec5.html