LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2006, 05:35 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
mmap


Hi all,
can anyone tell me how to implement mmap function.. with some sample application code..??
thanks...
 
Old 12-12-2006, 10:49 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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);
		}
	}		
}
 
Old 12-13-2006, 04:00 AM   #3
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
sorry... repeated...

Last edited by culin; 12-13-2006 at 04:04 AM.
 
Old 12-13-2006, 04:00 AM   #4
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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....
 
Old 12-13-2006, 11:35 AM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
Old 12-13-2006, 10:51 PM   #6
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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 ???
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Implementing mmap Kumar Programming 0 09-08-2005 03:12 AM
mmap function professional1983 Programming 1 06-14-2005 04:23 AM
mmap() error FarAway Programming 3 03-30-2005 07:38 AM
using mmap AngryLlama Programming 1 02-09-2005 08:53 AM
kiobuf vs. mmap j4r0d Programming 8 01-10-2005 03:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration