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
What is the most efficient<br> form of transportation we<br> have?
alisha [4.7K]

Answer:

The bicycle is a tremendously efficient means of transportation. In fact cycling is more efficient than any other method of travel--including walking! The one billion bicycles in the world are a testament to its effectiveness

Explanation:

6 0
2 years ago
Read 2 more answers
Name the major types of computer systems from slowest to fastest. ​
Vadim26 [7]

Answer:

Cache is the fastest and most expensive, RAM is slower and less expensive, and virtual memory is the slowest and least expensive type.

Explanation:

3 0
2 years ago
A customer in Lowe's needs help. Matt works in shipping and receiving. It isn't Matt job to work with the customer, but he knows
klio [65]

Answer:

40

Explanation:

hhhhhjjjj

5 0
3 years ago
Read 2 more answers
Which of the following describes a Trojan horse?
HACTEHA [7]

Answer:

the answer is

c. Trojan horses enter a secure space, while an infected file proceeds to be downloaded and run.

7 0
3 years ago
Which of the following statements is true of licensing procedures for IT professionals?
melomori [17]

Answer:

the correct answer is b

Explanation:

4 0
3 years ago
Read 2 more answers
Other questions:
  • Navigational signs on the highway are often which colors?
    10·2 answers
  • Choosing firm goals for your business
    7·2 answers
  • When creating a presentation you should use a blank as a starting point?
    15·1 answer
  • Pictures that you can click on to tell your computer what to do.
    12·2 answers
  • In the writing and language test of the SAT, questions ask students to do which of the following? A. Write an essay B. Define a
    14·2 answers
  • Why is printer an output device​
    14·2 answers
  • When powering up a home network, make sure all computers and peripherals are turned on before turning on the modem/router?
    11·1 answer
  • In this project you will write a set of instructions (algorithm). The two grids below have colored boxes in different
    9·2 answers
  • Technology has had
    10·1 answer
  • Escribe 10 ejemplos de lo que consideras un byte
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!