From: Kikutani Makoto Date: 1997-11-11T00:09:12+09:00 Subject: [ruby-list:5243] read from subprocess --ZPt4rx8FFjLCG7dd Content-Type: text/plain; charset=iso-2022-jp Content-Transfer-Encoding: 7bit きくたにです。 前にもちょっと書いたことがありますが、添付1のC programを 走らせて、子プロセスの添付2のrubyスクリプトが文字列"ruby"を 受け取らないのはなぜでしょう? 似たようなことをPythonでやると添付の3番目で動くのですけど。 -- 人生を背負い投げ 菊谷 誠(Kikutani Makoto) kikutani@eis.or.jp hgf03701@niftyserve.or.jp http://www.eis.or.jp/muse/kikutani/ --ZPt4rx8FFjLCG7dd Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ipc-test0.c" #include #include #include #include main() { int pid, i, slen, l; char *cmd, *arg0, arg1[8], *str; int channel[2]; /* IPC channel with child process */ char buf[10]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, channel) < 0) { fprintf(stderr, "socketpair failed\n"); return; } if ((pid = vfork()) == -1) { return; } if (pid == 0) { /* child process */ #if 0 cmd = "./ipc0"; arg0 = "ipc0"; #else cmd = "./ipc0.rb"; arg0 = "ipc0.rb"; #endif sprintf(arg1, "%d", channel[0]); execl(cmd, arg0, arg1, NULL); perror("execl failed"); close( channel[0] ); _exit( 1 ); } /* parent process */ close(channel[0]); i = 0x12345678; if ((l = write( channel[1], &i, sizeof(int))) != sizeof(int)) printf("ipc-test0.c: int write failed. written length = %d\n", l); if ((l = read( channel[1], buf, 2)) != 2) printf("ipc-test0.c: int read failed. read length = %d\n", l); printf("buf = %s\n", buf); str = "ruby"; slen = strlen(str); fflush(stdout); if ((l = write( channel[1], str, slen)) != slen) printf("ipc-test0.c: str write failed. written length = %d\n", l); if ((l = read( channel[1], buf, 2)) != 2) printf("ipc-test0.c: int read failed. read length = %d\n", l); printf("buf = %s\n", buf); } --ZPt4rx8FFjLCG7dd Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ipc0.py" #!/usr/bin/python import sys,os,string def read_int(fd): i = fd.read(4) v = ord(i[3]) v = v * 256 + ord(i[2]) v = v * 256 + ord(i[1]) v = v * 256 + ord(i[0]) return v port = string.atoi(sys.argv[1]) sys.stderr.write("port = " + sys.argv[1] + "\n") fd = os.fdopen(port, "r+") i = read_int(fd) sys.stderr.write("i = %x\n" % i) fd.write("OK") line = fd.read(4) sys.stderr.write("line = " + line + "\n") fd.write("OK") --ZPt4rx8FFjLCG7dd--