
#include <exec/types.h>
#include <stdio.h>

extern long LastLWPMem;

extern APTR ForkLWP();
extern APTR box();
int N;		/*  successes	*/

main(ac,av)
char *av[];
{
    int deep;
    int should, stack;
    APTR com;

    if (ac != 2) {
	puts("box <deep>");
	puts("numbers > 4 at your own risk");
	puts("defaulting to 4");
	deep = 4;
    } else {
	deep = atoi(av[1]);
    }

    com = box(deep);
    stack = LastLWPMem;
    should = count(deep);
    RunLWP();               /*  Initialize  */
    printf("init done, %d/%d & (%d per)%ld/%ld memory boxes\n", N, should, stack, (long)N * (long)stack, (long)should * (long)stack);
    puts("running");
    AlertLWP(com);
    RunLWP();
    puts("");
}

count(n)
{
    if (n)
	return(1+4*count(n-1));
    return(1);
}

APTR
box(n)
register int n;
{
    APTR com[4];
    APTR desc;

    if (desc = ForkLWP(32L, (long)sizeof(int)))
	return(desc);
    if (n) {
	com[0] = box(n-1);
	com[1] = box(n-1);
	com[2] = box(n-1);
	com[3] = box(n-1);
    }
    ++N;
    WaitLWP();
    if (n) {
	AlertLWP(com[0]);
	AlertLWP(com[1]);
	AlertLWP(com[2]);
	AlertLWP(com[3]);
    }
    if (ForkLWP(2048L, (long)sizeof(int)))
	return;
    printf("%d", n);
}

