runtime.GOMAXPROCS not working as expected

906 Views Asked by At

I have a simple go program -

main.go -

package main

import (
    "log"
    "runtime"
    "time"
)

func main() {
    runtime.GOMAXPROCS(1)
    log.Println("running")
    time.Sleep(10 * time.Minute)
}

I build binary like this -

GOOS=linux go build

and run it in a centos machine -

# ./test
2017/10/27 14:20:15 running

I wonder why 2 different cores (1 & 6) are used for this simple program even if GOMAXPROCS is set to 1.

process using 2 cpu cores

Sometimes 3-4 cores are also used.

Any idea about this?

Thank you.

1

There are 1 best solutions below

1
On

You are running four processes. The kernel schedules those onto cores. GOMAXPROCS has nothing to do with this; it only affects the number of threads for a single process, and only user-level code.