3

I am trying to make something work here, I have c program where my parent process creates a pipe so he can listen for request from children processes. These children are created dynamically, it is never the same number. So far, I managed to send the requests to the parent through the pipe, and have it synchronized through mutex.

My problem is, this request need an answer provided to the child that made such request, and I can't use the same pipe since its one way, and all the other children will be using it (I already tried and it generates a serious issue)

The next thing I tried was making a pipe before the request on the child and passing the value of the descriptor in the request. Also failed, since the parent doesn't know those descriptors, which I found out after failing.

So now I am clueless in how to make that request response reach the child... I am a beginner with C programming, so, all help is welcomed!

BTW: working in a Unix program, pure C programming

7
  • Have you tried passing a pipe descriptor to the parent process along the pipeline that the child is listening for a response on? Commented Jun 21, 2015 at 0:37
  • Also can we see some code Commented Jun 21, 2015 at 0:37
  • You could give each child process a pipe for input and a pipe for output, and the parent process would use the other ends. If you can use stdin and stdout for the pipes, the better. Sockets are another alternative. beej.us/guide/bgipc/output/html/singlepage/bgipc.html is a good source of info. So are Richard Stevens books on UNIX programming. Commented Jun 21, 2015 at 0:56
  • @phyrrus Hi! yes, i did try that, and it doesnt work, at least not how i put it, wich was calling pipe(var[]) in child and pasint var[1] to the parent. The parent doesnt know the descriptor id :/ Commented Jun 21, 2015 at 3:01
  • @PSkocik but arent sockets for tcp ip communication? can they be used locally as well? Commented Jun 21, 2015 at 3:01

1 Answer 1

6

Pipes are uni-directional and using sockets seems painful for a little ipc thing.

I recommend you to use socketpair(). You can consider them as bi-directional pipes.

Here is an example where the parent sends a character to its child.Then child makes it uppercase and sends it back to the parent.(It is from beej's tutorial)

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> int main(void) { int sv[2]; /* the pair of socket descriptors */ char buf; /* for data exchange between processes */ if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) { perror("socketpair"); exit(1); } if (!fork()) { /* child */ read(sv[1], &buf, 1); printf("child: read '%c'\n", buf); buf = toupper(buf); /* make it uppercase */ write(sv[1], &buf, 1); printf("child: sent '%c'\n", buf); } else { /* parent */ write(sv[0], "b", 1); printf("parent: sent 'b'\n"); read(sv[0], &buf, 1); printf("parent: read '%c'\n", buf); wait(NULL); /* wait for child to die */ } return 0; } 

Also you can easily use two pipes for your two directions.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.