Thread: help with pipeing stdin and stdout from child procces

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    111

    help with pipeing stdin and stdout from child procces

    Howdy,...

    I trying to create a pipe between two applications (gui and console) for that i need to understand the pipeing world ...

    A console application uses stdin and stdout and i wish to pipe it to the gui application so stdin of console application will be stdout of gui and stdout of console will be stdin of gui application.

    i read the man file of dup2() , popen() , pipe() and abit stuck.

    i wish to achieve the next :
    parent send command to child.
    child do some thing and answer to parent
    parent print the answer.

    Using a tutorial :

    Code:
    #include <sys/types.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
    	pid_t pid;
    	int rv;
    	int	commpipe[2];		/* This holds the fd for the input & output of the pipe */
    	char str[] = "Whats up dock?";
    	/* Setup communication pipeline first */
    	if(pipe(commpipe)){
    		fprintf(stderr,"Pipe error!\n");
    		exit(1);
    	}
    
    	/* Attempt to fork and check for errors */
    	if( (pid=fork()) == -1){
    		fprintf(stderr,"Fork error. Exiting.\n");  /* something went wrong */
    		exit(1);        
    	}
    
    	if(pid){
    		/* A positive (non-negative) PID indicates the parent process */
    		dup2(commpipe[1],1);	/* Replace stdout with out side of the pipe */
    		close(commpipe[0]);		/* Close unused side of pipe (in side) */
    		setvbuf(stdout,(char*)NULL,_IONBF,0);	/* Set non-buffered output on stdout */
    		scanf("&#37;s",str);
    		printf("%s\n",str);
    		fprintf(stderr,"Gui  sent %s \n",str); //Just to be sure.
    		wait(&rv);				/* Wait for child process to end */
    		fprintf(stderr,"Child exited with a %d value\n",rv);
    	}
    	else{
    		/* A zero PID indicates that this is the child process */
    		dup2(commpipe[0],0);	/* Replace stdin with the in side of the pipe */
    		close(commpipe[1]);		/* Close unused side of pipe (out side) */
    		/* Replace the child fork with a new process */
    		if(execl("child","child",NULL) == -1){
    			fprintf(stderr,"execl Error!");
    			exit(1);
    		}
    	}
    	return 0;
    }

    Code:
    /**
     *  The idea is to alter stdin and stdout  :
     *
     *  this part should use parent procces (parent.c) as its keyboard and screen
     * i wish to run this application as a demon.
     */
    #include <stdio.h>
    
    int main(){
    	char string[100];
    	
    	printf("External Process\n");
    	do{
    		printf("Enter Command: ");
    		fflush(stdout);				/* Must flush to see command prompt */
    		fgets(string,100,stdin);
    		printf("(child got) %s\n",string);		/* No flush necessary because new line flushes */
    	}while(!strstr(string,"exit"));
    	
    	return 0;
    }
    Last edited by jabka; 07-12-2008 at 10:49 AM.
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Would it be too much to ask you to describe what is (not?) happening that is different from what you expect. I only quickly scanned to code, and I didn't see anythign dramatically wrong, so I presume there's something that isn't right, but that I don't understand.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    111
    i made a cerius mystake to handle both input and output i need two pipes not one (as i did).
    So to fix this code i need to have two arrays of filediscriptors.

    on child part need to do :
    close(1); /* Close current stdout. */
    dup( cp[1]); /* Make stdout go to write
    end of pipe. */
    close(0); /* Close current stdin. */
    dup( pc[0]); /* Make stdin come from read
    end of pipe. */
    close( pc[1]);
    close( cp[0]);
    execl("child","child",NULL);
    on parent:

    /* Parent. */
    /* Close what we don't need. */
    write to client
    close(pc[1]);

    close(cp[1]);
    read from client...
    cp - child to perant
    pc - perant to child
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirecting STDOUT to STDIN of a child process
    By 0xception in forum Linux Programming
    Replies: 4
    Last Post: 09-13-2005, 11:58 PM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  3. stdin --> random interference --> stdout
    By j0hnb1ank in forum C Programming
    Replies: 3
    Last Post: 01-22-2003, 11:43 AM
  4. STDIN stdout
    By GreyMattr in forum Linux Programming
    Replies: 2
    Last Post: 08-01-2002, 01:29 PM
  5. What data type are stdout and stdin?
    By KwikDrawMcGraw in forum C Programming
    Replies: 1
    Last Post: 07-16-2002, 01:34 PM