C++ stuck with dup2 and pipe


 
Thread Tools Search this Thread
Top Forums Programming C++ stuck with dup2 and pipe
# 1  
Old 03-15-2015
C++ stuck with dup2 and pipe

What this code should do is: there are parent.cpp and child.cpp. Parent will send whatever is in the buffer to child and child will send back whatever received to parent. I do not know what I am doing wrong. I am confused what is missing in the parent and what else I should include into the child. Thanks.

Code:
//parent.cpp
//Check for fork error
 if ( (pid = fork()) < 0 )
{
    cerr << "FORK ERROR" << endl;
    return -3;
}
else  if (pid == 0)  // Child 
{
    close(fd1[1]);//Close parent's stdout-write
    close(fd2[0]);//Close child's stdin-read
    if (fd1[0] != STDIN_FILENO)//Make sure file desc. matches
    {
        if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
        {
            cerr << "dup2 error to stdin" << endl;
        }
        close(fd1[0]);
    }
    if (fd2[1] != STDOUT_FILENO)//Make sure file desc. mathces
    {
        if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
        {
            cerr << "dup2 error to stdout" << endl;
        }
        close(fd2[1]);
    }
    if ( execl("./child", "child", (char *)0) < 0 )
    {
        cerr << "system error" << endl;
        return -4;
    }
    return 0;
}//end of child
else //parent
{
    int rv;
   close(fd1[0]);//Close parent's read
   close(fd2[1]);//close child's write
   if ( write(fd1[1], buffer, strlen(buffer)) != strlen(buffer))
    {
        cerr << "Write ERROR FROM PIPE" << endl;
    }
    if ( (rv = read(fd2[0], buffer, MAXLINE)) < 0 )
    {
        cerr << "READ ERROR FROM PIPE" << endl;
    }
    else if (rv == 0)
    {
        cerr << "Child Closed Pipe" << endl;
        return 0;
    }
    cout << "Output of child is: " << buffer;
    return 0;
}//end of parent

//child.cpp
char line[1000];
int MAXLEN=1001;    
read(STDIN_FILENO, line, MAXLEN);

# 2  
Old 03-15-2015
First, post your entire code. That's not complete enough for anyone to say what's going wrong.

Second, post what's going wrong.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Troubles with pipes, fork, and dup2

I want to execute metasploit by two pipes to communicate with it, but I have troubles with that communication. When I run my program, I get this error: "stty: standard input: Inappropriate ioctl for device" and I don't receive the metasploit promt. just select an exploit. This is my code:... (2 Replies)
Discussion started by: dano88
2 Replies

2. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

3. UNIX for Advanced & Expert Users

Dup2 - for file descriptor opened by a different process

is it possible to duplicate file descriptors(opened by a different process) with the help of dup or dup2. the two process do not share parent child relationship as well. (2 Replies)
Discussion started by: replytoshishir
2 Replies

4. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

5. Programming

Implementation of dup2

Hi all,I'm reading <Advanced programming in the UNIX environment>,that book asked the reader to implement a function which has same functions with dup2 without calling fcntl.Could anyone give me a tip?Any help will be appreciated.:) (8 Replies)
Discussion started by: homeboy
8 Replies

6. UNIX for Advanced & Expert Users

dup2 filedescriptor redirecting output

int redirect() { int fd,rc; fd = open("sample.DAT",O_CREAT | O_RDWR , 00777 ); rc = dup2(fd , 1 ) ; close (fd ); return 0; } I used the above to redirect all the cout statements to sample.DAT. process is redirecting the output and I had two questions 1. All stdout/cout statements... (2 Replies)
Discussion started by: satish@123
2 Replies

7. Programming

a issue with dup2

Hi, i have in one program such a pice of code ................ static int old_stderr_handle = -1; static int old_stdout_handle = -1; log_handle = open(log_file_name,O_CREAT|O_RDWR,932); old_stderr_handle = dup(STDERR_FILENO); if (dup2(log_handle,STDERR_FILENO) < 0) { //... (1 Reply)
Discussion started by: vovan
1 Replies

8. Shell Programming and Scripting

help! im stuck..

I want to search for the line with the group name and add the user into the group. The file format is the same as /etc/group The code i wrote is egrep "^$newGID" $group >/dev/null FS=":" oldData=awk -F: '{print $3}' newData= "$oldData,$newUser" sed -n $4/$newData $group but a friend... (1 Reply)
Discussion started by: cherrywinter
1 Replies

9. Programming

Understanding the purpose of dup/dup2

I'm having difficulty understanding the purposes of using dup/dup2 when involving forks. for example, if we call fork() once, that is, we are creating a child process. In what cases would we need to use dup or dup2 to duplicate the file descriptors for standard output and standard error? What... (1 Reply)
Discussion started by: Yifan_Guo
1 Replies

10. UNIX for Advanced & Expert Users

stuck....!

I have been busy reading away on devices and filesystems and I am stuck on a particular subject matter.. I'm not understanding the concept behind mknod mkfifo makedev or related commands.. can anyone shed some light on the subject.! any feedback welcome! moxxx68 (0 Replies)
Discussion started by: moxxx68
0 Replies
Login or Register to Ask a Question