author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 05 Mar 2010 22:03:16 +0100 | |
changeset 112 | 9bfb339a8156 |
parent 91 | 1181deef3bd6 |
child 113 | 397270b5d21a |
permissions | -rw-r--r-- |
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
1 |
/** |
77 | 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> |
64
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
12 |
#include <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 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
20 |
int set_limit (int); |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
21 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
22 |
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
|
23 |
{ |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
24 |
int i, n = 0; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
25 |
|
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
26 |
long int *p; |
29
7abf6146898e
indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
27
diff
changeset
|
27 |
long int *p_new; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
28 |
|
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
29 |
char *args[] = { "/usr/bin/free", "-m", NULL }; |
29
7abf6146898e
indent -bad changed to -nbad:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
27
diff
changeset
|
30 |
|
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
31 |
int size = sizeof (*p); |
51
a03372ef9714
base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
48
diff
changeset
|
32 |
int megs = DEFAULT_MEGS; |
64
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
33 |
int pid; |
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
34 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
35 |
if (argc == 2) |
51
a03372ef9714
base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
48
diff
changeset
|
36 |
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
|
37 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
38 |
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
|
39 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
40 |
if (!set_limit (megs)) |
51
a03372ef9714
base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
48
diff
changeset
|
41 |
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
|
42 |
else { |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
|
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
51
diff
changeset
|
46 |
perror ("Limit Error"); |
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
47 |
return EXIT_FAILURE; |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
48 |
} |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
50 |
if ((p = malloc (N * size)) == NULL) |
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
51 |
return EXIT_FAILURE; |
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
52 |
|
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
53 |
for (;;) { |
82
7ff8fc49cce4
Useless braces removed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
p = p_new; |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
58 |
else |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
59 |
break; |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
60 |
} |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
61 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
62 |
if (p) { |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
63 |
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
|
64 |
free (p); |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
65 |
} |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
66 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
67 |
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
|
68 |
|
64
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
69 |
pid = fork (); |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
70 |
switch (pid) { |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
71 |
case 0: |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
72 |
execve ("/usr/bin/free", args, NULL); |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
73 |
case -1: |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
74 |
perror ("Fork Error"); |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
75 |
return EXIT_FAILURE; |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
76 |
default: |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
77 |
wait (&pid); |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
78 |
} |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
79 |
|
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
80 |
return pid; |
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
81 |
} |