mmap, why do not work?

S

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.html#SECTION002730000000000000000
files:
shm_server.c
shm_client.c
but those examples use shmget, shmat etc.
Those do not use mmap.

Thank you!
br, a coder
 
A

Artie Gold

sr said:
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
or
HTH,
--ag
[snip]
 
A

Artie Gold

Artie said:
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
HTH now,
--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top