赞
踩
- 1 #include <stdio.h>
- 2 #include <unistd.h>
- 3 #include <stdlib.h>
- 4 #include <sys/types.h>
- 5
- 6 int main()
- 7 {
- 8 pid_t id = fork();
- 9 if(id == 0)//child
- 10 {
- 11 printf("child do something... ,pid:%d,ppid:%d\n",getpid(),getppid());
- 12 sleep(10);
- 13 exit(1);//退出该进程
- 14 }
- 15
- 16 else if(id > 0)//father
- 17 {
- 18 while(1)
- 19 {
- 20 printf("father do something... ,pid:%d,ppid:%d\n",getpid(),getppid());
- 21 sleep(1);
- 22 }
- 23 }
- 24
- 25 else
- 26 {
- 27 perror("fork");
- 28 }
- 29 return 0;
- 30 }

- 1 #include <stdio.h>
- 2 #include <stdlib.h>
- 3 #include <unistd.h>
- 4 #include <sys/types.h>
- 5
- 6 int main()
- 7 {
- 8
- 9 pid_t id = fork();
- 10 if(id == 0)//child
- 11 {
- 12 while(1)
- 13 {
- 14 printf("child do something... ,pid:%d,ppid:%d\n",getpid(),getppid());
- 15 sleep(1);
- 16 }
- 17 }
- 18
- 19 else if(id > 0)//father
- 20 {
- 21 printf("father do something... ,pid:%d,ppid:%d\n",getpid(),getppid());
- 22 sleep(3);
- 23 exit(1);
- 24 }
- 25
- 26 else
- 27 {
- 28 perror("fork");
- 29 }
- 30 return 0;

在运行中,查看进程的状态:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。