Dining Philosopher problem solution- Vulnerable to starvation in c language or either java - using threads(can use semaphores but with threads) - with proper explanations and comments Please provide output of program and explanation of how to run program.
file name dinning.c
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5
#define THINKING 2
#define HUNGRY 1
#define EATING 0
#define LEFT (phnum + 4) % N
#define RIGHT (phnum + 1) % N
int state[N];
int phil[N] = { 0, 1, 2, 3, 4 };
sem_t mutex;
sem_t S[N];
void test(int phnum)
{
if (state[phnum] == HUNGRY
&& state[LEFT] !=
EATING
&& state[RIGHT] != EATING)
{
// state that eating
state[phnum] = EATING;
sleep(2);
printf("Philosopher %d takes
fork %d and %d\n",
phnum + 1, LEFT + 1, phnum +
1);
printf("Philosopher %d is Eating\n", phnum + 1);
// sem_post(&S[phnum]) has
no effect
// during takefork
// used to wake up hungry
philosophers
// during putfork
sem_post(&S[phnum]);
}
}
// take up chopsticks
void take_fork(int phnum)
{
sem_wait(&mutex);
// state that hungry
state[phnum] = HUNGRY;
printf("Philosopher %d is Hungry\n", phnum + 1);
// eat if neighbours are not eating
test(phnum);
sem_post(&mutex);
// if unable to eat wait to be signalled
sem_wait(&S[phnum]);
sleep(1);
}
// put down chopsticks
void put_fork(int phnum)
{
sem_wait(&mutex);
// state that thinking
state[phnum] = THINKING;
printf("Philosopher %d putting fork %d and %d
down\n",
phnum + 1, LEFT + 1, phnum +
1);
printf("Philosopher %d is thinking\n", phnum + 1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
void* philospher(void* num)
{
while (1) {
int* i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}
int main()
{
int i;
pthread_t thread_id[N];
// initialize the semaphores
sem_init(&mutex, 0, 1);
for (i = 0; i < N; i++)
sem_init(&S[i], 0, 0);
for (i = 0; i < N; i++) {
// create philosopher
processes
pthread_create(&thread_id[i],
NULL,
philospher,
&phil[i]);
printf("Philosopher %d is
thinking\n", i + 1);
}
for (i = 0; i < N; i++)
pthread_join(thread_id[i],
NULL);
}
to run the program use
gcc dinning.c -pthread -o din
Output
./din
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 5 is thinking
Philosopher 1 is Hungry
Philosopher 2 is Hungry
Philosopher 4 is Hungry
Philosopher 3 is Hungry
Philosopher 3 takes fork 2 and 3
Philosopher 3 is Eating
Philosopher 5 is Hungry
Philosopher 5 takes fork 4 and 5
Philosopher 5 is Eating
Philosopher 3 putting fork 2 and 3 down
Philosopher 3 is thinking
Philosopher 2 takes fork 1 and 2
Philosopher 2 is Eating
Philosopher 5 putting fork 4 and 5 down
Philosopher 5 is thinking
Philosopher 4 takes fork 3 and 4
Philosopher 4 is Eating
Philosopher 3 is Hungry
Philosopher 2 putting fork 1 and 2 down
Philosopher 2 is thinking
In this program when you create an pthread all the threads run independently, so any philosopher can take the fork and put the fork back.
Dining Philosopher problem solution- Vulnerable to starvation in c language or either java - using threads(can use semaphores but with threads) - with proper explanations and comments Please provide o...
Hi, please provide a code solution written in the C++ language
to the following programming problem. The answer you provide has to
include code comments, as it is being explained to a person with no
technical background. In your answer, please show which portion of
the code is a .cpp and which is a .h (if used). We are asked to
separate them out (ie. main.cpp, classname.cpp, headername.h).
Please show the output of your program to show it compiled error...
Language is in Java, please explain the answer using comments, Thank you. I always rate!!! PLEASE DO NOT import any packages to solve the problem. PLEASE use classes and methods from java.lang.*, since they are automatically imported without import statement. build a method called; public static in[] multbygrp(int[] elems, int grpsize) This method splits arr into subgroups of (equal) size groupSize, and multiply the contents of each subgroup. It returns the individual product in a new list. If splitting can't...
Must be done in Java.
Provide the rest of the code with full comments and
explanation and with proper indentation.
Use simple methods for better
understanding.
Must compile.
CODE PROVIDED:
import java.util.ArrayList;
import java.util.Scanner;
public class Problem3 {
public static void main( String [] args )
{
Scanner in = new
Scanner( System.in );
//PLEASE START YOUR WORK
HERE
//**********************************************************
//**********************************************************
// PLEASE END YOUR WORK
HERE
System.out.print("END OF
OUTPUT");
}
}
20 points! PROBLEM 3: EXTENDED FAMILY Complete this...
C++ language. please provide detailed comments (for beginners) and use easy to understand/trace code. Thank you --------------- A positive integer n is triangular if it can be obtained by the product of three consecutive positive integers. Given n > 0, determine whether n is triangular. For example, 120 is triangular, since 4 · 5 · 6 = 120.
TRUE-FALSE Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting policy. 2. Priority inversion avoids deadlocks. 3. Spinlock mutex can be used as an adaptive mutex. 4. Java RTE can be blocked for Input/Output operation. 5. Interrupted user thread, which executes a method in a monitor, must be rolled back to undo any changes it performed. 6. The synchronization primitive by disabling interrupts can be used by an application program. 7. Bounded-waiting requirement is...
Can you use a class “AnotherClassX” written in another language (not using .NET) (for instance C++ using gcc compiler in a Unix Box) to create a new class that inherits “AnotherClassX” in a C# program using Visual Studios? Please answer Yes or No for both scenarios with explanations. If you don’t have the source code of “AnotherClassX” If you do have the source code for “AnotherClassX”
please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...
Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 1” as in the lecture note. This program should work as follows: 1.The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through...
The question of validity is to answer the question of whether for a Boolean expression consisting of n variables . There is a combination of values of variables that make the result of the expression true or not. In this phrase each The variable can be simple or contradictory. Force brute method of all compounds . Creates and evaluates the possible and the first time the result is true (if it is) the answer to the problem Come and stop...