LinuxQuestions.org
Review your favorite Linux distribution.
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-20-2004, 07:58 PM   #1
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Rep: Reputation: 30
mmap problem


hi

i created a programme who use a library.
the library talk to a driver.

the library need to write in shared memory between the application and kernel.

i try to use mmap.

in my application, i do:

Code:
static const int _MAXMSGSIZE = 256;
static const int _MAXMSGNB = 256;
int fd;

static char *mapdev (const char *, int, int);

int main (int argc, char **argv)
{
	unsigned int offset, size, i;
	char *addr, *addr1, *maddr;

	if (argc != 4)
	{
		fprintf (stderr, "Usage: mapcmp dev1 offset pages\n");
		exit (1);
	}
 	size = _MAXMSGSIZE * _MAXMSGNB;
	
 	maddr = malloc(size);
	
	offset = 0;
	
	addr = mapdev (argv[1], offset, size);
	
	addr1 = addr;

	
	for (i = 0; i < size; i++){
		*addr1 = "a";
		addr1++;
	}
	
	addr1 = addr;
	
	
	for (i = 0; i < size; i++){
		printf("%c",*addr1); 
		addr1++;
	}

	munmap(addr,size);
	close(fd);
	
	exit (0);
}

static char *mapdev (const char *dev, int offset, int size)
{
	char *addr;
	fd = open (dev, O_RDWR);

	printf("Va utiliser mmap1\n");
	
	if (fd < 0)
	{
		perror (dev);
		return (1);
	}
	printf("Va utiliser mmap2\n");
	
	addr = mmap (0, size, PROT_READ, MAP_PRIVATE, fd, offset);
	if (addr == MAP_FAILED)
	{
		perror (dev);
		return (1);
	}
	printf ("Mapped %s (%d @ %d) at 0x%p\n", dev, size, offset, addr);
	return (addr);
}
they are a problem when i do:

Code:
*addr1 = "a";
the machine freeze

any idea?
 
Old 06-20-2004, 08:12 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I don't know if this will solve your problem, but you're trying to assign a string to a character.

*addr1 is the location of a single character in memory.
"a" is a string (not a single character; use single quotes to specify a character)

Try:
Code:
*addr1 = 'a';
 
Old 06-20-2004, 09:14 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
when u say 'the machine freezes', do u mean the entire machine hangs and u have to reboot?? if so, can u plz tell me what device it is you are trying to open?
 
Old 06-21-2004, 03:39 PM   #4
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: openSUSE Tumbleweed
Posts: 209

Original Poster
Rep: Reputation: 30
no not need to reboot... i can kill the application

i got the same problem, Dark_Helmet with your solution
 
Old 06-21-2004, 05:24 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you are only specifying PROT_READ, and u r trying to write to the memory.
 
  


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
gdb and mmap dcfnef Programming 8 09-30-2005 10:25 AM
mmap function professional1983 Programming 1 06-14-2005 04:23 AM
using mmap AngryLlama Programming 1 02-09-2005 08:53 AM
kiobuf vs. mmap j4r0d Programming 8 01-10-2005 03:18 AM
can I use mmap with sockets? kanth Linux - Networking 0 02-28-2004 05:11 AM

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

All times are GMT -5. The time now is 12:46 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