Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to read and write with mmap() on a Linux system
    primarykey
    data
    text
    <p>I need to make some stream in and out classes using mmap() in Linux. To do so I tried to make some test code that writes some integers to a file, saves it, loads it again and write the data in the file to cout. If that test code works, then It wont be a problem making stream in and out afterwards. </p> <p>When I first started out I got segment faults and If I did not get that nothing happened, so I googled a little. I found this book <a href="http://www.advancedlinuxprogramming.com/alp-folder/alp-ch05-ipc.pdf" rel="nofollow">http://www.advancedlinuxprogramming.com/alp-folder/alp-ch05-ipc.pdf</a> where around page 107 there is some usefull code. I copy pasted that code and made some small changes and got this code:</p> <pre><code>int fd; void* file_memory; /* Prepare a file large enough to hold an unsigned integer. */ fd = open ("mapTester", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); //Make the file big enough lseek (fd, 4 * 10 + 1, SEEK_SET); write (fd, "", 1); lseek (fd, 0, SEEK_SET); /* Create the memory mapping. */ file_memory = mmap (0, 4 * 10, PROT_WRITE, MAP_SHARED, fd, 0); close (fd); /* Write a random integer to memory-mapped area. */ sprintf((char*) file_memory, "%d\n", 22); /* Release the memory (unnecessary because the program exits). */ munmap (file_memory, 4 * 10); cout &lt;&lt; "Mark" &lt;&lt; endl; //Start the part where I read from the file int integer; /* Open the file. */ fd = open (argv[1], O_RDWR, S_IRUSR | S_IWUSR); /* Create the memory mapping. */ file_memory = mmap (0, 4 * 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close (fd); /* Read the integer, print it out, and double it. */ scanf ((char *) file_memory, "%d", &amp;integer); printf ("value: %d\n", integer); sprintf ((char*) file_memory, "%d\n", 2 * integer); /* Release the memory (unnecessary because the program exits). */ munmap (file_memory, 4 * 10); </code></pre> <p>But I get a segment fults after the "mark" cout. </p> <p>Then I replace the "read part" with this:</p> <pre><code>fd = open("mapTester", O_RDONLY); int* buffer = (int*) malloc (4*10); read(fd, buffer, 4 * 10); for(int i = 0; i &lt; 1; i++) { cout &lt;&lt; buffer[i] &lt;&lt; endl; } </code></pre> <p>That is some working code that shows me that the file is empty. I tries out a couple of ways to write to the mapping without any change in result. </p> <p>So how am I amble to make my code write? And does my mmap read code seem okay (just in case you can see some obvious flaws)? </p> <p>I have found some other resources that did not help me yet, but because I am a new user I may only post max 2 links. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload