How to properly connect these three processes using pipes?


 
Thread Tools Search this Thread
Top Forums Programming How to properly connect these three processes using pipes?
# 8  
Old 02-27-2012
If they don't want you to fork inside the scanner program, I don't understand why forking outside the scanner program would be okay, but whatever...

Code:
int closen(int *fd, int len)
{
        while((--len) >= 0) close(fd[len]);
}

int main(void)
{
        pid_t proca, procb, scanner;
        int pipes[8];
        int from_proca, to_proca, from_procb, to_procb;
        pipe(pipes); pipe(pipes+2); pipe(pipes+4); pipe(pipes+6);

        proca=fork();
        if(proca == 0)
        {
                 dup2(pipes[(0*2)+0], STDIN_FILENO);
                 dup2(pipes[(1*2)+1], STDOUT_FILENO);

                 closeall(pipes, 8);
                 exec(...);
                 exit(1);
        }

        procb=fork();
        if(procb == 0)
        {
                 dup2(pipes[(2*2)+0], STDIN_FILENO);
                 dup2(pipes[(3*2)+1], STDOUT_FILENO);
                 closeall(pipes, 8);
                 exec(...);
                 exit(1);
        }

        from_proca=pipes[(1*2)+0;
        to_proca=pipes[(0*2)+1];
        from_procb=pipes[(3*2)+0;
        to_procb=pipes[(2*2)+1];

        scanner=fork();
        if(scanner == 0)
        {
                dup2(pipes[(1*2)+0], 10); // From proca
                dup2(pipes[(0*2)+1], 11); // To proca
                dup2(pipes[(3*2)+0], 12); // From procb
                dup2(pipes[(2*2)+1], 13); // To procb
                closen(pipes, 8);
                // You will have to tell the scanner process which FD's to use somehow
                exec(...);
                exit(1);
        }

        closen(pipes, 8);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Connect direct - SFTP - List of servers that I can connect

Greetings Experts, I am working for a bank client and have a question on connect-direct and SFTP. We are using Linux RedHat servers. We use connect-direct to transfer (NDM) files from one server to another server. At times, we manually transfer the files using SFTP from one server to another... (2 Replies)
Discussion started by: chill3chee
2 Replies

2. Cybersecurity

When i start CSF i cant connect VPS or download any data into it It appears i cant connect Linux VP?

It appears i cant connect linux VPS server via SSH or i cant SCP any file to it and i cant wget any file TO it (from inside it) while CSF (Config Server Firewall, LFD is running. Just after isntall in default configuration and after changing TESTING mode to LIVE mode. Trying to wget & install... (1 Reply)
Discussion started by: postcd
1 Replies

3. AIX

AIX Remote Connect Fail With “No more multiple IP addresses to connect” Error

We have a production server at a client site running AIX. And recently when users are trying to connect to it via telnet, it prompts "No more multiple IP addresses to connect". Can I know what does this error mean? and how to rectify this? Thanks. (2 Replies)
Discussion started by: a_sim
2 Replies

4. Programming

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: #include <stdio.h> #include <stdlib.h> #include <unistd.h> main() { pid_t A, B, C; int fd; int fd2; ... (1 Reply)
Discussion started by: tfarmer4
1 Replies

5. Programming

IPC between processes, pipes, etc

I need help with understanding this in C-programming style(the systemcalls only): Three processes communicates via two pipes. (when the processes creates all stdin is the keyboard and all stdout is the screen) This is how the communication goes: Process 2 stdin (keyboard) and stdout goes via... (3 Replies)
Discussion started by: oskis
3 Replies

6. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

7. Shell Programming and Scripting

Multiple SQLPLUS background processes not working properly

Hi All, I am running 25 background process from a Unix shell script which calls a single Oracle procedure with different paramenters each time. These 25 process creates 25 different files. When i run these 25 Background SQLPLUS processes, few files are not created completly but if i run 25... (1 Reply)
Discussion started by: rawat_me01
1 Replies

8. Solaris

Identifying and grouping OS processes and APP processes

Hi Is there an easy way to identify and group currently running processes into OS processes and APP processes. Not all applications are installed as packages. Any free tools or scripts to do this? Many thanks. (2 Replies)
Discussion started by: wilsonee
2 Replies

9. UNIX for Advanced & Expert Users

Monitoring Processes - Killing hung processes

Is there a way to monitor certain processes and if they hang too long to kill them, but certain scripts which are expected to take a long time to let them go? Thank you Richard (4 Replies)
Discussion started by: ukndoit
4 Replies

10. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies
Login or Register to Ask a Question