How can "let's go to Golang" to transfer process resources and set the number of available CPU cores

Posted by fewtrem on Mon, 03 Jan 2022 01:51:52 +0100

How can "let's go to Golang" to transfer process resources and set the number of available CPU cores

Previously, I learned about the basic knowledge of the collaboration process, the CPS concurrency model, and the million level concurrency of Golang. Let's transfer the collaboration resources and set the number of available CPU cores.

Transfer of process resources

Let's look at the implementation results first:

Subprocess 0
 Subprocess 0 1
 Subprocess 0 2
 Subprocess 0 3
 Subprocess 0 4
 Subprocess 0 5
 Subprocess 2 0
 Sub process 2 1
 Sub process 2
 Sub process 2 3
 Sub process 2 4
 Sub process 2 5
 Sub process 2 6
 Sub process 2 7
 Subprocess 1 0
 Subprocess 0 6
 Subprocess 0 7
 Sub process 1
 Sub process 1 2
 Sub process 1 3
 Sub process 1 4
 Sub process 1 5
 Sub process 1 6
 Subprocess 1 7

We can see that first the print content of sub process 0, then the print content of word process 2, then the print result of sub process 1, then the print result of sub process 0 suddenly appears, and then the print result of sub process 1.

Why? Don't worry, take a look at the code and think about why!

func main() {
	for i:=0;i<3;i++{
		go func(index int) {
			task("Subprocess"+strconv.Itoa(index))
		}(i)
	}
	time.Sleep(5 * time.Second)
}

func task(name string)  {
	for i:=0;i<8;i++{
		if name == "Subprocess 1"{
            //Gosched yields the processor, allowing other goroutines to run. It does not suspend the current goroutine, so execution resumes automatically.
			runtime.Gosched()
		}
		fmt.Println(name,i)
	}
}

This code first activates three sub processes: sub process 0, sub process 1 and sub process 2. Then, the task function is executed for each coroutine, but the runtime is executed for child coroutine 1 Gosched() operation.

Let's introduce runtime Gosched(), gosched generates a processor that allows other goroutines to run first. It will not abort the current goroutine, so the current goroutine will automatically resume operation. Its function is to reduce the priority of the current collaboration.

We know that the three processes are concurrent. However, runtime is implemented for coroutine 1 Gosched (), which enables sub process 1 to transfer the process resources. Therefore, the last printout must be subprocess 1. However, not all sub processes 1 are the last to execute the print operation. There is a child coroutine 1 printed out before child coroutine 0. We have increased the number of collaborative processes. Change the range of the for loop in the main function from 03 to 0108, and we will find that the sub coroutine 1 is printed at the end (because there are too many output results, the output results are not shown here). However, it should be noted that if there are millions of concurrent processes and one million processes are opened at the same time, the last one may not be sub process 1, because although runtime.Gosched() will reduce the priority of the process and transfer the process resources, it does not necessarily complete the absolute final execution of the process.

Set the number of available CPU cores

We open the task manager, enter the "performance" column, right-click the CPU and select "change graphics to", and change the "overall utilization" to "logical processor".

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-sd4sr31f-1628902271033)( https://raw.githubusercontent.com/ReganYue/ForPicGo/main/20210801161637.png )]

Here you can see that the number of CPU cores in the picture is eight.

Then we can use GO language to check the number of cores of the computer CPU.

package main

import (
	"fmt"
	"runtime"
)

func main() {

	fmt.Println("available CPU The number of cores is",runtime.NumCPU())
	
    // GOMAXPROCS sets the maximum number of CPUs that can be executing
    // simultaneously and returns the previous setting. If n < 1, it does not
    // change the current setting.
    // The number of logical CPUs on the local machine can be queried with NumCPU.
    // This call will go away when the scheduler improves.
	fmt.Println("set up CPU The number of available cores for is 1, which was previously set to",runtime.GOMAXPROCS(1))
}

Use runtime Numcpu () can print.

Instead, use runtime Gomaxprocs (n) can set the maximum number of cores to run. Here is to set the maximum number of available cores of the CPU to 1 And the function will have a return value to return the previous maximum number of available cores. If n < 1, the current setting is not changed.

Topics: Python Java Javascript Go