Project 9 Lab Java
Project 9 Lab Java
pthread_mutex_t mutex;
sem_t emptyCount;
sem_t fullCount;
int *arr1dDynamic[100];
int arr2d[100][100];
int totalPrime = 0;
int out = 0;
int in = 0;
in = (in + 1) % BufferSize;
pthread_mutex_unlock(&mutex);
sem_post(&fullCount);
}
}
if (n == 0 || n == 1)
flag = 1;
for (int k = 2; k <= n / 2; k++) {
if (n % k == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("%d ", n);
totalPrimeCon += 1;
totalPrime += 1;
}
}
printf("\nConsumer %d: Finds Prime Number from Row: %d, Total Prime
Numbers Found in a Row: %d\n\n", *((int *)cno), out, totalPrimeCon);
out = (out + 1) % BufferSize;
pthread_mutex_unlock(&mutex);
sem_post(&emptyCount);
}
- This is the function for the consumer and we wait for a full slot and also
we lock the mutex and we iterate the eements in the row and retrive the
numbers from the dynamic array and check the prime numbar and then
we update the indices and unlock the mutex and call signal
int main() {
// Initializing each row of 2d array from 0 to 99:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
arr2d[i][j] = j;
}
/*
Creating Threads for Producer and Consumer,
Number of iterations of each loop will decide
how much producer and consumer has to be created,
Right now creating 2 Producers and 2 Consumers:
*/
for (int i = 0; i < 2; i++)
pthread_create(&pro[i], NULL, (void *)producer, (void
*)&proConNum[i]);
for (int i = 0; i < 2; i++)
pthread_create(&con[i], NULL, (void *)consumer, (void
*)&proConNum[i]);
Output: