473,503 Members | 544 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_pointer, 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 2231
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.programming or news:comp.os.linux.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.programming or news:comp.os.linux.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
3971
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...
4
3513
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...
7
3090
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...
1
2162
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...
13
3487
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...
1
5449
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...
1
2679
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...
2
4614
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...
1
1664
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...
0
7087
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...
0
7281
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,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5579
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,...
0
4675
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...
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
383
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...

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.