How to make parent and child bidirectional pipe in C
I am trying to do a bidirectional pipe, the parent sends n number (int) to the child and the child return them doubled. I can't figure out what's my error?
I scanned the number n is the parent, sent it through fd1[1], and then proceeded to send those n numbers for the child to double.
In the child, I read the number n and then for every number I read, I double and send back.
int main()
int pid,n,c,p,k,nbread;
char buf1[2], buf2[2];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid=fork();
if(pid==0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0],buf2,sizeof(int));
n = atoi(buf2);
for(int i = 0; i<n;i++)
nbread = read(fd1[0],buf2,sizeof(int));
sleep(3);
if(nbread == -1)
exit(1);
c = atoi(buf2);
c = c*2;
sprintf(buf2,"%d",c);
write(fd2[1],buf2, sizeof(int));
close(fd1[0]);
close(fd2[1]);
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d",&p);
sprintf(buf1,"%d",p);
write(fd1[1],buf1,sizeof(int));
sleep(3);
for(int i=0;i<n;i++)
sprintf(buf1,"%d",i);
write(fd1[1],buf1,sizeof(int));
read(fd2[0],buf1,sizeof(int));
printf("number is: %s",buf1);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
c pipe fork
|
show 3 more comments
I am trying to do a bidirectional pipe, the parent sends n number (int) to the child and the child return them doubled. I can't figure out what's my error?
I scanned the number n is the parent, sent it through fd1[1], and then proceeded to send those n numbers for the child to double.
In the child, I read the number n and then for every number I read, I double and send back.
int main()
int pid,n,c,p,k,nbread;
char buf1[2], buf2[2];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid=fork();
if(pid==0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0],buf2,sizeof(int));
n = atoi(buf2);
for(int i = 0; i<n;i++)
nbread = read(fd1[0],buf2,sizeof(int));
sleep(3);
if(nbread == -1)
exit(1);
c = atoi(buf2);
c = c*2;
sprintf(buf2,"%d",c);
write(fd2[1],buf2, sizeof(int));
close(fd1[0]);
close(fd2[1]);
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d",&p);
sprintf(buf1,"%d",p);
write(fd1[1],buf1,sizeof(int));
sleep(3);
for(int i=0;i<n;i++)
sprintf(buf1,"%d",i);
write(fd1[1],buf1,sizeof(int));
read(fd2[0],buf1,sizeof(int));
printf("number is: %s",buf1);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
c pipe fork
Have you done any debugging?
– benc
Nov 15 '18 at 6:06
A 'bidirectional pipe' is different from using two pipes to have bidirectional communication between two processes.
– Jonathan Leffler
Nov 15 '18 at 6:07
@benc output: Enter integer: 3 Enter integer:
– M. Chakhtoura
Nov 15 '18 at 6:09
You need to have the child code exit, rather than also try to execute the parent code. It's easier if you use functions —be_childish()
andbe_parental()
are the names I normally use because they're the same length. Your buffers (buf1
andbuf2
) are tiny; only big enough for single-digit numbers. OK if that's what you really intend to enter, but very brittle (and unnecessarily brittle).
– Jonathan Leffler
Nov 15 '18 at 6:09
The secondEnter integer
is because the child continues into the parent's code after it's finished closing its pipes.
– Jonathan Leffler
Nov 15 '18 at 6:12
|
show 3 more comments
I am trying to do a bidirectional pipe, the parent sends n number (int) to the child and the child return them doubled. I can't figure out what's my error?
I scanned the number n is the parent, sent it through fd1[1], and then proceeded to send those n numbers for the child to double.
In the child, I read the number n and then for every number I read, I double and send back.
int main()
int pid,n,c,p,k,nbread;
char buf1[2], buf2[2];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid=fork();
if(pid==0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0],buf2,sizeof(int));
n = atoi(buf2);
for(int i = 0; i<n;i++)
nbread = read(fd1[0],buf2,sizeof(int));
sleep(3);
if(nbread == -1)
exit(1);
c = atoi(buf2);
c = c*2;
sprintf(buf2,"%d",c);
write(fd2[1],buf2, sizeof(int));
close(fd1[0]);
close(fd2[1]);
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d",&p);
sprintf(buf1,"%d",p);
write(fd1[1],buf1,sizeof(int));
sleep(3);
for(int i=0;i<n;i++)
sprintf(buf1,"%d",i);
write(fd1[1],buf1,sizeof(int));
read(fd2[0],buf1,sizeof(int));
printf("number is: %s",buf1);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
c pipe fork
I am trying to do a bidirectional pipe, the parent sends n number (int) to the child and the child return them doubled. I can't figure out what's my error?
I scanned the number n is the parent, sent it through fd1[1], and then proceeded to send those n numbers for the child to double.
In the child, I read the number n and then for every number I read, I double and send back.
int main()
int pid,n,c,p,k,nbread;
char buf1[2], buf2[2];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid=fork();
if(pid==0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0],buf2,sizeof(int));
n = atoi(buf2);
for(int i = 0; i<n;i++)
nbread = read(fd1[0],buf2,sizeof(int));
sleep(3);
if(nbread == -1)
exit(1);
c = atoi(buf2);
c = c*2;
sprintf(buf2,"%d",c);
write(fd2[1],buf2, sizeof(int));
close(fd1[0]);
close(fd2[1]);
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d",&p);
sprintf(buf1,"%d",p);
write(fd1[1],buf1,sizeof(int));
sleep(3);
for(int i=0;i<n;i++)
sprintf(buf1,"%d",i);
write(fd1[1],buf1,sizeof(int));
read(fd2[0],buf1,sizeof(int));
printf("number is: %s",buf1);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
c pipe fork
c pipe fork
asked Nov 15 '18 at 5:50
M. ChakhtouraM. Chakhtoura
156
156
Have you done any debugging?
– benc
Nov 15 '18 at 6:06
A 'bidirectional pipe' is different from using two pipes to have bidirectional communication between two processes.
– Jonathan Leffler
Nov 15 '18 at 6:07
@benc output: Enter integer: 3 Enter integer:
– M. Chakhtoura
Nov 15 '18 at 6:09
You need to have the child code exit, rather than also try to execute the parent code. It's easier if you use functions —be_childish()
andbe_parental()
are the names I normally use because they're the same length. Your buffers (buf1
andbuf2
) are tiny; only big enough for single-digit numbers. OK if that's what you really intend to enter, but very brittle (and unnecessarily brittle).
– Jonathan Leffler
Nov 15 '18 at 6:09
The secondEnter integer
is because the child continues into the parent's code after it's finished closing its pipes.
– Jonathan Leffler
Nov 15 '18 at 6:12
|
show 3 more comments
Have you done any debugging?
– benc
Nov 15 '18 at 6:06
A 'bidirectional pipe' is different from using two pipes to have bidirectional communication between two processes.
– Jonathan Leffler
Nov 15 '18 at 6:07
@benc output: Enter integer: 3 Enter integer:
– M. Chakhtoura
Nov 15 '18 at 6:09
You need to have the child code exit, rather than also try to execute the parent code. It's easier if you use functions —be_childish()
andbe_parental()
are the names I normally use because they're the same length. Your buffers (buf1
andbuf2
) are tiny; only big enough for single-digit numbers. OK if that's what you really intend to enter, but very brittle (and unnecessarily brittle).
– Jonathan Leffler
Nov 15 '18 at 6:09
The secondEnter integer
is because the child continues into the parent's code after it's finished closing its pipes.
– Jonathan Leffler
Nov 15 '18 at 6:12
Have you done any debugging?
– benc
Nov 15 '18 at 6:06
Have you done any debugging?
– benc
Nov 15 '18 at 6:06
A 'bidirectional pipe' is different from using two pipes to have bidirectional communication between two processes.
– Jonathan Leffler
Nov 15 '18 at 6:07
A 'bidirectional pipe' is different from using two pipes to have bidirectional communication between two processes.
– Jonathan Leffler
Nov 15 '18 at 6:07
@benc output: Enter integer: 3 Enter integer:
– M. Chakhtoura
Nov 15 '18 at 6:09
@benc output: Enter integer: 3 Enter integer:
– M. Chakhtoura
Nov 15 '18 at 6:09
You need to have the child code exit, rather than also try to execute the parent code. It's easier if you use functions —
be_childish()
and be_parental()
are the names I normally use because they're the same length. Your buffers (buf1
and buf2
) are tiny; only big enough for single-digit numbers. OK if that's what you really intend to enter, but very brittle (and unnecessarily brittle).– Jonathan Leffler
Nov 15 '18 at 6:09
You need to have the child code exit, rather than also try to execute the parent code. It's easier if you use functions —
be_childish()
and be_parental()
are the names I normally use because they're the same length. Your buffers (buf1
and buf2
) are tiny; only big enough for single-digit numbers. OK if that's what you really intend to enter, but very brittle (and unnecessarily brittle).– Jonathan Leffler
Nov 15 '18 at 6:09
The second
Enter integer
is because the child continues into the parent's code after it's finished closing its pipes.– Jonathan Leffler
Nov 15 '18 at 6:12
The second
Enter integer
is because the child continues into the parent's code after it's finished closing its pipes.– Jonathan Leffler
Nov 15 '18 at 6:12
|
show 3 more comments
1 Answer
1
active
oldest
votes
Fixing the parent loop to test p
and not n
fixes the main problems. Making sure that the buffers are big enough is a good idea too. Writing the whole buffer is OK though not necessarily ideal.
This code works; it has more debugging output in it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
int pid, n, c, p, k, nbread;
char buf1[12], buf2[12];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid = fork();
if (pid == 0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0], buf2, sizeof(buf2));
n = atoi(buf2);
printf("Child read %dn", n);
for (int i = 0; i < n; i++)
printf("child dozes...n");
sleep(3);
printf("child wakes...n");
nbread = read(fd1[0], buf2, sizeof(buf2));
if (nbread == -1)
fprintf(stderr, "child exits after read failuren");
exit(1);
c = atoi(buf2);
c = c * 2;
sprintf(buf2, "%d", c);
write(fd2[1], buf2, sizeof(buf2));
printf("Child wrote [%s]n", buf2);
close(fd1[0]);
close(fd2[1]);
printf("Child donen");
exit(0);
else
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d", &p);
sprintf(buf1, "%d", p);
write(fd1[1], buf1, sizeof(buf1));
printf("Parent wrote [%s]n", buf1);
printf("parent dozes...n");
sleep(3);
printf("parent wakes...n");
for (int i = 0; i < p; i++)
sprintf(buf1, "%d", i);
write(fd1[1], buf1, sizeof(buf1));
printf("parent wrote [%s]n", buf1);
read(fd2[0], buf2, sizeof(buf2));
printf("number is: %sn", buf2);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
Sample output:
Enter integer: 4
Parent wrote [4]
parent dozes...
Child read 4
child dozes...
parent wakes...
parent wrote [0]
child wakes...
Child wrote [0]
child dozes...
number is: 0
parent wrote [1]
child wakes...
Child wrote [2]
child dozes...
number is: 2
parent wrote [2]
child wakes...
Child wrote [4]
child dozes...
number is: 4
parent wrote [3]
child wakes...
Child wrote [6]
Child done
number is: 6
The code puts the child code and parent code into separate if
and else
blocks. It doesn't detect failures in pipe()
or fork()
which is suboptimal. The child exit(0)
is not crucial any more.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313194%2fhow-to-make-parent-and-child-bidirectional-pipe-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Fixing the parent loop to test p
and not n
fixes the main problems. Making sure that the buffers are big enough is a good idea too. Writing the whole buffer is OK though not necessarily ideal.
This code works; it has more debugging output in it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
int pid, n, c, p, k, nbread;
char buf1[12], buf2[12];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid = fork();
if (pid == 0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0], buf2, sizeof(buf2));
n = atoi(buf2);
printf("Child read %dn", n);
for (int i = 0; i < n; i++)
printf("child dozes...n");
sleep(3);
printf("child wakes...n");
nbread = read(fd1[0], buf2, sizeof(buf2));
if (nbread == -1)
fprintf(stderr, "child exits after read failuren");
exit(1);
c = atoi(buf2);
c = c * 2;
sprintf(buf2, "%d", c);
write(fd2[1], buf2, sizeof(buf2));
printf("Child wrote [%s]n", buf2);
close(fd1[0]);
close(fd2[1]);
printf("Child donen");
exit(0);
else
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d", &p);
sprintf(buf1, "%d", p);
write(fd1[1], buf1, sizeof(buf1));
printf("Parent wrote [%s]n", buf1);
printf("parent dozes...n");
sleep(3);
printf("parent wakes...n");
for (int i = 0; i < p; i++)
sprintf(buf1, "%d", i);
write(fd1[1], buf1, sizeof(buf1));
printf("parent wrote [%s]n", buf1);
read(fd2[0], buf2, sizeof(buf2));
printf("number is: %sn", buf2);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
Sample output:
Enter integer: 4
Parent wrote [4]
parent dozes...
Child read 4
child dozes...
parent wakes...
parent wrote [0]
child wakes...
Child wrote [0]
child dozes...
number is: 0
parent wrote [1]
child wakes...
Child wrote [2]
child dozes...
number is: 2
parent wrote [2]
child wakes...
Child wrote [4]
child dozes...
number is: 4
parent wrote [3]
child wakes...
Child wrote [6]
Child done
number is: 6
The code puts the child code and parent code into separate if
and else
blocks. It doesn't detect failures in pipe()
or fork()
which is suboptimal. The child exit(0)
is not crucial any more.
add a comment |
Fixing the parent loop to test p
and not n
fixes the main problems. Making sure that the buffers are big enough is a good idea too. Writing the whole buffer is OK though not necessarily ideal.
This code works; it has more debugging output in it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
int pid, n, c, p, k, nbread;
char buf1[12], buf2[12];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid = fork();
if (pid == 0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0], buf2, sizeof(buf2));
n = atoi(buf2);
printf("Child read %dn", n);
for (int i = 0; i < n; i++)
printf("child dozes...n");
sleep(3);
printf("child wakes...n");
nbread = read(fd1[0], buf2, sizeof(buf2));
if (nbread == -1)
fprintf(stderr, "child exits after read failuren");
exit(1);
c = atoi(buf2);
c = c * 2;
sprintf(buf2, "%d", c);
write(fd2[1], buf2, sizeof(buf2));
printf("Child wrote [%s]n", buf2);
close(fd1[0]);
close(fd2[1]);
printf("Child donen");
exit(0);
else
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d", &p);
sprintf(buf1, "%d", p);
write(fd1[1], buf1, sizeof(buf1));
printf("Parent wrote [%s]n", buf1);
printf("parent dozes...n");
sleep(3);
printf("parent wakes...n");
for (int i = 0; i < p; i++)
sprintf(buf1, "%d", i);
write(fd1[1], buf1, sizeof(buf1));
printf("parent wrote [%s]n", buf1);
read(fd2[0], buf2, sizeof(buf2));
printf("number is: %sn", buf2);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
Sample output:
Enter integer: 4
Parent wrote [4]
parent dozes...
Child read 4
child dozes...
parent wakes...
parent wrote [0]
child wakes...
Child wrote [0]
child dozes...
number is: 0
parent wrote [1]
child wakes...
Child wrote [2]
child dozes...
number is: 2
parent wrote [2]
child wakes...
Child wrote [4]
child dozes...
number is: 4
parent wrote [3]
child wakes...
Child wrote [6]
Child done
number is: 6
The code puts the child code and parent code into separate if
and else
blocks. It doesn't detect failures in pipe()
or fork()
which is suboptimal. The child exit(0)
is not crucial any more.
add a comment |
Fixing the parent loop to test p
and not n
fixes the main problems. Making sure that the buffers are big enough is a good idea too. Writing the whole buffer is OK though not necessarily ideal.
This code works; it has more debugging output in it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
int pid, n, c, p, k, nbread;
char buf1[12], buf2[12];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid = fork();
if (pid == 0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0], buf2, sizeof(buf2));
n = atoi(buf2);
printf("Child read %dn", n);
for (int i = 0; i < n; i++)
printf("child dozes...n");
sleep(3);
printf("child wakes...n");
nbread = read(fd1[0], buf2, sizeof(buf2));
if (nbread == -1)
fprintf(stderr, "child exits after read failuren");
exit(1);
c = atoi(buf2);
c = c * 2;
sprintf(buf2, "%d", c);
write(fd2[1], buf2, sizeof(buf2));
printf("Child wrote [%s]n", buf2);
close(fd1[0]);
close(fd2[1]);
printf("Child donen");
exit(0);
else
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d", &p);
sprintf(buf1, "%d", p);
write(fd1[1], buf1, sizeof(buf1));
printf("Parent wrote [%s]n", buf1);
printf("parent dozes...n");
sleep(3);
printf("parent wakes...n");
for (int i = 0; i < p; i++)
sprintf(buf1, "%d", i);
write(fd1[1], buf1, sizeof(buf1));
printf("parent wrote [%s]n", buf1);
read(fd2[0], buf2, sizeof(buf2));
printf("number is: %sn", buf2);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
Sample output:
Enter integer: 4
Parent wrote [4]
parent dozes...
Child read 4
child dozes...
parent wakes...
parent wrote [0]
child wakes...
Child wrote [0]
child dozes...
number is: 0
parent wrote [1]
child wakes...
Child wrote [2]
child dozes...
number is: 2
parent wrote [2]
child wakes...
Child wrote [4]
child dozes...
number is: 4
parent wrote [3]
child wakes...
Child wrote [6]
Child done
number is: 6
The code puts the child code and parent code into separate if
and else
blocks. It doesn't detect failures in pipe()
or fork()
which is suboptimal. The child exit(0)
is not crucial any more.
Fixing the parent loop to test p
and not n
fixes the main problems. Making sure that the buffers are big enough is a good idea too. Writing the whole buffer is OK though not necessarily ideal.
This code works; it has more debugging output in it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
int pid, n, c, p, k, nbread;
char buf1[12], buf2[12];
int fd1[2], fd2[2];
pipe(fd1);
pipe(fd2);
pid = fork();
if (pid == 0)
close(fd1[1]);
close(fd2[0]);
read(fd1[0], buf2, sizeof(buf2));
n = atoi(buf2);
printf("Child read %dn", n);
for (int i = 0; i < n; i++)
printf("child dozes...n");
sleep(3);
printf("child wakes...n");
nbread = read(fd1[0], buf2, sizeof(buf2));
if (nbread == -1)
fprintf(stderr, "child exits after read failuren");
exit(1);
c = atoi(buf2);
c = c * 2;
sprintf(buf2, "%d", c);
write(fd2[1], buf2, sizeof(buf2));
printf("Child wrote [%s]n", buf2);
close(fd1[0]);
close(fd2[1]);
printf("Child donen");
exit(0);
else
close(fd1[0]);
close(fd2[1]);
printf("Enter integer: ");
scanf("%d", &p);
sprintf(buf1, "%d", p);
write(fd1[1], buf1, sizeof(buf1));
printf("Parent wrote [%s]n", buf1);
printf("parent dozes...n");
sleep(3);
printf("parent wakes...n");
for (int i = 0; i < p; i++)
sprintf(buf1, "%d", i);
write(fd1[1], buf1, sizeof(buf1));
printf("parent wrote [%s]n", buf1);
read(fd2[0], buf2, sizeof(buf2));
printf("number is: %sn", buf2);
close(fd1[1]);
close(fd2[0]);
wait(NULL);
return 0;
Sample output:
Enter integer: 4
Parent wrote [4]
parent dozes...
Child read 4
child dozes...
parent wakes...
parent wrote [0]
child wakes...
Child wrote [0]
child dozes...
number is: 0
parent wrote [1]
child wakes...
Child wrote [2]
child dozes...
number is: 2
parent wrote [2]
child wakes...
Child wrote [4]
child dozes...
number is: 4
parent wrote [3]
child wakes...
Child wrote [6]
Child done
number is: 6
The code puts the child code and parent code into separate if
and else
blocks. It doesn't detect failures in pipe()
or fork()
which is suboptimal. The child exit(0)
is not crucial any more.
answered Nov 15 '18 at 6:48
Jonathan LefflerJonathan Leffler
570k916821034
570k916821034
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313194%2fhow-to-make-parent-and-child-bidirectional-pipe-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Have you done any debugging?
– benc
Nov 15 '18 at 6:06
A 'bidirectional pipe' is different from using two pipes to have bidirectional communication between two processes.
– Jonathan Leffler
Nov 15 '18 at 6:07
@benc output: Enter integer: 3 Enter integer:
– M. Chakhtoura
Nov 15 '18 at 6:09
You need to have the child code exit, rather than also try to execute the parent code. It's easier if you use functions —
be_childish()
andbe_parental()
are the names I normally use because they're the same length. Your buffers (buf1
andbuf2
) are tiny; only big enough for single-digit numbers. OK if that's what you really intend to enter, but very brittle (and unnecessarily brittle).– Jonathan Leffler
Nov 15 '18 at 6:09
The second
Enter integer
is because the child continues into the parent's code after it's finished closing its pipes.– Jonathan Leffler
Nov 15 '18 at 6:12