Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6910667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:50:39+00:00 2026-05-27T08:50:39+00:00

Have a look at the following code: #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<sys/types.h> main() {

  • 0

Have a look at the following code:

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<sys/types.h>

main() {
    int pipdes[2];
    char buff[50];
    const char parent[]="Parent Writes. Child Reads\n";
    const char child[]="Child Writes. Parent Reads\n";
    if(pipe(pipdes)==0) {
        pid_t pid=fork();
        if(pid<0)
              printf("Error\n");
        if(pid==0){
            read(pipdes[0],buff,50);
            printf("Parent: %s",buff);
            write(pipdes[1], child, strlen(child));
            exit(0);
        }
        else if(pid>0) {
            write(pipdes[1], parent, strlen(parent));
            wait(pid);
            read(pipdes[0], buff, 50);
            printf("Child: %s", buff);
        }
    }
    else
        printf("Error in pipe\n");
}

Now, here I have created just one pipe, but both the processes can read and write from. Aren’t pipes supposed to be uni-directional.
Also, when i put the conventional ‘close(pipdes[0])’ for parent and ‘close(pipdes[1])’ for child, the code doesn’t work, though I add the open(pipdes[0]) function later.

My concepts with UNIX and pipes is still raw, so I might come out a bit lame here, but please do assist.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:50:39+00:00Added an answer on May 27, 2026 at 8:50 am

    On some systems, pipes can be bidirectional. But they don’t have to be, and any assumption that they will be is non-portable. In particular, they aren’t on Linux.

    As it is, your code has a problem — both processes are trying to read from and write to the same pipe. The intended use for pipes is that the child writes and the parent reads, or vice versa. The current way you’re doing things works for you right now, because you’re reading and writing once and waiting on the child. But when you loop while trying to do things the way you’re doing, you can’t wait — and without synchronization, the child will often (but not always!) end up reading what it intended to send to the parent, and vice versa.

    If you want data flowing in both directions, you could use two pairs of pipes. Let’s call them parent_pipe and child_pipe. The parent would read from parent_pipe[0] and write to child_pipe[1], and the child would read from child_pipe[0] and write to parent_pipe[1].

    #include<unistd.h>
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #include<sys/types.h>
    
    int main() {
        int parent_pipe[2];
        int child_pipe[2];
        char buff[50];
    
        if(pipe(parent_pipe) || pipe(child_pipe)) {
            perror("pipe(...)");
            exit(1);
        }
    
        // As noted elsewhere, you're using `fork()` incorrectly.
        // `fork()` returns 0 to the child, and a pid to the parent, or -1 if an error
        // occurs.
        int pid = fork();
        if (pid == -1) {
            perror("fork()");
            exit(1);
        }
    
        if (pid == 0) {
            // this is the child process.  read from child_pipe, write to parent_pipe
            const char child[]="Child Writes. Parent Reads\n";
            int in, out;
            in = child_pipe[0];
            // in = parent_pipe[0];  // uncomment me to test with one pipe pair
            out = parent_pipe[1];
    
            for (int i = 0; i < 10; ++i) {
                read(in,buff,50);
                printf("Parent: %s",buff);
                // NOTE: `strlen(child)` doesn't include the nul at the end!
                write(out, child, strlen(child) + 1);
            }
        }
        else {
            // this is the parent process
            const char parent[]="Parent Writes. Child Reads\n";
            int in, out;
            in = parent_pipe[0];
            out = child_pipe[1];
            // out = parent_pipe[1];  // uncomment me to test with one pipe pair
    
            for (int i = 0; i < 10; ++i) {
                write(out, parent, strlen(parent) + 1);
                read(in, buff, 50);
                printf("Child: %s", buff);
            }
        }
    }
    

    Alternatively, you could use a pair of UNIX sockets created with socketpair(AF_LOCAL, SOCK_STREAM, 0, sockdes) (where sockdes is what we renamed pipdes to, since it’s sockets now and not pipes). The child would read from and write to sockdes[0], and the parent would read from and write to sockdes[1]. Or vice versa.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at the following piece of code: #include <stdio.h> int main(void) {
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
please take a look at the following code snippet: #include <stdio.h> int main ()
I have the following code: #include <stdio.h> int main() { printf ("hello world\n"); return
Please have a look at the following code Main.cpp #include <iostream> #include <string> using
Please have a look at the following code #include <iostream> using namespace std; int
Please have a look at the following code #include <iostream> #include <stdlib.h> using namespace
Have a look at the following code snippet... File1.h void somefunc(int); File1.c #include File1.h
Please have a look at the following code. Main.cpp #include <iostream> #include Maximum.h using
Please have a look at the following code #include <iostream> #include <stdlib.h> #include <ctime>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.