C Assignment Help

profilekam13

C program. NOT C++
specialcountmulthreads.c is not completed. Need some modification

31 text files named input_00.txt to input_30.txt.

Requirements:

Multiple threads are expected to run in parallel to share the workload, i.e. suppose 3 threads to process 30 files totally, then each thread should process 10 files;

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!

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

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 !

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.]

======specialcountmulthreads.c=====================

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <string.h>

#include <pthread.h>

#define MAX_FILES 30

#define MAX_LEN 1000

int num_threads;

char *filename[MAX_FILES];

int num_files;

long specialfreq[MAX_FILES];

int *index_array[MAX_FILES];

void *thread_function(void *arg) {

int *index = (int *) arg;

int i;

int my_index = *index;

int starting_index = my_index * (num_files / num_threads);

int ending_index = starting_index + (num_files / num_threads);

FILE *fp;

char ch;

int count = 0;

char line[MAX_LEN];

printf(“Thread ID: %lu starts working on files %d to %d.n”, pthread_self(), starting_index, ending_index);

for (i = starting_index; i < ending_index; i++) {

fp = fopen(filename[i], “r”);

if (fp == NULL) {

perror(“Error opening file”);

} else {

printf(“Thread ID: %lu is working on file %sn”, pthread_self(), filename[i]);

while (fgets(line, sizeof (line), fp) != NULL) {

ch = fgetc(fp);

if (ch == ‘!’) {

count++;

}

}

specialfreq[i] = count;

fclose(fp);

}

}

printf(“Thread ID: %lu is done!n”, pthread_self());

pthread_exit(NULL);

}

void specialcountmulthreads(char *path, char *filetowrite, long specialfreq[], int num_threads) {

pthread_t tid;

int i;

int err;

int total_count = 0;

if (argc < 3) {

printf(“Usage: ./a.out num_threads input_file1 input_file2 … input_file30”);

exit(0);

}

num_threads = atoi(argv[1]);

num_files = argc – 2;

//printf(“Num thread: %dn”, num_threads);

//printf(“Num files: %dn”, num_files);

for (i = 2; i < argc; i++) {

filename[i – 2] = argv[i];

//printf(“Argv: %sn”, argv[i]);

//printf(“Filename: %sn”, filename[i – 2]);

}

for (i = 0; i < num_files; i++) {

index_array[i] = malloc(sizeof (int));

*index_array[i] = i;

//printf(“Index array: %dn”, *index_array[i]);

//printf(“Index array: %dn”, index_array[i]);

}

for (i = 0; i < num_threads; i++) {

err = pthread_create(&tid, NULL, thread_function, index_array[i]);

if (err != 0) {

printf(“ncan’t create thread :[%s]”, strerror(err));

}

}

for (i = 0; i < num_files; i++) {

total_count += specialfreq[i];

}

printf(“Total count: %dn”, total_count);

return 0;

}

======================testmulthreads.c=====================================================

#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 -> %dn”, “he”, specialfreq[i]);

break;

case 1:

printf(“%s -> %dn”, “she”, specialfreq[i]);

break;

case 2:

printf(“%s -> %dn”, “they”, specialfreq[i]);

break;

case 3:

printf(“%s -> %dn”, “him”, specialfreq[i]);

break;

case 4:

printf(“%s -> %dn”, “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

}

  • attachment

    ProgrammingAssignment2DUEby11_59pmJuly25th.pdf
  • attachment

    two.zip
  • a month ago
  • 24.07.2022
  • 25
Report Issue
ANSWER(0)

BIDS(88)

  • Dr. Sophie Miles
  • Musyokiones
  • abdul_rehman_
  • Dr. Diana_Ashley
  • Bethuel Best
  • Dr. Michelle_PhD
  • Teacher A+ Work
  • RihAN_Mendoza
  • PROF. ANN
  • Dr. Rachael Malk
  • Teachermulandy
  • sochien
  • Creative Geek
  • Dr shamille Clara
  • Abdullah Anwar
  • Coleen Anderson
  • Louisa95
  • Discount Assign
  • Dr Clover
  • PapersGuru
OTHER QUESTIONS(10)

  • Improved Health Behavior
  • Upload your Term Project Paper here. You may write on any topic related to technology security. You must produce a minimum of a 10 pages paper. You must use a minimum of 5 references. – Assure you are citing in APA format -You must use a minimum of one
  • for Prof Washington Watson
  • Hello
  • DQ2
  • far 15.607 proposals DUE 19 AUG NLT 2000 EST
  • Macroeconomic Multiple Choice online exam
  • Case Study 2: Implementation Strategies
  • Researching Marketing Questions
  • case study