about summary refs log tree commit diff stats
path: root/dev/c/src/fork/fork.c
blob: 25e490915af55b76862d6a2ef78400a343bf1a8b (plain) (blame)
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
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(){ 

	pid_t cpid,ppid;
	char errbuf[1024];

	cpid = fork();
	if (cpid == -1) {
		(void) snprintf(errbuf, sizeof(errbuf),
				"fork: %s", strerror(errno));
		printf("%s\n", errbuf);
		exit(1);
	}

	if (cpid == 0) {	
		//child
		ppid = getppid();
		if(ppid == 1){
			printf("parent died ?\n");
			_exit(1);
		}
		printf("I'm child with  %i, parent %i\n", getpid(), ppid);
		_exit(0);
	}
/* parent */
	wait(NULL);
	printf("Child id: %i\n", cpid);
	return 0;
}