Thread: creating an array of pipes

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    creating an array of pipes

    Hi,
    I have a specific number of processes, which were created using fork(), those processes should communicate each with other using pipes. I want to create a dynamic array of pipes or use a Vector so it would contain all the pipes there were created.

    each pipe should be of size of 2 integers so the array of the vector size should be arr[N][2].

    my problem is that I don't know how to create properly this Vector or Array and pass it correctly to the dup2 function.

    Maybe some one could help me with an example?

    I build those two functions:

    Code:
    int** createArrOfPipes(int amount)
    {
        int** arr = new int*[amount];
    
        for (int i=0;i<amount;i++)
        {
            arr[i] = new int[2];
        }
    
        return arr;
    }
    
    void freeArrOfPipes(int** arr, int amount)
    {
        for (int i=0;i<amount;i++)
        {
            delete[] arr[i];
        }
        delete[] arr;
    }
    and tried to use vector, like this:

    vector< vector<int> > pipes;
    vector<int> read_vec1;
    vector<int> write_vec2;
    pipes.push_back(read_vec1);
    pipes.push_back(write_vec2);


    but how to do some thing like this:

    Code:
           for(int i=0;i<(sub_commands.size()-1);i++)
            {
                pipe(pipes[i]);
            }
    or this:

    Code:
                  while (dup2 (pipes[(command_index-1)][read], STDIN_FILENO) < 0)   //dup client's sock with stdin
                    {
                        if (errno == EINTR) continue;
    
                        fprintf(stderr,"dup2 failed.");
                    }
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    int** pipes = createArrOfPipes(amount);
           for(int i=0;i<amount;i++)
            {
                pipe(pipes[i]);
            }
    It's just not going to understand STL vectors or anything else.

    You might try creating a myPipe class, containing int p[2] say, then making a std::vector of that object, if you want to wrap some C++ around the low-level API.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  2. Creating a Spreadsheet using a multidimensional array
    By Apiatan in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2009, 04:18 PM
  3. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  4. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM

Tags for this Thread