I am using pipe in an IPC communication between a parent and child. Child using stdout to send messages. while parent is blocked at read function.
The problem is child doesn't send the message one by one. it send some of them together at the same time when it reaches some size (4096 charcter i think).
here is my read function:
void waitMessage(int id,char* buffer,int* size,struct Pipe * pipes){
printf("waiting %d\n",id);
char c = '\0';
int i = 0;
fprintf(stderr,"recieved: ");
fcntl( pipes[id].fd[0][0], F_SETFL, fcntl(pipes[id].fd[0][0], F_GETFL) | O_NONBLOCK);
int res = -1;
while(res == -1)
res = read(pipes[id].fd[0][0],&c,1);
while(res != -1){
buffer[i] = c;
printf("%c",buffer[i]);
i++;
res = read(pipes[id].fd[0][0],&c,1);
}
(*size) = i;
printf("\n");
}
- please ignore my non-blocking usage of the function