Sponsored Content
Top Forums Programming Pipes connecting 3 processes in a "circle" Post 302501049 by Corona688 on Wednesday 2nd of March 2011 10:30:23 AM
Old 03-02-2011
Create absolutely everything you need in advance. Your child processes can pick what they need from it and close the rest.

Code:
int ab[2], bc[2], ca[2];
pipe(ab); pipe(bc); pipe(ca);

Each time you fork(), duplicate the appropriate ends of the pipe over stdin/stdout/etc and close all other pipes and copies.

You don't need to put if's inside if's inside if's to fork more than once. Just exit() at the bottom of the child code so it doesn't go places you don't want it to.

Code:
pid_t pid[3];
int n;

pid[0]=fork();
if(pid[0] == 0)
{
        // Process A reads from process C
        dup2(ca[0], STDIN_FILENO);
        // Process A writes to process B
        dup2(ab[1], STDOUT_FILENO);
        // Essential: close all other pipes and copies of pipes
        for(n=0; n<2; n++)
        { close(ab[n]); close(bc[n]); close(ca[n]); }

        // do whatever you want
        ...
        // quit so your child doesn't end up in the main program
        exit(0);
}

pid[1]=fork();
if(pid[1] == 0)
{
        // Process B reads from process A
        dup2(ab[0], STDIN_FILENO);
        // Process B writes to process C
        dup2(bc[1], STDOUT_FILENO);
        // Essential: close all other pipes and copies of pipes
        for(n=0; n<2; n++)
        { close(ab[n]); close(bc[n]); close(ca[n]); }

        // do whatever you want
        ...
        // quit so your child doesn't end up in the main program
        exit(0);
}

pid[2]=fork();
if(pid[2] == 0)
{
        // Process C reads from process B
        dup2(bc[0], STDIN_FILENO);
        // Process C writes to process A
        dup2(ca[1], STDOUT_FILENO);
        // Essential: close all other pipes and copies of pipes
        for(n=0; n<2; n++)
        { close(ab[n]); close(bc[n]); close(ca[n]); }

        // do whatever you want
        ...
        // quit so your child doesn't end up in the main program
        exit(0);
}

// Only the parent will be running outside of those if statements.
// Essential: close all other pipes and copies of pipes
for(n=0; n<2; n++)
{ close(ab[n]); close(bc[n]); close(ca[n]); }

for(n=0; n<3; n++)
{
        int status;
        waitpid(pid[n], &status, 0);
        printf("Child %d exited with status %d\n", n, WEXITSTATUS(status));
}

 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. UNIX for Advanced & Expert Users

problem using pipes with "ls"

Hi all, I tried the following command $ find / -name xyx | ls -l so logically it should show the listing of directory xyz , assuming there's only one instance of xyz . But the above command shows the listing of current directory instead. I got the desired result using it in the... (4 Replies)
Discussion started by: bijeet_sunny
4 Replies

3. UNIX for Dummies Questions & Answers

Do pipes know when they have to "wait" for all the data?

Hi, I was wondering if pipes ("|"), or rather the command that follow them, know when they're supposed to wait for all the data? For instance, if you take this: cat my_file | sort | uniq for uniq to work well, it needs to have rows sorted, but for lines to be sorted properly, it needs... (5 Replies)
Discussion started by: a.brassac
5 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

need to kill a number of processes with name "XYZ" at a time using shell script

Hi, when i grep for the process "XYZ" , there will be some good number of processes with that name, i want to kill all the these processes at a time using shell script? Any help needed for this action. Thanks Regards, Anil (6 Replies)
Discussion started by: anilmanepu
6 Replies

6. UNIX for Advanced & Expert Users

"Network error: No route to host" While connecting guest RHEL4 using putty

Hi, I have installed RHEL4 using vmware workstation.. Host OS: Windows XP Guest OS: RHEL4 Pls refer step 17 & 18 in below link... ORACLE-BASE - Red Hat Enterprise Linux 4 and Centos 4 Installation 1) If i choose to assign IP automatically (using DHCP) means, i am able to connect RHEL4... (3 Replies)
Discussion started by: thomasraj87
3 Replies

7. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

8. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

9. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies
PIPE(2) 							System Calls Manual							   PIPE(2)

NAME
pipe - create an interprocess channel SYNOPSIS
pipe(fildes) int fildes[2]; DESCRIPTION
The pipe system call creates an I/O mechanism called a pipe. The file descriptors returned can be used in read and write operations. When the pipe is written using the descriptor fildes[1] up to 4096 bytes of data are buffered before the writing process is suspended. A read using the descriptor fildes[0] will pick up the data. Writes with a count of 4096 bytes or less are atomic; no other process can inter- sperse data. It is assumed that after the pipe has been set up, two (or more) cooperating processes (created by subsequent fork calls) will pass data through the pipe with read and write calls. The Shell has a syntax to set up a linear array of processes connected by pipes. Read calls on an empty pipe (no buffered data) with only one end (all write file descriptors closed) returns an end-of-file. SEE ALSO
sh(1), read(2), write(2), fork(2) DIAGNOSTICS
The function value zero is returned if the pipe was created; -1 if too many files are already open. A signal is generated if a write on a pipe with only one end is attempted. BUGS
Should more than 4096 bytes be necessary in any pipe among a loop of processes, deadlock will occur. ASSEMBLER
(pipe = 42.) sys pipe (read file descriptor in r0) (write file descriptor in r1) PIPE(2)
All times are GMT -4. The time now is 04:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy