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
Multiple Intelligence Theory explains that...
Alex

Answer:

A. We all learn differently (edge 2020)

Explanation:

4 0
3 years ago
Read 2 more answers
Which server tier is considered to be the most robust and less prone to failure?
Ne4ueva [31]
Tier 4 data center considered as most robust and less prone to failures. Tier 4 is designed to host mission critical servers and computer systems, with fully redundant subsystems (cooling, power, network links, storage etc) and compartmentalized security zones controlled by biometric access controls methods.
6 0
3 years ago
Read 2 more answers
Poor language skills can lead to a (an) ______?
pashok25 [27]
<span>If you are referring to this question:
What kind of block can result from poor language skills?
</span><span>Poor language skills can lead to a (an) ______
</span>
The answer would be a mental block. This is a common occurrence when learning things, but it is notable in language learning. At times, they also call this a plateau of the mind. Poor language skills can impede the train of thought, which can also cause this type of block.
7 0
3 years ago
n macOS, what launch point provides access to almost all the settings needed to administer a macOS system?
Fynjy0 [20]

Answer:

System preferences.

Explanation:

MacOS is the primary operating system designed and developed for Apple computers (MacBook).

In macOS, system preferences is a launch point that provides access to almost all the settings needed to administer a macOS system. The system preferences allow users to change various settings on their Mac computers such as changing the desktop layout, desktop picture (wallpaper), size and location of the dock, font size, appearance, energy saver etc.

5 0
3 years ago
Disadvantages of using animation in advertising?
vlabodo [156]
DISADVANTAGES O USING ANIMATION IN ADVERTISEMENT <span>Ignored – Ads placed on the side or page in “traditional” ad spots tend to be ignored (because people know that they are ads without paying attention to them – especially if they are animated).</span> <span><span>§  </span>Blocked – As with most ads which are served from an ad server, display ads may be blocked by viewers who either turn off scripting in their browser or have some other type of software that blocks ads (there are some ways that ad servers try to get around this – more on that in a future post).</span> <span><span>§  </span>Limited – Display ads are limited in the amount of information they can convey. Even with animation you usually only have 10-15 seconds of time to get your message across in a limited space.</span> <span><span>§  </span>Little Information – Information collected about display ads is usually limited to impressions, clicks and possibly some information about who clicked on the ad (if the site has registration tied to their ad serving – more on analytics in a future post).</span> <span>Advantages of the animation technology in education</span> <span><span>1.     </span><span>Skill and ability Improvement 2. Interactivity</span> 3. Engagement 4. Flexibility and safety  </span> <span>Disadvantages of the animation technology in education</span> <span>· requires extensive memory and storage space: Since the animation consists of a series of graphic objects and mathematical calculations, it uses more system processing and storage resources than graphics and text.</span> <span>· requires special equipment for a quality presentation: While some good animation can be created using a basic graphics package, producing quality animation requires specialized software and programming and educational knowledge.</span> <span>· cannot depict actuality like video.</span> Advantages and Disadvantages of using animation in entertainment? <span>Advantage: It's much more realistic than traditional animation. It's a lot more you can do in 3d than in 2d. <span>

<span>Disadvantage: If it isn't done properly it could be disastrous. Also, with games, not everybody has complete access to the game. You still need advanced video cards and higher memories.</span></span></span>

5 0
3 years ago
Other questions:
  • Which of these tools can best be used as a self assessment for career planning purposes
    12·1 answer
  • .Why is DNS important?
    6·1 answer
  • "In about 100 words, discuss the technologies that Walmart’s trucking fleet might use to better manage their operations. Include
    7·1 answer
  • 1.   Which of the following is an example of a primary cost of an accident or injury?
    5·2 answers
  • _____ is an environment where processors are embedded in objects that communicate with one another via the Internet or wireless
    15·1 answer
  • Your friend is an intern at the local Department of Health and needs to prepare a report about the recent activity of the influe
    9·2 answers
  • When creating a shape in Word, what are some available options? Check all that apply. adding text to the shape changing the size
    6·1 answer
  • 6.7 — Trickier Trap 4
    7·2 answers
  • Please help...........​
    6·2 answers
  • If you want to remove the autocorrect options button from the screen, you can press the ____ key.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!