Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/timeb.h>
#include <signal.h>
#include <iostream>
#include <sys/time.h>
#define DATA_LOCATION "/home/codes/expt/graph/"
#define EXEC_LOCATION "/home/codes/lcm4/"
#define MAX_FILENAME_LEN 200
#define MAX_LINE_LEN 500
#define MAX_COMD_LEN 500
void timer_handler (int signum)
{
static int count = 0;
char str_cmd[MAX_COMD_LEN];
printf ("timer expired %d times\n", ++count);
sprintf(str_cmd, "kill -0 $! && kill $!");
system(str_cmd);
}
void LCM(char* dataset_name, int start_min_sup, int iterations, int inc_step, bool bout, int tnum)
{
FILE* fpsum_file;
char str_cmd[MAX_COMD_LEN];
double running_time, total_time;
int ntotal_pats, nmin_sup, i;
struct timeb start, end, end1;
fpsum_file = fopen("aaLCMtransaction.time.sum.txt", "at");
if(fpsum_file==NULL)
{
printf("Error: cannot open file afopt.time.sum.txt for write\n");
exit(-1);
}
if(fpsum_file==NULL)
{
printf("Error: cannot open file afopt.time.sum.txt for write\n");
exit(-1);
}
for(i=0;i<iterations;i++)
{
nmin_sup = i*inc_step + start_min_sup;
if(bout) {
sprintf(str_cmd, "%slcm C %s%s %d ", EXEC_LOCATION, DATA_LOCATION, dataset_name, nmin_sup);
printf("%s\n", str_cmd);
ftime(&start);
ntotal_pats = system(str_cmd);
ftime(&end);
total_time = running_time = end.time-start.time+(double)(end.millitm-start.millitm)/1000;
fprintf(fpsum_file, "LCM\t");
fprintf(fpsum_file, "%s\t", dataset_name);
fprintf(fpsum_file, "%d\t", nmin_sup);
// fprintf(fpsum_file, "%d\t", ntotal_pats);
fprintf(fpsum_file, "%f\n", running_time);
}
}
fclose(fpsum_file);
}
void BatchExec(char* config_filename)
{
FILE* fpconfig_file;
char str_cmd[MAX_COMD_LEN], str_out_flag[10], dataset_name[MAX_FILENAME_LEN];
int nmin_sup, inc_step, iteration, ntype, tnum;
bool bout;
char ch;
fpconfig_file = fopen(config_filename, "rt");
if(fpconfig_file==NULL)
{
printf("Erorr: cannot open file %s for read\n", config_filename);
exit(-1);
}
fscanf(fpconfig_file, "%s ", str_cmd);
while(!feof(fpconfig_file))
{
if(str_cmd[0]=='#')
{
ch = getc(fpconfig_file);
while(!feof(fpconfig_file) && ch!='\n')
ch = getc(fpconfig_file);
fscanf(fpconfig_file, "%s\n", str_cmd);
continue;
}
fscanf(fpconfig_file, "%s ", dataset_name);
fscanf(fpconfig_file, "%d", &nmin_sup);
fscanf(fpconfig_file, "%d", &iteration);
fscanf(fpconfig_file, "%d", &inc_step);
fscanf(fpconfig_file, "%s", str_out_flag);
fscanf(fpconfig_file, "%d", &tnum);
if(!strcmp(str_out_flag, "true"))
bout = true;
else
bout = false;
if(!strcmp(str_cmd, "lcm"))
LCM(dataset_name, nmin_sup, iteration, inc_step, bout, tnum);
fscanf(fpconfig_file, "%s ", str_cmd);
}
fclose(fpconfig_file);
}
int main(int argc, char* argv[])
{
struct sigaction sa;
struct itimerval timer;
/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sigaction (SIGVTALRM, &sa, NULL);
/* Configure the timer to expire after 250 msec... */
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 250000;
/* ... and every 250 msec after that. */
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 250000;
/* Start a virtual timer. It counts down whenever this process is
executing. */
setitimer (ITIMER_VIRTUAL, &timer, NULL);
while (1) {
if(argc==2)
BatchExec(argv[1]);
}
return 0;
}