linux 下程序學習(1)
阿新 • • 發佈:2018-12-21
fork()
-----
#include <unistd.h> #include <stdio.h> int main() { pid_t t; printf("father pid %d\n",(int)getpid() ); t = fork(); if(t!=0) { printf("father pid %d\n",(int)getpid() ); } if(t==0) { printf("son pid %d\n",(int)getpid() ); } while(-1); }
[email protected]:/mnt/hgfs/ubunutu share/程序測試函式$ ./a.out father pid 4191 father pid 4191 son pid 4192
ps -ef
gec 4191 2649 40 18:57 pts/2 00:00:01 ./a.out gec 4192 4191 40 18:57 pts/2 00:00:01 ./a.out
子程序在fork()後執行,這就可以避免子程序無數次呼叫fork()