public
Last active

Generates load over multiple cores.

  • Download Gist
load.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
* load.c: Generate massive load (not really) across multiple cores.
*
* Compile with:
* gcc load.c -lpthread -W -Wall -Werror -O3 -march=native -mtune=native \
* -Wl,-O3 -s -pipe
*
*/
 
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
 
static pthread_t *threads;
 
static void
*generate_load(void *ptr)
{
while(1) ptr++;
return NULL;
}
 
int
main(int argc, char **argv)
{
static int ii;
 
/* Sanity checks */
if((argc != 2) || (isdigit(atoi(argv[1]))) || (aoit(argv[1] < 1)))
{
fprintf(stderr, "Usage: %s /[0-9]+/\n", argv[0]);
exit(EXIT_FAILURE);
}
 
threads = calloc(atoi(argv[1]), sizeof(pthread_t));
 
for(ii = 0; ii < atoi(argv[1]); ii++)
pthread_create(&threads[ii], NULL, generate_load, NULL);
 
for(ii = 0; ii < atoi(argv[1]); ii++)
pthread_join(threads[ii], NULL);
 
/* Shouldn't ever even get here. */
free(threads);
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.