jehanne/qa/lib/c/hcube.c
Giacomo Tesio e70feee4a3 libc: introduce "jehanne_" namespace
With this commit all functions declared in libc.h have been renamed
with the "jehanne_" prefix. This is done for several reason:

- it removes conflicts during symbol resolution when linking
  standard C libraries like newlib or musl
- it allows programs depending on a standard C library to directly
  link to a library depending on our non standard libc (eg libsec).

To ease transiction two files are provided:

- sys/include/lib9.h that can be included instead of <libc.h> to use
  the old names (via a simple set of macros)
- sys/src/lib/c/lib9.c that can be compiled with a program where the
  macro provided by lib9.h are too dumb (see for example rc or grep).

In the kernel port/lib.h has been modified accordingly and some of
the functions it directly provides has been renamed too (eg malloc
in qmalloc.c and print in devcons.c).
2017-04-19 23:48:21 +02:00

126 lines
2.3 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(write(pfd, buf, strlen(buf)) != strlen(buf)){
remove(path);
sysfatal("fdfork: post '%s': %r", buf);
}
close(ch[0]);
if(write(fd, path, len) != len){
remove(path);
sysfatal("fdfork: write to peer: %r");
}
len = read(fd, buf, sizeof buf-1);
if(len <= 0){
remove(path);
sysfatal("fdfork: read ack: %s from peer: %r", len == -1 ? "error" : "eof");
}
buf[len] = '\0';
if(strcmp(path, buf)){
remove(path);
print("ack from wrong peer: got '%s' want '%s'\n", buf, path);
sysfatal("ack from wrong peer");
}
close(pfd);
//remove(path);
return ch[1];
} else {
len = 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 = open(path, OWRITE);
if(pfd == -1)
sysfatal("fdfork: open: %r");
if(write(fd, path, len) != len)
sysfatal("fdfork: write ack to peer: %r");
return pfd;
}
}
int
hyperfork(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("rfork");
case 0:
for(i = 0; i < dim; i++){
close(fd[i]);
fd[i] = chfd[i];
}
id |= 1 << dim;
close(ch[0]);
fd[dim] = ch[1];
break;
default:
for(i = 0; i < dim; i++)
close(chfd[i]);
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 = hyperfork(fd, id, i);
for(i = 0; i < dim; i++){
int tlen, rlen;
tlen = snprint(buf, sizeof buf, "hello %d\n", id);
write(fd[i], buf, tlen);
rlen = 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");
}