Government Security
Network Security Resources

Jump to content

Photo

Concurrent Routines In Google's Go Language

the go programming language go googles go language concurrent programming

  • Please log in to reply
2 replies to this topic

#1 Arssss

Arssss

    Private

  • Members
  • 1 posts

Posted 22 November 2011 - 03:32 PM

Is it possible in go: Suppose, I have 3 concurrent routines which can send integers to each other. Now, suppose both concurrent routine 2 & 3 sends an integer to concurrent routine 1. Is it possible in go that routine 1 takes both two values and process it farther ? To make it clear, I have following code:


package main
import "rand"

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

if i%2 != 0 {
command13 <- i
}

print(<-response13, " 1st\n");
}
close(command12)
}

func Routine2(command12 chan int, response12 chan int, command23 chan int, response23 chan int) {
for i := 0; ; i++ {
x, open := <-command12
if !open {
return;
}
print(x , " 2nd\n");
y := rand.Intn(100)
if i%2 == 0 {
command12 <- y
}

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



}
}

func Routine3(command13 chan int, response13 chan int, command23 chan int, response23 chan int) {
for i := 0; ; i++ {
x, open := <-command13
if !open {
return;
}
print(x , " 3nd\n");
y := rand.Intn(100)
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 )
Routine2(command12, response12,command23, response23)
Routine3(command13, response13,command23, response23 )
}
Here, in this example routine 1 can send an int to routine 2 or 3. I assume it is routine 3. Now suppose, routine 3 also send an int to routine 2. Is it possible for routine 2 to take those two values and process further? Can any body help to modify it accordingly.

#2 BobS0327

BobS0327

    Private First Class

  • Members
  • 27 posts

Posted 25 November 2011 - 05:24 AM

I have 3 concurrent routines which can send integers to each other


I really only see one concurrent routine in your example. A concurrent routine is preceded by the go keyword. Only Routine1 is concurrent. The other two routines are run on the caller.

Also, when your code is compiled and run, it generates a lot of runtime errors, primarily because of the above issue. I would suggest you fix the concurrency issue and any other runtime issues and post the updated code.

#3 BobS0327

BobS0327

    Private First Class

  • Members
  • 27 posts

Posted 26 November 2011 - 09:01 PM

An addendum...

Here is a very basic example of what I believe you're attempting to do.

Concurrent routines 2 and 3 send an integer to concurrent routine 1. Concurrent routine 1 then multiplies the integers received from concurrent routines 2 and 3.

package main

import (
"fmt"
)

type Any interface {}

func Routine2(start, count int, out chan<- int) {
for i := 0; i < count; i++ {
out <- start + i
}
close(out)
}

func Routine3(start, count int, out chan<- int) {
for i := 0; i < count; i++ {
out <- start + i
}
close(out)
}

func Routine1(in3 <-chan int, in2 <-chan int,done chan<- bool) {
numindex := 0
array2 := make(map[Any]int)
array3 := make(map[Any]int)

for num2 := range in2 {
fmt.Printf("in2 %d\n", num2)
array2[numindex] = num2
numindex += 1
}

numindex = 0
for num3 := range in3 {
fmt.Printf("in3 %d\n", num3)
array3[numindex] = num3
numindex += 1
}
for i := 0; i < 3; i++ {
fmt.Printf("in2 %d times ln3 %d equals %d\n", array2[i], array3[i], array2[i] * array3[i])
}

done <- true
}

func main() {
OutRoutine2 := make(chan int)
OutRoutine3 := make(chan int)
done := make(chan bool)
go Routine3(1, 3, OutRoutine3)
go Routine2(6, 3, OutRoutine2)
go Routine1(OutRoutine3,OutRoutine2, done)

<-done
}




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users