dongwei6457 2016-12-25 15:28
浏览 161
已采纳

Golang,进程和共享内存

Today a friend of mine told me that Go programs can scale themselves on multiple CPU cores. I were quite surprised to hear that knowing that system task schedulers do not know anything about goroutines and hence can't run them on multiple cores.

I did some search and found out that Go programs can spawn multiple OS tasks to run them on different cores (the number is controlled by GOMAXPROCS environment variable). But as far as I know forking a process leads to complete copy of process data and different processes run in different address spaces.

So what about global variables in Go programs? Are they safe to use with multiple goroutines? Do they somehow synchronize between system processes? And if they do then how? I am mainly concerned about linux and freebsd implementations.

  • 写回答

1条回答 默认 最新

  • dongpao5127 2016-12-25 23:26
    关注

    I figured it out! It's all in go sources.

    There is a Linux system call that I were unaware of. It's called "clone". It is more flexible than fork and it allows a child process to live in its parent's address space.

    Here is a short overview of the thread creation process.

    First there is a newm function in src/runtime/proc.go. This function is responsible for creating a new working thread (or machine as it is called in comments).

    // Create a new m. It will start off with a call to fn, or else the scheduler.
    // fn needs to be static and not a heap allocated closure.
    // May run with m.p==nil, so write barriers are not allowed.
    //go:nowritebarrier
    func newm(fn func(), _p_ *p) {
    
        // ... some code skipped ...
    
        newosproc(mp, unsafe.Pointer(mp.g0.stack.hi))
    }
    

    This function calls newosproc which is OS-specific. For Linux it can be found in src/runtime/os_linux.go. Here are relevant parts of that file:

    var (
        // ...
    
        cloneFlags = _CLONE_VM | /* share memory */
            _CLONE_FS | /* share cwd, etc */
            _CLONE_FILES | /* share fd table */
            _CLONE_SIGHAND | /* share sig handler table */
            _CLONE_THREAD /* revisit - okay for now */
    )
    
    // May run with m.p==nil, so write barriers are not allowed.
    //go:nowritebarrier
    func newosproc(mp *m, stk unsafe.Pointer) {
    
        // ... some code skipped ...
    
        ret := clone(cloneFlags, /* ... other flags ... */)
    
        // ... code skipped
    }
    

    And the clone function is defined in architecture-specific files. For amd64 it is in src/runtime/sys_linux_amd64.s. It is the actual system call.

    So Go programs do run in multiple OS threads which enables spanning across CPUs, but they use one shared address space.

    Phew... I love Go.

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

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。