0% found this document useful (0 votes)
32 views

Project 9 Lab Java

This project proposal summarizes a C program that uses threads and semaphores to efficiently search for prime numbers in a 2D array. The program defines producer and consumer threads that insert and retrieve rows from the 2D array using shared buffers protected by mutex locks and counting semaphores. The main function initializes the arrays, creates the threads, and joins the threads before printing the total prime numbers found and destroying the synchronization objects.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Project 9 Lab Java

This project proposal summarizes a C program that uses threads and semaphores to efficiently search for prime numbers in a 2D array. The program defines producer and consumer threads that insert and retrieve rows from the 2D array using shared buffers protected by mutex locks and counting semaphores. The main function initializes the arrays, creates the threads, and joins the threads before printing the total prime numbers found and destroying the synchronization objects.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Project Proposal

"Efficient Prime Number Search with


Thread Synchronization using
Semaphores and Mutex"

Instructor: Dr. Raheel Memon.

Student: Syed Muhammad Raza


023-21-0121
BSCS-IV-E
This code is an example of a producer-consumer problem
implemented using threads and semaphores in C. It simulates a
scenario where producers produce data into a shared buffer, and
consumers consume the data from the buffer.
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#define BufferSize 100

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;

Including necessary libraries.


And declarations of mutex and counting semaphores.
And the variables for shared resources and tracking prime numbers.

void *producer(void *pno) {


for (int i = 0; i < 5; i++) {
sem_wait(&emptyCount);
pthread_mutex_lock(&mutex);

// Initializing Address of first item of each row of 2d array to dynamic 1d


array:
arr1dDynamic[in] = &arr2d[i][0];
printf("Producer %d: Insert Address of Value: %d, From Row: %d\n\n",
*((int *)pno), *arr1dDynamic[in], in);

in = (in + 1) % BufferSize;
pthread_mutex_unlock(&mutex);
sem_post(&fullCount);
}
}

- This is the thread function of the producer we wait for and


empty slot and lock the mutex and we are assigning the address
of a row in the 2D array to the dynamic 1D array and prints the
address and row number.
- After that we update the idices and unlock the mutex and also
call the signal from semaphoreS.

void *consumer(void *cno) {


for (int i = 0; i < 5; i++) {
sem_wait(&fullCount);
pthread_mutex_lock(&mutex);
int totalPrimeCon = 0;
/*
Printing all prime numbers in each row in this loop,
out contains the row number on which currently working:
*/
for (int j = 0; j < 5; j++) {
int n = *((arr1dDynamic[out]) + j);
int flag = 0;

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;
}

pthread_t pro[5], con[5];


pthread_mutex_init(&mutex, NULL);
sem_init(&emptyCount, 0, BufferSize);
sem_init(&fullCount, 0, 0);
/*
proConNum array contains number of Producer and Consumers
Created
So that to print on output Screen Producer or Consumer Number:
*/
int proConNum[2] = {1, 2};

/*
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]);

// Applying join so that program will not terminate before completion of


thread:
for (int i = 0; i < 2; i++)
pthread_join(pro[i], NULL);
for (int i = 0; i < 2; i++)
pthread_join(con[i], NULL);

printf("Total Prime Number found in 2d-Array: %d\n\n", totalPrime);


// Destroying empty, full and mutex lock created before:
pthread_mutex_destroy(&mutex);
sem_destroy(&emptyCount);
sem_destroy(&fullCount);
return 0;
}

- In the main func we initialize the @D array with numbers and


declaire the threads mutex and the semaphores and we create
procuders and consumer threads and we wait for the threads to
finish finally we print the total number of primes and destroy the
mutex and the semaphores.

Output:

You might also like