mem2swap.c
changeset 3 820ed7fb9314
child 8 96d16dfe787a
equal deleted inserted replaced
2:97beb75e5ac7 3:820ed7fb9314
       
     1 /**
       
     2  * $Id: mem2swap.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $
       
     3  * $Author: mbroeker $
       
     4  *
       
     5  * mem2swpapc usage: mem2spap [<MB>]
       
     6  */
       
     7 
       
     8 #include <stdio.h>
       
     9 #include <stdlib.h>
       
    10 #include <unistd.h>
       
    11 #include <errno.h>
       
    12 
       
    13 #define N 1000
       
    14 
       
    15 #ifndef DEFAULT_MEGS
       
    16 #define DEFAULT_MEGS 64
       
    17 #endif
       
    18 
       
    19 int set_limit (int);
       
    20 
       
    21 int main (int argc, char **argv)
       
    22 {
       
    23     int i, n = 0;
       
    24     long int *p;
       
    25     long int *p_new;
       
    26     char *args[] = { "/usr/bin/free", "-m", NULL };
       
    27     int size = sizeof (*p);
       
    28     int megs = DEFAULT_MEGS * 1024 * 1024;
       
    29 
       
    30     if (argc == 2)
       
    31         megs = atoi (argv[1]) * 1024 * 1024;
       
    32 
       
    33     printf ("Mem2Swap - Version 1.0\n");
       
    34 
       
    35     if (!set_limit (megs))
       
    36         printf ("%d MB demanded ", megs / 1024 / 1024);
       
    37     else {
       
    38         printf ("\tUsage: %s [MEM]\n\n", argv[0]);
       
    39         printf ("Report bugs to mbroeker@largo.homelinux.org\n");
       
    40         return EXIT_FAILURE;
       
    41     }
       
    42 
       
    43     if ((p = malloc (N * size)) == NULL)
       
    44         return EXIT_SUCCESS;
       
    45 
       
    46     while (1) {
       
    47         for (i = 0; i < N; i++) {
       
    48             p[n * N + i] = size * (n * N + i);
       
    49         }
       
    50         if ((p_new = realloc (p, (++n + 1) * N * size)))
       
    51             p = p_new;
       
    52         else
       
    53             break;
       
    54     }
       
    55 
       
    56     if (p) {
       
    57         printf ("and %ld MB allocated\n", p[n * N - 1] / 1024 / 1024);
       
    58         free (p);
       
    59     }
       
    60 
       
    61     printf ("\n\n");
       
    62     execve ("/usr/bin/free", args, NULL);
       
    63     return EXIT_SUCCESS;
       
    64 }