Hello everyone,
I'm currently working on inter-process communication.I want that a program call process1.c will take a user input (one character at a time) and write it in a named pipe,then a second program call process2.c will dynamically read the pipe and print the character each time a user press a new character.
In summary,the user start process1.c in one terminal window and process2.c in another terminal window. In process1.c terminal window,she is ask to enter an input,each time she enter a character,the character will immediately appear in the process2.c terminal window.This must be done without her pressing enter in process1.c terminal window.
I'm on linux,I try to do this,but when the user enter a character in process1.c terminal window it doesn't show up on process2.c terminal window unless I hit enter.Here is the code:
Code:
// PROGRAM process1.c //
///////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
int main() {
mkfifo("my_pipe_p1_p2", 0600);
int desc = open("my_pipe_p1_p2", O_WRONLY);
printf("your input: ");
char c[1];
while(1) {
c[0] = getchar();
write(desc, c, 1);
}
return 0;
}
// PROGRAM process2.c //
///////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
int main() {
mkfifo("my_pipe_p1_p2", 0600);
int desc = open("my_pipe_p1_p2", O_RDONLY);
char c[1];
while(1) {
read(desc, c, sizeof(c));
printf("%s", c);
}
return 0;
}
What am I doing wrong? Thanks in advance for your help.