Hello all,
I am try to see how forking the process works. I am having some problem with the code I have written :


Code:
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>

childprocess(int pid, int pnum);
main () {
	int pid, pid0, pid1, status, FD, coremaker, tochild[2], toparent[2];
	FD = creat("new file.txt", S_IRWXU);
	if((pid = fork()) < 0) {
		printf("Could not fork process");
	} else {
		if(pid !=0)
			printf("\nProcess forked");
	}
	
	if(pid == 0) childprocess(pid,1);
	//if(pid != 0) {
	
	pid = wait(&status);
	printf("\nProcess (id %d ) exited ", pid);
		
	//}
	
	if((pid = fork()) < 0) {
		printf("Could not fork second process");
	}else{
		if(pid !=0)
			printf("\nSecond process forked");
	}	
	if(pid == 0) childprocess(pid,2);
	
//	if(pid != 0) {
		pid = wait(&status);
		printf("\nProcess (id %d ) exited " , pid);
//	} 
	close(FD);
	coremaker = 1/0; // SIGFPE to produce the core file
	exit(0);
}

childprocess(int pid, int pnum) {
	float i = 0;
	int index = 0;
	for(index = 0; index < 900; index++) {
	}
	//printf("\nPID %d matches index : %d", pid, pnum); 
	exit(0);
}
I xpect the parent process to fork and wait for the second process to exit which goes to the function childprocess where it exits. Then the process should print on the terminal "Process id <child process id> terminated". Then it should fork again and do the same with the second chid process.

However the output is as follows :


Process forked
Process (id 16237 ) exited Process (id 16237 ) exited
Second process forked
Floating point exception (core dumped)


That is, I get the floating point exception before I get the message that the second child process has also exited.

When I comment out the division by zero, I get the followng output :


Process forked
Process (id 16324 ) exited Process (id 16324 ) exited
Second process forked
Process (id 16325 ) exited

Here I get the message, that the first child proces has exited, two times.
This seems a little strange. Please help me with it
Dhruv