- 论坛徽章:
- 0
|
如何在ucLinux使用mmap() ?
uClinux: 2.4.22, CPU: ARM9-based 32-bit RISC CPU, 166MHz
now I write a single programe which open() & mmap() a regular file, read and modify it.
but after the programe have finished, the file's content has not changed.
the code likes below:
/*--------------------------------------------------------------------------------
int main( int argc, char **argv)
{
char *p_str;
struct stat stStat;
iFd = open( "/tmp/aa.txt", O_RDWR | O_NONBLOCK);
if( iFd == -1)
{
fprintf( stderr, "%s.%d \n", __FILE__, __LINE__);
perror( "open faild :"
abort();
}
iRetSts = fstat( iFd, &stStat);
if( iRetSts == -1)
{
fprintf( stderr, "%s.%d \n", __FILE__, __LINE__);
perror( "fstat error:"
return -1;
}
p_str = mmap( NULL,
stStat.st_size,
PROT_READ ,
MAP_SHARED,
iFd,
0);
if( p_str == (char *)-1)
{
fprintf( stderr, "%s.%d \n", __FILE__, __LINE__);
perror( "mmap failed ;"
abort();
}
printf( "p_str[0] = %c \n", p_str[0]);
p_str[0] = 'A';
return 0;
}
/*--------------------------------------------------------------------------------------
If call mmap() with PROT_WRITE, the mmap() will return -1, with "invalidate argument".
If call mmap() without PROT_WRITE, the mmap() will work correctly, but after the programe
terminated , the content of the /tmp/aa.txt has not changed.
the /tmp is RAMFS filesystem.
please help me. thank you very much. |
|