-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfork3.c
More file actions
47 lines (42 loc) · 1004 Bytes
/
fork3.c
File metadata and controls
47 lines (42 loc) · 1004 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*************************************************************************
>文件名称: fork3.c
>程序功能:孤儿进程-父进程在子进程前执行完毕,之后该子进程被init进程收养
>注意事项:
>创建时间: 2014年12月10日 星期三 20时53分55秒
>保持好奇,虚心接受
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#define FALSE 0
#define TRUE 1
#define OK 1
#define ERROR 0
#define MAXSIZE 100
int main(int argc, char *argv[])
{
pid_t pid;
int k = 5;
pid = fork();
switch(pid)
{
case 0:
while(1)
{
printf("子进程 is running, PID 为%d, his parent's PID 为%d\n", getpid(), getppid());
sleep(1);
//k--;
if(k == 0)
exit(0);
k--;
}
case -1:
perror("Process creation failed\n");
exit(-1);
default:
printf("父进程正在执行,PID为%d", getpid());
exit(0);
}
return 0;
}