-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnp.c
More file actions
82 lines (63 loc) · 1.5 KB
/
np.c
File metadata and controls
82 lines (63 loc) · 1.5 KB
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
#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 fillBuffer(int buf[], int bufSize)
{
for (int i = 0; i < bufSize; i++)
{
buf[i] = rand() % 9;
}
}
int main(int argc, char *argv[])
{
struct timespec start, finish;
double time;
int buffer[SIZE];
int bufSize = atoi(argv[2]);
// Producer Process (Child).
if(fork() == 0){
if (bufSize < SIZE){
fillBuffer(buffer, bufSize);
}
else{
fillBuffer(buffer, SIZE);
}
int fd = open(argv[1], O_WRONLY);
clock_gettime(CLOCK_REALTIME, &start);
for (int i = 0; i < bufSize - 1; i++){
write(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));
// printf("\n\ntime= %lf \n", time);
close(fd);
//close(fd_fifo);
//sleep(10);
return 0;
}
// Consumer Process (Parent).
else{
int fd = open(argv[1], O_RDONLY);
for (int i = 0; i < bufSize - 1; i++){
read(fd, &buffer[(i) % SIZE], sizeof(int));
}
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));
//printf("\n\ntime= %lf \n", time);
//close(fd);
//close(fd_fifo);
return 0;
}
}