Skip to content

Commit

Permalink
Source Code
Browse files Browse the repository at this point in the history
  • Loading branch information
HasanYahya101 committed Nov 8, 2024
1 parent a0478f5 commit 1d4ade2
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 0 deletions.
139 changes: 139 additions & 0 deletions Lab_10/Question_01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <time.h>

#define SIZE 10
#define PRODUCERS 3
#define CONSUMERS 2
#define ITEMS 50

int buffer[SIZE];
int in = 0;
int out = 0;
int items_produced = 0;
int items_consumed = 0;

sem_t empty;
sem_t full;
sem_t mutex;

void *producer(void *arg)
{
int producer_id = *((int *)arg);

while (1)
{
sem_wait(&mutex);
if (items_produced >= ITEMS)
{
sem_post(&mutex);
break;
}
sem_post(&mutex);

sem_wait(&empty);
sem_wait(&mutex);

if (items_produced >= ITEMS)
{
sem_post(&mutex);
sem_post(&empty);
break;
}

int item = rand() % 100;
buffer[in] = item;
printf("Producer %d produced: %d at position %d\n", producer_id, item, in);
printf("Total items produced: %d\n", items_produced + 1);
in = (in + 1) % SIZE;
items_produced++;

sem_post(&mutex);
sem_post(&full);

usleep(rand() % 1000000);
}
return NULL;
}

void *consumer(void *arg)
{
int consumer_id = *((int *)arg);

while (1)
{
sem_wait(&mutex);
if (items_consumed >= ITEMS)
{
sem_post(&mutex);
break;
}
sem_post(&mutex);

sem_wait(&full);
sem_wait(&mutex);

if (items_consumed >= ITEMS)
{
sem_post(&mutex);
sem_post(&full);
break;
}

int item = buffer[out];
printf("Consumer %d consumed: %d from position %d\n", consumer_id, item, out);
printf("Total items consumed: %d\n", items_consumed + 1);
out = (out + 1) % SIZE;
items_consumed++;

sem_post(&mutex);
sem_post(&empty);

usleep(rand() % 1000000);
}
return NULL;
}

int main()
{
srand(time(NULL));

sem_init(&empty, 0, SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);
pthread_t producers[PRODUCERS];
pthread_t consumers[CONSUMERS];
int producer_ids[PRODUCERS];
int consumer_ids[CONSUMERS];

for (int i = 0; i < PRODUCERS; i++)
{
producer_ids[i] = i + 1;
pthread_create(&producers[i], NULL, producer, &producer_ids[i]);
}

for (int i = 0; i < CONSUMERS; i++)
{
consumer_ids[i] = i + 1;
pthread_create(&consumers[i], NULL, consumer, &consumer_ids[i]);
}

for (int i = 0; i < PRODUCERS; i++)
{
pthread_join(producers[i], NULL);
}
for (int i = 0; i < CONSUMERS; i++)
{
pthread_join(consumers[i], NULL);
}

sem_destroy(&empty);
sem_destroy(&full);
sem_destroy(&mutex);

printf("All items have been produced and consumed.\n");
return 0;
}
100 changes: 100 additions & 0 deletions Lab_10/Question_02/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define ARRAY_SIZE 5
#define TOTAL_SHIPMENTS 10
#define RATE 15

struct SharedData
{
int shipments[ARRAY_SIZE];
int in;
int out;
int count;
sem_t empty;
sem_t full;
sem_t mutex;
};

struct SharedData shared;
int processed_count = 0;

void *verifier(void *arg)
{
int shipment_id = 1001;

while (processed_count < TOTAL_SHIPMENTS)
{
sem_wait(&shared.empty);
sem_wait(&shared.mutex);

// Add shipment to array
if (shared.count < ARRAY_SIZE)
{
shared.shipments[shared.in] = shipment_id;
shared.in = (shared.in + 1) % ARRAY_SIZE;
shared.count++;
shipment_id++;
}

sem_post(&shared.mutex);
sem_post(&shared.full);

usleep(100000);
}
return NULL;
}

void *processor(void *arg)
{
while (processed_count < TOTAL_SHIPMENTS)
{
sem_wait(&shared.full);
sem_wait(&shared.mutex);

if (shared.count > 0)
{
int id = shared.shipments[shared.out];
shared.out = (shared.out + 1) % ARRAY_SIZE;
shared.count--;
processed_count++;

int invoice = id * RATE;
printf("Shipment ID: %d, Invoice: $%d\n", id, invoice);
}

sem_post(&shared.mutex);
sem_post(&shared.empty);

usleep(150000); // sleep for 0.15 seconds
}
return NULL;
}

int main()
{
shared.in = 0;
shared.out = 0;
shared.count = 0;

sem_init(&shared.empty, 0, ARRAY_SIZE);
sem_init(&shared.full, 0, 0);
sem_init(&shared.mutex, 0, 1);

pthread_t verifier_thread, processor_thread;
pthread_create(&verifier_thread, NULL, verifier, NULL);
pthread_create(&processor_thread, NULL, processor, NULL);

pthread_join(verifier_thread, NULL);
pthread_join(processor_thread, NULL);

sem_destroy(&shared.empty);
sem_destroy(&shared.full);
sem_destroy(&shared.mutex);

printf("Processing complete. Total shipments processed: %d\n", processed_count);
return 0;
}

0 comments on commit 1d4ade2

Please sign in to comment.