Creating a pipe using parent and child processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating a pipe using parent and child processes
# 1  
Old 04-27-2010
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!



Code:
/* 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");
			}			
	}
	
	
		
}


Last edited by jre247; 04-28-2010 at 10:19 AM.. Reason: Please use code tags!
# 2  
Old 04-27-2010
Hello, jre247, and welcome to the forums.

Quote:
Originally Posted by jre247
Did I use execv() correctly?
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
# 3  
Old 04-27-2010
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!

Last edited by jre247; 04-28-2010 at 10:24 AM.. Reason: Please use code tags!
# 4  
Old 04-29-2010
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:

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

parent and child directory

does anyone know how to check in an 'if' statement if a particular directory is a child directory of a particular directory? help ~ (2 Replies)
Discussion started by: ymc1g11
2 Replies

2. Shell Programming and Scripting

Kill Parent/ Child processes

I am trying to kill PIDs that are tied to a KSH "load_sqlplus" and I am using the below code LIST_PID=`ps -ef | grep -i "load_sqlplus" | grep -v grep | awk '{print $2}'` if ; then echo "Processes killed" "PID : " $LIST_PID kill -9 $LIST_PID else echo "Nothing to Kill" fi... (4 Replies)
Discussion started by: venky338
4 Replies

3. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

4. Programming

fork(), parent and child processes???

Hi friends, I have a small question regarding unix system call fork, I hope you will solve my problem. Here is the small program $ cat fork1.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { int pid; int x = 0; x = x + 1; pid = fork(); if(pid < 0) {... (2 Replies)
Discussion started by: gabam
2 Replies

5. Homework & Coursework Questions

Need help with deleting childīs parent and child subprocess

1. The problem statement, all variables and given/known data: I need to make an program that in a loop creates one parent and five children with fork(). The problem i'm trying to solve is how to delete the parent and child of the childīs process. 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: WhiteFace
0 Replies

6. UNIX for Dummies Questions & Answers

Kill child processes, when parent is "bash"

Consider this simple command line bash -c 'echo $$ ; sleep 10000'This will print the newly created bash PID and sleep for a long time. If I go to another terminal and do something like ps -flax | grep leepI'll see something like 501 92418 91910 0 0:00.00 ttys000 0:00.00 bash -c echo $$... (5 Replies)
Discussion started by: teras
5 Replies

7. UNIX for Advanced & Expert Users

How to find all the child processes of a parent process

Hi I am trying to see if there are some options in ps command or if there is a shell script which basically shows you all the processes spawned by a parent process , then all the processes of its child processes and so on down the hierarchy may be like a tree structure. It might be a generic... (6 Replies)
Discussion started by: clifford
6 Replies

8. Shell Programming and Scripting

Parent/Child Processes

Hello. I have a global function name func1() that I am sourcing in from script A. I call the function from script B. Is there a way to find out which script called func1() dynamically so that the func1() can report it in the event there are errors? Thanks (2 Replies)
Discussion started by: yoi2hot4ya
2 Replies

9. UNIX for Dummies Questions & Answers

kill parent and child

Hello all, I have gone through the search and looked at posting about idle users and killing processes. Here is my question I would like to kill an idle user ( which I can do) but how can I asure that all of his process is also killed whit out tracing his inital start PID. I have tried this on a... (4 Replies)
Discussion started by: larry
4 Replies

10. UNIX for Dummies Questions & Answers

what are parent and child processes all about?

I don't follow what these are... this is what my text says... "When a process is started, a duplicate of that process is created. This new process is called the child and the process that created it is called the parent. The child process then replaces the copy for the code the parent... (1 Reply)
Discussion started by: xyyz
1 Replies
Login or Register to Ask a Question