1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
NARA [144]
4 years ago
13

Implement the Dining Philosophers problem (described on pages 167-170 in the textbook (chapter 2.5.1)). Create a Graphical User

Interface - showing which philosopher is eating, and which is waiting/thinking at any given time. Show the forks. Use Java programming language for this project.
Computers and Technology
1 answer:
zhuklara [117]4 years ago
3 0

Answer: Provided in the explanation section

Explanation:

class philosopher extends Thread

{

public void run()

{

diningps.count++;

philosopher(diningps.count);

}

public void philosopher(int i)

{

take_forks(i);

System.out.println(i+"philosopher Eating");

put_forks(i);

}

public void take_forks(int i)

{

while(diningps.mutex<=0)

{

System.out.println(i+"philosopher has to be wait while other philosopher in testing");

}

diningps.state[i]="HUNGRY";

test(i);

diningps.mutex=1;

while(diningps.S[i]<=0)

{

System.out.println(i+"philosopher has to be wait while his left or right side philosopher eating.");

}

}

public void put_forks(int i)

{

while(diningps.mutex<=0)

{

System.out.println(i+"philosopher has to be wait while other philosopher in testing");

}

diningps.state[i]="THINK";

test((i+4)%5);

test((i+1)%5);

diningps.mutex=1;

}

public void test(int i)

{

if(diningps.state[i]=="HUNGRY" && diningps.state[(i+4)%5]!="EAT" && diningps.state[(i+1)%5]!="EAT")

{

diningps.state[i]="EAT";

diningps.S[i]=1;

}

}

}

class diningps

{

static int mutex=1;

static int S[]={0,0,0,0,0};

static String state[]={"THINK","THINK","THINK","THINK","THINK"};

static int count=-1;

public static void main(String ar[])

{

philosopher ob=new philosopher();

philosopher ob1=new philosopher();

philosopher ob2=new philosopher();

philosopher ob3=new philosopher();

philosopher ob4=new philosopher();

ob.start();

ob1.start();

ob2.start();

ob3.start();

ob4.start();

}

}

cheers i hope this helped !!

You might be interested in
What is renewable energy
Marat540 [252]
Renewable energy is energy that is collected from renewable resources, which are naturally replenished on a human timescale, such as sunlight, wind, rain, tides, waves, and geothermal heat.
8 0
3 years ago
Putting commands in correct order so computers can read the commands
lana66690 [7]

Answer:

its b I think I'm pretty sure

8 0
3 years ago
Modify the WordCount program so it outputs the wordcount for each distinct word in each file. So the output of this DocWordCount
stepladder [879]

Answer and Explanation:

package PackageDemo;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.GenericOptionsParser;

public class WordCount {

public static void main(String [] args) throws Exception

{

Configuration c=new Configuration();

String[] files=new GenericOptionsParser(c,args).getRemainingArgs();

Path input=new Path(files[0]);

Path output=new Path(files[1]);

Job j=new Job(c,"wordcount");

j.setJarByClass(WordCount.class);

j.setMapperClass(MapForWordCount.class);

j.setReducerClass(ReduceForWordCount.class);

j.setOutputKeyClass(Text.class);

j.setOutputValueClass(IntWritable.class);

FileInputFormat.addInputPath(j, input);

FileOutputFormat.setOutputPath(j, output);

System.exit(j.waitForCompletion(true)?0:1);

}

public static class MapForWordCount extends Mapper<LongWritable, Text, Text, IntWritable>{

public void map(LongWritable key, Text value, Context con) throws IOException, InterruptedException

{

String line = value.toString();

String[] words=line.split(",");

for(String word: words )

{

Text outputKey = new Text(word.toUpperCase().trim());

IntWritable outputValue = new IntWritable(1);

con.write(outputKey, outputValue);

}

}

}

public static class ReduceForWordCount extends Reducer<Text, IntWritable, Text, IntWritable>

{

public void reduce(Text word, Iterable<IntWritable> values, Context con) throws IOException, InterruptedException

{

int sum = 0;

for(IntWritable value : values)

{

sum += value.get();

}

con.write(word, new IntWritable(sum));

}

}

}

8 0
3 years ago
What is the abbreviation used for educational sites/?
Dovator [93]
Mainly .com 
educational institutes .edu
8 0
3 years ago
Is mayonnaise technically an instrument?
stich3 [128]
No it doesn’t count as an instrument
7 0
3 years ago
Other questions:
  • How many different integers can be represented with a 4-digit number in base 13?
    8·1 answer
  • Intro to cs 3.7 edhesive g=float(input("Enter your English test grade:")) if(g&lt;=64): print("F") if (g&gt;=65 and g&lt;69): pr
    11·1 answer
  • The position of a _____ is a nontechnical position responsible for defining and implementing consistent principles for setting d
    15·1 answer
  • How to get out of compatibility mode in word?
    15·1 answer
  • Consider the following code snippet:
    13·1 answer
  • Suppose you want to boot a VM from its virtual DVD drive, but it boots to the VM’s hard drive. Which of the following could be t
    12·1 answer
  • While using a web-based order form, an attacker enters an unusually large value in the Quantity field. The value he or she enter
    15·1 answer
  • Temperature converter. This program should prompt the user for two arguments, first a decimal number and second, a single letter
    10·1 answer
  • How can the system administrator give the executive assistant the ability to view, edit, and transfer ownership of all records,
    6·1 answer
  • _____ allows a function or operator to perform different tasks depending on the types of the arguments or operands. Group of ans
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!