Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <stdint.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <ctype.h>
- #include <string.h>
- static int N_THREADS;
- static int cnt = 0; // Shared variable for counting lowercase letters
- pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
- off_t getFileSize(const char fpath[]) {
- struct stat statBuf;
- if (stat(fpath, &statBuf)) {
- perror("stat");
- exit(1);
- }
- return statBuf.st_size;
- }
- typedef struct {
- int start;
- int end;
- char *file_content;
- } ThreadData;
- void *routine(void *arg) {
- ThreadData *data = (ThreadData *)arg;
- int local_cnt = 0;
- // Count lowercase letters in the assigned portion
- for (int i = data->start; i < data->end; i++) {
- if (islower(data->file_content[i])) {
- local_cnt++;
- }
- }
- // Update the shared counter with a mutex
- int r;
- if ((r = pthread_mutex_lock(&mtx)) != 0) {
- fprintf(stderr, "Mutex lock error: %s\n", strerror(r));
- pthread_exit(NULL);
- }
- cnt += local_cnt;
- if ((r = pthread_mutex_unlock(&mtx)) != 0) {
- fprintf(stderr, "Mutex unlock error: %s\n", strerror(r));
- pthread_exit(NULL);
- }
- return NULL;
- }
- void *zalloc(int nmemb, int size) {
- void *p = calloc(nmemb, size);
- if (!p) {
- perror("calloc");
- exit(1);
- }
- return p;
- }
- int main(int argc, char *argv[]) {
- if (argc != 3) {
- fprintf(stderr, "Usage: %s <file_name> <num_threads>\n", argv[0]);
- exit(1);
- }
- N_THREADS = atoi(argv[2]);
- if (N_THREADS <= 0) {
- fprintf(stderr, "Number of threads must be greater than 0.\n");
- exit(1);
- }
- // Get file size
- int fsize = getFileSize(argv[1]);
- if (fsize == 0) {
- fprintf(stderr, "File is empty.\n");
- exit(1);
- }
- // Open the file
- int fd = open(argv[1], O_RDONLY);
- if (fd == -1) {
- perror("open");
- exit(1);
- }
- // Read the entire file into memory
- char *file_content = zalloc(fsize + 1, sizeof(char));
- if (read(fd, file_content, fsize) != fsize) {
- perror("read");
- free(file_content);
- close(fd);
- exit(1);
- }
- file_content[fsize] = '\0'; // Null-terminate the file content
- close(fd);
- // Create threads
- pthread_t threads[N_THREADS];
- ThreadData thread_data[N_THREADS];
- int bytesPerTh = fsize / N_THREADS;
- for (int i = 0; i < N_THREADS; i++) {
- thread_data[i].start = i * bytesPerTh;
- thread_data[i].end = (i == N_THREADS - 1) ? fsize : (i + 1) * bytesPerTh;
- thread_data[i].file_content = file_content;
- if (pthread_create(&threads[i], NULL, routine, &thread_data[i]) != 0) {
- perror("pthread_create");
- free(file_content);
- exit(1);
- }
- }
- // Join threads
- for (int i = 0; i < N_THREADS; i++) {
- pthread_join(threads[i], NULL);
- }
- // Print the result
- printf("Total lowercase letters: %d\n", cnt);
- // Clean up
- free(file_content);
- pthread_mutex_destroy(&mtx);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement