
/*********************************************************
*
*	filename:	malloc.c
*	output file:	malloc
*	programmer:	Michael Day
*	copyright:	Michael Day, 1990; LAN TIMES, 1990
*
*	allocates arrays, fills arrays with characters, copies
*	arrays, frees arrays. Background processes do essentially
*	the same thing in an endless loop. 
*	
*	compile to output files and place executables in /bin
*	
**********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <prot.h>
#include <string.h>

#define  CHARS 	1000

char		structs[7], proc[7], instr[CHARS];

main()	{
	
	int 	i, x, r;
	time_t	beg_time, end_time;
	int	cum_time;
	int	*s;
	long	*tloc;
		
	char	*source_array, *temp_array;	
	int	randchar();
	
	printf("Multiprocess memory allocation benchmark\n");
	printf("Copyright 1990 Michael Day and LAN TIMES\n");
	printf("Enter the number of arrays to create for each iteration\n-->");
	scanf("%s", structs);
	printf("Enter the number of background processes to spawn\n-->");
	scanf("%s", proc);

/*****     start background processes     *****/

	beg_time = time(&tloc);	
	for (i = 0; i < atoi(proc); ++i)	{
		system("/bin/mspawn");	
		printf("%i spawned\n", i+1);
	}

/*****     now create static array  and fill it with data     *****/
		
	for(i = 1; i <= 1000; i++)		{
		source_array = (char *)malloc(CHARS + i);	
		memset(source_array, 'a', (sizeof(source_array)));
	
/*****     read and write each record to a temp array, free array    *****/
	
		for(x = 0; x < atoi(structs); x++)	{
			temp_array = (char *)malloc(sizeof(source_array)); 
			memcpy(temp_array, source_array, sizeof(source_array));
			free(temp_array);
		}
	}
/*****     clean up *****/

	free(source_array); 
	end_time = time(&tloc);
	cum_time = (end_time - beg_time);
	printf("Elapsed time: %i seconds\n", cum_time);
	printf("Parameters: %s structures\n", structs);
	printf("            %s background processes\n", proc); 
	exit(0);
}
