Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <time.h>
- #include <pthread.h>
- #define CHUNK_SIZE (256 * 1024 * 1024)
- #define TOTAL_SIZE (2UL * 1024 * 1024 * 1024)
- #define RANDOM_WRITE_SIZE 50
- #define NUM_THREADS 15
- #define TOTAL_RANDOM_WRITES 50000
- typedef struct {
- int fd;
- size_t num_writes;
- unsigned int seed;
- } thread_arg_t;
- void* thread_random_writes(void* arg) {
- thread_arg_t* t_arg = (thread_arg_t*)arg;
- char random_data[RANDOM_WRITE_SIZE];
- off_t position;
- // Fill random_data with some values
- for (size_t i = 0; i < RANDOM_WRITE_SIZE; i++) {
- random_data[i] = 'R';
- }
- for (size_t i = 0; i < t_arg->num_writes; i++) {
- // Generate a random position within the file
- position = rand_r(&t_arg->seed) % (TOTAL_SIZE - RANDOM_WRITE_SIZE);
- // Seek to the random position
- if (lseek(t_arg->fd, position, SEEK_SET) == -1) {
- perror("Failed to seek in file");
- pthread_exit(NULL);
- }
- // Write random data to the file
- if (write(t_arg->fd, random_data, RANDOM_WRITE_SIZE) != RANDOM_WRITE_SIZE) {
- perror("Failed to write random data to file");
- pthread_exit(NULL);
- }
- }
- pthread_exit(NULL);
- }
- int main() {
- int fd;
- char *buffer;
- ssize_t written;
- size_t total_written = 0;
- // Allocate memory for the buffer
- buffer = (char *)malloc(CHUNK_SIZE);
- if (buffer == NULL) {
- perror("Failed to allocate memory");
- return EXIT_FAILURE;
- }
- // Initialize buffer with some data
- for (size_t i = 0; i < CHUNK_SIZE; i++) {
- buffer[i] = 'A';
- }
- // Open the file for writing
- fd = open("/mnt/test/hi.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
- if (fd == -1) {
- perror("Failed to open file");
- free(buffer);
- return EXIT_FAILURE;
- }
- while (total_written < TOTAL_SIZE) {
- written = write(fd, buffer, CHUNK_SIZE);
- if (written != CHUNK_SIZE) {
- perror("Failed to write to file");
- close(fd);
- free(buffer);
- return EXIT_FAILURE;
- }
- total_written += written;
- }
- free(buffer);
- // Prepare threads
- pthread_t threads[NUM_THREADS];
- thread_arg_t thread_args[NUM_THREADS];
- size_t writes_per_thread = TOTAL_RANDOM_WRITES / NUM_THREADS;
- // Measure time for random writes
- struct timespec start, end;
- clock_gettime(CLOCK_MONOTONIC, &start);
- for (int i = 0; i < NUM_THREADS; i++) {
- thread_args[i].fd = fd;
- thread_args[i].num_writes = writes_per_thread;
- thread_args[i].seed = (unsigned int)time(NULL) ^ (i * 7919); // Unique seed per thread
- if (pthread_create(&threads[i], NULL, thread_random_writes, &thread_args[i]) != 0) {
- perror("Failed to create thread");
- close(fd);
- return EXIT_FAILURE;
- }
- }
- // Wait for all threads to finish
- for (int i = 0; i < NUM_THREADS; i++) {
- pthread_join(threads[i], NULL);
- }
- clock_gettime(CLOCK_MONOTONIC, &end);
- // Calculate and print the time taken
- long elapsed_ns = (end.tv_sec - start.tv_sec) * 1000000000L + (end.tv_nsec - start.tv_nsec);
- double elapsed_seconds = elapsed_ns / 1000000000.0;
- printf("Time taken: %.4f seconds\n", elapsed_seconds);
- close(fd);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment