-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
93 lines (75 loc) · 2.11 KB
/
main.c
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
/*
* =====================================================================================
*
* Filename: main.c
*
* Description: Main logic of votes
*
* Version: 1.0
* Created: 2013/08/29 22时26分17秒
* Revision: none
* Compiler: gcc
*
* Author: Baoxu Shi (http://github.com/bxshi), [email protected]
* Organization: University of Notre Dame
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "opts.h"
#include "constants.h"
#include "file_reader.h"
#include "vote_counter.h"
#include "threadpool.h"
#include "count.h"
int main(int argc, char **argv)
{
int i, dup_vote;
int cores, block_size;
cores = 16;
block_size = 1024*1024;
int *thread;
file_struct *fs;
threadpool_t *pool;
thread = malloc(sizeof(int));
init_count_mutex();
if(argc<2)
{
printf("[usage] vote_counter [-dupvote] [-cores C] [-blocksize B] [filename1] ... [filenameN]\n");
exit(-1);
}
char **file_list = malloc(sizeof(char *) * MAX_FILE_NUM);
memset(file_list, 0, (sizeof(char*))*MAX_FILE_NUM);
//analyze input arguments
if(!(dup_vote = opts(argc, argv, file_list, &cores, &block_size, &thread)))
{
exit(-1);
}
//printf("cores %d; block_size %d\n", cores, block_size);
//create pool according to total file size & number
if((pool = threadpool_create(8, 8192, 0)) == NULL)
{
printf("[error] can not initalize thread\n");
exit(-1);
}
init_mutex();
//dup_vote = dup_vote == 2 ? TRUE : FALSE;
dup_vote = FALSE;
for (i=0; file_list[i]!=NULL; i++)
{
fs = malloc(sizeof(file_struct));
fs->filename = file_list[i];
fs->dup_vote = FALSE;
fs->block_size = block_size;
//send read task to thread pool
threadpool_add(pool, &readfile, fs, 0);
}
//wait until all thread finish their job
threadpool_destroy(pool, 1);
get_vote_result(3);
output();
return 0;
}