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;
- typedef struct {
- char *str; // Portion of the file assigned to the thread
- int cnt; // Count of lowercase letters in this portion
- } ThArg;
- off_t getFileSize(const char fpath[]) {
- struct stat statBuf;
- if (stat(fpath, &statBuf)) {
- perror("stat");
- exit(1);
- }
- return statBuf.st_size;
- }
- void *routine(void *arg) {
- ThArg *thArg = (ThArg *)arg;
- // Count lowercase letters in the assigned portion
- for (int i = 0; thArg->str[i]; i++) {
- if (islower(thArg->str[i])) {
- thArg->cnt++;
- }
- }
- // Free the allocated memory for the string
- free(thArg->str);
- thArg->str = 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);
- }
- // Allocate memory for thread arguments
- ThArg *thArgs = (ThArg *)zalloc(N_THREADS, sizeof(ThArg));
- pthread_t threads[N_THREADS];
- // Calculate the number of bytes each thread will process
- int bytesPerTh = fsize / N_THREADS;
- for (int i = 0; i < N_THREADS; i++) {
- // Allocate memory for the thread's portion of the file
- int start = i * bytesPerTh;
- int end = (i == N_THREADS - 1) ? fsize : (i + 1) * bytesPerTh;
- int chunkSize = end - start;
- thArgs[i].str = (char *)zalloc(chunkSize + 1, sizeof(char));
- thArgs[i].cnt = 0;
- // Read the thread's portion of the file
- if (lseek(fd, start, SEEK_SET) == -1) {
- perror("lseek");
- free(thArgs[i].str);
- free(thArgs);
- close(fd);
- exit(1);
- }
- if (read(fd, thArgs[i].str, chunkSize) != chunkSize) {
- perror("read");
- free(thArgs[i].str);
- free(thArgs);
- close(fd);
- exit(1);
- }
- thArgs[i].str[chunkSize] = '\0'; // Null-terminate the string
- // Create the thread
- if (pthread_create(&threads[i], NULL, routine, &thArgs[i]) != 0) {
- perror("pthread_create");
- free(thArgs[i].str);
- free(thArgs);
- close(fd);
- exit(1);
- }
- }
- // Join threads
- for (int i = 0; i < N_THREADS; i++) {
- pthread_join(threads[i], NULL);
- }
- // Sum up the results
- int total_lowercase = 0;
- for (int i = 0; i < N_THREADS; i++) {
- total_lowercase += thArgs[i].cnt;
- }
- printf("Total lowercase letters: %d\n", total_lowercase);
- // Clean up
- free(thArgs);
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement