Sponsored Content
Top Forums Programming two-way piping values between parent and child Post 302718679 by jakubs11 on Saturday 20th of October 2012 12:57:55 PM
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
 

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

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

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

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

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

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

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

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

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) 						     Linux Programmer's Manual							   PIPE(2)

NAME
pipe, pipe2 - create pipe SYNOPSIS
#include <unistd.h> int pipe(int pipefd[2]); #define _GNU_SOURCE #include <unistd.h> int pipe2(int pipefd[2], int flags); DESCRIPTION
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe. For fur- ther details, see pipe(7). If flags is 0, then pipe2() is the same as pipe(). The following values can be bitwise ORed in flags to obtain different behavior: O_NONBLOCK Set the O_NONBLOCK file status flag on the two new open file descriptions. Using this flag saves extra calls to fcntl(2) to achieve the same result. O_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the two new file descriptors. See the description of the same flag in open(2) for reasons why this may be useful. RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately. ERRORS
EFAULT pipefd is not valid. EINVAL (pipe2()) Invalid value in flags. EMFILE Too many file descriptors are in use by the process. ENFILE The system limit on the total number of open files has been reached. VERSIONS
pipe2() was added to Linux in version 2.6.27; glibc support is available starting with version 2.9. CONFORMING TO
pipe(): POSIX.1-2001. pipe2() is Linux-specific. EXAMPLE
The following program creates a pipe, and then fork(2)s to create a child process; the child inherits a duplicate set of file descriptors that refer to the same pipe. After the fork(2), each process closes the descriptors that it doesn't need for the pipe (see pipe(7)). The parent then writes the string contained in the program's command-line argument to the pipe, and the child reads this string a byte at a time from the pipe and echoes it on standard output. #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int pipefd[2]; pid_t cpid; char buf; if (argc != 2) { fprintf(stderr, "Usage: %s <string> ", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipefd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } cpid = fork(); if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (cpid == 0) { /* Child reads from pipe */ close(pipefd[1]); /* Close unused write end */ while (read(pipefd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, " ", 1); close(pipefd[0]); _exit(EXIT_SUCCESS); } else { /* Parent writes argv[1] to pipe */ close(pipefd[0]); /* Close unused read end */ write(pipefd[1], argv[1], strlen(argv[1])); close(pipefd[1]); /* Reader will see EOF */ wait(NULL); /* Wait for child */ exit(EXIT_SUCCESS); } } SEE ALSO
fork(2), read(2), socketpair(2), write(2), popen(3), pipe(7) COLOPHON
This page is part of release 3.25 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 2009-09-15 PIPE(2)
All times are GMT -4. The time now is 07:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy