Question: [In C] You are given the following code and you will change it using mutex lock functions. You will implement a multi-threaded ASCII character count
[In C]
You are given the following code and you will change it using mutex lock functions.
You will implement a multi-threaded ASCII character count program and compare the results with and without synchronization. First, update the code and use a one-dimensional global int array 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.
Compare the character counts of both programs on the same text file.
[Given Code]:
#include
//define the thread count as 8 #define NUMBER_OF_THREAD 8
//set the buffer of length 65536 bytes #define BUFFERLENGTH 65536
//global 128 element 2d int array int countCharacters[NUMBER_OF_THREAD][128];
//character array char buffer[BUFFERLENGTH];
//structure to store the thread information typedef struct{ int thred_limit; int count_thread; } myThread;
//the function to perform the thread run void *threadRun(void *arrayMythread) { int itr, locationOfThread; char ascii_char_encoutered;
myThread myThreadInstance = *(myThread*) arrayMythread; locationOfThread = myThreadInstance.count_thread * myThreadInstance.thred_limit; ascii_char_encoutered = buffer[locationOfThread];
//the for loop to process each thread for(itr = 0; locationOfThread < BUFFERLENGTH && itr < myThreadInstance.thred_limit; itr++) { ascii_char_encoutered = buffer[locationOfThread++]; countCharacters[myThreadInstance.count_thread][ascii_char_encoutered]++; } pthread_exit(NULL); }
//function to compute the time double timeInSec(void){ return (double)clock()/CLOCKS_PER_SEC; }
//the main function int main(int argc, char* argv[]){
//the pthread array pthread_t arrayPthread[NUMBER_OF_THREAD]; //create the structre array myThread arrayMythread[NUMBER_OF_THREAD]; //local variables int itr, i, asciiFile, threadParts, totalNumOfchar, threadObj; double threadVal;
//open the file that passed as first commandLine asciiFile = open(argv[1], O_RDONLY);
//if error in opening the file if (asciiFile < 0){ fprintf(stderr, "File Open error! "); return -1; }
asciiFile = read(asciiFile, buffer, BUFFERLENGTH);
threadVal = timeInSec();
threadParts = (double) asciiFile / (double) NUMBER_OF_THREAD; close(asciiFile);
for(itr = 0; itr < NUMBER_OF_THREAD; itr++) { arrayMythread[itr].count_thread = itr; arrayMythread[itr].thred_limit = threadParts; threadObj = pthread_create(&arrayPthread[itr], NULL, &threadRun, &arrayMythread[itr]); if(threadObj < 0) printf("Error creating thread! %itr ", itr); }
itr = 0; do{ pthread_join(arrayPthread[itr], NULL); itr++; } while (itr < NUMBER_OF_THREAD);
for(itr = 0; itr < 128; itr++) { totalNumOfchar = 0; for(i = 0; i < NUMBER_OF_THREAD; i++) { totalNumOfchar += countCharacters[i][itr]; }
printf("%i occurrences of ", totalNumOfchar);
if(itr < 33 || itr == 127){ printf("%#0x ", itr); } else{ printf("%c ", itr); } }
threadVal = timeInSec() - threadVal;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
