-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmypipe.c
107 lines (81 loc) · 1.9 KB
/
mypipe.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
#include "mypipe.h"
struct buffinfo {
int size;
int rdptr;
int wrptr;
int full;
int empty;
};
typedef struct buffinfo *bufferT;
volatile bufferT buff = NULL;
int pipefd[2];
int fdin;
int fdout;
volatile int endOfFile = 0;
void pipe_init(int size) {
buff = (bufferT)malloc( sizeof(bufferT) );
if (buff == NULL) {
printf("Error allocating memory\n");
exit(-1);
}
buff->size=size;
buff->rdptr=0;
buff->wrptr=0;
buff->full =size;//semaphore
buff->empty=0;//semaphore
if ( pipe(pipefd) == -1) {
printf("Error at pipe\n");
exit(-1);
}
}
void pipe_write(char c) {
int nxt_wr_ptr = (buff->wrptr + 1)%buff->size;
//allaksa apo nxt se curr
while (buff->full == 0) {
}
buff->full--;
//arxi krisimou tmimatos-grapse ston agwgo
if (write (pipefd[1], &c,1) != 1) {//circbuffer[wrptr] = c;
printf("pipe_write:Error writing 1 byte:\n");
exit(-1);
}
//telos krisimou tmimatos-auksise ton write-pointer
printf("write: %c, pos: %d\n", c,buff->wrptr);
buff->wrptr = nxt_wr_ptr;
buff->empty++;
return ;
}
int pipe_read(void) {
char c;
int bytesWrite;
while (buff->empty == 0) {//if empty
if (endOfFile == 1)
return 0;
}
buff->empty--;
//arxi krisimou tmimatos
if (1 != read (pipefd[0], &c, 1) ) {
printf("Error reading 1 Byte\n");
exit(-1);
}
//telos krisimou kwdika - auksise ton read-pointer
bytesWrite = write(fdout, &c, sizeof(char) );
if (bytesWrite == -1 ) {
printf("Write error at writer() function.Exiting\n");
exit(-1);
}
printf("read: %c, pos: %d\n", c,buff->rdptr);
buff->rdptr = (buff->rdptr+1)%buff->size;
buff->full++;
return 1;
}
void pipe_close() {
if ( close(pipefd[0]) == -1 ) {
printf("Error closing pipe read\n");
return ;
}
if ( close(pipefd[1]) == -1 ) {
printf("Error closing pipe write\n");
return ;
}
}