E
E
efim232014-12-10 23:08:49
go
efim23, 2014-12-10 23:08:49

How to properly implement channels in Go?

Good evening. I really ask for help, for several days I can’t implement the program, the condition of which is:

  • There are N carts on a roller coaster that can hold P people.
  • People come to the attraction at random intervals and wait in line. If there is a free cart at the station, it is served for boarding when at least P people are recruited in the queue. People sit in order. After landing, the cart leaves, and another cart can take its place.
  • When the cart returns, people get out of it in turn, after which it either gets on board or waits in line for other carts. At unloading, a queue of carts can also form.
  • There can be several carts on the way at the same time, but they cannot overtake each other.
  • The trolleys travel along the route time T.
  • N, P and T are user defined.

I would be very grateful for any help, whether it be tips or pieces of code. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tyranron, 2014-12-12
@efim23

Alternatively: here .
I tried to fulfill all the conditions of the assignment. Unfortunately, there is no word about the size of the queue in the task. I made it 2*N*P.

V
Vitaly Yakovenko, 2014-12-11
@Xazzzi

type Тележка chan Человек
тележки := make(chan Тележка,N)
for i:=0;i<N; i++ {
тележки <- make(chan Человек,P)
}
//очередь так сказать с запасом, всё равно больше чем P*N человек на аттракцион не влезет
людиВходящие := make(chan Человек,P*N)
людиВыходящие := make(chan Тележка,P*N)
go func() {
//ждем телегу, рассовываем по ней людей
//не стоять же им в очереди, пусть сразу в телегу садятся, если место есть =)
свободнаяТелега := <-тележки
for len(свободнаяТелега)!=cap(свободнаяТелега) {
свободнаяТелега <- <- людиВходящие
}
time.Sleep(T) //WHEEEE
for довольныйЧеловек := range свободнаяТелега {
людиВыходящие <- довольныйЧеловек
}
тележки <- свободнаяТелега
}()

Pretty close to what is asked in the assignment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question