File: mlock.cpp

package info (click to toggle)
libstxxl 1.3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,532 kB
  • sloc: cpp: 30,003; ansic: 3,507; makefile: 665; sh: 515; perl: 289
file content (44 lines) | stat: -rw-r--r-- 1,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/***************************************************************************
 *  utils/mlock.cpp
 *
 *  Allocate some memory and mlock() it to consume physical memory.
 *  Needs to run as root to block more than 64 KiB in default settings.
 *
 *  Part of the STXXL. See http://stxxl.sourceforge.net
 *
 *  Copyright (C) 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
 *
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or copy at
 *  http://www.boost.org/LICENSE_1_0.txt)
 **************************************************************************/

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sys/mman.h>
#include <unistd.h>

int main(int argc, char ** argv)
{
    if (argc == 2) {
        long M = atol(argv[1]);
        if (M > 0) {
            char * c = (char *)malloc(M);
            for (long i = 0; i < M; ++i)
                c[i] = 42;
            if (mlock(c, M) == 0) {
                std::cout << "mlock(, " << M << ") successful, press Ctrl-C to finish" << std::endl;
                while (1)
                    sleep(86400);
            } else {
                std::cerr << "mlock(, " << M << ") failed!" << std::endl;
                return 1;
            }
        }
    } else {
        std::cout << "Usage: " << argv[0] << " <bytes>" << std::endl;
    }
}

// vim: et:ts=4:sw=4