1 SyStem CallS
what is a system call
A system call is the interface provided by the underlying operating system that your application is currently running on. Using this interface, your application can communicate with the operating system to perform an operation. In general, the operating system provides numerous services that applications can take advantage of

sys/unix Package
The sys/unix package is a package provided by the Go language that provides a system-level interface to interact with the operating system. Go can run on a variety of operating systems, which means that it provides different interfaces to applications for different operating systems.
Listing 1-2. Go System Call
package main
import (
u "golang.org/x/sys/unix"
"log"
)
func main() {
c := make([]byte, 512)
log.Println("Getpid : ", u.Getpid())
log.Println("Getpgrp : ", u.Getpgrp())
log.Println("Getppid : ", u.Getppid())
log.Println("Gettid : ", u.Gettid())
_, err := u.Getcwd(c)
if err != nil {
log.Fatalln(err)
}
log.Println(string(c))
}
2022/02/19 21:25:59 Getpid : 12057 2022/02/19 21:25:59 Getpgrp : 12057 2022/02/19 21:25:59 Getpgrp : 29162 2022/02/19 21:25:59 Gettid : 12057 2022/02/19 21:25:59 /home/nanik/
- Getpid : Obtains the process id of the current running sample app
- Getpgrp : Obtains the group process id of the current running app
- Getppid : Obtains the parent process id of the current running app
- Gettid : Obtains the caller’s thread id
2nd example:
import (
....
"golang.org/x/sys/unix"
)
....
func main() {
log.SetFlags(0)
flag.Parse()
if len(flag.Args()) < 1 {
flag.Usage()
os.Exit(1) }
....
for _, arg := range flag.Args() {
var statx unix.Statx_t
if err := unix.Statx(unix.AT_FDCWD, arg, flags, mask,
&statx); err != nil {
....
dev := unix.Mkdev(statx.Dev_major, statx.Dev_minor)
....
}
type Statx_t struct {
Mask uint32
Blksize uint32
Attributes uint64
Nlink uint32
Uid uint32
Gid uint32
Mode uint16
_ [1]uint16
Ino uint64
Blocks uint64
Attributes_mask uint64
Atime
...
Dev_major
Dev_minor
...
}
浙公网安备 33010602011771号