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

Please help me reedit the code to make it shows what I wish to show on the scree

ID: 3768439 • Letter: P

Question

Please help me reedit the code to make it shows what I wish to show on the screen.

There is a program like this:(a multithread C program to count the frequency of words in a text file. In the program, the main thread 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.)

#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);
printf("%d ",words);
return 0;
}

After compiling and running, it shows below on the screen:

./a.out test2.txt ----commond line arguement
Enter the number of threads: 10
len= 286
size= 29
Segmentation fault

However, what I wish it to show on the screen is:

./b.out test2.txt 10   ----commond line arguement
a 3
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
./b.out ----commond line arguement
Usage: ./b.out <input text file name> <number of threads>

./b.out input.txt output.txt   ----commond line arguement
Usage: ./b.out %s <input text file name> <number of threads>
./b.out input.txt 8 ----commond line arguement
Input file input.txt cannot be opened.

Note: There is a test2.txt file should be used in this program, it is a text file contains:

It is a "test" file, and it is very large.
And it can have one single line or multiple lines.
It is a "test" file, and it is very large.
And it can have one single line or multiple lines.
It is a "test" file, and it is very large.
And it can have one single line or multiple lines.

please create this txt file and copy above words in it after you compile your code and before running it. Thank you!

Explanation / Answer

program :