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
alexira [117]
3 years ago
14

In an office there is a unisex bathroom with n toilets. The bathroom is open to both men and women, but it cannot be used by men

and women at the same time. Develop a concurrent program that simulates the above scenario using semaphores. Your solution should be deadlock free, but it does not have to be starvation free. Note that your program has to implement two functions arriveAtTheBathroom(type) and leaveTheBathroom(type), where type is either MAN or WOMAN
#include "BathroomProblemSolverInterface.cpp"
#include

class MyBathroomProblemSolver : public BathroomProblemSolverInterface
{
int n;//Number of toilets
public:
MyBathroomProblemSolver(int n)
{
this->n = n;
}
void arriveAtTheBathroom(gender type) override
{
throw std::runtime_error("Not Implemented");
/******************************************
*
*
* Your Code goes here
*
*
*
* ***************************************/
}

void leaveTheBathroom(gender type) override
{
throw std::runtime_error("Not Implemented");
/******************************************
*
*
* Your Code goes here
*
*
*
* ***************************************/
}
};
Computers and Technology
1 answer:
Gnesinka [82]3 years ago
5 0

Answer:

Check the explanation

Explanation:

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <semaphore.h>

int mcount,wcount;

sem_t x,y,z,wsem,msem,cap;

void delay(void)

{

int i;

int delaytime;

delaytime = random();

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

}

void *woman(void *param)

{

sem_wait(&z);

sem_wait(&wsem);

sem_wait(&y);

wcount++;

if(wcount==1)

{ sem_wait(&msem); }

sem_post(&y);

sem_post(&wsem);

sem_post(&z);

sem_wait(&cap);

printf("woman in!\n");

delay();

printf("\twoman out!\n");

sem_post(&cap);  

sem_wait(&y);

wcount--;

if(wcount==0)

{ sem_post(&msem); }

sem_post(&y);

}

void *man(void *param)

{  

sem_wait(&z);

sem_wait(&msem);

sem_wait(&x);

mcount++;

if(mcount==1)

{ sem_wait(&wsem); }

sem_post(&x);

sem_post(&msem);

sem_post(&z);

sem_wait(&cap);

printf("\t\tman in!\n");

delay();

printf("\t\t\tman out!\n");

sem_post(&cap);

sem_wait(&x);

mcount--;

if(mcount==0)

{sem_post(&wsem);}

sem_post(&x);

}

int main(void)

{

int i;

srandom(60);

mcount = 0;

wcount = 0;

sem_init(&x,0,1); // for sem_init, initial value is 3rd argument

sem_init(&y,0,1);

sem_init(&z,0,1);

sem_init(&wsem,0,1);

sem_init(&msem,0,1);

sem_init(&cap,0,4); // eg. cap initialized to 4

pthread_t *tid;

tid = malloc(80*sizeof(pthread_t));

// You can use your cobegin statement here, instead of pthread_create()  

// I have forgone the use of pthread barriers although I suppose they would nicely imitate the functionality of cobegin.

// This is merely to retain simplicity.

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

{

pthread_create(&tid[i],NULL,woman,NULL);

}

for(i=10;i<20;i++)

{  

pthread_create(&tid[i],NULL,man,NULL);

}

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

{  

pthread_join(tid[i],NULL);

}

return(0);

}

You might be interested in
PLEASE ANYONE!!which present margins would be best to get the most information to fit on the page​
-Dominant- [34]

Answer:

Normal Margin is fit on the page. The default margins for Microsoft Word is normal margin.                                      

Explanation:

The paper visual effect give the document’s theme and quality. Margins send visual effects. Making a document right helps you to send the right message to readers. Use a small paper and adding a border.

You can change the page layout that people notice first with size, orientation, and margins.

Page margins make your document readable. Margins make the document more readable, make text look inviting, and given reviewers.

With narrower margins, you can reduce more words on-page.

To many words in a line make it difficult to read the document.

Margins are of many types

  • Normal Margin
  • Narrow margins
  • Moderate margins
  • Wide margins

Normal gives an equal inch to all sides of the page.

Narrow margins give little room to each column.

Average margins squeeze words from left and right.

Wide gives space when proofing the manuscript.

3 0
2 years ago
Read 2 more answers
Where are methods listed in a UML class diagram showing three parts?
denis23 [38]

Answer:

the bottom third

Explanation:

Check out the exampe below.

swim() would be an example of a method.

3 0
2 years ago
Files and e-mail messages sent over the Internet are broken down into smaller pieces called _____.
Fynjy0 [20]
Files and email messages sent over the internet are broken down into smaller pieces called packets
6 0
3 years ago
Andrew’s Complete Cameras offers its customers an interactive website to help them choose the best camera for their lifestyle. B
julsineya [31]

Answer:

The correct answer is letter "A": Engage.

Explanation:

Social media has gained importance not only as an informative medium but as a means for marketing. The 4E framework states the four basic characteristics social media advertisement should fulfill. Those components are:

  • Excite.<em> Provide customers attractive offers.</em>
  • Educate.<em> Give consumers information about the product and the offering.</em>
  • Experience.<em> Allow direct or indirect interaction with the good.</em>
  • <u>Engage</u>.<em> Involve customers in activities related to the product that transcend the simple buying of the good.</em>
8 0
3 years ago
Stella likes to work with colors and moving pictures. She is good at drawing. Which job role should she choose
PilotLPTM [1.2K]

Answer:

Digital Art or Animator for moving picture art.

Explanation:

7 0
3 years ago
Read 2 more answers
Other questions:
  • c++ design a class named myinteger which models integers. interesting properties of the value can be determined. include these m
    14·1 answer
  • Document design techniques make your document easier for readers to skim. Therefore, these techniques give your writing "High Sk
    14·1 answer
  • Which term describes a protocol to manage a network, able to configure a network, monitor activity, and control devices?
    10·2 answers
  • Assume that the following code exists inside a method of MyClass, and that this code compiles without errors: int result = book.
    13·1 answer
  • Token stories of success and upward mobility (illustrated by Oprah, Ross Perot, and Madonna) reinforce ________ and perpetuate t
    8·1 answer
  • You are having problems on your Windows 7 computer and you pull up Device Manager to see if there are any alerts. Two of your de
    11·1 answer
  • Which of the following represents the biggest difference between asymmetric and symmetric cryptography?
    9·1 answer
  • If you wish to maintain a consistent style to all the documents you create, it would be helpful to use a _​
    5·1 answer
  • You will implement three different types of FFs with two different reset types. You have to show your results on your FPGA. You
    9·1 answer
  • MP3 BrainPoP Quiz
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!