two-way piping values between parent and child


 
Thread Tools Search this Thread
Top Forums Programming two-way piping values between parent and child
# 1  
Old 10-20-2012
two-way piping values between parent and child

Hello everyone.
I'm pretty new to the topic of fork(), pipe() etc. All day I've been trying to make my code execute but it doesn't seem to be working the way I understand it. Smilie

Anyway, my task is:
Create a child process that will recive from its parent two intiger values. Than it will multiply them and send back the result to the parent.

===EDIT===
all's working just fine... except the final piping the result form child to parent...
Code:
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <wait.h>
using namespace std;

#define READ 0
#define WRITE 1

int main(){
	int n1, n2, res, buf1, buf2, p1[2], p2[2], p3[2];
	pid_t pp;
	pipe(p1);
	pipe(p2);
	pipe(p3);

	pp=fork();

	if (pp==-1){ //error
	perror("fork");
	cout << "error n = " << errno << endl;
	return 0;}

	if (pp!=0){ //father
		cout << endl << "FATHER(" << (int)getpid() << "): type a number: ";
		cin >> n1;
		cout << endl << "FATHER(" << (int)getpid() << "): type a number: ";
		cin >> n2;

		cout << endl << "FATHER(" << (int)getpid() << "): I will send those numbers to my child process to multiply them." << endl;

		close(p1[READ]);
		write(p1[WRITE],&n1,sizeof(n1));
		close(p1[WRITE]);

		close(p2[READ]);
		write(p2[WRITE],&n2,sizeof(n2));
		close(p2[WRITE]);
	}

	if(pp==0){ //child
		close(p1[WRITE]);
		read(p1[READ],&buf1,sizeof(buf1));
		close(p1[READ]);

		close(p2[WRITE]);
		read(p2[READ],&buf2,sizeof(buf2));
		close(p2[READ]);

		cout << endl << "CHILD(" << (int)getpid() << "): Yay, father process sent me two numbers: " << buf1 <<" and " << buf2 << "." << endl;

		buf1=buf1*buf2;

		cout << endl << "CHILD(" << (int)getpid() << "): I will multiply them and send back: " << buf1 << "." << endl;

		close(p3[READ]);
		write(p3[WRITE],&buf1,sizeof(buf1));
		close(p3[WRITE]);
		exit(0);
	}
	close(p2[WRITE]);
	read(p2[READ],&res,sizeof(res));
	close(p2[READ]);
	wait(&pp);
	cout << endl << "FATHER(" << (int)getpid() << "): Child process sent me back the result: " << res << "." << endl;

	return 0;
}


Last edited by jakubs11; 10-20-2012 at 06:44 PM.. Reason: almost SOLVED
# 2  
Old 10-20-2012
Hi Jakubs11. I'm probably not the one to help you because I'm having trouble with piping in a program of mine too. I'm new to the concept just like you. Anyhow, just looking at your code both conditional branches with the if statements end in a call to exit. If its the parent it terminates at the end of the if block of code, and same goes with the child. How is your program ever going to reach the part where you print the result of the values?
# 3  
Old 10-20-2012
Well, good point, I've changed my code a little a while ago. Tried putting the final
Code:
	close(p2[WRITE]);
	read(p2[READ],&result,sizeof(result));
	close(p2[READ]);
	cout << endl << "Child process send me back the result: " << result << "." << endl;

inside father's "if" but it doesn't look like changing anything...

Please, have a look now, It's working almost like I want it. The thing I'm missing is piping back the result from child to parent.
# 4  
Old 10-20-2012
Child is writing result on p3, but parent is reading, the already closed, p2.
This User Gave Thanks to agama For This Post:
# 5  
Old 10-20-2012
no comment... Thank you my friend.
# 6  
Old 10-20-2012
Alright, well, it looks like the result of the multiplication is being written to pipe p3. Shouldn't you be getting the result from the read end of p3 and not p2?

---------- Post updated at 05:09 PM ---------- Previous update was at 05:07 PM ----------

Oh, looks like someone beat me to the punch. Nevermind...I guess you've got it solved then Jakubs11.
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

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

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

4. Programming

To share fd between parent and child

i used function fork(). so i made two process. parent process accepted socket fd and writing to shared memory. then now. how can child process share parent's socket fd? is this possible? Thanks in advance (1 Reply)
Discussion started by: andrew.paul
1 Replies

5. UNIX for Advanced & Expert Users

Child Killing Parent

Hi all, I am writing a script which calls other third party scripts that perform numerous actions. I have no control over these scripts. My problem is, one of these scripts seems to execute and do what it is meant to do, but my calling / parent script always exits at that point. I need to... (4 Replies)
Discussion started by: mark007
4 Replies

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

7. Shell Programming and Scripting

Returning values from child to parent shell

I need to send the status from child shell failure to parent shell. I would like to know how could we accomplish this. My parent.sh is as below: #!/bin/ksh set -x echo "I am in parent shell now..." child.sh ret_stat=$? echo "rest_stat=$ret_stat" echo "I am below parent shell end..." ... (4 Replies)
Discussion started by: acheepi
4 Replies

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

9. Filesystems, Disks and Memory

How hard can it be? ps child/parent

:( Since I'm fairly new to the scene and don't have much experience in shell programming, I decided to check out the net for a useful script or two. What I'm looking for is a script that would let me enter a PID and then show the process tree associated with it. So it would display the (grand-)... (2 Replies)
Discussion started by: velde046
2 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