1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <errno.h>
6
7 int main(int argc ,char* argv[])
8 {
9 pid_t my_pid = getpid();
10 pid_t parent_pid = getppid();
11
12 printf("ProcessID =%ld ParentID =%ld\n", my_pid, parent_pid);
13
14 pid_t child = fork();
15
16 printf("-------------------------------------\n");
17 printf("Fork Process ID = %ld \n", child );
18
19 switch( child )
20 {
21 case -1:
22 printf("Create my child process error by fork func, errno=%ld\n", strerror(errno) );
23 break;
24
25 case 0:
26 printf("This is child process! Process ID = %ld\n", getpid() );
27 break;
28
29 default:
30 printf("This is not child process! Process ID= %ld Child Process ID=%ld\n", getpid(), child);
31
32 }
33
34 printf("who am I? Process ID=%ld Parent Process ID= %ld\n", getpid(), getppid());
35
36 return 0;
37 }
38
http://www.cppblog.com/Files/bujiwu/fork.rar