c - Can't read the value from pipe correctly -


i trying initiate simple pipe in c (using cygwin , dev-c++) pass values between parent , single child. here parent code (pipesnd.c):

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() {     int fifo[2];     char *msg = "this test message";     char str[10];        if (pipe(fifo) == -1)     {         printf("cannot create pipe\n");         exit(1);     }     write(fifo[1], msg, strlen(msg));      sprintf(str, "%d", fifo[0]);     printf("i parent , in pipe: %s \n", str);     fflush(stdout);         switch (fork())     {     case 0:         execl("c:/dev-cpp/lift 2/pipercv", "pipercv", str, null);           exit(1);     case -1:         perror("fork() failed:");         exit(2);     default:      }           exit(0); } 

and child code (pipercv.c):

#include <stdio.h>  #include <stdlib.h>  #include <string.h>  #define nbuf 100 int main(int argc, char *argv[]) {     int fd;     char buf[nbuf];     if (argc != 2)     {         printf("expect pipercv fd\n");         exit(1);     }     fd = atoi(argv[1]);       read(fd, buf, 20);     buf[20] = '\0';     printf("i child , in pipe: %s \n", buf);     fflush(stdout);     sleep(10);  } 

result:

enter image description here

how can pass/see entire message in both child , parent (bidirectional)?

the issue read 20 characters buf, add \0 20th character , end output. message this test message contains more characters.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -