Please help me write a program in a operating system class using C program, I wi
ID: 3768361 • Letter: P
Question
Please help me write a program in a operating system class using C program, I wish that the answer code can be compiled successfully and I also wish that I will get the same output as the "example output"
please please help me get a perfect answer, because I really need it. I attached the requirement of two parts(means there should be two .c program file but the second one is a updated version of the first one), two example output of part A and Part B, and a code which is similar to the correct answer for reference.
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.
Example output for part A:
./a.out test2.txt
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
where the content of the input text file is
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.
Part B:
Extend the program in the Part A to create multiple children threads. The number of children threads should be input as another command line argument.
Example output for Part B:
./b.out test2.txt 10
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
Usage: ./b.out
./b.out input.txt output.txt
Usage: ./b.out %s
./b.out input.txt 8
Input file input.txt cannot be opened.
Example code:(This is a version of code which is similar to the answer, however it does not output correct information on the screen.)
#include
#include
#include
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 ");
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;
}
Hint:
1. You may need to use string manipulation functions such as strlen(), strcpy(),
strncpy(), strcmp(), strstr(), etc. by include "string.h" in your code.
2. You may want to identify and divide the whole job into different smaller and easy-to-handle tasks (such as error checking, reading a word from the file, changing all letters in a word into lower case, removing punctuation, updating the word frequency, etc.) and implement them in different functions.
3. You may want to refer to the codes of your programming assignments, adopting and modifying part of them to be used in your project (e.g., create, maintain and update linked list).
4. You may want to start from the one thread version, where all the tasks are done within the main thread. Then create the child thread and move tasks to it.
5. You may assume the maximum length of a word is 512.
Explanation / Answer
Program :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.