author | Markus Bröker<broeker.markus@googlemail.com> |
Sun, 10 Feb 2019 13:17:01 +0100 | |
changeset 173 | 374a86886bc5 |
parent 169 | df7c720bcaa6 |
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> |
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" |
169
df7c720bcaa6
Service Pack 1 for a software from 2008 :gg
Markus Bröker<broeker.markus@googlemail.com>
parents:
167
diff
changeset
|
26 |
#else |
df7c720bcaa6
Service Pack 1 for a software from 2008 :gg
Markus Bröker<broeker.markus@googlemail.com>
parents:
167
diff
changeset
|
27 |
#define CMD_FREE "/sbin/sysctl" |
df7c720bcaa6
Service Pack 1 for a software from 2008 :gg
Markus Bröker<broeker.markus@googlemail.com>
parents:
167
diff
changeset
|
28 |
#define CMD_ARGS "hw.usermem" |
138
dff18d1ac2af
Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
113
diff
changeset
|
29 |
#endif |
dff18d1ac2af
Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
113
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 set_limit (int); |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
32 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
33 |
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
|
34 |
{ |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
35 |
int i, n = 0; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
36 |
|
167
7c6b221900bf
Proper Datatypes to leave the 80's
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
139
diff
changeset
|
37 |
unsigned long *p; |
7c6b221900bf
Proper Datatypes to leave the 80's
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
139
diff
changeset
|
38 |
unsigned long *p_new; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
39 |
|
138
dff18d1ac2af
Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
113
diff
changeset
|
40 |
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
|
41 |
|
167
7c6b221900bf
Proper Datatypes to leave the 80's
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
139
diff
changeset
|
42 |
unsigned long size = sizeof (*p); |
51
a03372ef9714
base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
48
diff
changeset
|
43 |
int megs = DEFAULT_MEGS; |
64
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
44 |
int pid; |
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 |
if (argc == 2) |
51
a03372ef9714
base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
48
diff
changeset
|
47 |
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
|
48 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
49 |
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
|
50 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
51 |
if (!set_limit (megs)) |
51
a03372ef9714
base10.c added and set_limit changed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
48
diff
changeset
|
52 |
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
|
53 |
else { |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
|
5a82f89d607e
uint vs size_t and two bugfixes in fak and unicode
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
51
diff
changeset
|
57 |
perror ("Limit Error"); |
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
58 |
return EXIT_FAILURE; |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
59 |
} |
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 |
if ((p = malloc (N * size)) == NULL) |
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
62 |
return EXIT_FAILURE; |
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
63 |
|
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
64 |
for (;;) { |
82
7ff8fc49cce4
Useless braces removed
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
p = p_new; |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
69 |
else |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
70 |
break; |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
71 |
} |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
72 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
73 |
if (p) { |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
74 |
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
|
75 |
free (p); |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
76 |
} |
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
77 |
|
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
78 |
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
|
79 |
|
64
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
80 |
pid = fork (); |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
81 |
switch (pid) { |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
82 |
case 0: |
138
dff18d1ac2af
Compatibility: We support Linux and BSD
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
113
diff
changeset
|
83 |
execve (CMD_FREE, args, NULL); |
139
cb1d3f4cf18e
execve returns a value on error and this catches it
Markus Brökers <mbroeker@largo.homelinux.org>
parents:
138
diff
changeset
|
84 |
exit (EXIT_FAILURE); |
64
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
85 |
case -1: |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
86 |
perror ("Fork Error"); |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
87 |
return EXIT_FAILURE; |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
88 |
default: |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
89 |
wait (&pid); |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
90 |
} |
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
91 |
|
993b97c4ad2d
FORK ERROR in prog_limit and mem2swap:
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
63
diff
changeset
|
92 |
return pid; |
3
820ed7fb9314
database, gauss, lotto, mem2swap, prog_limit moved to demos
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
93 |
} |