Home >Backend Development >Golang >## How do `syscall.RawSyscall()` and `syscall.Syscall()` differ in Go, and when should you use each?
The Syscall.RawSyscall() function in Go takes four uintptr type parameters:
It returns two uintptr values, r1 and r2, and an Errno value:
The assembly implementation of RawSyscall() for darwin/amd64 can be found at the following link:
[https://golang.org/src/pkg/syscall/asm_darwin_amd64.s?h=RawSyscall](https://golang.org/src/pkg/syscall/asm_darwin_amd64.s?h=RawSyscall)
The lines 61-80 in this file contain the following assembly code:
<code class="assembly">61 TEXT ·RawSyscall(SB),7, 62 MOVQ 16(SP), DI 63 MOVQ 24(SP), SI 64 MOVQ 32(SP), DX 65 MOVQ , R10 66 MOVQ , R8 67 MOVQ , R9 68 MOVQ 8(SP), AX // syscall entry 69 ADDQ x2000000, AX 70 SYSCALL 71 JCC ok1 72 MOVQ $-1, 40(SP) // r1 73 MOVQ , 48(SP) // r2 74 MOVQ AX, 56(SP) // errno 75 RET 76 ok1: 77 MOVQ AX, 40(SP) // r1 78 MOVQ DX, 48(SP) // r2 79 MOVQ , 56(SP) // errno 80 RET</code>
This code essentially performs the following tasks:
The "z" prefix in the filename zsyscall_darwin_amd64.go indicates that it contains system call wrappers for unprivileged processes. These wrappers handle additional tasks such as switching to a different user or thread ID before executing the syscall.
The primary difference between Syscall() and RawSyscall() is that Syscall() calls runtime.entersyscall(SB) and runtime.exitsyscall(SB) before and after executing the syscall, respectively. These functions allow the runtime system to track the blocking status of goroutines. RawSyscall(), on the other hand, does not make these calls, so it does not interact with the runtime system in the same way.
Syscall() is typically used when you have a specific syscall number and arguments that you want to call directly. RawSyscall() is useful when you want more control over the syscall execution, such as when you need to call a syscall that is not exposed by the os package or when you need to manually handle the blocking status of the syscall.
The above is the detailed content of ## How do `syscall.RawSyscall()` and `syscall.Syscall()` differ in Go, and when should you use each?. For more information, please follow other related articles on the PHP Chinese website!