《unix systems programming》令牌环项目中的torus问题

merlinran 2006-11-18 03:27:51
7.3节第13个题。我找到了英文的text:
Modify ring1 to create a bidirectional torus of processes. Accumulate ID arrays to test connectivity. A torus has a two-dimensional structure. It is like a mesh except that the processes at the ends are connected together. The n2 processes are arranged in n rings in each dimension (torus). Each process has four connections (North, South, East, and West).
图看起来应该像下面的论文第一页所示(我的理解):
http://citeseer.ist.psu.edu/cache/papers/cs/10356/http:zSzzSzwww.ctr.columbia.eduzSz%7EmilanzSzpaperszSzicccn94.pdf/kovacevic94analysis.pdf
有没有哪位仁兄做成过这个题?
...全文
166 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
merlinran 2006-11-18
  • 打赏
  • 举报
回复
我现在的程序,这是改了N多遍的结果。我现在已经进入到program by coincidence的状态,完全理不清思路了。
里面的vec虽然有4个fd数组,但只用了两个。

merlin@colinux:~/usp/ch7$ cat torustopology.c
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define STR_SUB(line) #line
#define STR(line) STR_SUB(line)
#define check(func) {if ((func) == -1) perror(STR(__LINE__)": error "#func);}
#define check_return(ret, func) {if (((ret) = (func)) == -1) perror(STR(__LINE__)": error" #func);}

int main(int argc, const char *argv[])
{
int nprocs = atoi(argv[1]);
int vec[4][2]; // the four direction of one node's vector
int fd[2];
char *dir[4] = {"up ", "right", "down ", "left "};
int i, j;

int child;
for (i = 0; i < nprocs - 1; ++i) {
for (j = 0; j < 2; ++j) {
check(pipe(vec[j]));
check(pipe(fd));
check(dup2(fd[0], vec[j][0]));
check(dup2(fd[1], vec[j][1]));
check(close(fd[0]));
check(close(fd[1]));

check(pipe(fd));
check_return(child, fork());
if ( child > 0) { // parent
check(dup2(fd[1], vec[j][1]));
}
else if ( child == 0) {// child
check(dup2(fd[0], vec[j][0]));
}
check(close(fd[0]));
check(close(fd[1]));
}
if ( child > 0) // parent
break;
}

int pids[4][nprocs];
for ( i = 0; i < 2; ++i) {
int next_id = getpid();
pids[i][0] = next_id;
for ( j = 1; j < nprocs; ++j) {
check(write(vec[i][1], &next_id, sizeof(next_id)));
check(read(vec[i][0], &next_id, sizeof(next_id)));
pids[i][j] = next_id;
}
}

for ( i = 0; i < 2; ++i) {
fprintf(stderr, "pid %d wrote its %s vector: ", getpid(), dir[i]);
for ( j = 0; j < nprocs; ++j)
fprintf(stderr, "%d\t", pids[i][j]);
fprintf(stderr, "\n");
}
wait();
return 0;

}

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧