creating an array of pipes

This is a discussion on creating an array of pipes within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I have a specific number of processes, which were created using fork(), those processes should communicate each with other ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21