Date: Wed, 10 Mar 1999 14:23:55 +0000 From: Mark Ovens <marko@uk.radan.com> To: Steve Price <sprice@hiwaay.net> Cc: Jeroen Ruigrok/Asmodai <asmodai@wxs.nl>, Nathan Ahlstrom <nrahlstr@winternet.com>, chat@freebsd.org Subject: Re: Book/URL on C programming (inter-process comms)? Message-ID: <36E6807B.2BD8FFCF@uk.radan.com> References: <Pine.OSF.4.02.9903081815220.25487-100000@fly.HiWAAY.net>
next in thread | previous in thread | raw e-mail | index | archive | help
Steve Price wrote: > > Advanced Programming in the Unix Environment by W. Richard Stevens. > ISBN 0-201-56317-7. > > Unix Network Programming Volume 2: Interprocess Communications by > W. Richard Stevens. ISBN 0-130-81081-9. > > In fact I've found that just about all of Mr. Stevens books are > a must have on my shelf. One of his books even features excerpts > by one of our very own, Bill Fenner <fenner@freebsd.org>. :) > ..and Nathan Ahlstrom wrote: > http://www.ecst.csuchico.edu/~beej/guide/ ..and Jeroen Ruigrok/Asmodai wrote: > R.W.Stevens series has a book, part II of the series, which is about IPC, > and is surprisingly called: Unix Networkprogramming: Interprocess > Communications. ... and Narvi wrote: > What about /usr/share/doc/psd/ ? Thanks for all these pointers guys. I tried them all, except for buying Richard Stevens' books, although I did d/l the source code for all the examples in the books. I've spent several hours reading through it all. Unfortunately most of the examples show one-way communication between child and parent, which is not what I need (I've got that bit working). I have managed to get what I'm trying to do *nearly* working. and have included the code I've used below. I would appreciate it very much if someone could spare a few minutes to look at it for me and point out what's wrong. I'm certain that what I'm trying to do is simple but there is something about IPC that I haven't quite grasped. As an excercise, applying the KISS principle, I've got a simple prog, d2c, that asks for a circle diameter and returns the circumference. When run from the command line it does this (user enters the "34"): % d2c Diameter: 34 Circumference = 106.814150 % Now I want to start this from another program to read and write to it. popen() seemed the function to use as it returns a FILE* which implies that I can just use f{print,scan}f() to communicate with the popen()'d program so I wrote this program: #include <stdio.h> #include <string.h> void main() { FILE *prog; char input[BUFSIZ], result[BUFSIZ]; int diam = 34; if ((prog = popen("/usr/radan/d2c", "r+")) == NULL) { perror("Failed"); exit(0); } fscanf(prog, "%s", input); if (strstr(input, "Diameter")) { fprintf(prog, "%d\n", diam); fscanf(prog, "%s", result); printf("RESULT! - %s\n", result); } else printf("Didn't work\n"); } The problem is it doesn't work, just hangs :-(. Stepping through it in gdb shows that the first fscanf never returns, it just sits there waiting. I tried using fgets(), fread(), read() instead but still no luck. The only thing I did that made it work (sort of) is by adding fprintf(prog, "34\n")'' before the first fscanf(). I then find that, after fscanf() returns ``input'' contains *both* outputs from ``d2c'', i.e. "Diameter: Circumference = 106.814150" although not the ``34'' sent by the fprintf(). This suggests that I'm nearly right (there is communication established) but not completely. I would be very grateful of any help. Thanks. -- FreeBSD - The Power To Serve http://www.freebsd.org My Webpage http://www.users.globalnet.co.uk/~markov _______________________________________________________________ Mark Ovens, CNC Apps Engineer, Radan Computational Ltd. Bath UK CAD/CAM solutions for Sheetmetal Working Industry mailto:marko@uk.radan.com http://www.radan.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?36E6807B.2BD8FFCF>