1

after mapping the file to memory, how do i read the file from another program?

size_t getFilesize(const char* filename) { ...... }
int main(int argc, char** argv) {
    size_t filesize = getFilesize(argv[1]);
    int fd = open(argv[1], O_RDONLY, 0);
    void* mmappedData = mmap(NULL, filesize, PROT_READ, MAP_PRIVATE | MAP_POPULATE, fd, 0);

    system("/usr/bin/gnome-terminal program_B")
}

so that the file mapped in program A can be shown in a new pop-up terminal(exec program B)

also i don't know if the system(".......") is correct or not

danglingpointer
  • 4,708
  • 3
  • 24
  • 42
Alejandro
  • 11
  • 4
  • 1
    Above provided code is C not C++. Please remove the tag that is not relevant to the code you posted or else you will get down votes for this. – danglingpointer May 28 '18 at 06:21
  • 2
    You don't normally do it that way. If the other program needs the file, it should open and read (or memory map) the file. Otherwise, you could play with shared memory (but the other program has to attach to it), or various other tricks, but fundamentally, you don't share data that way — especially not with `MAP_PRIVATE`. – Jonathan Leffler May 28 '18 at 06:22
  • `/usr/bin/gnome-terminal program_B name_of_file`? – user253751 May 28 '18 at 06:27
  • In general, you deal with processes here. Those got separated virtual address spaces and the OS/MMU is trying hard to keep them separated. While means exist to share memory over processes (look at https://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c e.g.), I would not advice for it. Moreover, the technique discussed in the linked article does not work for you either. If you want processes, use some other mean of *interprocess communication* like sockets, pipes... To exchange data. – Michael Beer May 28 '18 at 07:10

0 Answers0