Home > OS >  mmap transfer file data from one process to other process
mmap transfer file data from one process to other process

Time:12-06

I try to transfer a data file from one process to another with mmap function,

I took this code from one of the websites that one process initial values to the array list and the other Updated values of the array elements by multiplying it by 10

I don't get how I can use mmap to transfer file data from one process to another I talk about big data file around 50MB

Thanks for any help in this issue

Here is my CODE:

    #include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/mman.h>

int main(){

    int N=5; // Number of elements for the array

    int *ptr = mmap(NULL,N*sizeof(int),
                    PROT_READ | PROT_WRITE,
                    MAP_SHARED | MAP_ANONYMOUS,
                    0,0);

    if(ptr == MAP_FAILED){
        printf("Mapping Failed\n");
        return 1;
    }

    for(int i=0; i < N; i  ){
        ptr[i] = i   1;
    }

    printf("Initial values of the array elements :\n");
    for (int i = 0; i < N; i   ){
        printf(" %d", ptr[i] );
    }
    printf("\n");

    pid_t child_pid = fork();

    if ( child_pid == 0 ){
        //child
        for (int i = 0; i < N; i  ){
            ptr[i] = ptr[i] * 10;
        }
    }
    else{
        //parent
        waitpid ( child_pid, NULL, 0);
        printf("\nParent:\n");

        printf("Updated values of the array elements :\n");
        for (int i = 0; i < N; i   ){
            printf(" %d", ptr[i] );
        }
        printf("\n");
    }

    int err = munmap(ptr, N*sizeof(int));

    if(err != 0){
        printf("UnMapping Failed\n");
        return 1;
    }
    return 0;
}

CodePudding user response:

The mmap function is used to create a memory mapping between a process' virtual memory and a file on disk. In your code, you are using mmap to create a shared memory region between the parent and child processes. This shared memory region is used to transfer data between the two processes.

In your code, the parent process initializes the values in the array by writing to the shared memory region. The child process then updates the values in the array by multiplying them by 10 and writing the new values back to the shared memory region. Finally, the parent process reads the updated values from the shared memory region and prints them.

To transfer a data file from one process to another using mmap, you can follow a similar approach. First, the parent process would open the data file and create a memory mapping using mmap. The parent process would then write the contents of the data file to the memory mapping. The child process would then read the contents of the memory mapping and write them to a new file. Finally, the parent process would unmap the memory region and close the data file.

Here is an example of how this could be implemented:

#include <stdio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
    // Open the data file and get its size
    int fd = open("data.txt", O_RDONLY);
    struct stat st;
    fstat(fd, &st);
    size_t file_size = st.st_size;

    // Create a memory mapping for the data file
    char *ptr = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fd, 0);

    if (ptr == MAP_FAILED) {
        printf("Mapping Failed\n");
        return 1;
    }

    // Fork a child process
    pid_t child_pid = fork();

    if (child_pid == 0) {
        // Child process

        // Open a new file to write the data to
        int new_fd = open("new_data.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);

        // Write the contents of the memory mapping to the new file
        write(new_fd, ptr, file_size);

        // Close the new file
        close(new_fd);
    } else {
        // Parent process

        // Wait for the child process to finish
        waitpid(child_pid, NULL, 0);

        // Unmap the memory region and close the data file
        munmap(ptr, file_size);
        close(fd);
    }

    return 0;
}

Note that this is just an example to illustrate how mmap can be used to transfer data from one process to another. In a real-world application, you may want to add error handling and other logic to ensure that the data transfer is performed safely and efficiently.

  • Related