Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

深入淺出《Unix環境高級編程》:程序與進程的核心概念(三)

編輯:關於 Android 開發

深入淺出《Unix環境高級編程》:程序與進程的核心概念(三)

坦白說有點慚愧,這個系列一停就是一年半。期間我換到了一家新單位,終於做上了心心念念的嵌入式開發。回頭再看,其實當初的那些執著也沒什麼大不了的。這次我想把「深入淺出 Unix 環境高級編程」這個系列重新拾起來,希望能堅持寫下去。

一、程序與進程的區別

這兩個概念其實很好理解:程序是存放於磁碟上的可執行檔,而進程則是這個可執行檔載入記憶體、真正跑起來之後的運行態。

打個不太嚴謹但好懂的比方:程序就像一台停在車庫裡的汽車,而進程則是它真正上路行駛時的狀態。

二、進程號 PID

在 Unix 及類 Unix 系統中,每個進程都由一個進程號來標識,它是個非負整數。我們可以寫個簡單的小程式把 PID 印出來:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. int main(int argc, char * argv[])
  5. {
  6. pid_t pid = 0;
  7. pid = getpid();
  8. printf("this proccess id is %d\n", pid);
  9. return 0;
  10. }

編譯指令為 gcc -o pid pid.c,執行結果如下:

  1. ./pid
  2. this proccess id is 24047
  3. ./pid
  4. this proccess id is 24048
  5. ./pid
  6. this proccess id is 24050
  7. ./pid
  8. this proccess id is 24051
  9. # ./pid
  10. this proccess id is 24052

可以發現,每次執行的 PID 都不一樣。利用這個特性,我們可以在程式啟動時檢查是否已經有一個相同程式在運行——最常見的做法就是把 PID 寫進一個 .pid 檔裡,作為判斷依據。

於是我把程式改造了一下:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. int main(int argc, char * argv[])
  8. {
  9. pid_t pid = 0;
  10. char * pidfile = "./freesir.pid";
  11. pid = getpid();
  12. int fd = 0;
  13. char pids[16] = {0};
  14. if(access(pidfile, F_OK) != -1)
  15. {
  16. printf("this program is already run in system.\n");
  17. return -1;
  18. }
  19. fd = open(pidfile, O_TRUNC | O_CREAT | O_RDWR);
  20. if(fd < 0)
  21. {
  22. printf("open pid file error!\n");
  23. return -2;
  24. }
  25. sprintf(pids, "%d", pid);
  26. if(write(fd, pids, strlen(pids)) != strlen(pids))
  27. {
  28. printf("write pid file error!\n");
  29. return -3;
  30. }
  31. sleep(10);
  32. unlink(pidfile);
  33. close(fd);
  34. printf("this proccess id is %d\n", pid);
  35. return 0;
  36. }

執行之後,會在當前目錄生成一個 freesir.pid 檔;若在它尚未結束前再次啟動,就會看到「程式已在執行」的提示。原理大致如此。

三、進程控制

這裡先對進程控制做個簡單介紹,後面章節會有更詳細的說明。下面的範例分別示範父子進程的建立與等待:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. int main(int argc, char * argv[])
  5. {
  6. pid_t pid = 0;
  7. int status = 0;
  8. pid = fork();
  9. if(pid < 0)
  10. {
  11. printf("fork error!\n");
  12. return 0;
  13. }
  14. else if(pid == 0)
  15. {
  16. printf("i'm the child proccess. pid = %d\n", getpid());
  17. sleep(5);
  18. exit(127);
  19. }
  20. else if(pid > 0)
  21. {
  22. printf("i'm parent prccess. pid = %d, childpid = %d\n", getpid(), pid);
  23. }
  24. pid = waitpid(pid, &status, 0);
  25. if(pid < 0)
  26. {
  27. printf("waitpid error!\n");
  28. }
  29. return 0;
  30. }

這段程式會印出父子進程的 PID。可以觀察到:子進程中 fork 的回傳值為 0,並能用 getpid 取得自己的 PID;父進程則拿到子進程的 PID。最後以 waitpid 等待子進程結束。

需要留意的是,範例中只是簡單地印出訊息,實務上應搭配 waitpid 提供的三個巨集來判斷子進程的結束狀態:

  1. WIFEXITED(status)
  2. WEXITSTATUS(status)
  3. WIFSTOPPED(status)

這些細節留待後續再展開。今天就先聊到這裡,越往下看越覺得 Linux 的設計實在強大,令人折服。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved