Bug in mlock??

Randy Appleton (randy@euclid.acs.NMU.EDU)
Wed, 22 Sep 1999 11:49:13 -0400


The program below should (I hope) lock 20M of memory into ram. When it
first runs, 20M are allocated and in RAM, but after lots of other
programs running (compiles mostly) I *sometimes* find the RSS to be
under 1M. Othertimes everything works fine. Checking the amount of
memory locked (VmLck from /proc/#/status) shows 20M, even though the
RSS is under 1M. I've seen this behavior in several kernels from 2.1.77
to 2.3.10. What am I doing wrong, or is this a real kernel bug?

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

void usage() {
cout << "Read the source, young Luke!\n";
}

main(int argc, char *argv[]) {
int size = 20 * 1024 * 1024;

// allocate the pages
char *ptr = (char *)malloc(size);
if (ptr == 0) {
perror("Error in malloc");
exit(1);
}
// touch every page
for(int i = 0; i < size; i++)
ptr[i] = 0;

// Lock the pages
int ret = mlock(ptr, size);
if (ret != 0) {
perror("Mlock");
exit(1);
}
while (1)
sleep(1);
}

--
========================================================================
|| Randy Appleton, Professor of Computer Science at Northern Michigan ||
|| University.  And a big fan of Linux!                               ||
================= mailto:randy@euclid.nmu.edu ==========================

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/