Pipes connecting 3 processes in a "circle"


 
Thread Tools Search this Thread
Top Forums Programming Pipes connecting 3 processes in a "circle"
# 1  
Old 03-02-2011
Network Pipes connecting 3 processes in a "circle"

I am trying to get a better understanding of pipes and processes. I have code in which I link 3 processes A,B,C. I have A->B->C but how would I go about getting C->A?

Here is my code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main()
{
pid_t A, B, C;
int fd[2];
int fd2[2];

if (pipe(fd) == -1)
{
    perror("pipe");
    exit(1);
}
B = fork();
if (B == -1)
{
    perror("fork");
    exit(2);
}
if (B == 0)
{
    if (pipe(fd2) == -1)
    {
        perror("pipe");
        exit(11);
    }
    C=fork();
    if(C == -1)
    {
        perror("fork 2");
        exit(22);
    }
    if (C == 0)
    {
        //i am C
        //do some stuff
    }
    else
    {
        //i am B
        //do some stuff
    }
}
else
{
    //i am A
    //do some stuff
}

# 2  
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));
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
Login or Register to Ask a Question