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 :
-
I understand that
fd[0]is used for reading andfd[1]is for writing , but
what exactly do they hold ? addresses ? -
The function
shm_pipe_pipethat I wrote above doesn’t seem to work , what’s wrong with it ?
Thanks
`
Following image is valid for disk files.
In case of two independent process :
In case of pipes..
This a general view of file descriptor table

pipefdfrom values returned byshmget().From
man 2 shmgetIts 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 callshmat(), you query this table with theshmidand the returned address is attached with your process address space.So the
shmidreturned byshmget()is a entry in a key-value pair table maintained by system, and has nothing to do with file descriptor which is used bypipe().Bottom line is:
you can’t implement
pipethat way. why don’t you look at the actual implementation ofpipeitself?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 ofpipe.