Go runtime 调度器精讲(九):系统调用引起的抢占
前言 第八讲介绍了当 goroutine 运行时间过长会被抢占的情况。这一讲继续看 goroutine 执行系统调用时间过长的抢占。 系统调用时间过长的抢占 看下面的示例: 1func longSyscall() { 2 timeout := syscall.NsecToTimeval(int64(5 * time.Second)) 3 fds := make([]syscall.FdSet, 1) 4 5 if _, err := syscall.Select(0, &fds[0], nil, nil, &timeout); err != nil { 6 fmt.Println("Error:", err) 7 } 8 9 fmt.Println("Select returned after timeout") 10} 11 12func main() { 13 threads := runtime.GOMAXPROCS(0) 14 for i := 0; i < threads; i++ { 15 go longSyscall() 16 } 17 18 time.Sleep(8 * time.Second) 19} longSyscall goroutine 执行一个 5s 的系统调用,在系统调用过程中,sysmon 会监控 longSyscall,发现执行系统调用过长,会对其抢占。 ...