所有的goroutine都处于休眠状态 – 死锁!——- 错误

huangapple go评论115阅读模式
英文:

All goroutines are asleep - deadlock! ------- Error

问题

我想写三个并发的go例程,它们彼此发送整数。现在,我的代码已经编译成功,但是在第一次执行后出现错误“所有goroutine都处于休眠状态-死锁!”。我试图找到错误,但是我找不到代码逻辑中的任何错误。有人可以帮我找到代码中的错误吗?我的代码如下所示。提前致谢。

package main

import "rand"

func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) {
    for i := 0; i < 10; i++ {
        y := rand.Intn(10)
        if y%2 == 0 {
            command12 <- y
        }

        if y%2 != 0 {
            command13 <- y
        }
        select {
        case cmd1 := <-response12:
            print(cmd1, " 1st\n")
        case cmd2 := <-response13:
            print(cmd2, " 1st\n")
        }
    }
    close(command12)
}

func Routine2(command12 chan int, response12 chan int, command23 chan int, response23 chan int) {
    for i := 0; i < 10; i++ {
        select {
        case x, open := <-command12:
            {
                if !open {
                    return
                }
                print(x, " 2nd\n")
            }

        case x, open := <-response23:
            {
                if !open {
                    return
                }
                print(x, " 2nd\n")
            }
        }

        y := rand.Intn(10)
        if y%2 == 0 {
            response12 <- y
        }

        if y%2 != 0 {
            command23 <- y
        }

    }
}

func Routine3(command13 chan int, response13 chan int, command23 chan int, response23 chan int) {
    for i := 0; i < 10; i++ {
        select {
        case x, open := <-command13:
            {
                if !open {
                    return
                }
                print(x, " 2nd\n")
            }
        case x, open := <-command23:
            {
                if !open {
                    return
                }
                print(x, " 2nd\n")
            }
        }

        y := rand.Intn(10)
        if y%2 == 0 {
            response13 <- y
        }

        if y%2 != 0 {
            response23 <- y
        }

    }
}

func main() {
    command12 := make(chan int)
    response12 := make(chan int)
    command13 := make(chan int)
    response13 := make(chan int)
    command23 := make(chan int)
    response23 := make(chan int)

    go Routine1(command12, response12, command13, response13)
    go Routine2(command12, response12, command23, response23)
    Routine3(command13, response13, command23, response23)
}

有人可以告诉我为什么如果我将Routine2和Routine3声明为go例程,为什么输出是[无输出]。我是GO的新手,并且根据我从“http://golang.org/doc/effective_go.html#concurrency”中理解的,go用于在同一地址空间中与其他goroutine并行执行goroutine。那么,问题是什么,为什么所有的例程都在运行但是没有输出。

为了使程序更清晰:我实际上正在尝试创建每两个例程之间的两个通道,然后使用一个通道将整数发送到另一个通道,并通过另一个通道从该例程接收整数。例如,例程1和3之间的通道是command13和response13。例程1使用command13发送整数,并使用response13从例程3接收整数。对于例程3,response13用于发送整数,command13用于从例程1接收整数(command/response 13表示例程1和3之间的通道)。现在,由于三个例程是并发的,并且它们有特定的例程来处理接收到的消息或发送消息,为什么它们会陷入死锁状态?

英文:

I want to write three concurrent go routines that sends integers to each other. Now, my code is compiled properly, however after first execution it gives error "all goroutines are asleep - deadlock!". I tried to find the error but I could not able to find any error in code logic.Can anybody help me to find the mistake with my code. My code is given below. Thanks in advance.

package main

import &quot;rand&quot;

func Routine1(command12 chan int, response12 chan int, command13 chan int, response13 chan int) {
	for i := 0; i &lt; 10; i++ {
		y := rand.Intn(10)
		if y%2 == 0 {
			command12 &lt;- y
		}

		if y%2 != 0 {
			command13 &lt;- y
		}
		select {
		case cmd1 := &lt;-response12:
			print(cmd1, &quot; 1st\n&quot;)
		case cmd2 := &lt;-response13:
			print(cmd2, &quot; 1st\n&quot;)
		}
	}
	close(command12)
}

func Routine2(command12 chan int, response12 chan int, command23 chan int, response23 chan int) {
	for i := 0; i &lt; 10; i++ {
		select {
		case x, open := &lt;-command12:
			{
				if !open {
					return
				}
				print(x, &quot; 2nd\n&quot;)
			}

		case x, open := &lt;-response23:
			{
				if !open {
					return
				}
				print(x, &quot; 2nd\n&quot;)
			}
		}

		y := rand.Intn(10)
		if y%2 == 0 {
			response12 &lt;- y
		}

		if y%2 != 0 {
			command23 &lt;- y
		}

	}
}

func Routine3(command13 chan int, response13 chan int, command23 chan int, response23 chan int) {
	for i := 0; i &lt; 10; i++ {
		select {
		case x, open := &lt;-command13:
			{
				if !open {
					return
				}
				print(x, &quot; 2nd\n&quot;)
			}
		case x, open := &lt;-command23:
			{
				if !open {
					return
				}
				print(x, &quot; 2nd\n&quot;)
			}
		}

		y := rand.Intn(10)
		if y%2 == 0 {
			response13 &lt;- y
		}

		if y%2 != 0 {
			response23 &lt;- y
		}

	}
}

func main() {
	command12 := make(chan int)
	response12 := make(chan int)
	command13 := make(chan int)
	response13 := make(chan int)
	command23 := make(chan int)
	response23 := make(chan int)

	go Routine1(command12, response12, command13, response13)
	go Routine2(command12, response12, command23, response23)
	Routine3(command13, response13, command23, response23)
}

Can anyone inform me why if I declare Routine2 and Routine3 as go routine, why the output is [no output]. I am new in GO and as per I understood from "http://golang.org/doc/effective_go.html#concurrency", go is used for executing goroutine in parallel with other goroutines in the same address space. So, what is the problem, that all routines are running but output is [no output].

To make program more clear: What actually I am tiring to do is creating two channels between each two routines and then use one channel to send int to other channel and receive int by another channel from that routine. For example between routine 1 & 3 channels are command13 & response13. routine 1 uses command13 to send int and response13 to receive int to/from routine 3.For routine 3 response13 used to send int and command13 to receive int to/from routine 1 (command/response 13 represents channel between routine 1 and 3).Now, as three routines are concurrent and they have specific routines to handle received msg or sending msg, why they go to deadlock ?

答案1

得分: 8

go Routine1(command12,response12,command13,response13)
go Routine2(command12,response12,command23,response23)// go routine
Routine3(command12,response12,command23,response23)

这将在一个新的goroutine中启动Routine1,主goroutine将继续执行下一条语句。因此,Routine1和Routine2将同时执行,但是Routine3将在Routine2完成后启动。你可能在这里漏掉了另一个"go"语句。

然后,我试图按照你的程序进行操作。在Routine1中,你执行了

command13 <- y

这将阻塞Routine1,直到有另一个准备好接收你的消息的goroutine。所以你需要在另一个goroutine中使用y := <-command13

但是现在,让我们仔细看一下另外两个goroutine的参数:

Routine2(command12,response12,command23,response23)
Routine3(command12,response12,command23,response23)

正如你所看到的,没有一个goroutine可以访问command13(但你将command12传递了两次)。因此,无论是Routine1还是Routine2或Routine3都无法继续。死锁!

我建议你回到绘图板上。首先考虑你想要做什么,画一些关于预期消息流的图表,然后尝试实现该行为。

目前很难调试你的程序,因为:

  • 我不知道你想要做什么。没有关于消息流或其他任何详细描述。实际上,你的代码根本没有文档。
  • 你将名为response23的通道传递给名为response13的参数等等。很容易混淆它们。
  • 所有那些通用名称,如command12等,使人难以理解这个通道应该做什么
  • 在发布之前对源代码进行gofmt是一个好主意 所有的goroutine都处于休眠状态 – 死锁!——- 错误

作为一个起点,我可以向你推荐Go教程中的“质数”示例。在这个示例中,可能的质数从一个goroutine传递到另一个goroutine。此外,这个示例还包含一些关于消息流的漂亮图形以及一些非常好的解释。你可能会喜欢它。

英文:
go Routine1(command12, response12,command13, response13 )
go Routine2(command12, response12,command23, response23) // go routine
Routine3(command12, response12,command23, response23 )

This will start Routine1 in a new goroutine, and the main goroutine will continue with the next statement. Therefore, Routine1 and Routine2 will be executed concurrently, but Routine3 will be started after Routine2 has finished. You might miss another "go" statement here.

Then, I was trying to follow your program. In Routine1 you do

command13 &lt;- y

This will block Routine1 until there is another goroutine ready which is able to receive your message. So you need a y := &lt;-command13 in another goroutine.

But now, lets look closely at the parameter of the other two goroutines:

Routine2(command12, response12,command23, response23)
Routine3(command12, response12,command23, response23 )

As you can see, none of the goroutines has access to command13 (but you are passing command12 twice). So, neither Routine1 nor Routine2 or Routine3 is able to continue. Deadlock!

I would recommend you to go back to the drawing board. Think about what you are trying to do first, draw some diagrams about the expected flow of messages and then, try to implement that behavior.

It's really hard to debug your program at the moment since,

  • I do not know what you are trying to do. There is no detailed description about the message flow or anything like that. In fact, your code doesn't contain any documentation at all.
  • You are passing channels which are called response23 to a parameter called response13 and so on. It's quite easy to mix them up.
  • All those generic names like command12 etc. make it hard to understand what this channel is supposed to do
  • It's a good idea to gofmt your source code before you post it 所有的goroutine都处于休眠状态 – 死锁!——- 错误

As a starting point, I can recommend you the "Prime Numbers" example form the Go tutorial. In this example, possible prime numbers are passed from one goroutine to another. Additionally, this example also contains some nice graphics about the message flow as well as some really good explanations. You might like it.

答案2

得分: 3

你已经将你的通道声明为阻塞通道。一旦你尝试从其中一个通道发送或接收数据,goroutine将会阻塞,直到该值被读取或接收到一个值为止。

例如,在Routine1中,如果你调用command12 <- y,那个goroutine将会阻塞,直到有其他东西从通道中取出ycommand13也是如此。由于你在循环中运行这些发送操作,而Routine2Routine3是同步运行的,所以你遇到了死锁问题。

通道和goroutine都不能保证不会发生死锁,正如你已经发现的那样。相反,它们通常用于同步和协调并发执行的程序的不同部分。

如果Routine2Routine3也是goroutine,那么没有任何东西可以阻止你的程序终止,这就是为什么在这种情况下你没有输出的原因。

坐下来用一张纸画出你的程序中各个元素之间的交互可能会有益处。

英文:

You have declared your channels as blocking channels. As soon as you try to do a send or receive from one of the channels the goroutine will block until that value is read or until it receives a value.

For example, in Routine1 if you call command12 &lt;- y that goroutine will block until something else pulls y off the channel. Ditto for command13. Since you are running those sends in a loop, and Routine2 and Routine3 run synchronously you hit your deadlock problem.

Neither channels nor goroutines are a guarantee against deadlock as you have found out. Instead they are usually used to synchronize and coordinate different pieces of a concurrently executing program.

If Routine2 and Routine3 are also goroutines, then there is nothing to stop your program from terminating -- that is why you get no output in that case.

It would probably be beneficial to sit with a piece of paper and draw out the interaction between the various elements in your program.

huangapple
  • 本文由 发表于 2011年11月24日 00:56:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/8246065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定