Advertisement
STANAANDREY

cnt lowers with shared cnt

Dec 8th, 2024
61
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 <pthread.h>  
  4. #include <stdint.h>  
  5. #include <unistd.h>  
  6. #include <sys/types.h>  
  7. #include <sys/stat.h>  
  8. #include <fcntl.h>  
  9. #include <ctype.h>  
  10. #include <string.h>  
  11.  
  12. static int N_THREADS;  
  13. static int cnt = 0; // Shared variable for counting lowercase letters  
  14. pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;  
  15.  
  16. off_t getFileSize(const char fpath[]) {  
  17.     struct stat statBuf;  
  18.     if (stat(fpath, &statBuf)) {  
  19.         perror("stat");  
  20.         exit(1);  
  21.     }  
  22.     return statBuf.st_size;  
  23. }  
  24.  
  25. typedef struct {  
  26.     int start;  
  27.     int end;  
  28.     char *file_content;  
  29. } ThreadData;  
  30.  
  31. void *routine(void *arg) {  
  32.     ThreadData *data = (ThreadData *)arg;  
  33.     int local_cnt = 0;  
  34.  
  35.     // Count lowercase letters in the assigned portion  
  36.     for (int i = data->start; i < data->end; i++) {  
  37.         if (islower(data->file_content[i])) {  
  38.             local_cnt++;  
  39.         }  
  40.     }  
  41.  
  42.     // Update the shared counter with a mutex  
  43.     int r;  
  44.     if ((r = pthread_mutex_lock(&mtx)) != 0) {  
  45.         fprintf(stderr, "Mutex lock error: %s\n", strerror(r));  
  46.         pthread_exit(NULL);  
  47.     }  
  48.     cnt += local_cnt;  
  49.     if ((r = pthread_mutex_unlock(&mtx)) != 0) {  
  50.         fprintf(stderr, "Mutex unlock error: %s\n", strerror(r));  
  51.         pthread_exit(NULL);  
  52.     }  
  53.  
  54.     return NULL;  
  55. }  
  56.  
  57. void *zalloc(int nmemb, int size) {  
  58.     void *p = calloc(nmemb, size);  
  59.     if (!p) {  
  60.         perror("calloc");  
  61.         exit(1);  
  62.     }  
  63.     return p;  
  64. }  
  65.  
  66. int main(int argc, char *argv[]) {  
  67.     if (argc != 3) {  
  68.         fprintf(stderr, "Usage: %s <file_name> <num_threads>\n", argv[0]);  
  69.         exit(1);  
  70.     }  
  71.  
  72.     N_THREADS = atoi(argv[2]);  
  73.     if (N_THREADS <= 0) {  
  74.         fprintf(stderr, "Number of threads must be greater than 0.\n");  
  75.         exit(1);  
  76.     }  
  77.  
  78.     // Get file size  
  79.     int fsize = getFileSize(argv[1]);  
  80.     if (fsize == 0) {  
  81.         fprintf(stderr, "File is empty.\n");  
  82.         exit(1);  
  83.     }  
  84.  
  85.     // Open the file  
  86.     int fd = open(argv[1], O_RDONLY);  
  87.     if (fd == -1) {  
  88.         perror("open");  
  89.         exit(1);  
  90.     }  
  91.  
  92.     // Read the entire file into memory  
  93.     char *file_content = zalloc(fsize + 1, sizeof(char));  
  94.     if (read(fd, file_content, fsize) != fsize) {  
  95.         perror("read");  
  96.         free(file_content);  
  97.         close(fd);  
  98.         exit(1);  
  99.     }  
  100.     file_content[fsize] = '\0'; // Null-terminate the file content  
  101.  
  102.     close(fd);  
  103.  
  104.     // Create threads  
  105.     pthread_t threads[N_THREADS];  
  106.     ThreadData thread_data[N_THREADS];  
  107.  
  108.     int bytesPerTh = fsize / N_THREADS;  
  109.     for (int i = 0; i < N_THREADS; i++) {  
  110.         thread_data[i].start = i * bytesPerTh;  
  111.         thread_data[i].end = (i == N_THREADS - 1) ? fsize : (i + 1) * bytesPerTh;  
  112.         thread_data[i].file_content = file_content;  
  113.  
  114.         if (pthread_create(&threads[i], NULL, routine, &thread_data[i]) != 0) {  
  115.             perror("pthread_create");  
  116.             free(file_content);  
  117.             exit(1);  
  118.         }  
  119.     }  
  120.  
  121.     // Join threads  
  122.     for (int i = 0; i < N_THREADS; i++) {  
  123.         pthread_join(threads[i], NULL);  
  124.     }  
  125.  
  126.     // Print the result  
  127.     printf("Total lowercase letters: %d\n", cnt);  
  128.  
  129.     // Clean up  
  130.     free(file_content);  
  131.     pthread_mutex_destroy(&mtx);  
  132.  
  133.     return 0;  
  134. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement