So I have 2 processes, a client who gets 2 operands and one operation (either + or -), sends them to the second process, the server, who makes the computation, then sends the result back to the client.
This is the client:
#define FIFO_NAME1 "fifo1" #define FIFO_NAME2 "fifo2" int main() { if (access(FIFO_NAME1, 0) == 0){ unlink(FIFO_NAME1); } int fd1 = -1, fd2 = -1; int result, send[3]; char sign; if(mkfifo(FIFO_NAME1, 0600) != 0){ perror("Err creating FIFO"); return 1; } fd1 = open(FIFO_NAME1, O_WRONLY); if(fd1 == -1) { perror("Could not open FIFO"); return 1; } fd2 = open(FIFO_NAME1, O_RDONLY); if(fd2 == -1) { perror("Could not open FIFO"); return 1; } printf("Write expression: (x sign y): n"); scanf("%d %c %d", &send[0], &sign, &send[1]); if (sign == '+') send[2] = 1; else if (sign == '-') send[2] = 2; //cheking if reading was good for(int i = 0; i < 3; i++) printf("%d ", send[i]); printf("n"); write(fd1, send, sizeof(int) * 3); read(fd2, &result, sizeof(int)); printf("result: %dn", result); close(fd1); close(fd2); unlink(FIFO_NAME1); return 0; }
This is the server:
#define FIFO_NAME1 "fifo1" #define FIFO_NAME2 "fifo2" int main() { if (access(FIFO_NAME2, 0) == 0){ unlink(FIFO_NAME2); } int fd1 = -1, fd2 = -1; int result, recive[3]; if(mkfifo(FIFO_NAME2, 0600) != 0){ perror("Err creating FIFO"); return 1; } fd1 = open(FIFO_NAME1, O_RDONLY); if(fd1 == -1) { perror("Could not open FIFO1"); return 1; } fd2 = open(FIFO_NAME1, O_WRONLY); if(fd2 == -1) { perror("Could not open FIFO2"); return 1; } read(fd1, recive, sizeof(int) * 3); //checking if reading form pipe is good printf("recives %d sign:%d %dn", recive[0], recive[2], recive[1]); if(recive[2] == 1) result = recive[0] + recive[1]; else if (recive[1] == 2) result = recive[0] - recive[1]; //cheking result before writing to pipe printf("%dn", result); write(fd2, &result, sizeof(int)); close(fd1); close(fd2); unlink(FIFO_NAME2); return 0; } <br>
There are 3 outcomes to this code. First one is that it works, this is the least encountered case. Second one is that the data read from the server is not correct (I do a data check in the server). The first operand is read correctly but the sign and second operand are garbage values. Third case is that the operands and the sign are read correctly, but the result is again garbage. I don’t know why the third case even happens. So the data is correctly read, and my code that does the computation seems pretty straight forward and simple, but doesn’t work. I do a result check in the server before sending it back, and again, and it has garbage value. What aspect am I missing here?
Advertisement
Answer
The problem is probably, that all your communication is done via FIFO1, so your operands and the result get mixed up.
In the client you open FIFO1 for writing and reading and you write and read to it:
fd1 = open(FIFO_NAME1, O_WRONLY); if(fd1 == -1) { perror("Could not open FIFO"); return 1; } fd2 = open(FIFO_NAME1, O_RDONLY); //should be FIFO_NAME2 if(fd2 == -1) { perror("Could not open FIFO"); return 1; } ... write(fd1, send, sizeof(int) * 3); read(fd2, &result, sizeof(int));
in the server it is the same:
fd1 = open(FIFO_NAME1, O_RDONLY); if(fd1 == -1) { perror("Could not open FIFO1"); return 1; } fd2 = open(FIFO_NAME1, O_WRONLY); //should be FIFO_NAME2 if(fd2 == -1) { perror("Could not open FIFO2"); return 1; }
so you end up with two listeners to FIFO1