Search code examples
cforkpipe

fork() and pipe()


I need help with this sample application. When I run it, it gets stuck after the child process prints "Child sending!".

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>

#define INPUT 0
#define OUTPUT 1
int main()
{
    int fd1[2];
    int fd2[2];
    int pid;
    if (pipe(fd1) < 0)
        exit(1);
    if (pipe(fd2) < 0)
        exit(1);
    if ((pid = fork()) < 0)
    {
        perror("fork");
        exit(1);
    }
    else if (pid == 0)
    {
        close(fd1[INPUT]);
        close(fd2[OUTPUT]);
        char *str = "Hello World!";
        printf("Child sending!\n");
        write(fd1[OUTPUT], str, strlen(str));
        char *bufferc = (char *)malloc(1000);
        char *readbufferc = (char *)malloc(80);
        int rdc;
        int gotdata = 0;
        while (gotdata == 0)
             while ((rdc = read(fd2[INPUT], readbufferc, sizeof(readbufferc))) > 0)
             {
               strncat(bufferc,readbufferc,rdc);
               gotdata = 1;
             }
        printf("Child received: %s",bufferc);
        free(readbufferc);
        free(bufferc);
        exit(0);
    }
    else
    {
        close(fd1[OUTPUT]);
        close(fd2[INPUT]);
        int rd;
        char *buffer = (char *)malloc(1000);
        char *readbuffer = (char *)malloc(80);
        int gd = 0;
        while (gd == 0)
             while ((rd = read(fd1[INPUT],readbuffer, sizeof(readbuffer))) > 0)
             {
               strncat(buffer, readbuffer,rd);
               gd = 1;
             }
        printf("Parent received: %s\n",buffer);
        free(readbuffer);
        printf("Parent sending!");
        write(fd2[OUTPUT], buffer, strlen(buffer));
        free(buffer);
    }
    return 0;
}

On a side note, is there a way to debug when I use fork, because GDB automatically goes to the parent process?


Solution

  • After the child writes to the parent, it must close the write end of the pipe so the parent knows it has reached EOF.