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
Look at the following Python code:
aliya0001 [1]

Ump will be assigned to word2

word1[0:3] gets all of the letters from index 0 to index 3 exclusive (that means index 3 is not included)

4 0
2 years ago
Where should you look for most objective and unbaised information
jekas [21]
Google, would be the best because of all of the different site, hope this helps!
: )
8 0
2 years ago
Match the parts of the website address with its correct explanation. http://www.usa.gov/Agencies/federal.shtml
storchak [24]

HTTP means Hyper Text Transfer Protocol underlying protocol used by the World Wide Web(www).

usa is the server name which is used address in the DNS record

.gov is the Domain names which  are used to identify more then one IP addresses

Agencies/federal.shtml a directory is a collection of file resources also called the resource ID.

6 0
2 years ago
Read 2 more answers
All of these acts performed when doing an oil and filter change except ?
Nat2105 [25]
B. Loosening the drain plug with a screwdriver
6 0
2 years ago
Read 2 more answers
Dakota's friend Stephen created a cypher using the QWERTY keyboard, but Dakota is confused. Stephen said to move one to the righ
Radda [10]

Answer:

people are so confusing nowadays... Mr. Kopin told me to get him a coffee. I'm not his servant!

Explanation: QWERTY Q=P A=L Z=M and the others are what is to the left of them.

5 0
2 years ago
Other questions:
  • Which of these statements best describes an application programming interface?
    5·1 answer
  • The inflationary gap occurs when you obtain no increase in output, but only an increase in the Average Price Level from an incre
    13·1 answer
  • Select the best answer for the question. 2. What is the simplest way to permanently get rid of an unwanted file?
    13·1 answer
  • Please conduct some research and find an article on Security Threats and please provide link of the article.
    5·1 answer
  • Conduct online research and provide some guidelines on how to secure a web browser. (CyberSecurity plato)
    10·1 answer
  • When disabling inherited permissions on an object, what happens if you select Remove all inherited permissions from this object?
    11·1 answer
  • An aircraft departs an airport in the mountain standard time zone at 1615 MST for a 2-hour 15-minute flight to an airport locate
    14·1 answer
  • If an author is creating a reference list and wants the second and succeeding lines indented for a reference, they should select
    13·2 answers
  • Please help with this coding question
    8·2 answers
  • Write a format operation that builds a string for the float variable amount that
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!