diff --git a/DP.c b/DP.c new file mode 100644 index 0000000..9ddeec8 --- /dev/null +++ b/DP.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#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); +} diff --git a/fix_me.bhailang.txt b/fix_me.bhailang.txt index e027aa7..365a49a 100644 --- a/fix_me.bhailang.txt +++ b/fix_me.bhailang.txt @@ -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 diff --git a/fix_me.c b/fix_me.c index fa99550..f1ce0bb 100644 --- a/fix_me.c +++ b/fix_me.c @@ -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 diff --git a/fix_me.cpp b/fix_me.cpp index 40b2599..6e29830 100644 --- a/fix_me.cpp +++ b/fix_me.cpp @@ -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; -} \ No newline at end of file +} diff --git a/fix_me.py b/fix_me.py index 1506169..534a523 100644 --- a/fix_me.py +++ b/fix_me.py @@ -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) \ No newline at end of file +print("Division",division)