I do C ++ programming under LINUX. I have two independent processes. I have to provide communication using a named pipe.
Reader: - creates FIFO using mkfifo - status = mkfifo (myFIFO, 0666) - opens a pipe using open-fifo = open (myFIFO, O_RDONLY) -reads from pipe - num = read (fifo, temp, sizeof (temp))
Writer: -opens pipe - fifo = open (myFIFO, O_WRONLY); -writes to pipe - num = write (fifo, string, strlen (string));
I noticed that the file descriptor was returned for the reading process and the writing process is 0. In addition, after the write command, I can see a line on my terminal that should be written to the channel. I do not know why this is shown on the terminal ... Also, the number of bytes written is 0 ...
Could you help me? Thank!
#define myFIFO "/temp/FIFO"
int main(){
int num, fifo, status;
char temp[32];
if (status = mkfifo(myFIFO, 0666) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}
if (fifo = open(myFIFO, O_RDONLY) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}
if (num= read(fifo, temp, sizeof(temp)) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}
printf("In FIFO is %s \n", temp);
}
And in another file:
#define myFIFO "/temp/FIFO"
int main() {
int status, num, fifo;
char string[]="Testing...";
if (fifo = open(myFIFO, O_WRONLY) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}
if (num= write(fifo, string, strlen(string)) < 0) {
printf("\n %s \n", strerror(errno));
return 0;
}
}