LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   mmap implementation (https://www.linuxquestions.org/questions/programming-9/mmap-implementation-423279/)

ash_lly 03-09-2006 04:24 PM

mmap implementation
 
Hi..fellows, am new to linux so please bare with me.

Iam trying to use mmap() to perform IO and to have the file updated...but I cannot seem to make write to the file.
heres my code..
---------------

//to create(if not there) or open file and truncate)
int file = open("input",O_RDWR|O_TRUNC);
if (file < 0)
{
fprintf(stderr,"FAiled");
return -1;
}
else
{
//to map file
char* buff = (char*)mmap(0,page_size,PROT_READ|PROT_WRITE,MAP_SHARED,file,0);
buff = (char*)"ABC";
printf("%s",buff); //it outputs to screen ABC
if (msync(0,page_size,MS_SYNC) == -1)
printf("%s",str_error[errno]);//it gives a message CANNOT ALLOCATE MEMORY
munmap(buff,page_size);
close(file);

---------------------
i seem to be able to write to buff but it does not update the file

jlliagre 03-09-2006 04:32 PM

You are not writing to buff, just replacing the location it points to and loosing the mmaped area in doing that.

ash_lly 03-09-2006 04:48 PM

instead of
buff = (char*)"ABC";
now i have used
buff[0] = 'A';
printf("%c",buff[0]); //NO OUTPUT TO SCREEN
now still get the CANNOT ALLOCATE MEMORY error with msync() but the file is still not updated (still 0 bytes)

ash_lly 03-09-2006 04:49 PM

BTW thanks a lot for replying

jlliagre 03-10-2006 01:16 AM

You give msync a null pointer instead of buff, the reason why you got an error.

Your printf doesn't reach your screen because you do not flush stdout (either by calling fflush or with a \n in the format string).


All times are GMT -5. The time now is 04:25 AM.