mem2swap.c
author Markus Brökers <mbroeker@largo.homelinux.org>
Tue, 17 Aug 2010 14:47:41 +0200
changeset 138 dff18d1ac2af
parent 113 397270b5d21a
child 139 cb1d3f4cf18e
permissions -rw-r--r--
Compatibility: We support Linux and BSD
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
77
49e0babccb23 HEADER TAGS
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 64
diff changeset
     2
 * mem2swap.c
9
c3fecc82ade6 standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 8
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
91
1181deef3bd6 fixed a typo in mem2swap.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 82
diff changeset
     5
 * mem2swap usage: mem2swap [<MB>]
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
 */
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdio.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <stdlib.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <unistd.h>
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <errno.h>
113
397270b5d21a sys/wait.h is the proper header, not wait.h
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 91
diff changeset
    12
#include <sys/wait.h>
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
#define N 1000
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
#ifndef DEFAULT_MEGS
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
#define DEFAULT_MEGS 64
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
#endif
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    20
#ifdef __linux__
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    21
#define CMD_FREE "/usr/bin/free"
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    22
#define CMD_ARGS "-m"
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    23
#elif __unix__
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    24
#define CMD_FREE "/sbin/sysctl"
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    25
#define CMD_ARGS "hw.usermem"
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    26
#endif
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    27
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
int set_limit (int);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
int main (int argc, char **argv)
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
{
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    int i, n = 0;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    33
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    long int *p;
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 27
diff changeset
    35
    long int *p_new;
27
81a574d60c15 typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 9
diff changeset
    36
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    37
    char *args[] = { CMD_FREE, CMD_ARGS, NULL };
29
7abf6146898e indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 27
diff changeset
    38
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
    int size = sizeof (*p);
51
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 48
diff changeset
    40
    int megs = DEFAULT_MEGS;
64
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    41
    int pid;
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
    if (argc == 2)
51
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 48
diff changeset
    44
        megs = atoi (argv[1]);
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    printf ("Mem2Swap - Version 1.0\n");
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
    if (!set_limit (megs))
51
a03372ef9714 base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 48
diff changeset
    49
        printf ("%d MB demanded ", megs);
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
    else {
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
        printf ("\tUsage: %s [MEM]\n\n", argv[0]);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
        printf ("Report bugs to mbroeker@largo.homelinux.org\n");
63
5a82f89d607e uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    53
5a82f89d607e uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 51
diff changeset
    54
        perror ("Limit Error");
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        return EXIT_FAILURE;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    if ((p = malloc (N * size)) == NULL)
48
b94d657a9acb Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 29
diff changeset
    59
        return EXIT_FAILURE;
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
48
b94d657a9acb Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 29
diff changeset
    61
    for (;;) {
82
7ff8fc49cce4 Useless braces removed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 77
diff changeset
    62
        for (i = 0; i < N; i++)
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
            p[n * N + i] = size * (n * N + i);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
        if ((p_new = realloc (p, (++n + 1) * N * size)))
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
            p = p_new;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        else
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
            break;
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
    if (p) {
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
        printf ("and %ld MB allocated\n", p[n * N - 1] / 1024 / 1024);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
        free (p);
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
    }
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
    printf ("\n\n");
8
96d16dfe787a We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 3
diff changeset
    76
64
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    77
    pid = fork ();
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    78
    switch (pid) {
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    79
    case 0:
138
dff18d1ac2af Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents: 113
diff changeset
    80
        execve (CMD_FREE, args, NULL);
64
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    81
    case -1:
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    82
        perror ("Fork Error");
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    83
        return EXIT_FAILURE;
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    84
    default:
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    85
        wait (&pid);
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    86
    }
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    87
993b97c4ad2d FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 63
diff changeset
    88
    return pid;
3
820ed7fb9314 database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    89
}