Part of my bigger project is using pipes to communicate with 2 processes.
P1 process generates numbers, converts them to hex and next sends them through pipe to P2 process.
P2 prints the numbers.
pipe1.c
#define WRITE 1
#define READ 0
int main() {
int i;
char hex[30];
int my_pipe[2];
pipe(my_pipe);
int var;
close(my_pipe[READ]);
for(i=50;i<100;i++){
printf("P1 generated: %d\n",i);
sprintf(hex, "%x", i);
write(my_pipe[WRITE], &hex, sizeof(hex));
printf("P1 in hex %s\n",hex);
sleep(3);
}
return 0;
}
pipe2.c
#define WRITE 1
#define READ 0
int main() {
char hex[30];
int my_pipe2[2];
pipe(my_pipe2);
int var;
close(my_pipe2[WRITE]);
for(;;){
read(my_pipe2[READ], &hex, sizeof(hex));
printf("P2 received: %s",hex);
sleep(3);
}
return 0;
}
All processes are just started by my main project file:
int main(int argc, char *argv[]) {
//.....
if (fork() == 0) {
execlp("./pipe1", "./pipe1", NULL);
}
sleep(1);
if (fork() == 0) {
execlp("./pipe2", "./pipe2", NULL);
}
sleep(1);
//....
}
Can someone can help me and tell me where I make an error?
I tried many things but without result.
I just got one print:
P1 generated