Question: [In C] Part 1: Your program will receive the name of an ASCII text file as a command-line argument and output the number of each

[In C]

Part 1:

Your program will receive the name of an ASCII text file as a command-line argument and output the number of each kind of ASCII character in the file.

In your code, set up a 64KB (65,536 bytes) buffer in global memory and read the contents of the file into the buffer (or as much of it as will fit).

Then partition the buffer and spawn a number of threads to count the number of occurrences of each ASCII character in each partition. (Use #define directive to set the number of threads to 8.)

Have each thread record these counts in a separate 128- element int array in global memory (stored collectively as a 2-dimensional int array).

The partition bounds can be calculated in the main thread to be roughly equal in size and then passed to each worker thread using a struct.

You will also need to pass a thread index to each thread so that it knows where to write its local count in the global 2-dimensional int array.

Once the worker threads finish writing their local counts, they should exit.

After spawning the worker threads, the main thread should wait for them all to complete.

It should then add the partition counts for each ASCII character and print each overall count. (For nonprintable and white space ASCII characters, i.e., ASCII characters with decimal values smaller than 33, you should just print the hexadecimal character code.)

Here is the example execution result with the test file:

0 occurrences of 0x0 0 occurrences of 0x1 0 occurrences of ! 500 occurrences of A 500 occurrences of B 500 occurrences of C 500 occurrences of D ...

Hints:

You can use the function fopen()to open a file, and then use fread() to read the contents of the file into a buffer. You should use the functions pthread_attr_init(), pthread_create(), pthread_exit() and pthread_join()to create and manage the threads.

Part 2:

You will then change the program and compare the results with and without synchronization. Use a one-dimensional global int array this time, to hold all of the character counts. All the threads increment the same array entries without using any synchronization. Then, create a second version of the program that protects the increment of the global int array using a mutex. Run both versions on the same file and compare the character counts.

In order to observe the differences in the results of the two programs, we need to use a Ubuntu virtual machine with multiple CPUs. Below are the steps to change the number of CPUs in virtualbox:

1. Shut down the Ubuntu virtual machine;

2. Go to Setting -> Systems -> Processors.

3. Change the number of CPUs.

Hints:

Use the functions pthread_mutex_init(), pthread_mutex_lock()and pthread_mutex_unlock()to create, acquire, and release the mutex, respectively. Name your C program without synchronization as ASCIICount2, and name your C program with synchronization as ASCIICountS.

Compare the character counts of both programs on the same text file.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!