Guest User

program

a guest
Aug 29th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <time.h>
  6. #include <pthread.h>
  7.  
  8. #define CHUNK_SIZE (256 * 1024 * 1024)
  9. #define TOTAL_SIZE (2UL * 1024 * 1024 * 1024)
  10. #define RANDOM_WRITE_SIZE 50
  11. #define NUM_THREADS 15
  12. #define TOTAL_RANDOM_WRITES 50000
  13.  
  14. typedef struct {
  15.     int fd;
  16.     size_t num_writes;
  17.     unsigned int seed;
  18. } thread_arg_t;
  19.  
  20. void* thread_random_writes(void* arg) {
  21.     thread_arg_t* t_arg = (thread_arg_t*)arg;
  22.     char random_data[RANDOM_WRITE_SIZE];
  23.     off_t position;
  24.     // Fill random_data with some values
  25.     for (size_t i = 0; i < RANDOM_WRITE_SIZE; i++) {
  26.         random_data[i] = 'R';
  27.     }
  28.     for (size_t i = 0; i < t_arg->num_writes; i++) {
  29.         // Generate a random position within the file
  30.         position = rand_r(&t_arg->seed) % (TOTAL_SIZE - RANDOM_WRITE_SIZE);
  31.         // Seek to the random position
  32.         if (lseek(t_arg->fd, position, SEEK_SET) == -1) {
  33.             perror("Failed to seek in file");
  34.             pthread_exit(NULL);
  35.         }
  36.         // Write random data to the file
  37.         if (write(t_arg->fd, random_data, RANDOM_WRITE_SIZE) != RANDOM_WRITE_SIZE) {
  38.             perror("Failed to write random data to file");
  39.             pthread_exit(NULL);
  40.         }
  41.     }
  42.     pthread_exit(NULL);
  43. }
  44.  
  45. int main() {
  46.     int fd;
  47.     char *buffer;
  48.     ssize_t written;
  49.     size_t total_written = 0;
  50.     // Allocate memory for the buffer
  51.     buffer = (char *)malloc(CHUNK_SIZE);
  52.     if (buffer == NULL) {
  53.         perror("Failed to allocate memory");
  54.         return EXIT_FAILURE;
  55.     }
  56.  
  57.     // Initialize buffer with some data
  58.     for (size_t i = 0; i < CHUNK_SIZE; i++) {
  59.         buffer[i] = 'A';
  60.     }
  61.     // Open the file for writing
  62.     fd = open("/mnt/test/hi.txt", O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  63.     if (fd == -1) {
  64.         perror("Failed to open file");
  65.         free(buffer);
  66.         return EXIT_FAILURE;
  67.     }
  68.     while (total_written < TOTAL_SIZE) {
  69.         written = write(fd, buffer, CHUNK_SIZE);
  70.         if (written != CHUNK_SIZE) {
  71.             perror("Failed to write to file");
  72.             close(fd);
  73.             free(buffer);
  74.             return EXIT_FAILURE;
  75.         }
  76.         total_written += written;
  77.     }
  78.     free(buffer);
  79.     // Prepare threads
  80.     pthread_t threads[NUM_THREADS];
  81.     thread_arg_t thread_args[NUM_THREADS];
  82.     size_t writes_per_thread = TOTAL_RANDOM_WRITES / NUM_THREADS;
  83.     // Measure time for random writes
  84.     struct timespec start, end;
  85.     clock_gettime(CLOCK_MONOTONIC, &start);
  86.     for (int i = 0; i < NUM_THREADS; i++) {
  87.         thread_args[i].fd = fd;
  88.         thread_args[i].num_writes = writes_per_thread;
  89.         thread_args[i].seed = (unsigned int)time(NULL) ^ (i * 7919); // Unique seed per thread
  90.         if (pthread_create(&threads[i], NULL, thread_random_writes, &thread_args[i]) != 0) {
  91.             perror("Failed to create thread");
  92.             close(fd);
  93.             return EXIT_FAILURE;
  94.         }
  95.     }
  96.     // Wait for all threads to finish
  97.     for (int i = 0; i < NUM_THREADS; i++) {
  98.         pthread_join(threads[i], NULL);
  99.     }
  100.     clock_gettime(CLOCK_MONOTONIC, &end);
  101.     // Calculate and print the time taken
  102.     long elapsed_ns = (end.tv_sec - start.tv_sec) * 1000000000L + (end.tv_nsec - start.tv_nsec);
  103.     double elapsed_seconds = elapsed_ns / 1000000000.0;
  104.     printf("Time taken: %.4f seconds\n", elapsed_seconds);
  105.     close(fd);
  106.     return EXIT_SUCCESS;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment