Bulletin number: 24 Products affected: D711D Description: Bi-directional communication Unix/Transputer Component: Date: Thu Mar 8 12:05:14 GMT 1990 ----- We have been asked several times how a C program on a Transputer can talk (bi-directionally) to a C program on a unix host system. I have written a sample program to demonstrate the general principle involved. The sample consists of two programs "host" and "prog". To show the generality I have made two version of the prog binary, one for unix (uprog), the other for transputer (tprog). Prog reads a number from stdin, squares it and writes it to stdout. If EOF is reached or the number input is -99 the message EOF is written. Host creates two (unnamed) pipes. Fork is called to create a new process. The child process connects the two pipes to stdin and stdout and exec's the prog process. If the host program was given any parameter when it was started it calls the transputer version, i.e. the iserver with the name the transputer program. The parent process closes unused ends of pipes, prepares for stream i/o and enters a read write loop. Any numbers typed in at the keyboard are transfered by pipe to the child task (prog). The results are then read from the output pipe and printed on the terminal. Almost all error checking has been omitted to clarify the example. +file:host.c+--------------------------------------------------------- #include #include int host_to_prog[2], prog_to_host[2]; FILE *ho_to_pr, *pr_to_ho; int flag; void parent() { char line1[80], line2[80]; int looping; close(host_to_prog[0]); close(prog_to_host[1]); ho_to_pr = fdopen(host_to_prog[1], "w"); pr_to_ho = fdopen(prog_to_host[0], "r"); if (flag > 1) /* read booting transputer message */ { fgets(line2, sizeof line2, pr_to_ho); printf("%s", line2); } for (looping = 1; looping == 1;) { if (gets(line1) == NULL) { looping = 0; fprintf(ho_to_pr, "-99\n"); fflush(ho_to_pr); } else { fprintf(ho_to_pr, "%s\n", line1); fflush(ho_to_pr); } fgets(line2, sizeof line2, pr_to_ho); printf("%s", line2); } wait((union wait *)NULL); } void child() { close(1); /* fix stdout */ dup(prog_to_host[1]); close(prog_to_host[0]); close(prog_to_host[1]); close(0); /* fix stdin */ dup(host_to_prog[0]); close(host_to_prog[0]); close(host_to_prog[1]); if (flag > 1) /* transputer version */ execlp("iserver", "iserver", "-sb", "tprog.b4x", (char *) NULL); else /* unix version */ execl("./uprog", "uprog", (char *) NULL); } int main(argc, argv) int argc; char *argv[]; { flag = argc; pipe(host_to_prog); pipe(prog_to_host); if (fork()) parent(); else child(); } +file:prog.c+--------------------------------------------------------- #include main() { int i, j, k; for (;;) { i = scanf("%d", &j); if ((i == EOF) || (j == -99)) { printf("EOF\n"); exit(0); } k = j * j; printf("%d %d\n", j, k); fflush(stdout); } } +file:makefile+------------------------------------------------------- all : host uprog tprog.c4x host : host.c cc host.c -o host uprog : prog.c cc prog.c -o uprog tprog.c4x : prog.c t4c prog ilink mainent.c4x crtl.lib prog.bin -o tprog.c4x iboot tprog.c4x +endfile+-------------------------------------------------------------