Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C Eile Edit View History Bookmarks Iools Help x G segmentation fault Googl x

ID: 3778770 • Letter: I

Question

In C

Eile Edit View History Bookmarks Iools Help x G segmentation fault Googl x G Write a multithread C prog x c Chegg Study I Guided Solu x pdf oject, Bb Assignments-Csci 423 Int. Writing Assignment4 https://blackboard.olemiss.edu/bbcswebd WANG 2017 FALL/Project,pdf G All LB board Home Pa O Spiceworks -open Tic... SurveyMonkey Free o citrix Secure sign In LBb Assignments-csci 42. Part A Write a multithread C program to count the frequency of words in a text file. In the program, the main thread should get the input file name from the command line arguments, open the file, do necessary global variable initialization, and perform necessary error checking during these steps. The main thread then creates a child thread to read each word from the text file. If the word has not appeared before, the child thread should access one global variable to store this word and set its frequency to 1. If the word has appeared before, the child thread should access the global variable to increase its frequency by 1. After the entire file is processed, the main thread then access the global variable and output the word-frequency data on the screen. The output should be one word each line, in the form of "word frequency". And all the words should be output alphabetically. For example, assume the compiled program is named as "a.out" a. out test2 txt and 6 can 3 file 3 have 3 is 6 it 9 large 3 line 3 lines 3 multiple 3 one 3 or 3 single 3 test 3 very 3 Pr 9:44 AM /29/2016

Explanation / Answer

#include <stdio.h>

#include <pthread.h>

#include <stdlib.h>

struct thread_data

{

FILE *fp;

long int offset;

int start;

int blockSize;

};

int words=0;

void *countFrequency(void* data)

{

struct thread_data* td=data;

char *buffer = malloc(td->blockSize);

int i,c;

i=0;c=0;

enum states { WHITESPACE, WORD };

int state = WHITESPACE;

fseek(td->fp, td->offset, td->start);

char last = ' ';

while ((fread(buffer, td->blockSize, 1, td->fp))==1)

{

if ( buffer[0]== ' ' || buffer[0] == ' ' )

{

state = WHITESPACE;

}

else if (buffer[0]==' ')

{

state = WHITESPACE;

}

else

{

if ( state == WHITESPACE )

{

words++;

}

state = WORD;

}

last = buffer[0];

}

free(buffer);

pthread_exit(NULL);

return NULL;

}

int main(int argc, char **argv)

{

int nthreads, x, id, blockSize,len;

FILE *fp;

pthread_t *threads;

struct thread_data data[nthreads];

if (argc < 2)

{

fprintf(stderr, "Usage: ./a.out <file_path>");

exit(-1);

}

if((fp=fopen(argv[1],"r"))==NULL)

{

printf("Error opening file");

exit(-1);

}

printf("Enter the number of threads: ");

scanf("%d",&nthreads);

threads = malloc(nthreads*sizeof(pthread_t));

fseek(fp, 0, SEEK_END);

len = ftell(fp);

printf("len= %d ",len);

blockSize=(len+nthreads-1)/nthreads;

printf("size= %d ",blockSize);

for(id = 0; id < nthreads; id++)

{

data[id].fp=fp;

data[id].offset = blockSize;

data[id].start = id*blockSize+1;

}

data[nthreads-1].start=(nthreads-1)*blockSize+1;

for(id = 0; id < nthreads; id++)

pthread_create(&threads[id], NULL, &countFrequency,&data[id]);

for(id = 0; id < nthreads; id++)

pthread_join(threads[id],NULL);

fclose(fp);

//free(threads);

//pthread_exit(NULL);

printf("%d ",words);

return 0;

}