From owner-freebsd-chat Wed Mar 10 6:25:18 1999 Delivered-To: freebsd-chat@freebsd.org Received: from finch-post-10.mail.demon.net (finch-post-10.mail.demon.net [194.217.242.38]) by hub.freebsd.org (Postfix) with ESMTP id 96FBC1511E for ; Wed, 10 Mar 1999 06:25:11 -0800 (PST) (envelope-from marko@uk.radan.com) Received: from [158.152.75.22] (helo=uk.radan.com) by finch-post-10.mail.demon.net with smtp (Exim 2.12 #1) id 10Kjuj-00061q-0A; Wed, 10 Mar 1999 14:24:50 +0000 Organisation: Radan Computational Ltd., Bath, UK. Phone: +44-1225-320320 Fax: +44-1225-320311 Received: from beavis.uk.radan.com (beavis [193.114.228.122]) by uk.radan.com (8.6.10/8.6.10) with SMTP id OAA03142; Wed, 10 Mar 1999 14:24:14 GMT Received: from uk.radan.com (gppsun4) by beavis.uk.radan.com (4.1/SMI-4.1) id AA06081; Wed, 10 Mar 99 14:24:12 GMT Message-Id: <36E6807B.2BD8FFCF@uk.radan.com> Date: Wed, 10 Mar 1999 14:23:55 +0000 From: Mark Ovens Organization: Radan Computational Ltd X-Mailer: Mozilla 4.5 [en] (X11; I; SunOS 4.1.3_U1 sun4m) X-Accept-Language: en-GB Mime-Version: 1.0 To: Steve Price Cc: Jeroen Ruigrok/Asmodai , Nathan Ahlstrom , chat@freebsd.org Subject: Re: Book/URL on C programming (inter-process comms)? References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-chat@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org 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 . :) > ..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 #include 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