views:

332

answers:

3

I don't know if SO or SF is the right place for that kind of question but I think it is something you could achieve with code.

I have a program that requires much memory, like 2/3 of all the physical ram. After some runtime my operating system begins to swap the program to hdd. But I need the program to respond very fast all the time, so I need to prevent paging for that process.

How can you prevent the OS to swap one process?

Thanks for any help!

Solution

//main.cpp
include <sys/mman.h>
[...]
int main(int argc, char *argv[]) {
     mlockall(MCL_CURRENT | MCL_FUTURE);
     [...]
}
+4  A: 

Use mmap() instead of malloc, and use the "MAP_LOCKED" flag. (works on linux > 2.5.37)

dicroce
Note that this will have the same effect (with respect to swapping) as using *mlock*; so the same caveats apply.
sleske
Here's a link to a discussion @ kerneltrap regarding this technique: http://kerneltrap.org/node/7878
dicroce
+6  A: 

Well, there's mlock for locking memory (telling the kernel it may not be swapped out), but that's meant for relatively small amounts of memory, and would require modification of the program.

The other option might be to adjust Linux's "swappiness", i.e. its tendency to swap out pages. See here for an interesting discussion. That is not possible per process, however.

I'm not aware of any per-process solution for your problem.

sleske
+5  A: 

At the start of the program, call:

mlockall(MCL_CURRENT | MCL_FUTURE);

(If you do not have the source to the program, you'll have to debauch the process with ptrace to do this).

Be aware that this will increase the chances of memory allocations made by the process failing.

caf
I don't think that you *can* "debauch" the process. Not unless you have access to the technology from Tron. :)
Zan Lynx
"debauch"ing a process means to get it drunk, party with it, etc... :)
dicroce
I think that's a fair metaphor :)
caf