Shashish Chandra
Shashish Chandra

Reputation: 499

Pipe is not working correctly in the parent-child process

As I have just started with these concepts I might be missing out a few elementary things. I was trying to link the parent and the child processes (created by fork() function) using pipe. In the parent process, I wanted to write in the pipe descriptor (af[1]) and after closing up the write end, I wanted to read from the read end of the pipe with descriptor (af[0]) in the child process.

Here is my code:

#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <cstdlib>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
using namespace std;

int main()
{
    pid_t pid1;
    pid1 = fork();

    int af[2],nbytes,wbytes;
    pipe(af);
    char inside[20];

    if(pid1 == -1)
    {
        cout << "No child process formed: " << getpid() <<endl;
        exit(1);
    }
    else if(pid1 == 0)
    {   cout<< "inchild" <<endl;
        close(af[1]);
        nbytes = read(af[0],inside,strlen(inside));
            cout << "read bytes: "<< nbytes << endl;
        cout << "child(read) inside descriptor: " << inside << endl;
        close(af[0]);
        cout << "in child's end" << endl;
        exit(0);
    }   
    else
    {   cout<< "inparent" << endl;
        close(af[0]);
        wbytes = write(af[1],"Hello World",12);
        cout<< "wrote bytes: " << wbytes<<endl;
        cout << "Parent(write) inside string: " << af[1] << endl;
        close(af[1]);
        cout << "in parent's end" << endl;
        exit(0);
    }

    return 0;

}

Then I was expecting this to run as follows:

But what I was getting here is this result:

inparent
shashish-vm@shashishvm-VirtualBox:~/Desktop$ inchild
read bytes: 0
child(read) inside descriptor: 
                               A��M�N��sf�
in child's end

And it was still not terminating.

I was using Ubuntu 14.04 LTS on Oracle VM VirtualBox (32-bit O.S.). And I have no idea why it was doing like this. I knew it is the job of the scheduler to switch the processes but still pipe functionality of IPC was not working there. The write process occurred even if I removed close(af[0]) statement but still the reading was not happening properly.

Upvotes: 0

Views: 2081

Answers (1)

user1937198
user1937198

Reputation: 5348

You problem is that you open the pipe after calling fork. This means the parent and child have different pipes. You can fix it by moving the call to pipe before the fork to create a single linked pipe.

Upvotes: 2

Related Questions