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
PIPE(2) 							System Calls Manual							   PIPE(2)

NAME
pipe - create an interprocess channel SYNOPSIS
#include <u.h> #include <libc.h> int pipe(int fd[2]) DESCRIPTION
Pipe creates a buffered channel for interprocess I/O communication. Two file descriptors are returned in fd. Data written to fd[1] is available for reading from fd[0] and data written to fd[0] is available for reading from fd[1]. After the pipe has been established, cooperating processes created by subsequent fork(2) calls may pass data through the pipe with read and write calls. The bytes placed on a pipe by one write are contiguous even if many processes are writing. Write boundaries are preserved: each read terminates when the read buffer is full or after reading the last byte of a write, whichever comes first. The number of bytes available to a read(2) is reported in the Length field returned by fstat or dirfstat on a pipe (see stat(2)). When all the data has been read from a pipe and the writer has closed the pipe or exited, read(2) will return 0 bytes. Writes to a pipe with no reader will generate a note sys: write on closed pipe. SOURCE
/sys/src/libc/9syscall SEE ALSO
intro(2), read(2), pipe(3) DIAGNOSTICS
Sets errstr. BUGS
If a read or a write of a pipe is interrupted, some unknown number of bytes may have been transferred. When a read from a pipe returns 0 bytes, it usually means end of file but is indistinguishable from reading the result of an explicit write of zero bytes. PIPE(2)
All times are GMT -4. The time now is 04:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy