/*********************************************************
*
*	data.c
*	output file:	data
*	programmer:	Michael Day
*	copyright:	Michael Day, 1990; LAN TIMES, 1990
*
*	creates a fake database file using random characters,
*	reads records one at a time from the database file,
*	writes each record to a temp file, closes the temp
*	file.
*	background processes open file, write a string to 
*	file, close file, delete file in an endless loop.
*
*	compile each "c" file to the output file listed in 
*	its header.
*	place all files in the /bin directory
*	execute the benchmark by typing "data" at the prompt	
**********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <prot.h>

#define  CHARS 	1000
#define	 TEN_MEG 409600

char		records[7], proc[7], size[7], cache[2];

main()	{
	
	int 	i,x, r;
	time_t	beg_time, end_time;
	int	cum_time;
	long	*tloc;
		
	FILE	*db_file, *temp_file, *source;	
	int	randfill(int r);

	printf("LAN TIMES multiprocess database benchmark\n");
	printf("Copyright 1990 Michael Day and LAN TIMES\n");
	printf("Enter the number of records to create\n-->");
	scanf("%s", records);
	printf("Enter the size of each record in K\n-->");
	scanf("%s", size);
	printf("Enter the number of background processes to spawn\n-->");
	scanf("%s", proc);
	printf("Enter <1> to fill disk cache with background copy, <0> otherwise\n-->");
	scanf("%s", cache);	

/*****     now create the file and fill it with data     *****/
		
		printf("Creating Database Records . . .\n");
		r = (atoi(size) * 1000);
		randfill(r);

	if (atoi(cache) == 1)	{
		source = fopen("source.txt", "a");	
		printf("Creating 10MB file for background copy. . .\n");
		for(i = 0; i < TEN_MEG; i++)
			fputs("LAN TIMES Testing Center", source);
	}

/*****     start background processes     *****/


	if(atoi(cache) == 1)		
		system("/bin/cspawn");

	for (i = 0; i < atoi(proc); ++i)
		if(system("/bin/dspawn") == 0)
			printf("%i spawned\n", i +1); 
	

/*****     read and write each record to a temp file, delete file    *****/
	
	beg_time = time(&tloc);	
	printf("Reading and Writing each record . . .\n");
	db_file = fopen("db", "ab+");
	printf("dbfile opened\n");
	for(i = 0; i < atoi(records); i++)	{
		temp_file = tmpfile(); 
		fread( temp_file, *size, 1, db_file);
		fclose(temp_file);
	}

/*****     clean up *****/

/*	fclose(db_file);  */
	end_time = time(&tloc);
	cum_time = (end_time - beg_time);
	printf("Elapsed time: %i seconds\n", cum_time);
	printf("Parameters: %s records of %sK each\n", records, size);
	printf("            %s background processes\n", proc); 
	exit(0);
}




/*************************************************************************
*	randfill()
*************************************************************************/

int randfill(int r)	{
	
	int 	x, y;
	
	FILE 	*db_file;		
	
	db_file = fopen("db", "wb+");
	srand48(x);		
	for (x = 0; x < (r * atoi(records)); x++)	{
		y = (int)lrand48();
		putc((y/10000000), db_file);
	}
	fclose(db_file);
	return(0);
}



	
