Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6850249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:06:02+00:00 2026-05-27T01:06:02+00:00

I want to write three concurrent go routines that sends integers to each other.

  • 0

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 "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)
}

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:06:03+00:00Added an answer on May 27, 2026 at 1:06 am
    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 <- y
    

    This will block Routine1 until there is another goroutine ready which is able to receive your message. So you need a y := <-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 🙂

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write three concurrent routines that sends integer to each other. Now,
I just want to write DNS client program using C sockets that takes three
I want to write a class with three int values in them and manipulate
I have a database with three tables: user_table country_table city_table I want to write
I want to write a program in Prolog that confirms if a b-tree of
Let's say there are two python scripts that want to write data to the
I want write a little code analyzer which parses nested structures and translates into
I want to write a java program to calculate integral with three-point Gauss. How
I have three list A,B and C. I want to write the contents of
I wrote this n-array tree class. I want to write a method to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.