-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_sorted.c
396 lines (334 loc) · 9.62 KB
/
pthread_sorted.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <semaphore.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/time.h>
#include <pthread.h>
#include "pthread_common.h"
#define MAX_PORT 4
#define THREAD_COUNT 2
char master_of_the_buffer [20][BUFSIZ]; /* obey your masters, your life burns faster */
extern int errno;
short unsigned int ports = 0x9123; /* higher port starting from 37155*/
int current = 0;
struct element *list, *head;
struct sock_s {
struct sockaddr_in sin;
unsigned short int port;
int sd;
};
struct local_s {
struct sockaddr_in pin;
int sock;
fd_set active;
int i;
int *count;
char input[BUFSIZ];
char input1[BUFSIZ];
int fd_pipe;
pthread_mutex_t lock;
};
void populate_sock(int *sock, struct sockaddr_in *sin, const unsigned short int port)
{
if ((*sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = INADDR_ANY;
sin->sin_port = htons(port);
if (bind(*sock, (struct sockaddr *) sin, sizeof(*sin)) == -1) {
perror("bind");
close(*sock);
exit(EXIT_FAILURE);
}
}
void get_me_the_fds(fd_set active_sd, int *fd)
{
int status;
int i;
fd_set read_sd;
FD_ZERO(&read_sd);
read_sd = active_sd;
if ((status = select (FD_SETSIZE, &read_sd, NULL, NULL, NULL)) < 0) {
printf("%s %d\n",strerror(errno), __LINE__);
exit (EXIT_FAILURE);
}
// anuz_debug("number of descriptor %d\n", status);
for(i = 0; i < FD_SETSIZE; ++i)
if (FD_ISSET (i, &active_sd)) {
*fd = i;
}
}
void log_stuff(const char *message, int parameter)
{
char msg[BUFSIZ];
memset(msg, 0, sizeof(msg));
sprintf(msg, "echo %s %d >> /tmp/error.txt", message, parameter);
system(msg);
}
void get_me_the_buffer(int sock, char* input, struct sockaddr_in pin)
{
char buf[BUFSIZ];
int i;
int addrlen = sizeof(pin);
time_t epoch_time = time((time_t*)0);
memset(buf, 0, sizeof(buf));
if( recvfrom(sock , buf, sizeof(buf), 0, (struct sockaddr *)&pin, &addrlen) == -1 ) {
printf("recvfrom error");
}
strcpy(input, buf);
// anuz_debug("%s\n",buf);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%ld %s",epoch_time ,"from_server");
if ( sendto(sock, buf , strlen(buf), 0, (struct sockaddr *)&pin, addrlen) == -1) {
printf("%s %d",strerror(errno), __LINE__);
exit(1);
}
}
void child_stuff( struct local_s *local)
{
memset(local->input, 0, sizeof(local->input));
get_me_the_fds(local->active, &local->sock );
get_me_the_buffer(local->sock, local->input, local->pin);
pthread_mutex_lock(&local->lock);
// printf(" holding lock at %d %d %d\n", __LINE__, *local->count, local->i);
//strcpy(local->input, "paapi");
strcat(local->input, local->input1);
write(local->fd_pipe,local->input, sizeof(local->input));
pthread_mutex_unlock(&local->lock);
// printf("lock released at %d %d %d\n", __LINE__, *local->count, local->i);
}
void error_exit(char *str)
{
fprintf(stderr,"%s", str);
exit(EXIT_FAILURE);
}
void * thread_function(void * args)
{
struct local_s * local = (struct local_s *)args;
int *count;
pthread_mutex_lock(&local->lock);
count = local->count;
pthread_mutex_unlock(&local->lock);
while(*count < 10) {
pthread_mutex_lock(&local->lock);
//printf(" holding lock at %d %d %d\n", __LINE__, *local->count, local->i);
memset(local->input1, 0, sizeof(local->input1));
sprintf(local->input1, "__In_Thread-child-%d,count-%d\n",local->i,*local->count);
pthread_mutex_unlock(&local->lock);
//anuz_debug("lock released at %d %d %d\n", __LINE__, *local->count, local->i);
child_stuff(local);
pthread_mutex_lock(&local->lock);
// printf(" holding lock at %d %d %d\n", __LINE__, *local->count, local->i);
(*count)++; /*if at all this works */
pthread_mutex_unlock(&local->lock);
// printf("lock released at %d %d %d\n", __LINE__, *local->count, local->i);
}
pthread_exit("Thank you for the CPU time");
}
void get_config(int *thread_count,int *child_count)
{
struct parameter param;
param.params[0] = (char *)malloc(BUFSIZ);
param.params[1] = (char *)malloc(BUFSIZ);
param.filepath = (char *)malloc(BUFSIZ);
strcpy(param.filepath, "deamon_param.cfg");
parse_file(¶m);
*thread_count = atoi(param.params[0]);
*child_count = atoi(param.params[1]);
free(param.filepath);
param.filepath = NULL;
free(param.params[0]);
param.params[0] = NULL;
free(param.params[1]);
param.params[1] = NULL;
}
void add_buf_to_list(char *buf)
{
struct element *temp = NULL;
temp = (struct element *)malloc(sizeof(struct element));
temp->count = current;
temp->string = (char *)malloc(strlen(buf)+1);
strcpy(temp->string, buf);
temp->next = NULL;
if(!head) {
list = temp;
head = list;
anuz_debug("list reinstialized = %d\n",head->count);
} else {
list->next = temp;
list = list->next;
}
}
void write_to_file(int fd1, char *buf, int limit)
{
int i;
int status;
struct element *temp;
add_buf_to_list(buf);
anuz_debug("%d\n",current);
//strcpy(master_of_the_buffer[current], buf);
current++;
if(current == limit) {
head = sort_list(head);
// merge_sort(master_of_the_buffer, current);
// for(i = 0; i < limit; i++) {
for(temp = head; temp; temp = temp->next){
if(status = write(fd1, temp->string, strlen(temp->string)))
anuz_debug("counter = %d status = %d line=%d\n",temp->count, status, __LINE__);
}
write(fd1, "===8===\n", strlen("===8===\n"));
current = 0;
free_list(head);
head = list = NULL;
}
}
void create_server(void)
//void demonize(void)
{
int pd[4][2];
int pid[4];
char buf[BUFSIZ];
int i = 0;
int status;
fd_set active;
fd_set readf;
int count = 0, count1 = 0;
fd_set active_sd, read_sd; /* i am running short on names */
struct sock_s s1[4]; /*cute names and stuff */
int fd1;
struct timeval timeout;
static pthread_mutex_t work_mutex[MAX_PORT]; /* the lock this file needs */
int thread_count, child_count;
fd1 = open ("/tmp/whatever.txt", O_CREAT | O_RDWR | O_TRUNC, 0644);
//anuz_debug("parent id %d\n",(int)getpid());
FD_ZERO(&active);
FD_ZERO(&readf);
FD_ZERO(&active_sd);
timeout.tv_sec = 1;
timeout.tv_usec = 60;
get_config(&thread_count, &child_count);
printf("thread count %d\n",thread_count);
printf("child count %d\n",child_count);
for( ;i < child_count; i++) {
pipe(pd[i]);
//fcntl(pd[i][0], F_SETFL, O_NONBLOCK);
FD_SET(pd[i][0], &active);
s1[i].port = ports + i;
populate_sock(&s1[i].sd, &s1[i].sin, s1[i].port);
FD_SET(s1[i].sd, &active_sd);
pid[i] = fork();
if(pid[i] == 0) {
struct local_s local_data;
int res;
int j;
pthread_t a_thread[thread_count];
void *thread_result;
res = pthread_mutex_init(&work_mutex[i], NULL);
if(res != 0) {
error_exit("mutex init error \n");
}
local_data.active = active_sd;
local_data.i = i;
local_data.fd_pipe = pd[i][1];
local_data.lock = work_mutex[i];
local_data.count = &count;
for(j = 0; j < thread_count; j++) {
res = pthread_create(&a_thread[j], NULL, thread_function, (void *)&local_data);
if(res != 0) {
error_exit("thread creation error \n");
}
}
while(count < 10) {
pthread_mutex_lock(&local_data.lock);
// anuz_debug(" lock holding in child at %d %d %d\n", __LINE__, count, i);
memset(local_data.input1, 0, sizeof(local_data.input1));
sprintf(local_data.input1, "__In_Main-child-%d,count-%d\n",local_data.i,*local_data.count);
pthread_mutex_unlock(&local_data.lock);
// anuz_debug(" lock released in child at %d %d %d\n", __LINE__, count, i);
child_stuff(&local_data);
pthread_mutex_lock(&local_data.lock);
// printf(" lock holding in child at %d %d %d\n", __LINE__, count, i);
count++; /* we need this count to be 10 in total from both the threads. */
pthread_mutex_unlock(&local_data.lock);
// anuz_debug(" lock released in child at %d %d %d\n", __LINE__, count, i);
}
for(j = 0; j < thread_count; j++) {
res = pthread_join(a_thread[j], &thread_result);
if(res != 0) {
error_exit("thread joined failed \n");
}
anuz_debug("Thread joined, it returned\n %s\n", (char *)thread_result);
}
pthread_mutex_destroy(&work_mutex[i]);
exit(EXIT_SUCCESS);/* safe exit */
}
}
while( count1 < 10 * child_count ) {
anuz_debug("id = %d\n",(int)getpid());
readf = active; /* ok this is confusing because of the name as active and read */
if ((status = select (FD_SETSIZE, &readf, NULL, NULL, NULL)) < 0) {
printf("%s",strerror(errno));
exit (EXIT_FAILURE);
}
for(i = 0; i < FD_SETSIZE; ++i) {
if (FD_ISSET (i, &readf)) {
memset(buf,0, sizeof(buf));
if(read(i, buf, BUFSIZ)>0) {
anuz_debug("%s\n",buf);
count1++;
write_to_file(fd1, buf, child_count * thread_count);
}
}
}
waitpid(-1, &status, WNOHANG);
if (WIFEXITED(status))
anuz_debug("exited, status=%d **COUNT**%d\n", WEXITSTATUS(status), count1);
}
close(fd1);
}
void deamonify(void)
{
pid_t sid;
int fd;
sid = setsid(); /* get a new process group. */
chdir("/");
umask(0);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
fd = open("/dev/null", O_RDWR);/* stdin */
(void) dup(fd); /* stdout */
(void) dup(fd); /* stderr */
anuz_debug ("again process = %d\n ",getpid());
create_server();
}
int demonize()
{
pid_t pid;
if ((pid = fork()) == 0) {
anuz_debug("child process = %d %d \n ",getpid(), __LINE__);
deamonify();
} else if (pid < 0) {
fprintf(stderr,"fork error %s\n", strerror(errno));
} else {
anuz_debug("parent process = %d\n ",getpid());
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
int main(void)
{
//return demonize();
create_server();
return 0;
}