LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   mmap (https://www.linuxquestions.org/questions/programming-9/mmap-509667/)

culin 12-12-2006 05:35 AM

mmap
 
Hi all,
can anyone tell me how to implement mmap function.. with some sample application code..??
thanks...

jim mcnamara 12-12-2006 10:49 AM

This is some older code - note the open flags on the file. You cannot write the buffer back to the file without closing the fd first
Code:

/******************************
* filemap.c
* provides a memory mapped file to caller
*
*******************************/
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>

/* returns the size in bytes of the file */

size_t filesize(int fd)
{
        struct stat st;
        if(fstat(fd,&st)==(-1))
        {
                perror("Cannot stat input file");
                exit(EXIT_FAILURE);
        }
        return st.st_size;
}
/***********************
* mapfile maps a file into memory
* arg:
*    filename - name of the file to map
*    fd - files descriptor - mapfile sets this to an open fd
*    filelen - length of file - mapfile sets this to len of file in bytes
* returns:
*    pointer to file (byte 0 of file) in memory
*
*    any changes made to file in memory are not reflected in the physical file.
***************************/
caddr_t *mapfile(const char *filename, int *fd, size_t *filelen)
{
    int filedesc=0;
    size_t len=0;
    caddr_t *mapptr;
        if( (filedesc=open(filename,O_RDONLY))<0)
        {
                perror("Input file open failure");
                exit(EXIT_FAILURE);
        }
        *fd=filedesc;
        len=filesize(filedesc);       
        *filelen=len;
        mapptr=(caddr_t *)mmap(NULL,
                              len,
                              PROT_READ|PROT_WRITE,
                              MAP_PRIVATE, 
                              filedesc,
                              0);
        if(mapptr==MAP_FAILED)
        {
                perror("Memory mapping of file failed.");
                exit(EXIT_FAILURE);
        }   
        return mapptr;
}

/* close files and release memory */
void mapcleanup(caddr_t *ptr, int fd, size_t filelen )
{
        if(munmap(ptr,filelen)==(-1))
        {
                perror("Error releasing memory");
                exit(EXIT_FAILURE);
        }
        while(close(fd)==(-1))
        {
                if(errno=EINTR)
                {
                        continue;
                }
                else
                {
                        perror("File I/O Error");
                        exit(EXIT_FAILURE);
                }
        }               
}


culin 12-13-2006 04:00 AM

sorry... repeated...

culin 12-13-2006 04:00 AM

wow thanks a lot jim mcnamara...

I am thinking of implementing mmap system call in my driver. I need to map kernal buffer which is allocated by kmalloc to user space. I am able to map the buffer area but i could not read from that buffer in the user space....Will u please in this regard with a sample code of driver.
thanks again....

jim mcnamara 12-13-2006 11:35 AM

If what I posted is news to you, then writing kernel mode code or drivers may also be interesting for you.

Have you considered reading 'Linux Device Drivers' by Corbet, Rubini & Kroah-Hartman?

Or some other primary material like that? Using mmap is covered in
http://www.advancedlinuxprogramming.com/alp-folder Advanced Linux Programming - a free online book.

culin 12-13-2006 10:51 PM

thanks jim mcnamara...
ya ya i have referred the same book, linux device drivers by Rubini.. i have implemented the code referring to that book.. but when is try to read the data from the user space i am not getting the actual data..that is the problem.... and also in the link given the only application code is implemented... so i am searching the driver side code for that...can u help me ???


All times are GMT -5. The time now is 02:09 AM.