jehanne/qa/lib/c/hcube.c

126 lines
2.4 KiB
C

#include <u.h>
#include <lib9.h>
int dim = 6;
int
fdfork(int fd, int post)
{
static int seq;
char path[64];
char buf[64];
int pfd, len;
if(post){
int ch[2];
pipe(ch);
len = snprint(path, sizeof path, "hcube.%d.%d", getpid(), seq++);
pfd = ocreate(path, OWRITE|ORCLOSE, 0666);
if(pfd == -1)
sysfatal("fdfork: create: %r");
snprint(buf, sizeof buf, "%d", ch[0]);
if(jehanne_write(pfd, buf, strlen(buf)) != strlen(buf)){
sys_remove(path);
sysfatal("fdfork: post '%s': %r", buf);
}
sys_close(ch[0]);
if(jehanne_write(fd, path, len) != len){
sys_remove(path);
sysfatal("fdfork: write to peer: %r");
}
len = jehanne_read(fd, buf, sizeof buf-1);
if(len <= 0){
sys_remove(path);
sysfatal("fdfork: read ack: %s from peer: %r", len == -1 ? "error" : "eof");
}
buf[len] = '\0';
if(strcmp(path, buf)){
sys_remove(path);
print("ack from wrong peer: got '%s' want '%s'\n", buf, path);
sysfatal("ack from wrong peer");
}
sys_close(pfd);
//sys_remove(path);
return ch[1];
} else {
len = jehanne_read(fd, path, sizeof path-1);
if(len <= 0)
sysfatal("fdfork: read path: %s from peer: %r", len == -1 ? "error" : "eof");
path[len] = '\0';
pfd = sys_open(path, OWRITE);
if(pfd == -1)
sysfatal("fdfork: open: %r");
if(jehanne_write(fd, path, len) != len)
sysfatal("fdfork: write ack to peer: %r");
return pfd;
}
}
int
hypesys_rfork(int *fd, int id, int dim)
{
int chfd[dim];
int ch[2];
int i;
for(i = 0; i < dim; i++)
chfd[i] = fdfork(fd[i], (id & (1<<i)) == 0);
pipe(ch);
switch(fork()){
case -1:
sysfatal("sys_rfork");
case 0:
for(i = 0; i < dim; i++){
sys_close(fd[i]);
fd[i] = chfd[i];
}
id |= 1 << dim;
sys_close(ch[0]);
fd[dim] = ch[1];
break;
default:
for(i = 0; i < dim; i++)
sys_close(chfd[i]);
sys_close(ch[1]);
fd[dim] = ch[0];
break;
}
return id;
}
void
main(int argc, char *argv[])
{
int fd[32];
char buf[32], buf2[32];
int id;
int i;
if(argc > 1)
dim = strtol(argv[1], nil, 0);
id = 0;
chdir("/srv");
for(i = 0; i < dim; i++)
id = hypesys_rfork(fd, id, i);
for(i = 0; i < dim; i++){
int tlen, rlen;
tlen = snprint(buf, sizeof buf, "hello %d\n", id);
jehanne_write(fd[i], buf, tlen);
rlen = jehanne_read(fd[i], buf, sizeof buf-1);
buf[rlen] = '\0';
snprint(buf2, sizeof buf2, "%d: %s", id, buf);
}
for(i = 0; i < dim; i++)
if((id & (1<<i)) == 0)
waitpid();
print("PASS\n");
exits("PASS");
}