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 8238433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:48:52+00:00 2026-06-07T19:48:52+00:00

I’m trying to understand the command pipe(2) , e.g : int pipefd[2]; if (pipe(pipefd)

  • 0

I’m trying to understand the command pipe(2) , e.g :

int pipefd[2];
if (pipe(pipefd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
}

I want to get two file-descriptors with shared memory , for anonymous pipes (father & son relation).

For example this is a simple talk between father and son processes :

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <sys/wait.h>  
#include <unistd.h>    
#include <string.h>   
#define SHMSIZE 16
int main() {
   int shmid;
   char *shm;

   if(fork() == 0)   // child first

   {

      shmid = shmget(2009, SHMSIZE, 0);
      shm = shmat(shmid, 0, 0);
      char *s = (char *) shm;
      *s = '\0';
      int i;

      // child enters the input that would be stored in the shared memory
      for(i=0; i<3; i++) {
         int n;
         printf("Enter number<%i>: ", i);
         scanf("%d", &n);

         // convert the input into a c-string
         sprintf(s, "%s%d", s, n);
      }
      strcat(s, "\n");

      // display the contents of the shared memory
      printf ("I'm the child process , and I wrote:%s\n",shm);

      // detaches the shared memory segment
      shmdt(shm);
   }


   else   // parent

   {

       // get the segment
      shmid = shmget(2009, SHMSIZE, 0666 | IPC_CREAT);

      // attaching the segment to the father
      shm = shmat(shmid, 0, 0);

      // father waits for the son the finish
      wait(NULL);

      // father displays what the son wrote
      printf ("I'm the father , and my child wrote :%s\n",shm) ;

      // detaches the shared memory segment
      shmdt(shm);

      shmctl(shmid, IPC_RMID, NULL);
   }
   return 0;
}

The output is pretty simple :

Enter number<0>: 123
Enter number<1>: 567
Enter number<2>: 789
I'm the child process , and I wrote:123567789

I'm the father , and my child wrote :123567789

This is my implementation for shm_pipe_pipe() , the substitution for pipe(2) :

int shm_pipe_pipe(int spd[2])
{
    spd[0] = shmget(2009, SHMSIZE, 0);
    spd[1] = shmget(2009, SHMSIZE, 0666 | IPC_CREAT);

     if (spd[0] == -1 || spd[1] == -1)
         return -1;
     return 1;

}

My questions are :

  1. I understand that fd[0] is used for reading and fd[1] is for writing , but
    what exactly do they hold ? addresses ?

  2. The function shm_pipe_pipe that I wrote above doesn’t seem to work , what’s wrong with it ?

Thanks
`

  • 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-07T19:48:53+00:00Added an answer on June 7, 2026 at 7:48 pm
    1. Answer to your first question, they hold file descriptors. They are not addresses. They are indexes to a table. This table actually has the addresses which will be interpreted by kernel(filesystem if the descriptor is disk file, network stack if its a network socket etc). The user of the file is never given the actual address. only index to the table which contains actual addresses are given to the user. The structure of this table is pretty complex and differ in various subsystem. but the underlying concept is relatively the same.

    Following image is valid for disk files.

    This image is valid for disk files

    In case of two independent process :

    Two independent process

    In case of pipes..

    This is how Pipe works, its just two file descriptors

    This a general view of file descriptor table
    FDT

    1. Your second question, it seems that you are trying to populate the file descriptor array pipefd from values returned by shmget().

    From man 2 shmget

    shmget() returns the identifier of the shared memory segment
    associated with the value of the argument key. A new shared memory
    segment, with size equal to the value of size rounded up to a multiple
    of PAGE_SIZE, is created if key has the value IPC_PRIVATE or key isn’t
    IPC_PRIVATE, no shared memory segment corresponding to key exists, and
    IPC_CREAT is specified in shmflg.

    Its important to understand what shmget() does. It requests a rounded up size(usnig system’s PAGE_SIZE). This segment of memory is identified by a key. So kernel maintains a key vlaue table. This table also contains the address of the shared memory region. Now when you call shmat(), you query this table with the shmid and the returned address is attached with your process address space.

    So the shmid returned by shmget() is a entry in a key-value pair table maintained by system, and has nothing to do with file descriptor which is used by pipe().

    Bottom line is:

    you can’t implement pipe that way. why don’t you look at the actual implementation of pipe itself?

    http://sourceware.org/git/?p=glibc.git;a=blob;f=io/pipe.c;h=07a37ae778046e56c13e62fd2a2fa678f5546424;hb=HEAD

    Although there is nothing useful in that file, because it just calls other __pipe. You can clone a git repo and use cscope to find the actual implementation of pipe.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
i want to parse a xhtml file and display in UITableView. what is the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.