Toys: cpusoak and memsoak

Useful for using up CPU time and Memory,

cpusoak.c

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>

int
main(int argc, char **argv)
{
	int children = argc > 1 ? atoi(argv[1]) : 1;

	while (children--) {
		pid_t pid = fork();
		if (!pid) {
			long n, i = 0;
			while (1) {
				n = ++i;
				while (n != 1) n = (n % 2) ? n * 3 + 1 : n / 2;
			}
		}
	}

	while (-1 != wait(NULL) && ECHILD != errno);

	return 0;
}

memsoak.c

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
	int children = argc > 1 ? atoi(argv[1]) : 1;
	int bytes = argc > 2 ? atoi(argv[2]) : 1024;
	int nap = argc > 3 ? atoi(argv[3]) : 3;

	while (children--) {
		pid_t pid = fork();
		if (!pid) {
			while (1) {
				malloc(bytes);
				sleep(nap);
			}
		}
	}

	while (-1 != wait(NULL) && ECHILD != errno);

	return 0;
}
Advertisements

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: