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
G The method of mapping where each memory locationis mapped to exactly one location in the cache is
Otrada [13]

Answer:

Direct Mapped Cache

Explanation:

Given that a Direct Mapped Cache is a form of mapping whereby each main memory address is mapped into precisely one cache block.

It is considered cheaper compared to the associative method of cache mapping, and it is faster when searching through it. This is because it utilizes a tag field only.

Hence, The method of mapping where each memory location is mapped to exactly one location in the cache is "Direct Mapped Cache"

4 0
3 years ago
It's not possible to die in an alcohol-related collision if you're not in an automobile.
frutty [35]

Answer:

B. False

Explanation:

  • Consumption of alcohol is not a good practice and is generally not allowed at the time of driving an automobile and is considered to be an offense as it may be injurious to health and property.
  • As too much alcohol can create possible chances of collusions and even if the person is not in an automobile can result in a collision if tries to cross the road. Like head injuries or leg injuries can occur.
8 0
3 years ago
Service Level Agreements are considered the most comprehensive way to fully measure service desk performance. TRUEOR FALSE
icang [17]
Since a service level agreement is between the service provider and the client I would say yes. The feedback for products or services can help the company to ensure that they are selling to the max quality of the products.
3 0
4 years ago
This type of connection uses radio waves to connect devices on a network.
Tresset [83]
I believe data carries radio waves
5 0
4 years ago
Read 2 more answers
If a system contains 1,000 disk drives, each of which has a 750,000- hour MTBF, which of the following best describes how often
exis [7]

Answer:

once per month

Explanation:

The correct answer is - once per month

Reason -

Probability of 1 failure of 1000 hard disk = 750,000/1000 = 750 hrs

So,

750/24 = 31.25 days

⇒ approximately one in a month.

6 0
3 years ago
Other questions:
  • Suppose you are the security manager of a company and one of your goals is to design security mechanisms based on three security
    10·1 answer
  • Which of the following is true regarding packaged software and custom software? Group of answer choices Packaged software are ap
    13·1 answer
  • The find and
    10·1 answer
  • Here's another question!<br><br> What is some iterative programming structure uses?
    13·1 answer
  • Suppose a computer has 1GB of memory. Operating System takes 256 MB of memory. Imagine each task takes up 196 MB of memory and t
    12·1 answer
  • Bao bì chủ động active packaging và bao bì thông minh intelligent packaging khác biệt như thế nào
    15·1 answer
  • ??????????????????????????
    13·1 answer
  • Given three floating-point numbers x, y, and z, output x to the power of z, x to the power of (y to the power of z), the absolut
    12·1 answer
  • Majken is turning 18 years old soon and wants to calculate how many seconds she will have been alive.
    7·1 answer
  • you have been tasked with configuring a digital information station in the office's lobby. guests will be able to use the statio
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!