Skip to content

Instantly share code, notes, and snippets.

@ethercflow
Created June 8, 2018 12:33
Show Gist options
  • Save ethercflow/06671605f6707b0c83fee617d4ebe706 to your computer and use it in GitHub Desktop.
Save ethercflow/06671605f6707b0c83fee617d4ebe706 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define BLOCKSIZE 1048576
#define N 200
#define BUSYTIME 1000
int main(int argc, char *argv[]) {
int fd;
if ((fd = open("log.txt", O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR)) == -1) {
fprintf(stderr, "open file failed\n");
}
int **p = malloc(N * sizeof(int*));;
int i;
while (1) {
for (i = 0; i < N; i++) {
p[i] = malloc(BLOCKSIZE);
memset(p[i], '0', BLOCKSIZE);
int j;
char *q = (char *)(p[i]);
for (j = 0; j < BLOCKSIZE; j++) {
q[j] = 'c';
}
write(fd, q, BLOCKSIZE);
usleep(BUSYTIME);
}
// fprintf(stderr, "malloced\n");
for (i = 0; i < N; i++) {
free(p[i]);
usleep(BUSYTIME);
}
// fprintf(stderr, "freed\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment