きくたにです。
助田さん、了解です。

Fri, Nov 28, 1997 at 11:10:37AM +0900 において
Kikutani Makoto さん曰く:

> 質問その2:
> ipc-test1.rb のように双方向のパイプ(?)を作るのはPythonではできない
> そうですが、Cで書くにはどうすればいいのでしょうか?

こっちはNutshell本のUsing C on the UNIX Systemを見てたら
似たよなのがあったので、こうしてみました。

------------------------------------------------------------
#include <stdio.h>
main()
{
   FILE *fpw, *fpr;
   int pid, pipefds[2];
   int i[1];
   char resp[5];

   if (pipe(pipefds) < 0) {
      perror("pipe");
      exit(1);
   }

   if ((pid = fork()) < 0) {
      perror("fork");
      exit(1);
   }

   if (pid == 0) { /* child */
      dup2(pipefds[0], 0);
      dup2(pipefds[1], 1);
      close(pipefds[0]);
      close(pipefds[1]);
      execl("./ipc1.rb", "ipc1.rb", 0);
      perror("exec");
      exit(1);
   }
   /* parent */
   if ((fpw = fdopen(pipefds[1], "w")) == NULL) {
      perror("fdopen w");
      exit(1);
   }
   if ((fpr = fdopen(pipefds[0], "r")) == NULL) {
      perror("fdopen r");
      exit(1);
   }

   i[0] = 0x12345678;
   fwrite(i, sizeof(int), 1, fpw);
   fflush(fpw);
   fread(resp, 1, 2, fpr);
   resp[2] = '\0';
   fprintf(stderr, "ipc-test1.c: %s\n", resp);
   fwrite("ruby", sizeof(char), 4, fpw);
   fflush(fpw);
   fread(resp, 1, 2, fpr);
   resp[2] = '\0';
   fprintf(stderr, "ipc-test1.c: %s\n", resp);

   exit(0);
}
------------------------------------------------------------

結果は

ipc-test1.c: xV
ipc-test1.c: 4
ipc1.rb: int byte = 72
ipc1.rb: int byte = 75
ipc1.rb: int byte = 62
ipc1.rb: int byte = 79

なんか変。
rubyじゃなくなってすみませんが、Unixプログラムのシロートに
教えてやってください。

-- 
人生を背負い投げ

菊谷 誠(Kikutani Makoto)  kikutani / eis.or.jp kikutani / jdc.ericsson.se
hgf03701 / niftyserve.or.jp    http://www.eis.or.jp/muse/kikutani/