Sobes.tech
Junior — Middle

In which cases and where is the state of a goroutine saved when calling a system call in Go?

sobes.tech AI

Answer from AI

In Go, when calling a system call (syscall), a goroutine can be suspended because the system call might block execution.

The state of the goroutine (stack, registers, execution context) is saved in a structure associated with that goroutine so that after the system call completes, execution can continue from the same point.

Goroutines are managed by the Go scheduler, which switches between goroutines when one is blocked. The state is stored in the process memory, in a structure called g (goroutine), which contains all necessary information for resumption.

Thus, the goroutine's state is saved in the process memory in the goroutine structure and restored by the scheduler after the system call completes.

In which cases and where is the state of a goroutine… - sobes.tech