Send a content to child from parent with using dup2

what I am trying to do is pass dynamically load a text file into buffer (in parent) and pass this data to another process (child). then child returns this data back to parent. I have done this much so far. by the way child and parent are in different such parent.cpp and child.cpp Any help please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 string fileName;// File will be opened
char* str=argv[1];
fileSetup(argv[1]);//check if file exists and load into buffer
int pipe_fds[2]; //Define and create pipes
pid_t pid;//child process ID
int read_fd; //pipe_fds[0]
int write_fd;//pipe_fds[1]
int p=pipe (pipe_fds);
read_fd = pipe_fds[0]; 
write_fd = pipe_fds[1]; 
pid = fork ();//Fork the child process. 
if(pid == -1)//Check if pipe successfully created
{
   cerr << "Failed to fork" << endl;
   exit(1);
}
if(pid == 0)//Child
{               
    // Child process closes up input side of pipe 
    dup2 (write_fd, STDIN_FILENO);
    execlp ("./child", "child",str, NULL);//Run child process       
    close(read_fd);         
}
else//Parent
{

}
Topic archived. No new replies allowed.