-
Notifications
You must be signed in to change notification settings - Fork 0
/
up.c
97 lines (72 loc) · 1.73 KB
/
up.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
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#define SIZE 200000
void bar(int percent_done);
void fillBuffer(int buf[], int bufSize){
for (int i = 0; i < bufSize; i++)
{
buf[i] = rand() % 10;
}
}
int main(int argc, char *argv[]){
double time;
int buffer[SIZE];
int bufSize = atoi(argv[3]);
struct timespec start, finish;
// Producer Process.
if(fork() == 0){
if (bufSize < SIZE) fillBuffer(buffer, bufSize);
else fillBuffer(buffer, SIZE);
int w_fd = atoi(argv[1]);
clock_gettime(CLOCK_REALTIME, &start);
for (int i = 0; i < bufSize - 1; i++)
{
write(w_fd, &buffer[(i) % SIZE], sizeof(int));
}
time = 1000000000 * (start.tv_sec) + (start.tv_nsec);
int fd_fifo = open("/tmp/my_time_p", O_WRONLY);
write(fd_fifo, &time, sizeof(double));
//sleep(10);
return 0;
}
// Consumer Process.
else{
int r_fd = atoi(argv[2]);
for (int i = 0; i < bufSize - 1; i++)
{
if(i % bufSize/100 == 0){
bar(i);
usleep(20000);
}
read(r_fd, &buffer[(i) % SIZE], sizeof(int));
}
printf("\n");
clock_gettime(CLOCK_REALTIME, &finish);
time = 1000000000 * (finish.tv_sec) + (finish.tv_nsec);
int fd_fifo = open("/tmp/my_time_c", O_WRONLY);
write(fd_fifo, &time, sizeof(double));
return 0;
}
}
void bar(int percent_done){
const int PROG_BAR_LENGHT = 30;
int num_chars = percent_done * PROG_BAR_LENGHT / 100 ;
printf("\r[");
for(int i = 0 ; i < num_chars ; i++){
printf("#");
}
for(int i = 0 ; i < PROG_BAR_LENGHT - num_chars; i++){
printf(" ");
}
printf("] %d%% DONE", percent_done);
fflush(stdout);
}