duancenxiao0482 2015-02-03 13:51
浏览 127
已采纳

GOLANG:致命错误:所有goroutine都在睡眠中-死锁

package main
import (
    "fmt"
    "os"
    "os/exec"
    "bufio"
    "reflect"
)

func runTheCommand(ch chan<- string, cmD string)  {
    ouT,_ := exec.Command("sh","-c",cmD).Output()
    ch <- string(ouT)
}
// Readln returns a single line (without the ending 
) from the input buffered reader. An error is returned iff there is an error with the buffered reader.
func Readln(r *bufio.Reader) (string, error) {
    var (isPrefix bool = true
        err error = nil 
        line, ln []byte
    )   
    for isPrefix && err == nil {
        line, isPrefix, err = r.ReadLine()
        ln = append(ln, line...)
    }   
    return string(ln),err
}



func main() {
    var chans = []chan string{}
    f, _ := os.Open("../tmpStatus.config")
    r := bufio.NewReader(f)
    cmD, e := Readln(r)
    for e == nil {
        ch := make(chan string)
        chans = append(chans, ch) 
        go runTheCommand(ch,cmD)
        cmD,e = Readln(r)
    }   

    cases := make([]reflect.SelectCase, len(chans))
    for i, ch := range chans {
        cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)}
    }   

    remaining := len(cases)
    for remaining > 0 { 
        chosen, value, ok := reflect.Select(cases)
        if !ok {
            cases[chosen].Chan = reflect.ValueOf(nil)
            remaining -= 1
            continue
        }
        fmt.Printf("%s", value.String())
    }   
}    

This code is correctly printing out all the received values from the channels. But in the end it is exiting with the below error :

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [select]: reflect.rselect(0xc200283480, 0x22, 0x22, 0xffffffffffffffff, 0x0, ...) /usr/local/go/src/pkg/runtime/chan.c:1212 +0x10d reflect.Select(0xc200066800, 0x22, 0x22, 0x1, 0x0, ...) /usr/local/go/src/pkg/reflect/value.go:1957 +0x1fb

I am a newbee at GO. I referred google and somehow managed to write a working code. I didn't understand fully how this script works especially the reflect package commands. My intention here is to execute the unix commands listed in ../tmpStatus.config file parallely and print out the results. I am excited by the concurrency features offered by GO and hence decided to give it a try. Now it is printing the results so fast. Till now, I was doing this in python one by one. I ran this script in go version go1.1.2 linux/amd64 installed to a custom location. Any idea why the deadlock is happening ?

  • 写回答

1条回答 默认 最新

  • drhs13583567608 2015-02-03 14:39
    关注

    It looks like you are trying to to read from a file and then split the work across multiple workers (fan out). I do not see any closing of channels in your code. See https://gist.github.com/kylewolfe/7c54018a9ed16517c0dd for an example of the pattern I'm referring to. For each line of your file, you would read into the one channel, where x workers would read from it (and reflect / do work).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 请教一下c语言的代码里有一个地方不懂
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了