I am writing a program that creates a logical ring of processes which utilizes pipes to pass a message around the ring. The ring consists of n process. The first process will pass a message to the next, containing its own process id (in character form). Each subsequent process will read the message from its incoming pipe, append a ':' and its own process id, and write the resulting message to its outgoing pipe. It also writes this to output. Eventually the message should get back to the initial process which should read the message and write it to output. The output will eventually look like this for 6 processes:

6563
6563:6564
6563:6564:6565
6563:6564:6565:6566
6563:6564:6565:6566:6567
6563:6564:6565:6566:6567:6568

I have created a recursive function which generates the correct number of processes. But I can't seem to get my pipes to work correctly. Basically, I think I should be able to create 2 pipes in main. The first will be for common use by all processes. The second will be for use by only the 1st and last processes, allowing them to connect logically and complete the ring. So both of these pipes need to be passed to the recursive function. The problem I get is that I don't quite understand how to read and write to pipes so that I can make this work. I have included my code, including comments as to what I think I want to do in certain places. I have gotten as far as I can at this point. I attempted to read and write with the pipes but all it does is produce bad output and it causes the program to hang. So I took those lines out. Hopefully someone out there can help. Thanks.

Code:
include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sys/wait.h>

using namespace std;

void forkByRecursion(int remain, int common[], int final[])
{
        int pid, stat_val;

        pid = fork();

        if(pid < 0)
        {
                //Error
                cout << "**Error** fork failed!" << endl;
                exit(3);
        }
        else if(pid == 0)
        {
                //Child
                cout << "I am child process " << getpid() << " and my parent is " << getppid() << endl;

                //read from common pipe
                //append pid to data read from common pipe
                //write data string to file

                if(remain > 1)
                        forkByRecursion(--remain, common, final);
                else
                {
                        cout << "Last child process was created, attempt to complete ring." << endl;

                        //write to final pipe
                }

                exit(1);
        }
        else
        {
                //Parent
                //write to common pipe

                waitpid(pid, &stat_val, WNOHANG);
        }
}

int main(int argc, char *argv[])
{
        if(argc != 2)
        {
                cout << "Usage: ring <int> -- <int> must be greater than 1" << endl;
                exit(1);
        }
        else
        {
                int processes = atoi(argv[1]);

                if(processes < 2)
                {
                        cout << "Usage: ring <int> -- <int> must be greater than 1" << endl;
                        exit(2);
                }
                else
                {
                        int common_pipe[2];
                        pipe(common_pipe);

                        int final_pipe[2];
                        pipe(common_pipe);

                        cout << "I am the top process " << getpid() << endl;

                        forkByRecursion(--processes, common_pipe, final_pipe);

                        cout << "Completing ring. I am process " << getpid() << endl;
                }
        }

        return 0;
}