/* gcc mem-hog.c -g -o mem-hog -Wall -Wextra */ #include #include #include #include #include #define UNUSED __attribute__ ((unused)) #define BUFFER_SIZE 1024 volatile int end; void sighandler1(UNUSED int signo) { } void sigint(UNUSED int signo) { end = 1; } int page_cache_hog(void) { int fd = -1; char buff[BUFFER_SIZE]; char path[BUFFER_SIZE]; int ret = 0; sprintf(path, "%s", "DATAFILE"); fd = open(path, O_RDONLY); if (fd == -1) { warn("open %s failed", path); return -1; } while ((ret = read(fd, buff, sizeof(buff))) > 0); if (ret == -1) warn("read %s failed", path); close(fd); return ret; } int mem_hog(void) { sigset_t sigset; int ret = 0; if (sigemptyset(&sigset) < 0) err(1, "sigemptyset()"); sigsuspend(&sigset); while (!end) { ret = page_cache_hog(); sigsuspend(&sigset); } return ret; } int main(UNUSED int argc, UNUSED char *argv[]) { struct sigaction sa1, sa2; sa1.sa_handler = sighandler1; if (sigemptyset(&sa1.sa_mask) < 0) err(1, "sigemptyset()"); sa1.sa_flags = 0; if (sigaction(SIGUSR1, &sa1, NULL) < 0) err(1, "sigaction()"); sa2.sa_handler = sigint; if (sigemptyset(&sa2.sa_mask) < 0) err(1, "sigemptyset()"); sa2.sa_flags = 0; if (sigaction(SIGINT, &sa2, NULL) < 0) err(1, "sigaction()"); return mem_hog(); }