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
Svetlanka [38]
4 years ago
6

on 5.7.3, we provide an outline of a solution to the dining-philosophers problem using monitors. This problem will require imple

menting a solution using Pthreads mutex locks and condition variables.The PhilosophersBegin by creating five philosophers, each identified by a number 0 . . 4. Each philosopher will run as a separate thread. Thread creation using Pthreads is covered in Section 4.4.1. Philosophers alternate between thinking and eating. To simulate both activities, have the thread sleep() for a random period between one and three seconds. When a philosopher wishes to eat, she invokes the functionpickup_forks(int philosopher_number)where philosopher number identifies the number of the philosopher wishing to eat. When a philosopher finishes eating, she invokesreturn_forks(int philosopher_number
Computers and Technology
1 answer:
shtirl [24]4 years ago
5 0

Answer:

See explaination for program code.

Explanation:

#include <stdio.h>

#include <pthread.h>

#include <stdlib.h>

#include <unistd.h>

#define NO_OF_PHILOSOPHERS 5

pthread_t philosophers[NO_OF_PHILOSOPHERS];

pthread_mutex_t mutex_forks = PTHREAD_MUTEX_INITIALIZER;;

int forks[NO_OF_PHILOSOPHERS];

void init()

{

int i;

for(i=0; i<NO_OF_PHILOSOPHERS; i++)

forks[i] = 0;

}

void philosopher(int i)

{

int right = i;

int left = (i - 1 == -1) ? NO_OF_PHILOSOPHERS - 1 : (i - 1);

int locked;

while(1)

{

locked = 0;

while(!locked)

{

pthread_mutex_lock(&mutex_forks);

if(forks[right] || forks[left])

{

pthread_mutex_unlock(&mutex_forks); // give up the forks unless you can take both at once.

printf("Philosopher %d cannot take forks. Giving up and thinking.\n",i);

usleep(random() % 1000); // think.

continue;

}

forks[right] = 1; // take forks.

forks[left] = 1;

pthread_mutex_unlock(&mutex_forks);

locked = 1;

}

printf("Philosopher %d took both forks. Now eating :)\n",i);

usleep(random() % 500);

printf("Philosopher %d done with eating. Giving up forks.\n",i);

pthread_mutex_lock(&mutex_forks); // give up forks.

forks[right] = 0;

forks[left] = 0;

pthread_mutex_unlock(&mutex_forks);

usleep(random() % 1000);

}

}

int main()

{

init();

int i;

for(i=0; i<NO_OF_PHILOSOPHERS; i++)

pthread_create( &philosophers[i], NULL, philosopher, (void*)i);

for(i=0; i<NO_OF_PHILOSOPHERS; i++)

pthread_join(philosophers[i],NULL);

return 0;

}

You might be interested in
Write the definition of a method dashedLine, with one parameter, an int. If the parameter is negative or zero, the method does n
ZanzabumX [31]

Answer:

import java.util.Scanner;

public class DashLine {

public static void main(String[] args) {

// Declaring variables

int n;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

// Getting the input entered by the user

System.out.print("Enter a number :");

n = sc.nextInt();

// calling the method by passing the user entered input as argument

dashedLine(n);

}

//This method will print the dashed line for number greater than zer

private static void dashedLine(int n) {

if (n > 0) {

for (int i = 1; i <= n; i++) {

System.out.print("-");

}

System.out.println();

}

}

}

Explanation:

5 0
3 years ago
COMPUTER SCIENCE HELP!!!!!
maw [93]
1.( I need to know what program this is for to answer this one)
2.Explain a game you played for this one and include a interactive element it had like a Side-quest/Npc/Cool Background ect.
3.For 3D games you have to worry about smoothing and lighting, as for 2D you have to worry about keeping the objects within the players POV as they are usually locked to character and stuck facing one direction.
4.???(need progam name)
5.If the game is based in a snow biome you would use snow landscape textures.
8 0
4 years ago
How do I add pictures to my questions on the mobile app?
taurus [48]
The camera is at the bottom of the screen,Take a pic
8 0
4 years ago
Help please if you know answer like these I’ll provide my discord.
Natasha_Volkova [10]

ins can insert a time and date.

5 0
3 years ago
2-3 Calculating the Body Mass Index (BMI). (Programming Exercise 2.14) Body Mass Index is a measure of health based on your weig
Luden [163]

Answer:

weight = float(input("Enter your weight in pounds: "))

height = float(input("Enter your height in inches: "))

weight = weight * 0.45359237

height = height * 0.0254

bmi = weight / (height * height)

print("Your BMI is: %.4f" % bmi)

Explanation:

*The code is written in Python.

Ask the user to enter weight in pounds and height in inches

Convert the weight into kilograms and height into meters using given conversion rates

Calculate the BMI using given formula

Print the BMI

5 0
4 years ago
Other questions:
  • Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both floats) as input, and output the ga
    12·2 answers
  • Match the limits of the user with an appropriate design response. 1. severe arthritis, unable to type or use a mouse on a reliab
    11·1 answer
  • First identify the formula to compute the sales in units at various levels of operating income using the contribution margin app
    12·1 answer
  • How should resumes and cover letters be written to help you obtain a job that meets your
    14·1 answer
  • You want to add a picture of a potential new office layout to the title page of a proposal. How can you adjust the size of the i
    14·1 answer
  • Please help!!!!!!! 50 POINTS!!!!
    10·1 answer
  • True or false<br>Y axis is also known as the value X.​
    9·2 answers
  • A document that summarizes and displays data is called a: Group of answer choices Report Procedure Form Requirement
    15·1 answer
  • Difine Mainframe Computer~<br><br><br><br><br><br><br><img src="https://tex.z-dn.net/?f=%20%5C%3A%20%20%5C%3A%20" id="TexFormula
    12·2 answers
  • Passing an argument by ___ means that only a copy of the arguments value is passed into the parameter variable and not the addrt
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!