首页 > 解决方案 > Creating Pipes with array in loop

问题描述

I need to create 10 pipes in C. In order to make them work I need to create 10 int arrays of size 2? Or can I just declare an array sized 20 and then give the pipe the adress where each pipe should start? If I have to create 10 is there any way i can create them in a loop and get kind of this result?

for(i=0; i<10; i++){
    vec0[2];    //create array vec0, vec1, vec2....
    pipe(vec0);
}

Is even an array the best way to do this?

标签: cpipe

解决方案


int您可以使用如下所示的二维数组。

int fds[10][2];
for(i=0; i<10; i++){
   pipe(fds[i]);
}

wherefds[i][0]代表读结束,fds[i][1]代表写结束。


如果您不想使用二维数组,则可以int如下声明 20 s 的数组,并使用基于偏移的方法来传递和读取fd's.

 int fds[20];
 for(i=0; i<10; i++){
       pipe(fds+i*2);
    }

然后fds[i*2]代表读结束,fds[i*2+1]代表写结束。


推荐阅读