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
Of the following which would be the best data representation for this puzzle in a puzzle class?
sladkih [1.3K]

please include the puzzle and or questions.

5 0
3 years ago
On the first line, place your name in a comment. Create a program that does the following: Take in three integer numbers from th
Helen [10]

Answer and Explanation:

//buchi

Var firstNumber=prompt("please enter first number");

Var secondNumber=prompt("please enter second number");

Var thirdNumber=prompt("please enter third number");

Var numberTotal=firstNumber+secondNumber+thirdNumber;

Function calculateNumbers(){

return numberTotal;

return int(numberTotal/3);

return firstNumber*secondNumber*thirdNumber;}

Console.log(calculateNumbers());

8 0
3 years ago
Implement a function getContent() that takes as input a URL (as a string) and prints only the text data content of the associate
Reil [10]
Print is good but url is not
3 0
3 years ago
I don't understand how to write code for this in Java using nested for loops only. The official question is: write a program tha
Bingel [31]

Answer:

public class Triangle

{

public static void main( String[] args )

{

show( 5 );

}

public static void show( int n )

{

int i,j,k;

for (i = 0; i < n - 1; i++ )

{

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

{

System.out.print( " " );

}

for (k = n - i; k > 0; k-- )

{

System.out.print( "* " );

}

System.out.println();

}

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

{

for (j = n - i; j > 1; j-- )

{

System.out.print( " " );

}

for (k = 0; k < i + 1; k++ )

{

System.out.print( "* " );

}

System.out.println();

}

4 0
3 years ago
Differentiate among a color display, gray scale display, and a black-and-white display​
kotykmax [81]

<u>Answer:</u>

<em>Black and white</em>:

It has only two values namely black or white. The white colour in the image will be represented as “white” and other colour part will be displayed as black.

<em>Grey-scale: </em>

Again the white part does not have a change, the black and other coloured items will be displayed in grey.

<em>Coloured image: </em>

It would display the actual colour of the image. The number of colours and shades depends on the original image from where actually it has been shooted and it also depends on the quality of the camera.

4 0
3 years ago
Other questions:
  • Survey Q. Non-scoring: What role is played in the team? (1 correct answer)
    14·1 answer
  • How to find i with superposition method
    8·1 answer
  • When Clara accesses the programs and documents on her computer by way of icons, she is said to be employing
    15·1 answer
  • Do the shape of a sign gives you a clue about the information contained on the sign
    7·1 answer
  • Of the key reasons for creating organizational units which of the following is not one of them?
    13·1 answer
  • To connect a peripheral device to a computer to exchange data, find the appropriate ________ for the device.
    7·1 answer
  • Which is true about lists and stacks?
    8·1 answer
  • What is also known as computer Network?
    6·2 answers
  • The first page of a website is what?​
    5·2 answers
  • Unscramble the given word and identify the correct statement about it. ATTEPLSR
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!