LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-09-2006, 04:24 PM   #1
ash_lly
LQ Newbie
 
Registered: Mar 2006
Posts: 9

Rep: Reputation: 0
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
 
Old 03-09-2006, 04:32 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,781

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
You are not writing to buff, just replacing the location it points to and loosing the mmaped area in doing that.
 
Old 03-09-2006, 04:48 PM   #3
ash_lly
LQ Newbie
 
Registered: Mar 2006
Posts: 9

Original Poster
Rep: Reputation: 0
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)

Last edited by ash_lly; 03-09-2006 at 05:35 PM.
 
Old 03-09-2006, 04:49 PM   #4
ash_lly
LQ Newbie
 
Registered: Mar 2006
Posts: 9

Original Poster
Rep: Reputation: 0
BTW thanks a lot for replying
 
Old 03-10-2006, 01:16 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,781

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
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).
 
  


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
Implementing mmap Kumar Programming 0 09-08-2005 03:12 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 02:22 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