473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Problem about mmap()!

Hi everyone!

I found that if you want to write to the memory got by mmap(), you have
to get the file descriptor for mmap() in O_RDWR mode. If you got the
file descriptor in O_WRONLY mode, then writing to the memory got by
mmap() will lead to segmentation fault. Anyone knows why? Is this a rule
or a bug?

What if I just want to write to the file and nothing else?

Please, look at my tiny program, I wrote this to test mmap(), it is cute.

#include <stdio.h>

typedef struct{
char name[4];
int age;
} people;

int main(int argc, char** argv) {
int fd, i;
people *p_map;
char temp;

fd = open(argv[1], O_RDWR|O_TRUNC); //this works fine

//fd = open(argv[1], O_WRONLY|O_TRUNC); this will cause segmentation fault

if (fd == -1) {
perror("open()");
}
lseek(fd, sizeof(people)*5-1, SEEK_SET);
write(fd, " " , 1);

p_map = (people *)mmap(NULL,sizeof(people)*5,PROT_WRITE,MAP_SHARED ,fd,0);
close(fd);

memset(p_map, '\0', sizeof(people)*5);

temp = 'b';
for(i=0; i<5; i++) {
temp++;
//(p_map+i)->name = "aaa";
//memcpy((p_map+i)->name, &temp, 2);
memset((p_map+i)->name, temp, 3);
(p_map+i)->age = 20+i;
}
printf("initialize over\n");
for(i=0; i<5; i++) {
printf("%s\t%d\n", (p_map+i)->name, (p_map+i)->age);
}
sleep(10);

munmap(p_map, sizeof(people)*5);
printf( "umap ok\n" );

return 0;
}
Nov 14 '05 #1
4 3504
On Tue, 2004-08-24 at 09:52 +0800, Hao Xu wrote:
Hi everyone!

I found that if you want to write to the memory got by mmap(), you have
to get the file descriptor for mmap() in O_RDWR mode. If you got the
file descriptor in O_WRONLY mode, then writing to the memory got by
mmap() will lead to segmentation fault. Anyone knows why? Is this a rule
or a bug?


That is highly implementation-defined. You would probably be much more
likely to get a sensible reply if you post to the newsgroup dealing with
your operating system and/or hardware platform.

Fredrik Tolf
Nov 14 '05 #2
Hao Xu <tr*******@vip.sina.com> wrote in message news:<cg***********@mail.cn99.com>...
Hi everyone!

I found that if you want to write to the memory got by mmap(), you have
to get the file descriptor for mmap() in O_RDWR mode. If you got the
file descriptor in O_WRONLY mode, then writing to the memory got by
mmap() will lead to segmentation fault. Anyone knows why? Is this a rule
or a bug?

What if I just want to write to the file and nothing else?

Please, look at my tiny program, I wrote this to test mmap(), it is cute.

#include <stdio.h>

typedef struct{
char name[4];
int age;
} people;

int main(int argc, char** argv) {
int fd, i;
people *p_map;
char temp;

fd = open(argv[1], O_RDWR|O_TRUNC); //this works fine

//fd = open(argv[1], O_WRONLY|O_TRUNC); this will cause segmentation fault

if (fd == -1) {
perror("open()");
}
lseek(fd, sizeof(people)*5-1, SEEK_SET);
write(fd, " " , 1);

p_map = (people *)mmap(NULL,sizeof(people)*5,PROT_WRITE,MAP_SHARED ,fd,0);
close(fd);

memset(p_map, '\0', sizeof(people)*5);

temp = 'b';
for(i=0; i<5; i++) {
temp++;
//(p_map+i)->name = "aaa";
//memcpy((p_map+i)->name, &temp, 2);
memset((p_map+i)->name, temp, 3);
(p_map+i)->age = 20+i;
}
printf("initialize over\n");
for(i=0; i<5; i++) {
printf("%s\t%d\n", (p_map+i)->name, (p_map+i)->age);
}
sleep(10);

munmap(p_map, sizeof(people)*5);
printf( "umap ok\n" );

return 0;
}


on UNIX/LINUX machine your mmap system call will fail if you open the
file
in O_WRONLY mode and try to map that file with PROT_WRITE set for
MAP_SHARED
mapping. See the man page of mmap.
so you are trying to write at some invalid address, thats why SIGSEGV.

i would advise you that after every system call, you *must* always
check whether your system call was successful or not.
Nov 14 '05 #3
Hao Xu wrote:

Hi everyone!

I found that if you want to write to the memory got by mmap(),
Mmap() is not part of ISO standard C, so this question is
off topic here.
you have
to get the file descriptor for mmap() in O_RDWR mode. If you got the
file descriptor in O_WRONLY mode, then writing to the memory got by
mmap() will lead to segmentation fault. Anyone knows why? Is this a rule
or a bug?


If you open the file write only and then read from it, I
would expect that it should not work. How it "doesn't
work" is probably unspecified and a segfault is not an
unreasonable version of "doesn't work".

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it's valid)
+-----------------------------------------------------------+
`If you want a vision of the future, it is a wireless broadband
network feeding requests for foreign money-laundering assistance
into a human temporal lobe, forever. With banner ads.'
-- John M. Ford
Nov 14 '05 #4
junky_fellow wrote:
Hao Xu <tr*******@vip.sina.com> wrote:

I found that if you want to write to the memory got by mmap(),
you have to get the file descriptor for mmap() in O_RDWR mode.
If you got the file descriptor in O_WRONLY mode, then writing
to the memory got by mmap() will lead to segmentation fault.
Anyone knows why? Is this a rule or a bug?
.... snip ...
on UNIX/LINUX machine your mmap system call will fail if you open
the file in O_WRONLY mode and try to map that file with PROT_WRITE
set for MAP_SHARED mapping. See the man page of mmap. so you are
trying to write at some invalid address, thats why SIGSEGV.

i would advise you that after every system call, you *must* always
check whether your system call was successful or not.


Please don't answer off-topic questions here other than to
redirect them to an appropriate newsgroup. Especially don't
answer them with more off-topic data, which won't be properly
evaluated because the knowledgeable people aren't here.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Parzival | last post by:
I have a program which works well on under Windows, which I am trying to get to work under Linux (Mandrake 9.1) It accesses a binary file with mmap. Under Windows, the file maps and reads withut...
5
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
6
by: Nick | last post by:
excuse me!! may i ask a simple problem here? if i dynamically allocate a memory(ex. new in C++ or malloc in C) in a sub-function and forget free the space end of the sub-function. does it...
1
by: Hao Xu | last post by:
Hi everyone! I found that if you want to write to the memory got by mmap(), you have to get the file descriptor for mmap() in O_RDWR mode. If you got the file descriptor in O_WRONLY mode, then...
2
by: beejisbrigit | last post by:
Hi there, I was wondering if anyone had experience with File I/O in Java vs. C++ using mmap(), and knew if the performance was better in one that the other, or more or less negligible. My...
1
by: koara | last post by:
Hello all, i am using the mmap module (python2.4) to access contents of a file. My question regards the relative performance of mmap.seek() vs mmap.tell(). I have a generator that returns...
2
by: Neal Becker | last post by:
On linux, I don't understand why: f = open ('/dev/eos', 'rw') m = mmap.mmap(f.fileno(), 1000000, prot=mmap.PROT_READ|mmap.PROT_WRITE, flags=mmap.MAP_SHARED) gives 'permission denied', but...
0
by: Kris Kennaway | last post by:
If I do the following: def mmap_search(f, string): fh = file(f) mm = mmap.mmap(fh.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ) return mm.find(string) def mmap_is_in(f, string): fh =...
0
by: Gabriel Genellina | last post by:
En Thu, 29 May 2008 19:17:05 -0300, Kris Kennaway <kris@FreeBSD.org> escribió: Looks like you should define the sq_contains member in mmap_as_sequence, and the type should have the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.