473,657 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mmap, why do not work?

sr

I have this kind of a code.
It is from a book Linux programming,
unleashed.
I am not able to make to work so that, when it is run in two different
xterm windows, the two different processes would affect to each others.
/* shared_mem.c
* Copyright Mark Watson 1998. Open Source Software License
*
* NOTE: You must allocate shared memory at boot time. Add
* the following to /etc/lilo.config:
*
* image=/vmlinux (this should already be in
lilo.config)
* append="mem=63m " (if you have 64m of physical memory)
*/

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

#define ADDRESS (63*0x100000)
void main() {
char *mem_pointer;
int f, i;
if ((f=open("/dev/mem", O_RDWR)) < 0) {
printf("Error opening /dev/mem\n");
exit(1);
}
mem_pointer = (char *)mmap(0, 8192, PROT_READ | PROT_WRITE,
MAP_FILE | MAP_SHARED, f, ADDRESS);

for (i=0; i<10; i++) {
printf("Test iteration %d\n", i);
printf("first two bytes: %d %d\n", mem_pointer[0], mem_pointer[1]);
mem_pointer[0] = 2*i;
mem_pointer[1] = 3*i;
printf("first two bytes: %d %d\n", mem_pointer[0], mem_pointer[1]);
system("sleep 3");
}

// use it here..

// close up:
munmap(mem_poin ter, 8192);
}

Does some one know, why that program do not work? (mmap)
(Or do I use it incorrectly?)

The output is all the time like this, even if the same program is run
in two xterm windows at the same time:
[ shared]# ./shared_mem
Test iteration 0
first two bytes: 0 0
first two bytes: 0 0
Test iteration 1
first two bytes: 0 0
first two bytes: 2 3
Test iteration 2
first two bytes: 2 3
first two bytes: 4 6
Test iteration 3
first two bytes: 4 6
first two bytes: 6 9
Test iteration 4
first two bytes: 6 9
first two bytes: 8 12
Test iteration 5
first two bytes: 8 12
first two bytes: 10 15
Test iteration 6
first two bytes: 10 15
first two bytes: 12 18
Test iteration 7
first two bytes: 12 18
first two bytes: 14 21
Test iteration 8
first two bytes: 14 21
first two bytes: 16 24
Test iteration 9
first two bytes: 16 24
first two bytes: 18 27
I was able to communicate through shared memory, with these examples:
http://www.cs.cf.ac.uk/Dave/C/node27...00000000000000
files:
shm_server.c
shm_client.c
but those examples use shmget, shmat etc.
Those do not use mmap.

Thank you!
br, a coder

Feb 25 '06 #1
2 2253
sr wrote:
I have this kind of a code.
It is from a book Linux programming,
unleashed.
I am not able to make to work so that, when it is run in two different
xterm windows, the two different processes would affect to each others.
/* shared_mem.c
* Copyright Mark Watson 1998. Open Source Software License
*
* NOTE: You must allocate shared memory at boot time. Add
* the following to /etc/lilo.config:
*
* image=/vmlinux (this should already be in
lilo.config)
* append="mem=63m " (if you have 64m of physical memory)
*/

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


You've left the realm of ISO standard C, making you off topic here. Try
news:comp.progr amming or news:comp.os.li nux.development .apps.

HTH,
--ag
[snip]

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Feb 25 '06 #2
Artie Gold wrote:
sr wrote:
I have this kind of a code.
It is from a book Linux programming,
unleashed.
I am not able to make to work so that, when it is run in two different
xterm windows, the two different processes would affect to each others.
/* shared_mem.c
* Copyright Mark Watson 1998. Open Source Software License
*
* NOTE: You must allocate shared memory at boot time. Add
* the following to /etc/lilo.config:
*
* image=/vmlinux (this should already be in
lilo.config)
* append="mem=63m " (if you have 64m of physical memory)
*/

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

You've left the realm of ISO standard C, making you off topic here. Try
news:comp.progr amming or news:comp.os.li nux.development .apps.

^^^^^^^^^^^^^^^ ^^^^^^
Should have been news:comp.unix. programmer.

HTH now,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
Feb 25 '06 #3

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

Similar topics

1
3974
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 porblems, but under linux an attempt to access the mapped data fails with a cryptic error message. I thought that perhaps the file was corrupted, so I recreated it in my Linux environment and examined it with a hex editor, and it appears to be...
4
3529
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 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?
7
3099
by: Michael | last post by:
I'm writing an application that decodes a file containing binary records. Each record is a particular event type. Each record is translated into ASCII and then written to a file. Each file contains the same events. At the moment each record is processed one after the other. It taks about 1m40s to process a large file containing 70,000 records. Would my application benifit from multiple threads and mmap? If so what is the best way to...
1
2170
by: Carl Mackey | last post by:
hi, i'm new to this list and new to python as well. i have a question on the memory mapped file ability python has. when i use a mmap on a file, will it copy the whole thing to ram or just whatever part of it i'm working on? basically, i'm wondering if it would be ok for me to have multiple mmap's open on very large files as i read or write from them.
13
3509
by: George Sakkis | last post by:
I've been trying to track down a memory leak (which I initially attributed erroneously to numpy) and it turns out to be caused by a memory mapped file. It seems that mmap caches without limit the chunks it reads, as the memory usage grows to several hundreds MBs according to the Windows task manager before it dies with a MemoryError. I'm positive that these chunks are not referenced anywhere else; in fact if I change the mmap object to a...
1
5461
by: James T. Dennis | last post by:
I've been thinking about the Python mmap module quite a bit during the last couple of days. Sadly most of it has just been thinking ... and reading pages from Google searches ... and very little of it as been coding. Mostly it's just academic curiosity (I might be teaching an "overview of programming" class in a few months, and I'd use Python for most of the practical examples to cover a broad range of programming topics, including the...
1
2689
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 stuff from the file, piece by piece. Since other things may happen to the mmap object in between consecutive next() calls (such as another iterator's next()), i have to store the file position before yield and restore it afterwards by means of tell()...
2
4627
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 this c++ code works: #include <sys/mman.h> #include <fcntl.h>
1
1671
by: magnus.lycka | last post by:
Does anyone recognize this little Python crasher? I'll file a bug report unless someone notices it as an already documented bug. I found some open mmap bugs, but it wasn't obvious to me that this problem was one of those... Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) on linux2 Type "help", "copyright", "credits" or "license" for more information. Segmenteringsfel (core dumped)
0
8402
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.