Created
November 6, 2023 18:20
-
-
Save rbranson/12b770fbcf79121ff06189950e5be708 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
void *worker(void *data) { | |
int fd = open("/tmp/setloadavg", O_RDWR|O_CREAT|O_DIRECT); | |
if (fd == -1) { | |
perror("open"); | |
return NULL; | |
} | |
FILE *fp = fdopen(fd, "rb+"); | |
if (fp == NULL) { | |
close(fd); | |
perror("fdopen"); | |
return NULL; | |
} | |
char buf[256]; | |
char mybyte = rand() % 256; | |
for (int i = 0; i < sizeof(buf); i++) { | |
buf[i] = mybyte; | |
} | |
while (1) { | |
fseek(fp, 0, SEEK_SET); | |
fwrite(buf, 1, sizeof(buf), fp); | |
fflush(fp); | |
} | |
return NULL; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
printf("usage: %s <target>\n", argv[0]); | |
return 1; | |
} | |
int target = atoi(argv[1]); | |
printf("spinning up %i threads...\n", target); | |
pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t) * target); | |
for (int i = 0; i < target; i++) { | |
pthread_create(&threads[i], NULL, worker, NULL); | |
} | |
for (int i = 0; i < target; i++) { | |
pthread_join(threads[i], NULL); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment