#include <stdio.h> |
| #include <sys/types.h> |
| #include <unistd.h> |
| #include <stdlib.h> |
| int main() |
| { |
| pid_t pid; |
| pid = fork(); |
| switch( pid ) { |
| case 0: |
| while(1) { |
| printf("A background process, PID:%d , ParentID: %d ", |
| getpid(),getppid()); |
| sleep(3); |
| } |
| case -1: |
| perror("Process creation failed "); |
| exit(-1); |
| default: |
| printf("I am parent process, my pid is %d ", getpid()); |
| exit(0); |
| } |
| return 0; |
| } |
beyes@linux-beyes:~/C> ./fork3.exe
A background process, PID:15465
, ParentID: 15464
I am parent process, my pid is 15464
beyes@linux-beyes:~/C> A background process, PID:15465
, ParentID: 1
A background process, PID:15465
, ParentID: 1
A background process, PID:15465
, ParentID: 1
| 欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |