+1 (315) 557-6473 

Threads and Synchronization Homework Help

Do you need someone to help you with your threads and synchronization homework? Are you tired of searching for a reliable threads and synchronization homework help? Programming Homework Help is the answer to all these questions. Students who avail of our top-notch threads and synchronization homework do not have to worry about homework. Our adept tutors handle the homework on their behalf and deliver custom-written solutions. You too can have your homework written by professionals by taking our threads and synchronization project help.

Two-Thread Program

You’ll write a program that has two threads. The first thread \tells" the second thread a phrase, and the second thread checks whether the phrase contains each vowel a-e-i-o-u and then \tells" the first thread the result of this check. You won’t have to worry about case: all of the words to be checked have only lower-case letters.
You’ll do this in two parts:
Part I: the thread infrastructure
Part II: the synchronization
First, get pthreads-example.c from the class gitlab repo. Compile this and run it. This program shows an example of how to create pthreads and pass data to them. On kaladin, you will need to compile this in the following way:
$ gcc pthreads-example.c -lpthread
You can also run this on macOS (but unfortunately, not on Windows). To compile it at the command line on macOS:
$ gcc pthreads-example.c

Creating A VowelChecker

Create your own vowelchecker1.netid.c
Next, write a function called void *checker(void *). Your main() will create a pthread that will execute this function.
Write a program that will do the following:
for each word in a hard-coded word list, have the main() write the word into a global bu er
the checker function will then look at the contents of the global bu er and decide whether or not the phrase in the bu er contains at least one instance of each vowel
if so, then the checker will put the value 1 in a global integer variable; otherwise it will put the value 0
1
the main thread will then look at the value in the global integer and write out a message based on the value
Use this structure to pass information to the checker thread:
typedef struct {
int numPhrases;
} SyncInfo;
It’s OK to put this structure de nition in your .c le. In Part II, you’ll add some other things to the structure.
Here are the phrases to check:
char *phrases[] = { "educated", "educated cat", "educated lion", "serious person", \ "serious panda", "curious student", "curious art student", "obnoxious web developer"};
Here’s a high-level description of the program:
#define BUFLEN 128
char buffer[BUFLEN];
int allVowelsPresent;
void *checker(void *param) {
// set n to numPhrases, from syncInfo structure, which is passed through param for (i=0; i
strcpy(localBuffer, buffer);
// if localBuffer has at least one instance of each vowel, then set allVowelsPresent to 1
// else set allVowelsPresent to 0
}
}
int main() {
SyncInfo syncInfo;
syncInfo.numPhrases = 8;
// pthread_create() to create the second thread; pass it a reference to syncInfo
for (i=0; i
strcpy(buffer, phrases[i]);
printf("result of checking ’%s’: %d\n", phrases[i], allVowelsPresent);
}
}
This won’t work in the way that we want it to (think about why it won’t). In Part II, you’ll x this.
3 What to Submit
Submit your vowelchecker1.netid.c le.

Adding Synchronization

Now, you’ll fix this by adding synchronization. At a high level, the two threads (the main thread and the checker thread) will coordinate their actions through the use of semaphores. After the main thread copies a phrase into the global bu er, it needs to \tell" the checker thread that the bu er is ready. And after the checker thread analyzes the phrase in the bu er and puts its result in the global allVowelsPresent variable, the checker thread \tells" the main thread that the result is ready.
Think about how to do this specifically, how each thread will execute its actions and coordinate its actions with the other thread. You’ll want to use the semaphore mechanism. See the example of semaphores in semaphore-example-threads.c, from the class gitlab.
In addition, there will be one more type of synchronization required. Since both threads will be accessing the same global variables, they will need a mutex to coordinate their access. Each time a thread wants to access a shared resource (in this case, one of the global variables), it must do pthread mutex lock() before the access and pthread mutex unlock() after the access. It’s su cient to have a single mutex variable.
To reduce contention, minimize the amount of processing done between the lock and the unlock. A good way to do this is to create a local char[] bu er in the checker thread: do the lock, strcpy the global bu er to the local bu er, and then unlock. Then, the checker can safely analyze the local bu er without needing the mutex.
Put the synchronization structures (mutex, semaphore) in the SyncInfo structure.
Put your second part in a le vowelchecker2.netid.c.

Code Testing

How will you know whether this works? You should see the coordinated exchange of words and 1/0 responses, in proper order. For example:
checker: will check 8 phrases
checked ’educated’: result is 0
result of checking ’educated’: 0
checked ’educated cat’: result is 0
result of checking ’educated cat’: 0
checked ’educated lion’: result is 1
result of checking ’educated lion’: 1
checked ’serious person’: result is 0
result of checking ’serious person’: 0
checked ’serious panda’: result is 1
result of checking ’serious panda’: 1
checked ’curious student’: result is 0
result of checking ’curious student’: 0
checked ’curious art student’: result is 1
result of checking ’curious art student’: 1
checked ’obnoxious web developer’: result is 0
result of checking ’obnoxious web developer’: 0
My checker() function prints the \checked" lines, and my main() prints the \result of" lines.

Related Blogs