进程初学(二)
首先,來看一下下面的源程序吧:
#include <stdio.h> #include <unistd.h>int main() {pid_t pid;printf("Now only one process\n");printf("Calling fork...\n");pid = fork();if(!pid){ // 這里是子進程執行的任務printf("I'm the child, pid = %d, getpid = %d\n", pid, getpid());}else if(pid > 0){// 這是父進程執行的任務printf("I'm the parent, child has pid = %d, getpid = %d\n", pid, getpid());}elseprintf("Fork failed\n");return 0; }?
運行結果如下:
[root@test #59]#./p0 Now only one process Calling fork... I'm the child, pid = 0, getpid = 6164 I'm the parent, child has pid = 6164, getpid = 6163
可見:
在子進程中輸出的pid是0,子進程的進程id是6164;
在父進程中輸出的pid是6164(即子進程的進程id),父進程的進程id為6163。
總結
- 上一篇: awk按分隔符的不同取出不同的列
- 下一篇: 已知思科ASA设备漏洞仍在其新版本中存在