Sponsored Content
Full Discussion: C++ stuck with dup2 and pipe
Top Forums Programming C++ stuck with dup2 and pipe Post 302938399 by ramono on Sunday 15th of March 2015 03:12:59 PM
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);

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Child(3pm)                                              User Contributed Perl Documentation                                             Child(3pm)

NAME
Child - Object oriented simple interface to fork() DESCRIPTION
Fork is too low level, and difficult to manage. Often people forget to exit at the end, reap their children, and check exit status. The problem is the low level functions provided to do these things. Throw in pipes for IPC and you just have a pile of things nobody wants to think about. Child is an Object Oriented interface to fork. It provides a clean way to start a child process, and manage it afterwords. It provides methods for running, waiting, killing, checking, and even communicating with a child process. NOTE: kill() is unpredictable on windows, strawberry perl sends the kill signal to the parent as well as the child. SYNOPSIS
BASIC use Child; my $child = Child->new(sub { my ( $parent ) = @_; .... # exit() is called for you at the end. }); my $proc = $child->start # Kill the child if it is not done $proc->complete || $proc->kill(9); $proc->wait; #blocking IPC # Build with IPC my $child2 = Child->new(sub { my $self = shift; $self->say("message1"); $self->say("message2"); my $reply = $self->read(1); }, pipe => 1 ); my $proc2 = $child2->start; # Read (blocking) my $message1 = $proc2->read(); my $message2 = $proc2->read(); $proc2->say("reply"); SHORTCUT Child can export the child() shortcut function when requested. This function creates and starts the child process in one action. use Child qw/child/; my $proc = child { my $parent = shift; ... }; You can also request IPC: use Child qw/child/; my $child = child { my $parent = shift; ... } pipe => 1; DETAILS
First you define a child, you do this by constructing a Child object. Defining a child does not start a new process, it is just the way to define what the new process will look like. Once you have defined the child you can start the process by calling $child->start(). One child object can start as many processes as you like. When you start a child an Child::Link::Proc object is returned. This object provides multiple useful methods for interacting with your process. Within the process itself an Child::Link::Parent is created and passed as the only parameter to the function used to define the child. The parent object is how the child interacts with its parent. PROCESS MANAGEMENT METHODS
@procs = Child->all_procs() Get a list of all the processes that have been started. This list is cleared in processes when they are started; that is a child will not list its siblings. @pids = Child->all_proc_pids() Get a list of all the pids of processes that have been started. Child->wait_all() Call wait() on all processes. EXPORTS
$proc = child( sub { ... } ) $proc = child { ... } $proc = child( sub { ... }, $plugin, @data ) $proc = child { ... } $plugin => @data Create and start a process in one action. CONSTRUCTOR
$child = Child->new( sub { ... } ) $child = Child->new( sub { ... }, $plugin, @plugin_data ) Create a new Child object. Does not start the child. OBJECT METHODS
$proc = $child->start() Start the child process. SEE ALSO
Child::Link::Proc The proc object that is returned by $child->start() Child::Link::Parent The parent object that is provided as the argumunt to the function used to define the child. Child::Link::IPC The base class for IPC plugin link objects. This provides the IPC methods. HISTORY
Most of this was part of Parrallel::Runner intended for use in the Fennec project. Fennec is being broken into multiple parts, this is one such part. FENNEC PROJECT
This module is part of the Fennec project. See Fennec for more details. Fennec is a project to develop an extendable and powerful testing framework. Together the tools that make up the Fennec framework provide a potent testing environment. The tools provided by Fennec are also useful on their own. Sometimes a tool created for Fennec is useful outside the greator framework. Such tools are turned into their own projects. This is one such project. Fennec - The core framework The primary Fennec project that ties them all together. AUTHORS
Chad Granum exodist7@gmail.com COPYRIGHT
Copyright (C) 2010 Chad Granum Child is free software; Standard perl licence. Child is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details. perl v5.10.1 2011-03-07 Child(3pm)
All times are GMT -4. The time now is 11:14 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy