// Program: breaknfs.c // // Compile: cc breaknfs.c -o breaknfs // Run: ./breaknfs 100 [] // // Args: arg1 = # of copies of program to run simultaneously // arg2 = A directory that is on an NFS-mounted filesystem #define NFS_MOUNT "/var/nfs" #include #include #include #include #include #define BUFFER_SIZE (1*1024*1024) static char buffer[BUFFER_SIZE]; int main(int argc, char *argv[]) { char* nfs_dir; int count; int fd; int i; int pid; char *files[] = {"file1", "file2", "file3", "file4", "file5"}; if (argc > 1) count = atoi(argv[1]); else count = 1; if (argc > 2) nfs_dir = argv[2]; else nfs_dir = NFS_MOUNT; /* cd -> ... */ if (chdir(nfs_dir) < 0) { perror(nfs_dir); exit(1); } /* Fill buffer with numbers and letters, etc! */ for ( i = 0; i < BUFFER_SIZE; i++ ) { buffer[i] = '0' + (i & 0x3f); if ( i && ((i % 80) == 0) ) buffer[i] = '\n'; } /* fork count-1 children */ while (count-- > 1) { pid = fork(); if (pid == 0) { /* child */ break; } else if (pid < 0) { perror("fork"); exit(1); } } srandom(getpid()); /* Forever and a day ... */ while(1) { for (i = 0; i < 5; i++) { int write_size; /* Write a random amount of bytes, truncating file before output */ write_size = (random() % BUFFER_SIZE) + 1; fd = open(files[i], O_WRONLY | O_CREAT | O_TRUNC, 0600); if (fd < 0) { perror("open"); exit(1); } if (write(fd, buffer, write_size) < 0) { perror("write"); exit(1); } close(fd); } } return 0; }