File size: 3,183 Bytes
910db9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include "vmax.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "comm.h"
#include <unistd.h>
#include <getopt.h>

char f1[JOBS_AT_A_TIME][256];
char f2[JOBS_AT_A_TIME][256];
long _start_ = 0;

static FILE *out = NULL;
static FILE *err = NULL;

void process_alarm(int sig)
{
	fprintf(stderr, "time out. terminate now.\n");
	stop_now = 1;
	signal(SIGALRM, process_alarm); /* maybe unnecessary, but won't hurt */
}

void leave(int sig)
{
	fprintf(stderr, "received SIGINT. Save and exit.\n");
	if (out) {
		fflush(out);
		fclose(out);
	}
	if (err) {
		fflush(err);
		fclose(err);
	}
	exit(1);
}

int main(int argc, char* argv[])
{
	signal(SIGALRM, process_alarm);
	signal(SIGINT, leave);

	int verbose = 0;
	int clean_start = 0;
	int mc = 1;
	char host[256] = "localhost";

	/* time stats */
	struct timeval begin, end;
	double time_total = 0;
	
	char c;
	int k;
	while ((c = getopt (argc, argv, "vcm:h:")) != -1) {
		switch(c) {
			case 'v' :
				verbose = 1;
				break;
			case 'c' :
				clean_start = 1;
				break;
			case 'h' :
				k = sscanf(optarg, "%s", host);
				if (k != 1) {
					fprintf(stderr, "Usage: %s [-v] [-c] [-m maxconn] [-h host] output\n", argv[0]);
					return 1;
				}
				break;
			case 'm' :
				k = sscanf(optarg, "%d", &mc);
				if (k != 1) {
					fprintf(stderr, "Usage: %s [-v] [-c] [-m maxconn] [-h host] output\n", argv[0]);
					return 1;
				}
				break;
			default :
				fprintf(stderr, "Usage: %s [-v] [-c] [-m maxconn] [-h host] output\n", argv[0]);
				return 1;
		}
	}

	if (optind != argc - 1) {
		fprintf(stderr, "Usage: %s [-v] [-c] [-m maxconn] [-h host] output\n", argv[0]);
		return 1;
	}

	char* output = argv[optind];

	char mode[3] = "";
	if (clean_start) strcat(mode, "w");
	else strcat(mode, "a");

	out = fopen(output, mode);
	if (out == NULL) {
		fprintf(stderr, "cannot open file %s for write\n", output);
		return 1;
	}

	char err_filename[256] = "";
	strcat(err_filename, output);
	strcat(err_filename, ".err");
	err = fopen(err_filename, mode);
	if (err == NULL) {
		fprintf(stderr, "cannot open file %s for write\n", err_filename);
		return 1;
	}

	int lines;
	int progress = 0;
	do {
		lines = refill(host, (char**)f1, (char**)f2);
		//printf("read %d lines\n", lines);
		int i;
		for (i = 0; i < lines; i ++) {

			Graph *g = read_graph(f1[i]);
			Graph *g2 = read_graph(f2[i]);
			if (g == NULL || g2 == NULL) {
				fprintf(err, "%s:%s:skipped\n", f1[i], f2[i]);
				if (g) destruct(g);
				if (g2) destruct(g2);
				continue;
			}

			if (verbose) fprintf(stdout, "%s:%s\n", f1[i], f2[i]);
			gettimeofday(&begin, NULL);
			alarm(MCS_TIMEOUT);
			int best_l = max(g, g2, mc, 0, 0);
			alarm(0);
			gettimeofday(&end, NULL);
			time_total += (end.tv_sec - begin.tv_sec + 
				(end.tv_usec - begin.tv_usec) * 0.000001);

			destruct(g);
			destruct(g2);

			fprintf(out, "%s:%s:%d\n", f1[i], f2[i], best_l);
		}
		fflush(out);
		fflush(err);
		progress += i;
		printf("finished %d lines\n", progress);
	} while (lines == JOBS_AT_A_TIME);

	fprintf(err, "Total time: %f\n", time_total);
	fclose(out); fclose(err);
	return 0;
}