0

I have a file I stored in a structure in the segment from a different process A. Now from process B I need to get this file and convert it to bytes so I can send it or send it while reading its bytes , what would be an ideal way of doing this? see below:

typedef struct mysegment_struct_t {
    FILE *stream;
    size_t size;
}

so I have the mapping to the segment and all just not sure how to get it now

size_t bytes_sent;
struct mysegment_struct_t *fileinfo = 
(struct mysegment_struct_t *)mmap(NULL,size,PROT_READ | PROT_WRITE, MAP_SHARED, fd,0);

//read stream into a byte array? (how can this be done in c)
//FILE *f  = fopen(fileinfo->stream, "w+b");   //i a bit lost here, the file is in the segment already

//send bytes
while (bytes_sent < fileinfo->size) {
  bytes_sent +=send_to_client(buffer, size); //some buffer containing bytes?
}

I am kind of new to C programming but I cant find something like read the file in memory to a byte array for example.

Thanks

enter image description here

from blog https://www.softprayog.in/programming/interprocess-communication-using-posix-shared-memory-in-linux

there has to be a way i can share the file between processes using the shared memory.

DunkRuZ
  • 83
  • 10
  • If the data is already in a file, why not just have the other process open that file. And yes, you can share the file contents between processes as shared memory - just `mmap()` it into both processes with the `MAP_SHARED` flag. (That's all that most implementations of POSIX "shared memory" are anyway - `mmap()`'d temp files...) – Andrew Henle Mar 09 '20 at 21:24

1 Answers1

3

You simply can't do this. The pointer stream points to objects that only exist in the memory of process A, and are not in the shared memory area (and even if they were, they wouldn't typically be mapped at the same address). You're going to have to design something else.

One possibility is to send the file descriptor over a Unix domain socket, see Portable way to pass file descriptor between different processes. However, it is probably worth stepping back and thinking about why you want to pass an open file between processes in the first place, and whether there is a better way to achieve your overall goal.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • Even if i have created a shared memory segment?, could i place the bytes of the file in the shared memory segment? (this is inter process comunication via Shared Memory) – DunkRuZ Mar 09 '20 at 21:09
  • see comments in original post – DunkRuZ Mar 09 '20 at 21:12
  • You could have process A read the data into a shared mmap or shared memory segment, where process B can get it. But the `FILE` object only makes sense within the process that created it, and there is no way to share or pass it, short of some major hacking of libc internals. mmap versus POSIX shared memory doesn't make any difference. – Nate Eldredge Mar 09 '20 at 21:13
  • @DunkRuZ Is there a reason why it's not appropriate to just have each process open the file for itself? This is sounding very much like an "XY problem". – Nate Eldredge Mar 09 '20 at 21:14
  • Question, and i am still learning about this, what about saving byte information on the `segment` is that possible? and not the FILE * per se – DunkRuZ Mar 09 '20 at 21:16
  • I don't understand what you mean by "byte information". – Nate Eldredge Mar 09 '20 at 21:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209341/discussion-between-dunkruz-and-nate-eldredge). – DunkRuZ Mar 09 '20 at 21:18