LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 06-06-2014, 12:12 AM   #1
Mallem kondaiah
LQ Newbie
 
Registered: Apr 2013
Location: bangalore
Posts: 6

Rep: Reputation: Disabled
How to read/write using mmap function with out using file


Hi, i am trying to read and write on a particular memory address using mmap function.with my write code i am able to write data on to a particular memory but with my read code i am not able to read data on that particular memory address.i am using MAP_ANONYMOUS in mmap API with out using of file If any one knows about mmap read/write on a particular memory address help me below i am mentioning those read/write files i have used.

Code:
WRITE.c
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define FILE_LENGTH 64
int main (int argc, char* const argv[])
{
int fd;
int i;
char buffer[10];
void* file_memory;
	for(i=0;i<10;i++)
	{
         buffer[i]='A';
      }
write (0, buffer, 10);
file_memory = mmap (0x5F000000,FILE_LENGTH, PROT_WRITE,MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	  if (file_memory == MAP_FAILED) {
	perror("Error mmapping the file");
	exit(EXIT_FAILURE);
    }
sprintf((char*) file_memory, "%d\n", 10);
munmap (file_memory, FILE_LENGTH);
return 0;
}
[
Code:
B]READ.c[/B]
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>

int main(int argc, char *argv[])
{
    int i;
    int fd;
    int *map;  /* mmapped array of int's */
	map = mmap (0x5F000000, 64, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
	printf(" map=%d\n", map);
	//map=1;

    if (map == MAP_FAILED) {
	perror("Error mmapping the file");
	exit(EXIT_FAILURE);
    }	

	printf("read data %s\n",map);

    if (munmap(map, 64) == -1) {
	perror("Error un-mmapping the file");
    }
    return 0;
}
Code:
Code:
out put of read.c 
read data
you can say memory sharing between two process only. i wanted to write data on to 0x5F000000 address location and read that same 0x5F000000 address location using mmap with out using file
 
Old 06-06-2014, 07:14 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
You've got 2 separate programs both with main() so I suspect you intended to use shared memory.

http://stackoverflow.com/questions/5...ith-linux-in-c
 
  


Reply



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
How to read/write using mmap function with out using file Mallem kondaiah Programming 2 06-06-2014 09:00 AM
mmap is not faster than read/write ??? muggabug Programming 10 11-02-2012 10:33 AM
too many arguments to function 'read'/'write' dynaudio Linux - Kernel 11 12-29-2009 02:46 AM
socket programming function to read/write integers? ashlesha Linux - Networking 4 04-25-2007 10:45 PM

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

All times are GMT -5. The time now is 07:25 PM.

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