Sponsored Content
Top Forums Shell Programming and Scripting Creating a pipe using parent and child processes Post 302416815 by jre247 on Tuesday 27th of April 2010 09:05:53 PM
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!
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
FIFO(7) 						     Linux Programmer's Manual							   FIFO(7)

NAME
fifo - first-in first-out special file, named pipe DESCRIPTION
A FIFO special file (a named pipe) is similar to a pipe, except that it is accessed as part of the file system. It can be opened by multi- ple processes for reading or writing. When processes are exchanging data via the FIFO, the kernel passes all data internally without writ- ing it to the file system. Thus, the FIFO special file has no contents on the file system; the file system entry merely serves as a refer- ence point so that processes can access the pipe using a name in the file system. The kernel maintains exactly one pipe object for each FIFO special file that is opened by at least one process. The FIFO must be opened on both ends (reading and writing) before data can be passed. Normally, opening the FIFO blocks until the other end is opened also. A process can open a FIFO in nonblocking mode. In this case, opening for read only will succeed even if no-one has opened on the write side yet, opening for write only will fail with ENXIO (no such device or address) unless the other end has already been opened. Under Linux, opening a FIFO for read and write will succeed both in blocking and nonblocking mode. POSIX leaves this behavior undefined. This can be used to open a FIFO for writing while there are no readers available. A process that uses both ends of the connection in order to communicate with itself should be very careful to avoid deadlocks. NOTES
When a process tries to write to a FIFO that is not opened for read on the other side, the process is sent a SIGPIPE signal. FIFO special files can be created by mkfifo(3), and are indicated by ls -l with the file type 'p'. SEE ALSO
mkfifo(1), open(2), pipe(2), sigaction(2), signal(2), socketpair(2), mkfifo(3), pipe(7) COLOPHON
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2008-12-03 FIFO(7)
All times are GMT -4. The time now is 03:02 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy