-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdog.c
105 lines (82 loc) · 2.41 KB
/
dog.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
/**
* The canine challenge
* @see http://1.61803398874.com/canine/
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#define BUFFERSIZE 1
struct thread_arg
{
int quantity;
char* filename;
int* fd;
};
void write_to_file(void* fd);
int main(int argc, char **argv){
unsigned char buffer[BUFFERSIZE];
int thread_quantity = argc - 1;
FILE *instream;
int bytes_read=0;
int buffer_size=0;
int i;
pthread_t* threadsIds = malloc(sizeof(pthread_t) * thread_quantity);
int** pipes = malloc(sizeof(int*) * thread_quantity);
buffer_size=sizeof(unsigned char)*BUFFERSIZE;
/* open stdin for reading */
instream=fopen("/dev/stdin","r");
if(instream!=NULL) {
/**
* Create one thread per output file
* Also generate the pipes needed to IPC
*/
for(i=0; i < thread_quantity; i++) {
int* fd = malloc(sizeof(int)*2);
struct thread_arg* param = malloc(sizeof(struct thread_arg));
pipes[i] = fd;
pipe(fd);
param->quantity = thread_quantity;
param->fd = fd;
param->filename = argv[i + 1];
pthread_create (&threadsIds[i], NULL, (void*)write_to_file, (void*)param);
}
/* read from stdin until it's end */
while((bytes_read=fread(&buffer, buffer_size, 1, instream))==buffer_size) {
/* Write on thread_quantity pipes */
for(i=0; i < thread_quantity; i++) {
write(pipes[i][1], buffer, buffer_size);
}
}
/* Waiting for others threads to join this*/
for(i=0; i < thread_quantity; i++) {
close(pipes[i][1]);
pthread_join(threadsIds[i], NULL);
}
/**
* Free all resources
*/
free(threadsIds);
free(pipes);
} else {
/* if any error occured, exit with an error message */
fprintf(stderr, "ERROR opening stdin. aborting.\n");
exit(1);
}
return(0);
}
void write_to_file(void* t_param) {
struct thread_arg* param = (struct thread_arg*) t_param;
int buffer_size = sizeof(unsigned char)*BUFFERSIZE;
unsigned char buffer[BUFFERSIZE];
FILE * ostream;
ostream = fopen(param->filename, "w");
/* Start reading pipe and writing output*/
while(read(param->fd[0], buffer, buffer_size)) {
fprintf(stdout, "%c", buffer[0]);
fwrite(buffer, buffer_size, 1, ostream);
}
free(param);
fclose(ostream);
}