Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get two file descriptors without using pipe()

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 `

like image 234
JAN Avatar asked Dec 09 '25 21:12

JAN


1 Answers

  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.

like image 200
Aftnix Avatar answered Dec 12 '25 12:12

Aftnix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!