# The unisex bathroom with unlimited capacity - message passing

Hey, I just started my adventure with the GO environment because I have a parallel programming subject in college and I have to do some assignments. My assignment involves the use of message passing and its exact content is as follows:

Suppose there is one bathroom in your department. It can be used by both men and women, but not at the same time.

Design the men and the woman processes and the server process bathroom. The only allowed way of communication between processes is message passing. Allow any number of men or women to be in the bathroom at the same time.

The solution need not to be fair, where a starvation can arise.

I based my solution on what I found on this website:

My solution is as follows:

``````package main

import (
"fmt"
"os"
)
var M int
var F int
//Note that while people exiting the bathroom require only one channel,
//people entering the bathroom require two because it’s necessary to
//stop a category from entering the bathroom when the other is already present.
//When exiting, the category doesn’t matter.

var females int //occupancy counters
var males int
type Female struct {
in chan bool
f chan bool
exit chan bool
}
type Male struct {
in chan bool
m chan bool
exit chan bool
}

func (pmale Male) Men(in, exit Male) {
in <- male
time.Sleep(time.Duration(250+rand.Intn(100)) * time.Millisecond)
exit <- pmale.exit
}
func (pfemale Female) Woman(in, exit Female) {
in <- pfemale
time.Sleep(time.Duration(250+rand.Intn(100)) * time.Millisecond)
exit <- pfemale.exit
}
/*//The two local variables m and f are used to control who can enter.
//When a male enters the bathroom, f is set to nil.
//When a female enters, m is set to nil.
// A receive from a nil channel blocks forever*/

func Bathroom (x []Male, y []Female, finish chan struct {}) {
m:= []m.x
f:= []f.y
for {
select {
case <-m:
males++
f = nil

case <-f:
females++
m = nil

case pmale := <-x.exit:
males--
m := []m.x
if males == 0 {
f := []f.y
}
case pfemale := <-y.exit:
females--
f = []y.Female
if females == 0 {
m = []x.Male
}
}
}
}
goto done
done:
finish<-struct{}{}
}
//When a male leaves the bathroom by sending the signal on the exit channel,
//the occupancy is decreased and the channel for entering the bathroom is restored.
//If the bathroom becomes empty, then the other channel is activated again.
//In this way the constraints are satisfied.

func main() {

fmt.Printf("%s\n", "\"enter number of woman and man waiting to toilet:\"")
fmt.Scanf("%d",&F)
fmt.Scanf("%d",&M)

connections_1 := [F]Person{
Female{make(chan bool),make(chan bool),make(chan bool),make(chan bool), make(chan bool)},
}
connections_2 := [M]Person{
Male{make(chan bool),make(chan bool),make(chan bool),make(chan bool), make(chan bool)},
}

go bathroom(connections_1[:],connections_2[:], finish)

for index, conn := range connections {
go woman(index, &conn)
}

for index, conn := range connections {
go men(index, &conn)
}

<-finish
}
``````

I would be very grateful for your help explaining what mistakes I made and tips on how to fix them so that my solution works.

Were you able to fix the mistakes?