Creating a pipe using parent and child processes

Hello,

I am trying to create a pipe that will direct stdout to in side of the pipe, and stdin to the out side of the pipe - I created two child processes to handle this. However, my pipe doesn't seem to be working correctly. Did I use execv() correctly? Command1[] and command2[] represent the two commands in the pipe statement that is written in a cissh shell (e.g. "ls -C | sort -r). So I want cisshPipe.c to direct stdout to the in side of the pipe and direct stdin to the out side of the pipe so that the command "ls -C | sort -r" would print a sorted list in reverse order of the files in the cissh directory. Below is my code for the pipe function. I'd greatly appreciate help from someone.Thanks!

/* cisshPipe.c
 */

/* External functions --
 */
extern void error(char* message);
//extern wait(int*status);



/* cisshPipe(char* command1[], char* command2[])
 * handles command lines with pipes.
 */

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

void
cisshPipe(char* command1[], char* command2[])
{
	pid_t pid;
	pid_t pid1;
	int status;
	int	commpipe[2];	/* This holds the input and output of the pipe */
	
	if(pipe(commpipe)){	/* Setup communication pipeline */ 
			fprintf(stderr,"Pipe error\n");
			exit(1);
	}
	
	if ( (pid = fork()) == -1 ){
		fprintf(stderr,"Fork error. Exiting.\n");	/* something went wrong with forking */
        }

		
	

	if (pid ==0) {
		// first child process
	
		

		close(commpipe[1]);
		dup2(commpipe[0],1);	/* Replace in side of pipe with stdout */
		close(commpipe[0]);
		execv(command1[0], command1);
		
		

		if ( (pid1 = fork()) == -1 ){
			fprintf(stderr,"Fork error. Exiting.\n");	/* something went wrong with forking */
		}

		if(pid1 == 0){
			/* second child process */

			close(commpipe[0]);
			dup2(commpipe[1],0);	/* Replace out side of pipe with stdin */
			close(commpipe[1]);
			execv(command2[0], command2);
					
		}
		
		else {
			
			close(commpipe[1]);
			close(commpipe[0]);
			

			if(wait(&status) < 0)
			{
	 			 error("cissh: error waiting for child.");
	 			 perror("wait");
			}	
			
		}	
	}


	else {

		


		close(commpipe[1]);
		close(commpipe[0]);
		

		if(wait(&status) < 0)
			{
	 			 error("cissh: error waiting for child.");
	 			 perror("wait");
			}			
	}
	
	
		
}

Hello, jre247, and welcome to the forums.

Reading unindented code is a pain; please edit your post so that it uses code tags (to preserve indentation and other formating).

I only took a cursory look, but I can tell you that it's impossible to say without knowing what's in command1 and command2. At the very least, provide that information, the behavior you expect, and the behavior that results. But, by far, it would be best to provide the source to a compilable test program, and not just fragments.

Regads,
Alister

I've edited the code in my original post above in order to retain the proper spacing using tags. Is there any more information that people need in order to assist me with fixing my pipe? Alister, do you want me to paste the code from the other functions of my cissh program into a post here, or do you prefer if maybe I paste the code of my other functions into a word document and then attach that document to this thread, or is there another more convenient and efficient way to show you the code of the other pieces of my program? Thanks for your help so far!

You are trying to fork your second child process from the first child process, not from the original parent. The main problem is that execv() never returns and so the second fork does not execute. Even if it did, the first child has already closed the side of the pipe that the second child wants to use.

You need to make the structure of you program something like this:

if ((pid0 = fork()) < 0) {
    error
}
if (pid0 == 0) {
    first child
} /* close brace here! */
if ((pid1 = fork()) < 0) {
    error
}
if (pid1 == 0) {
    second child
}
wait for children