In: Computer Science
I have the following code
#include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases; }SyncInfo; char buffer[BUFLEN] ; char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"}; char localBuffer[BUFLEN]; int allVowelsPresent; void *checker(void *param) { int a=0, e=0, i=0, o = 0, u= 0 ; int* n = (int*)param; // typecasting a void* to int* //printf("%d\n",*n); for (int q=0; q< (*n); ++q) { // dereferencing to get the value in int* strcpy(localBuffer, buffer); int length=strlen(localBuffer); // strlen is used to determine the length of localBuffer for(int j=0 ; j<length;j++) // iterating over the whole length of localBuffer and checking if vowel present { if(localBuffer[j]=='a') a=1; else if(localBuffer[j]=='e') e=1; else if(localBuffer[j]=='i') i=1; else if(localBuffer[j]=='o') o=1; else if(localBuffer[j]=='u') u=1; } if(a&&e&&i&&o&&u) // if all are present ie none of the value of a, e, i, o ,u is 0 then all vowels are present allVowelsPresent = 1 ; else allVowelsPresent = 0 ; } } int main() { SyncInfo syncinfo; syncinfo.numPhrases = 8; SyncInfo* p = &syncinfo; // defining pointer of type SyncInfo pointing to syncinfo void* param = &p->numPhrases; // a void pointer as we have to pass void* in checker function for (int i=0; i<syncinfo.numPhrases; ++i) { strcpy(buffer, phrases[i]); checker(param); // main() calling the checker function printf("result of checking ’%s’: %d\n", phrases[i], allVowelsPresent) ; } }
In this code I didn't create a separate thread to do the checking , may someone implement and modify this. Also My checker function should have Syncinfo *s =(Syncinfo*) param please add that modification as well . The code works perfectly fine , just need to make some modification .
#include <stdio.h>
#include<string.h>
#include<pthread.h>
#define BUFLEN 128
typedef struct {
int numPhrases;
}SyncInfo;
pthread_mutex_t lock;//mutex lock handle declaration
pthread_t thread_id;//checker thread id declaration
char buffer[BUFLEN] ;
char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"};
char localBuffer[BUFLEN];
int allVowelsPresent;
void *checker(void *param) {
pthread_mutex_lock(&lock); //it locks the mutex_lock
int a=0, e=0, i=0, o = 0, u= 0 ;
int* n = (int*)param; // typecasting a void* to int*
//printf("%d\n",*n);
for (int q=0; q< (*n); ++q) { // dereferencing to get the value in int*
strcpy(localBuffer, buffer);
int length=strlen(localBuffer); // strlen is used to determine the length of localBuffer
for(int j=0 ; j<length;j++) // iterating over the whole length of localBuffer and checking if vowel present
{
if(localBuffer[j]=='a') a=1;
else if(localBuffer[j]=='e') e=1;
else if(localBuffer[j]=='i') i=1;
else if(localBuffer[j]=='o') o=1;
else if(localBuffer[j]=='u') u=1;
}
if(a&&e&&i&&o&&u) // if all are present ie none of the value of a, e, i, o ,u is 0 then all vowels are present
allVowelsPresent = 1 ;
else
allVowelsPresent = 0 ;
}
printf("checked '%s' : result is %d\n",localBuffer,allVowelsPresent);
pthread_mutex_unlock(&lock); //it unlocks the mutex lock
}
int main() {
if (pthread_mutex_init(&lock, NULL) != 0) { //initializing mutex
printf("\n mutex init has failed\n");
return 1;
}
SyncInfo syncinfo;
syncinfo.numPhrases = 8;
SyncInfo* p = &syncinfo; // defining pointer of type SyncInfo pointing to syncinfo
void* param = &p->numPhrases; // a void pointer as we have to pass void* in checker function
for (int i=0; i<syncinfo.numPhrases; ++i) {
strcpy(buffer, phrases[i]);
pthread_create(&thread_id, NULL, checker ,param); //It creates checker thread
pthread_join(thread_id, NULL); //It deploys the thread
pthread_mutex_lock(&lock);
printf("result of checking %s %d\n", phrases[i], allVowelsPresent) ;
pthread_mutex_unlock(&lock);
}// main() calling the checker function
}