-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum_read.c
62 lines (49 loc) · 1.12 KB
/
sum_read.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
/*sending and receiving batches of data through fifo*/
//mkfifo sum
int main(int argc, char* argv){
//open fifo
int fd = open("sum", O_RDONLY);
if(fd==-1){
return 1;
}
int arr[5];
int i;
int sum = 0;
//reading from fifo
//read from fifo buffer as a single element
// for(int i=0;i<5;i++){
// if(read(fd, &arr, sizeof(int))==-1){
// return 2;
// }
// printf("Read %d\n", arr[i]);
// }
if(read(fd, arr, sizeof(int)*5)==-1){
return 2;
}
for(int i=0;i<5;i++){
sum+=arr[i];
}
printf("Sum: %d\n", sum);
close(fd);
//opening fifo again to send the sum back
int fd1 = open("sum", O_WRONLY);
if(fd1==-1){
return 1;
}
if(write(fd1, &sum, sizeof(int))==-1){
return 2;
}
printf("Sum sent back\n");
close(fd1);
//execte sum_read and sum_write smiultaeosly
return 0;
}