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

  • Home
  • SEARCH
  • 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 8604583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:36:31+00:00 2026-06-12T02:36:31+00:00

I have a simple Linux C program that I’m writing to help me better

  • 0

I have a simple Linux C program that I’m writing to help me better understand IPC, right now I’m trying to build it with pipes.
I have a single code base that I run in two different terminal windows as two different executables (so they can talk to each other). However I’m not doing something correct, because I never get any data to read, but I’m not sure what…

NOTE This is not the full code, I chopped out the output/input/validation to save space. But it’s noted in the comments in the program below.

void main()
{
  int pipefd[2], n;
  char input = 0;
  char buffer[100] = {0};
  char outpipe[100] = {0};

  if(pipe(pipefd) < 0) {
    printf("FAILED TO MAKE PIPES\n");
    return;
  }

  printf("Starting up, read fd = %d, write fd = %d\n", pipefd[0],pipefd[1]);

  do {
    //print menu options (send message, get message, get my fd, 
    // set a fd to talk to, quit)

    // if "send a message":
    {
      printf("What would you like to send?\n");
      fgets(buffer, 100, stdin);
      write(pipefd[1], buffer, strlen(buffer));
    }
    //else if "read a message":
    {
      if(open(outpipe, 0) < 0)
          printf("Couldn't open the pipe!\n");
      else {
        n = read(outpipe, buffer, 100);
        printf("I got a read of %d bytes\nIt was %s\n",n, buffer);
        close(outpipe);
      }
    }
    //else if "get my file descriptor":
      printf("My fd tag is: /proc/%d/fd/%d\n", (int)getpid(), pipefd[0]);
    //else if "set a file descriptor to talk to":
    {
      printf("What is the pipe's file descriptor?\n");
      fgets(outpipe, 100, stdin);
      n = strlen(outpipe) - 1;
      outpipe[n] = '\0';
    }
  } while (input != 'Q');
return;
}

I know the pipes are created successfully, I verified the file descriptors are in place:

lr-x------ 1 mike users 64 Sep 26 23:31 3 -> pipe:[33443]
l-wx------ 1 mike users 64 Sep 26 23:31 4 -> pipe:[33443]

Looks like the permissions are OK (read on pipe 3, write on pipe 4).

I use it as such:

//terminal 1
Pick an option:
3
My fd tag is: /proc/8956/fd/3

//terminal 2
Pick an option:
4
What is the pipe's file descriptor?
/proc/8956/fd/3

Pick an option:
1
What would you like to send?
hello

//terminal 1
Pick an option:
2
I got a read of -1 bytes
It was 

Is there anything obviously wrong that I’m doing here? My reads always get “-1” return value…

  • 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-06-12T02:36:32+00:00Added an answer on June 12, 2026 at 2:36 am

    It seems you have misunderstood how pipe works. A pipe is an anonymous file descriptor that is not going by file in the file system. The files in /proc/<pid>/fd you don’t have to care about.

    Here is a rewrite of what you are trying to do:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    int main(void)
    {
        int pipefds[2];
        char input[128];
        char output[128];
        ssize_t nread;
    
        if (pipe(pipefds) == -1)
        {
            perror("Could not create pipe");
            return EXIT_FAILURE;
        }
    
        printf("Enter input: ");
        if (fgets(input, sizeof(input), stdin) == NULL)
        {
            perror("Could not read input");
            return EXIT_FAILURE;
        }
    
        /* "Remove" newline from input */
        if (input[strlen(input) - 1] == '\n')
            input[strlen(input) - 1] = '\0';
    
        /* Now write the received input to the pipe */
        if (write(pipefds[1], input, strlen(input) + 1) == -1)
        {
            perror("Could not write to pipe");
            return EXIT_FAILURE;
        }
    
        /* Now read from the pipe */
        if ((nread = read(pipefds[0], output, sizeof(output))) == -1)
        {
            perror("Could not reaf from pipe");
            return EXIT_FAILURE;
        }
    
        /* We don't need to terminate as we send with the '\0' */
    
        printf("Received: \"%s\"\n", output);
    
        return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying simple buffer overflow attacks in linux. I have a vulnerable program
I have a simple OpenGL program using SDL on Linux that just attempts to
I have implemented a simple linux shell in c. Now, I am adding some
I have a made a simple program that reproduces the problem: #include <boost/archive/text_oarchive.hpp> #include
I have a C program that I'm developing using Ubuntu 11.10 (Linux version 3.0.0-12-generic-pae
I have a simple test program that when I run I get: ./hello: error
I have one simple question about signals in Linux systems. As I understand every
I have a program that generates and outputs a sequence of simple sample math
I have to program a simple polymorphic engine. I use linux (32-bit) and i
I have written a simple client server program in C under linux. I have

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.