Visit The New, Modern Unix Linux Community


mmap & msync


 
Thread Tools Search this Thread
Top Forums Programming mmap & msync
# 1  
mmap & msync

I really can't understand why this programm doesn't work (on linux); it open mapped memory, changes one byte and write back to disk:

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main() {
int fd, err;
void * mm;
fd = open("math.dat", O_RDWR);
mm = (char*)mmap(0, 10, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
if ((int)mm == -1) perror("mmap");

sprintf((char*) mm, "%d\n", 100);
close(fd);
err = msync(mm, 10, MS_ASYNC);
if (err == -1) perror("msync");
err = munmap(mm, 10);
if (err == -1) perror("munmap");
exit(0);
}

No errors, just no modification on disk.
TIA
marquis
# 2  
Change MAP_PRIVATE to MAP_SHARED.
# 3  
Thanks, it works now... And as I reread the manpage, I see it was stated there...
# 4  
i have the same problem and MAP_SHARED does not work. can anyone help?

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #950
Difficulty: Medium
The first academic work on the theory of self-replicating computer programs was done in 1949 by Alan Turing.
True or False?

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

mmap

I want to know whether this is possile or ever been tried out. I want to obtain a chuck of memory using mmap() I do it so : n = mmap(0, 8000, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); And hold on to that memory, when a process requests for memory, some memory is... (2 Replies)
Discussion started by: xerox
2 Replies

2. UNIX for Advanced & Expert Users

Regarding MMAP, MLOCK etc..

Hi I want to lock or prevent a portion of memory which I allocated. So I tried MLOCK, MPROTECT and some like this. But all these functions works only on page border. Can I know why that so. Is that possible to protect a portion of memory which is in middle of the page. Example. int A; ... (1 Reply)
Discussion started by: jionnet
1 Replies

3. Programming

mmap

hai, How do we map 'n' number of files into memory by using mmap system call?? Thanks in advance...... (5 Replies)
Discussion started by: andrew.paul
5 Replies

4. Homework & Coursework Questions

mmap

Descriptions: Develop a program that uses mmap() to map a file to memory space. Prepare such a file by yourself and do the follows. <LI class=MsoNormal>Display the content of the file after mapping; <LI class=MsoNormal>Output how many digits included in the file; <LI class=MsoNormal>Replace... (1 Reply)
Discussion started by: gokult
1 Replies

5. Programming

mmap()

how to use mmap() to map a file to memory space. Do you have any simple program???? Because I have to implement lot of concepts into it. (5 Replies)
Discussion started by: gokult
5 Replies

6. UNIX for Dummies Questions & Answers

mmap()

how to use mmap() to map a file to memory space. Do you have any simple program???? Because I have to implement lot of concepts into it. (3 Replies)
Discussion started by: gokult
3 Replies

7. Linux

"Invalid Argumemt" in msync.

Whne I try to execute following code then get an erro message that "Invalid Argumemt" in msync. #include <stdio.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/errno.h> #include <sys/time.h> #include <fcntl.h> #include <stdio.h>... (0 Replies)
Discussion started by: darshan.ghumare
0 Replies

8. UNIX and Linux Applications

Set up Msync

Hi guys,I'M a newbe and trying to set up msync. I run Ubuntu on my lap top and have got a sony-ericsson w910i. All I like to do is sync calendar/contacts with evolution. So I have installed multisync 0.82 and tried to sync my phone with evolution via bluetooth. However it seems to get stuck at... (0 Replies)
Discussion started by: sn0m
0 Replies

9. UNIX for Advanced & Expert Users

mmap and select

I'm using select() to monitor multiple file descriptors (inet sockets) in application. But this application must also collaborate with other applications on the same host via shared memory (mmap'ed file) due to performance reasons. How can I become notification that mmaped memory is changed or... (1 Reply)
Discussion started by: Hitori
1 Replies

10. Filesystems, Disks and Memory

mmap

Hello. I'm writing some random access i/o software on Solaris 8 using mmap64 to memory map large files (my test file is ~25 GB). The abbreviated code fragment is: fd = open(cbuf,O_RDONLY|O_LARGEFILE); struct stat statbuf; fstat(fd,&statbuf); off_t len =... (0 Replies)
Discussion started by: gusm
0 Replies

Featured Tech Videos