文件内存映射后如何在另一程序读取?及启动程序B的命令疑问
Got it, let's tackle this problem from two angles: getting the gnome-terminal launch right, and ensuring Program B can read the memory-mapped file from Program A.
Fixing the system() Call for gnome-terminal
Your initial system("/usr/bin/gnome-terminal program_B") won't work as expected—gnome-terminal doesn't treat the trailing argument as the program to run by default. You need to use the -- flag to tell the terminal to execute the following command. Here's the corrected version:
// Launch Program B in a new gnome-terminal system("/usr/bin/gnome-terminal -- ./program_B");
If you need to pass the mapped file's name to Program B (so it knows which file to access), add it as a command-line argument:
const char* mapped_file = "/path/to/your/mapped/file"; char cmd[256]; snprintf(cmd, sizeof(cmd), "/usr/bin/gnome-terminal -- ./program_B %s", mapped_file); system(cmd);
Sharing the Memory-Mapped File with Program B
The key here is understanding how file-based mmap works: when you map a file to memory with MAP_SHARED, any changes you make to the mapped region are synced back to the underlying file. Program B just needs to map the same file to its own memory space to access the same content.
Example Code for Program A
Make sure you use the MAP_SHARED flag (critical for syncing changes to the file):
#include <fcntl.h> #include <sys/mman.h> #include <sys/stat.h> #include <unistd.h> int main() { const char* filename = "/path/to/your/file"; int fd = open(filename, O_RDWR); if (fd == -1) { /* handle error */ } // Get file size struct stat st; fstat(fd, &st); size_t file_size = st.st_size; // Map file to memory (MAP_SHARED ensures changes sync to disk) void* mapped_addr = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mapped_addr == MAP_FAILED) { /* handle error */ } // Modify the mapped region (changes will sync to the file) // ... your code here ... // Launch Program B (using the corrected system call above) char cmd[256]; snprintf(cmd, sizeof(cmd), "/usr/bin/gnome-terminal -- ./program_B %s", filename); system(cmd); // Cleanup (keep mapped while Program B runs, or unmap later) // munmap(mapped_addr, file_size); // close(fd); return 0; }
Example Code for Program B
Program B just needs to open the same file and map it to its own memory:
#include <fcntl.h> #include <sys/mman.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> int main(int argc, char* argv[]) { if (argc < 2) { printf("Usage: %s <mapped-file-path>\n", argv[0]); return 1; } const char* filename = argv[1]; int fd = open(filename, O_RDONLY); // Use O_RDWR if you need to write if (fd == -1) { /* handle error */ } struct stat st; fstat(fd, &st); size_t file_size = st.st_size; // Map the same file to Program B's memory void* mapped_addr = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fd, 0); if (mapped_addr == MAP_FAILED) { /* handle error */ } // Read from the mapped region (same content as Program A's mapping) printf("Reading mapped content: %.*s\n", (int)file_size, (char*)mapped_addr); // Cleanup munmap(mapped_addr, file_size); close(fd); return 0; }
Key Notes
MAP_SHAREDvsMAP_PRIVATE: Always useMAP_SHAREDin Program A if you want changes to be visible to Program B.MAP_PRIVATEkeeps modifications private to Program A and doesn't sync them to the file.- File Permissions: Ensure Program B has read (or read-write) access to the mapped file.
- Syncing: If Program A makes frequent changes, you can use
msync()to force immediate sync to the file, butMAP_SHAREDwill handle this automatically for most cases.
内容的提问来源于stack exchange,提问作者Alejandro

