home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!news.ans.net!cmcl2!adm!news
- From: HERBER@fnal.fnal.gov (Randolph J. Herber, CD/DCD/SPG, x2966)
- Newsgroups: comp.sys.sgi
- Subject: Re: unix pipes and X
- Message-ID: <34984@adm.brl.mil>
- Date: 12 Jan 93 18:48:44 GMT
- Sender: news@adm.brl.mil
- Lines: 97
-
- +From: LANGER STEVEN C <sglanger@vela.acs.oakland.edu>
- +Newsgroups: comp.sys.sgi.graphics
- +Subject: unix pipes and X
- +Date: 12 Jan 93 02:54:40 GMT
- +To: info-iris-graphics@BRL.MIL
-
- +Despite the help of several netters, I cannot seem to master the
- +mechanics of inter-process communication via pipes. Ultimately,
- +I want 2-way communication between a parent and child process.
- +This is what I've tried so far;
-
- +/*
- +* parent.c launches child and should get child's output
- +* standard include files
- +*/
-
- +int pipes[2];
-
- ...
-
- + pipe (pipes);
-
- + if (fork() != 0)
-
- ...
-
- +Everything runs, the child prints to the screen via the stderr and I get
- +no error codes, but the parent callback func is never entered. Could
- +anyone please tell me what the error is?
-
- +--thanks in advance, steve
- +--
- +Steve Langer sglanger@argo.acs.oakland.edu (VMS)
- +Oakland University sglanger@vela.acs.oakland.edu (Ultrix)
- +---------------------------------------------------------------------------
- +Standard disclaimers apply. In addition, the author makes no guarantees,
- +concerning the grammatical accuracy of his writing. Therefore, any ^B's, ^C's,
- +midword character additions/deletions and other non-sense which occurs (after
- +the work leaves the author's decade old text editor via his decade old Amiga,
- +struggles through a local 1200 baud Merit server to be further mauled via the
- +remote VAX mail servers) is someone elses problem - namely yours.
- +---------------------------------------------------------------------------
-
- Remember that pipes are:
- 1. UNIdirectional
- 2. have two ends each; a pipe() system call returns both ends
- 3. fork() creates copies of both ends of pipes.
- 4. parents and children should close immediately after the fork()
- any unnecessary ends
-
- int to_child[2], from_child[2], pid;
-
- if(pipe(to_child)) {
- fprintf(stderr,"pipe(to_child) failed.\n");
- exit(1);
- }
-
- if(pipe(from_child)) {
- fprintf(stderr,"pipe(from_child) failed.\n");
- exit(1);
- }
-
- switch(pid = fork()) {
- case -1:
- fprintf(stderr,"fork() failed.\n");
- exit(1);
- case 0:
- close(0); make to_child pipe stdin
- if(dup(to_child[0]) != 0) {
- fprint(stderr,"unable to make child's stdin\n");
- exit(1);
- }
- close(to_child[1]); /* unnecessary end */
- close(1); make to_child pipe stdout
- if(dup(from_child[1]) != 1) {
- fprint(stderr,"unable to make child's stdout\n");
- exit(1);
- }
- close(from_child[0]); /* unnecessary end */
- exec...(child program);
- fprintf(stderr,"child program failed to run.\n");
- exit(1);
- default:
- close(to_child[0]); /* unnecessary end */
- close(from_child[1]); /* unnecessary end */
-
- ...
-
- /* if necessary, wait on pid here */
- }
-
- close(to_child[0]); close(to_child[1]); /* assure all ends */
- close(from_child[0]); close(from_child[1]); /* are closed */
-
- Randolph J. Herber, herber@fnalf.fnal.gov, +1 708 840 2966
- (Speaking for myself and not for US, US DOE, FNAL nor URA.)
- (Product, trade, or service marks herein belong to their respective owners.)
-