collapsing forks

Michael Pechner pechner at ddtg.com
Sat May 25 07:59:45 AEST 1991


I am having a very strange problem with A/UX.  I can cause a fork to 
"sort of" fail by having a huge automatic variable.  Here are two 
programs to show my point.  The only difference between the two programs 
are that junk is an automatic in the version that fails, and a global static 
in the version that works.

The working program:

	#include<signal.h>
	static char junk[256000];
	void catch()
	{
		printf("signal caught pid %d\n", getpid());
	}
	main(){
		int ret;
		int pip[2];
		char buf[3];

		printf("pipe call %d\n", pipe(pip));
		ret=fork();
		printf("fork just occured  %d \n", ret);
		if((ret=fork()) == 0){ /*child */
			signal(SIGALRM, catch);
			alarm(10);

			printf("return from child write %d\n", write(pip[1], "a", 1));
			sleep(2);
			exit(1);
		}
		else if(ret > 0){ /* parent */
			signal(SIGALRM, catch);
			alarm(10);
			printf("return from parent read %d\n", read (pip[0], buf, 1));
			printf("parent read %c\n", buf[0]);
			sleep(2);
			exit(1);
		}
	}

The output:
	pipe call 0
	fork just occured  0 
	fork just occured  991 
	return from child write 1
	return from parent read 1
	parent read a
	return from child write 1
	return from parent read 1
	parent read a

The non-working program.  The only difference is that "junk" is 
an automatic variable.

	#include<signal.h>
	void catch()
	{
		printf("signal caught pid %d\n", getpid());
	}
	main(){
		int ret;
		int pip[2];
		char buf[3];
		char junk[256000];

		printf("pipe call %d\n", pipe(pip));
		ret=fork();
		printf("fork just occured  %d \n", ret);
		if((ret=fork()) == 0){ /*child */
			signal(SIGALRM, catch);
			alarm(10);

			printf("return from child write %d\n", write(pip[1], "a", 1));
			sleep(2);
			exit(1);
		}
		else if(ret > 0){ /* parent */
			signal(SIGALRM, catch);
			alarm(10);
			printf("return from parent read %d\n", read (pip[0], buf, 1));
			printf("parent read %c\n", buf[0]);
			sleep(2);
			exit(1);
		}
	}


The output:
	pipe call 0
	fork just occured  1013 
	signal caught pid 1011
	return from parent read -1
	parent read 


Notice that on the failed call, the fork returns a valid pid to the parent.
The child does not return.
The child process collapses immediately.

Can anybody explain this to me?


-- 
pechner at mikey.ddtg.com (Michael Pechner)  | Pizza Probably The Worlds Most
DuPont Design Technologies Group          | Perfect Food.
Santa Clara, Ca                           | Carbo, Meat, Dairy, And Veggie 
                                          | All Food Groups In One.



More information about the Comp.unix.wizards mailing list