OS C Program question

2.1 Introduction
In this problem, you are required to implement a word counter with two processes to automatically count the number of words in text files (with suffix .txt). You should achieve this with a C program that uses two processes (named parent process and child process respectively, both of which use only one thread). The parent process reads text files (with suffix .txt) under the given directory (which can be obtained from the input parameter of this program). Then the child process counts the number of words in those text files. Two processes communicate with each other using a shared memory (the limited size is 1MB). You should write your code in the provided problem2.c file.
The following table lists some functions may be used in your program implementation:
opendir() readdir() stat(), lstat() fopen() fseek() fread() fclose()
2.2 Requirements
Open a directory
Read a directory, return a dirent structure
Get file/directory status, return a stat structure Open a file and associates a stream with it Reset the file position indicator for the stream Read the file data
Close a file and the stream
Please read the following requirements carefully, your implementation should comply with these requirements:
l The first step is to find all the text files (no more than 100) under the given directory. Since the
number of the text files and their locations in the directory are unknown, you should design your code to traverse the directory to find all the text files under it. You can directly implement this into the traverseDir() function. By invoking traverseDir() function with a given directory, the program should find all the text files and their access paths under the directory.
l The parent process should read the text files one-by-one and write the content of each text file into a shared memory with limited buffer size of 1MB. (Hint: you should handle the case that the total size of a text file exceeds the buffer size.) Please note only one shared memory with limited buffer size of 1MB is allowed to be used in your program.
l Once the parent has written the content of a text file into the shared memory, the child process can read the content from the shared memory and count the number of words. After that, the parent process can write data of another text file into the shared memory again.
l Since the parent process should not modify the shared memory when the child process is reading from it (and vice versa), you should use the semaphore to achieve mutual exclusion while the processes access (i.e., read or modify) the shared memory.
l It requires that the writing operation of the parent process and the reading operation of the child process must alternate. You should use semaphore to synchronize the operations of the two processes on the shared memory.
l We provide the wordCount() function in helplers.h, in which the counting is done in a simple manner by looking for spaces (i.e., ¡° ¡±) and newline characters (i.e., ¡°\n¡±) until the string
Github
terminator is reached. You must invoke this provided function to count the number of words in
each of the text files.
l When all text files have been counted, the child process should write the total number of words
in all text files into a text file by calling the saveResult() function, which is located in helper.h. The function has two input parameters, i.e., the name of the text file (must be ¡°p2_result.txt¡± for Problem 2) and the total number of words in all text files.
Hints: In Problem 1, the semaphore is used for the purpose of mutual exclusion. Mutual exclusion means that at any time only one process can access a ¡°critical section¡± (which can be the variables in this case) that you want only one process/thread to access at a time. In this Problem 2, you should also think about how to use the semaphore to achieve synchronization between the parent and child processes. That is, the parent process must first write data into the shared memory. And then the child process can read the data from the shared memory. After that, the parent process can write data again and then the child process reads the data. This is repeated for several times.
2.3 Test you program
We provide 3 test cases in the compressed file named project.zip for you to test your code. We will use other test cases when grading your submission. These 3 test cases are just to help you to evaluate your solution. Before submitting your program, please run it on the test cases and check whether it works correctly.
You can compile the files based on the provided Makefile (you may modify it before using it for this problem) following this command:
or you can directly use the following command:
gcc problem2.c helper.c -o problem2 -I. -pthread
After compiling, you can run the executable file to test your program with the provided test cases, by typing command:
./problem2where source_dir is the name of a source directory (such as, test_case1). If your program works correctly, it should be able to save the result, i.e., the total number of words in a given directory, into p2_result.txt file.
2.4 Describe your design in project report
In project report, you should describe your design details in implementing your program. See Grading/Design for more details. Please also answer the following four questions:
1) Could your program pass all the provided 3 test cases? If not, please give the possible reason.
2) How you find all the text files under a directory, i.e., the implementation of traverseDir() function.
3) How you achieve the synchronization between two processes using the semaphore.
4) How you handle the case that the total size of a text file exceeds the buffer size.

Code Help
In Problem 2, both the parent and child processes have only one thread. When the given directory contains many text files, or the text files have a large size of content, it would be inefficient. In this problem, you are asked to extend your program implemented in Problem 2 with multi-threaded programming. You should implement this program into a source file named as problem3.c (Please create problem3.c yourself. You may reuse the code in problem1.c and problem2.c).
3.1 Requirements
The requirements for this problem are same as those in Problem 2, except that the child process should be implemented with multiple threads to count the words of each text file based on POSIX thread. Your solution should comply with the following requirements:
l You should use 4 threads in the child process.
l You must call the provided wordCount() function to count the number of words. Please do not modify the implementation of wordCount() function.
l When all text files have been counted, the child process should write the total number of words in all text files into a text file (must be ¡°p3_result.txt¡± for this problem) by calling the saveResult().
2.3 Test you program
You can also use the provided 3 test cases to test your code. We will use other test cases when grading your submission. These 3 test cases are just to help you to evaluate your solution. Before submitting your program, please run it on the test cases and check whether it works correctly.
You can compile the files based on the provided Makefile (you may modify it before using it for this problem) following this command:
or you can directly use the following command:
gcc problem3.c helper.c -o problem3 -I. -pthread
After compiling, you can run the executable by typing command: ./problem3where source_dir is the name of a source directory (such as, test_case1). If your program works correctly, it should be able to save the result, i.e., the total number of words in a given directory, into p3_result.txt file.
3.3 Describe your design in project report
In your project report, please describe your design details in implementing your program. See Grading/Design for more details. Please also answer the following question:
1) How you parallelize the word counting operation with multiple threads in the child process.

程序代写 CS代考 加QQ: 749389476