mem2swap.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:47:18 +0200
changeset 27 81a574d60c15
parent 9 c3fecc82ade6
child 29 7abf6146898e
permissions -rw-r--r--
typo in min2time format string committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 * test/demos/mem2swap.c
 * Copyright (C) 2008 Markus Broeker
 *
 * mem2swpapc usage: mem2spap [<MB>]
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define N 1000

#ifndef DEFAULT_MEGS
#define DEFAULT_MEGS 64
#endif

int set_limit (int);

int main (int argc, char **argv)
{
    int i, n = 0;

    long int *p;

    long int *p_new;
    char *args[] = { "/usr/bin/free", "-m", NULL };
    int size = sizeof (*p);

    int megs = DEFAULT_MEGS * 1024 * 1024;

    if (argc == 2)
        megs = atoi (argv[1]) * 1024 * 1024;

    printf ("Mem2Swap - Version 1.0\n");

    if (!set_limit (megs))
        printf ("%d MB demanded ", megs / 1024 / 1024);
    else {
        printf ("\tUsage: %s [MEM]\n\n", argv[0]);
        printf ("Report bugs to mbroeker@largo.homelinux.org\n");
        return EXIT_FAILURE;
    }

    if ((p = malloc (N * size)) == NULL)
        return EXIT_SUCCESS;

    while (1) {
        for (i = 0; i < N; i++) {
            p[n * N + i] = size * (n * N + i);
        }
        if ((p_new = realloc (p, (++n + 1) * N * size)))
            p = p_new;
        else
            break;
    }

    if (p) {
        printf ("and %ld MB allocated\n", p[n * N - 1] / 1024 / 1024);
        free (p);
    }

    printf ("\n\n");
    execve ("/usr/bin/free", args, NULL);

    return EXIT_SUCCESS;
}