C assignment help
two/testmulthreads.c
/* This file is to test your program#2. * */ #include <stdio.h> #include <dirent.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include<unistd.h> #include <stdbool.h> #include "count.h" /* * Print the frequencies of special words stored in array: specialfreq[] to output screen in the format as: * letter -> frequency (one letter a line) * Input: specialfreq - array having the frequency of each special word size - the total number of special words * example: * he -> 250932 * she -> 181764 */ void displayalphabetfreq(long specialfreq[], int size) { for(int i = 0; i < size; i++) { switch (i) { case 0: printf("%s -> %d\n", "he", specialfreq[i]); break; case 1: printf("%s -> %d\n", "she", specialfreq[i]); break; case 2: printf("%s -> %d\n", "they", specialfreq[i]); break; case 3: printf("%s -> %d\n", "him", specialfreq[i]); break; case 4: printf("%s -> %d\n", "me", specialfreq[i]); break; defalut: printf("%s", "Wrong number of special words ... "); } } } int main(int argc, char *argv[]) { printf("Please enter 2 arguments only eg.\"./testmulthreads #_of__threads!!\"\n"); int num_threads = atoi(argv[1]); char *path = "../data"; // the data *.txt files are under this folder char *filetowrite = "../result/result.txt"; // the frequency of all alphabetical letters will be written in this file long specialfreq[SPECIALSIZE] = {0}; // array to store the frequency of each alphablet letter, which should be alway up-to-date; specialcountmulthreads(path, filetowrite, specialfreq, num_threads); // process the data files using mutiple threads printf("The results are counted as follows : \n"); displayalphabetfreq(specialfreq, SPECIALSIZE); // print the frequency stored in the array to output screen }
two/count.h
/* charcount.h - This header file include type definitions (including function prototypes) and macros used for the programing assignment two. */ #include <stdio.h> #define SPECIALSIZE 5 //The total number of special word "he","she","they","him" and "me" (case insensitive)
two/makefile
# A Makefile for Program 2, CS480 # This is a sample, you may need to modify this file to make it works PROGRAM =testmulthreads CC = gcc CFLAGS = -g -std=c99 ${PROGRAM}: ${PROGRAM}.o specialcountmulthreads.o ${CC} -o ${PROGRAM} ${PROGRAM}.o specialcountmulthreads.o -lpthread -lm specialcountmulthreads.o ${PROGRAM}.o: count.h clean: rm -f *.o ${PROGRAM}
two/README
Please answer the following questions to explain your implementation (Directly write your answer below the questions). 1) In your implementation, please describe your strategy/design to count the special words using multiple threads. 2) In your implementation, please describe how you keep the specialfreq[ ] always having the up-to-date count. 3) Please measure the total time cost of special word count uisng your program. Please repeat at least three times and write down your time below. 4) Can you think of any other improvement in your implementation to speedup the special word count? If no, please briefly justify why you think your design is the best.
two/specialcountmulthreads.c
/* * specialcountmulthreads.c - this file implements the alphabetcountmulthreads function. */ #include <stdio.h> #include "count.h" /** The specialcountmulthreads function counts the frequency of each special word (he, she, they, him and me (case insensitive)) in all the .txt files under directory of the given path and write the results to a file named as filetowrite. Different with programming assignment#0, you need to implement the program using mutithreading. Input: path - a pointer to a char string [a character array] specifying the path of the directory; and filetowrite - a pointer to a char string [a character array] specifying the file where results should be written in. specialfreq - a pointer to a long array storing the frequency of each special word "he","she","they","him", "me", which should be already up-to-date; specialfreq[0] - > # of "he" counted specialfreq[1] - > # of "she" counted specialfreq[2] - > # of "they" counted specialfreq[3] - > # of "him" counted specialfreq[4] - > # of "me" counted num_threads - number of the threads running in parallel to process the files Output: a new file named as filetowrite with the frequency of each alphabet letter written in Requirements: 1) Multiple threads are expected to run in parallel to share the workload, i.e. suppose 3 threads to process 30 files, then each thread should process 10 files; 2) When a thread is created, a message should be print out showing which files this thread will process, for example: Thread id = 274237184 starts processing files with index from 0 to 10! 3) When a file is being processed, a message should be print out showing which thread (thread_id = xxx) is processing this file, for example: Thread id = 265844480 is processing file input_11.txt 4) When a thread is done with its workload, a message should be print out showing which files this thread has done with work, for example: Thread id = 274237184 is done ! 5) The array: long specialfreq[ ] should always be up-to-date, i.e. it always has the result of all the threads counted so far. [You may need to use mutexes to protect this critical region.] You should have the screen printing should be similar as follows: Thread id = 274237184 starts processing files with index from 0 to 10! Thread id = 265844480 starts processing files with index from 11 to 22! Thread id = 257451776 starts processing files with index from 23 to 31! Thread id = 265844480 is processing file input_11.txt Thread id = 257451776 is processing file input_22.txt Thread id = 274237184 is processing file input_00.txt … … Thread id = 274237184 is done ! Thread id = 265844480 is done ! Thread id = 257451776 is done ! The results are counted as follows: he -> 250932 she -> 181764 ... ... … … */ void specialcountmulthreads(char *path, char *filetowrite, long specialfreq[], int num_threads) { // TO-DO: please fill your code here ... }