forked from fbaiodias/so
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleitor-threads-3.c
164 lines (130 loc) · 4.13 KB
/
leitor-threads-3.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
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
153
154
155
156
157
158
159
160
161
162
163
164
/* Grupo 55
*
* Cristiano Rocha nº62502
* Pedro Saraiva nº70848
* Francisco Dias nº75328
*
* Exercício 4
* */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/file.h>
#include <pthread.h>
#include "consts.h"
/*-------------------------------------------------------------------------------------
| fixed strings
+-------------------------------------------------------------------------------------*/
char* myfiles[N_FILES] = { "SO2014-0.txt",
"SO2014-1.txt",
"SO2014-2.txt",
"SO2014-3.txt",
"SO2014-4.txt"};
typedef struct {
int offset;
int linesToRead;
} offset_table;
/* declarações globais.
variáveis que vão ser acedidas por todas as threads */
offset_table *offsetTable; /*tabela com N_THREADS posições e que contem em casa posição o offset e o número de linhas a ler */
char *file; /*nome do ficheiro a ser aberto */
char stringToCompare[STR_LENGTH]; /*primeira string do ficheiro a ser comparado */
void initOffsetTable(int size)
{
int i, offset;
offset = size / N_THREADS;
for(i = 0; i < N_THREADS-1; i++)
{
offsetTable[i].offset = i * (offset - (offset % STR_LENGTH));
offsetTable[i].linesToRead = TOTAL_LINES / N_THREADS;
}
offsetTable[i].offset = i * (offset - (offset % STR_LENGTH));
offsetTable[i].linesToRead = TOTAL_LINES - ((N_THREADS-1) * (int)(TOTAL_LINES / N_THREADS)); /* no caso da última thread que vai verificar a parte final do ficheiro pode ter de ler mais linhas do que as outras */
}
int* threadedFunction(void *arguments)
{
int fd,j;
long i = (long) arguments;
fd = open (file, O_RDONLY);
if (fd == -1) {
perror ("Error opening file");
return (void*)-1;
}
else {
if(lseek(fd, (off_t) offsetTable[i].offset, SEEK_SET) == -1)
{
perror("Error moving the file pointer");
exit(-1);
}
char string_to_read[STR_LENGTH];
for (j = 0; j < offsetTable[i].linesToRead; j++ ){
if (read (fd, string_to_read, STR_LENGTH) == -1) {
perror ("Error reading file");
return (void*)-1;
}
if (strncmp(string_to_read, stringToCompare, STR_LENGTH)) {
fprintf (stderr, "Section %ld of the file %s is inconsistent\n",i, file);
return (void*)-1;
}
}
if (close (fd) == -1) {
perror ("Error closing file");
return (void*)-1;
}
}
printf("Section %ld of the file %s is consistent\n", i, file); /* pode ficar ou nao */
return (void*)0;
}
/*-------------------------------------------------------------------------------------
| main
+-------------------------------------------------------------------------------------*/
int main (int argc, char** argv) {
pthread_t * serverThreads;
serverThreads = (pthread_t *) malloc (sizeof (pthread_t) * N_THREADS);
offsetTable = (offset_table *) malloc(sizeof (offset_table) * N_THREADS);
int err, fd, size, random;
long i;
void *returnvalue;
srandom ((unsigned) time(NULL));
random = get_random(N_FILES);
file = myfiles[random]; /* para testar alterar para myfiles[0] e alterar ficheiro SO2014-0.txt */
fd = open (file, O_RDONLY);
if (read (fd, stringToCompare, STR_LENGTH) == -1) {
perror ("Error reading file");
exit (-1);
}
if((size = lseek(fd, (off_t) 0, SEEK_END)) ==-1 )
{
perror("Error while getting the file size");
exit(-1);
}
initOffsetTable(size);
if (close (fd) == -1) {
perror ("Error closing file");
exit (-1);
}
for (i = 0; i < N_THREADS; i++) {
err = pthread_create (&serverThreads[i], NULL, (void*) threadedFunction, (void*)i);
if (err != 0) {
perror ("\nerror: thread creation failed\n");
exit (EXIT_FAILURE);
}
}
for (i = 0; i < N_THREADS; i++) {
err = pthread_join (serverThreads[i], &returnvalue);
if (err != 0) {
perror ("\nerror: thread creation failed\n");
exit (EXIT_FAILURE);
}
if((long int)returnvalue != 0)
{
perror("File is inconsistent");
exit(-1);
}
}
printf("File %s is consistent\n", file);
return 0;
}