Sponsored Content
Full Discussion: hello friends
Top Forums UNIX for Advanced & Expert Users hello friends Post 88311 by flyingpenguin on Wednesday 2nd of November 2005 09:49:40 PM
Old 11-02-2005
Okay, visit:

http://www.ecst.csuchico.edu/~beej/guide/ipc/

This is a comprehensive guide to interprocess communication.


Today's lesson is on piping. You have process A, and process B, created by using the fork() command. You want the output of process A to be the input to process B. This is called piping. The following code excerpt demonstrates Inter Process Communication between two child processes, Process A, and Process B. Process A and Process B are running shell commands.

Code:
	int pipeID[2]; /* Pipes Have two values, one for the process A */
                                /* And one for process B. Think of each value */
                                /* as an end to the pipe */
/* pipeid[0] is process A, pipeid[1] is process B */
	pipe(pipeID); /* Create Pipe. */
	
    /* Fork current process to create process A and process B. */
/*Both child processes. Process A is created by the first fork(), */
/*process B by the second fork().  */

// Process A code
	if (fork()==0){ /* If the new process A is a child */
	    // child
		close(pipeID[0]);         //write stdout
		dup2(pipeID[1],1);       // to pipe using dup
		fillArgs(sPipe[0],&args); /* Create agrc and argv */
		execvp(args.argv[0],args.argv); // Execute
		close(pipeID[1]);// stdout goes to pipe.
	}
// Process B code
	if (fork()==0){ /* If the new process is a child */
		close(pipeID[1]);		//read from pipe 1 stdout
		dup2(pipeID[0],0);		//as stdin.
		fillArgs(sPipe[1],&args2);   /* Create argc and argv */
		execvp(args2.argv[0],args2.argv); // Execute 
		close(pipeID[0]);// process B reads stdin from pipe
	}
	close(pipeID[1]); /* Close pipes else, we don't want to have stdin, and stdout */	
	close(pipeID[0]); /* permanently redirected. */

I know my example isn't easy. At least I gave it a try Smilie

Last edited by flyingpenguin; 11-02-2005 at 10:51 PM.. Reason: Indenting Code
 

5 More Discussions You Might Find Interesting

1. IP Networking

hi friends

i have been in trouble please help me out i have developed a message queue.it is a simple message queue program after running it give error like NO SPACE LEFT ON DEVICE what is this error how could i solve this problem i am working on solaris9.2 (2 Replies)
Discussion started by: ramneek
2 Replies

2. Shell Programming and Scripting

hi friends....

hi friend i am facing problem in taking input from a file to the variable .. read fname if then cd $fname pwd ls > new_temp1 cat new_temp1 fi terminal=`tty` exec < $new_temp1 while read line do echo $line done exec < $terminal (2 Replies)
Discussion started by: newson
2 Replies

3. Shell Programming and Scripting

hi friends..........

hello friends ........ i want to visit the directory recusvely through and shell script . and want to get the output of " stat " command for each file .i tried by this way but unable to visit the each file . #!/bin/bash echo "enter the file name" read file fun() ... (3 Replies)
Discussion started by: newson
3 Replies

4. What is on Your Mind?

Time to have FUN my Unix/Linux friends...(One liners)...MUST read.. !!

As a mind refresher, I was thinking to start a new thread for ONE LINERS....funny/weird or any technical one liners.... Let me start first...... ================================= #!/bin/ssh #The Unix Guru's View of Sex unzip ; strip ; touch ; grep ; finger ; mount ; fsck ; more ; yes ;... (3 Replies)
Discussion started by: Rahulpict
3 Replies

5. What is on Your Mind?

You Want to Spend Time with Friends and Family a UNIX.com Cartoon Explainer

Well, this was kinda fun and different: You Want to Spend Time with Friends and Family a UNIX.com Cartoon Explainer https://youtu.be/6jPDpuxI2OA You want to spend time with friends and family. But you are stuck on some problem... Your Linux or Unix code is buggy and you don't know... (1 Reply)
Discussion started by: Neo
1 Replies
FORK(2) 							System Calls Manual							   FORK(2)

NAME
fork - create a new process SYNOPSIS
pid = fork() int pid; DESCRIPTION
Fork causes creation of a new process. The new process (child process) is an exact copy of the calling process except for the following: The child process has a unique process ID. The child process has a different parent process ID (i.e., the process ID of the parent process). The child process has its own copy of the parent's descriptors. These descriptors reference the same underlying objects, so that, for instance, file pointers in file objects are shared between the child and the parent, so that an lseek(2) on a descriptor in the child process can affect a subsequent read or write by the parent. This descriptor copying is also used by the shell to establish standard input and output for newly created processes as well as to set up pipes. The child processes resource utilizations are set to 0; see setrlimit(2). RETURN VALUE
Upon successful completion, fork returns a value of 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, a value of -1 is returned to the parent process, no child process is created, and the global variable errno is set to indicate the error. ERRORS
Fork will fail and no child process will be created if one or more of the following are true: [EAGAIN] The system-imposed limit on the total number of processes under execution would be exceeded. This limit is configuration- dependent. [EAGAIN] The system-imposed limit MAXUPRC (<sys/param.h>) on the total number of processes under execution by a single user would be exceeded. [ENOMEM] There is insufficient swap space for the new process. SEE ALSO
execve(2), wait(2) 3rd Berkeley Distribution May 22, 1986 FORK(2)
All times are GMT -4. The time now is 06:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy