Skip to content
This repository was archived by the owner on Aug 1, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions DP.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define NUM_PHILOSOPHERS 5
#define NUM_CHOPSTICKS 5

void dine(int n);
pthread_t philosopher[NUM_PHILOSOPHERS];
pthread_mutex_t chopstick[NUM_CHOPSTICKS];

int main()
{
// Define counter var i and status_message
int i, status_message;
void *msg;

// Initialise the semaphore array
for (i = 1; i <= NUM_CHOPSTICKS; i++)
{
status_message = pthread_mutex_init(&chopstick[i], NULL);
// Check if the mutex is initialised successfully
if (status_message == -1)
{
printf("\n Mutex initialization failed");
exit(1);
}
}

// Run the philosopher Threads using *dine() function
for (i = 1; i <= NUM_PHILOSOPHERS; i++)
{
status_message = pthread_create(&philosopher[i], NULL, (void *)dine, (int *)i);
if (status_message != 0)
{
printf("\n Thread creation error \n");
exit(1);
}
}

// Wait for all philosophers threads to complete executing (finish dining) before closing the program
for (i = 1; i <= NUM_PHILOSOPHERS; i++)
{
status_message = pthread_join(philosopher[i], &msg);
if (status_message != 0)
{
printf("\n Thread join failed \n");
exit(1);
}
}

// Destroy the chopstick Mutex array
for (i = 1; i <= NUM_CHOPSTICKS; i++)
{
status_message = pthread_mutex_destroy(&chopstick[i]);
if (status_message != 0)
{
printf("\n Mutex Destroyed \n");
exit(1);
}
}
return 0;
}
void dine(int n)
{
printf("\nPhilosopher % d is thinking ", n);

// Philosopher picks up the left chopstick (wait)
pthread_mutex_lock(&chopstick[n]);

// Philosopher picks up the right chopstick (wait)
pthread_mutex_lock(&chopstick[(n + 1) % NUM_CHOPSTICKS]);

// After picking up both the chopstick philosopher starts eating
printf("\nPhilosopher % d is eating ", n);
sleep(3);

// Philosopher places down the left chopstick (signal)
pthread_mutex_unlock(&chopstick[n]);

// Philosopher places down the right chopstick (signal)
pthread_mutex_unlock(&chopstick[(n + 1) % NUM_CHOPSTICKS]);

// Philosopher finishes eating
printf("\nPhilosopher % d Finished eating ", n);
}
4 changes: 2 additions & 2 deletions fix_me.bhailang.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ hi bhai

agar bhai (b == 0) {
bol bhai "b is equal to a";
} nahi to bhai (b == a) {
} nahi to bhai (b =/= a) {
bol bhai "b is equal to zero";
}

b += 1;
b += 0;
}

bye bhai
2 changes: 1 addition & 1 deletion fix_me.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ int main() {
scanf("%d %d", &number1, &number2);

// calculating sum
sum = number1 - number2;
sum = number1 + number2;

// Multiply number 1 and number 2
multi = number1 * number1
Expand Down
4 changes: 2 additions & 2 deletions fix_me.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ int main() {
cin >> first_number >> second_number;

// sum of two numbers in stored in variable sumOfTwoNumbers
product = first_number / second_number;
product = first_number * second_number;

// prints sum
cout << first_number << " + " << second_number << " = " << sum;

return 0;
}
}
6 changes: 3 additions & 3 deletions fix_me.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
a= int(input("Enter the first no: "))
b= int(input("Enter the second no: "))
# Divide two numbers
division= a-b
division= a/b

# Square of first no.
a = a+a
a = a*a
# Square of second no.
b = b*b
print("Division",division)
print("Division",division)