首页 > 其他分享 >1 SyStem CallS

1 SyStem CallS

时间:2023-01-27 19:55:56浏览次数:57  
标签:25 log CallS system SyStem unix operating 21

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

image

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/
  1. Getpid : Obtains the process id of the current running sample app
  2. Getpgrp : Obtains the group process id of the current running app
  3. Getppid : Obtains the parent process id of the current running app
  4. 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
  ...
}

标签:25,log,CallS,system,SyStem,unix,operating,21
From: https://www.cnblogs.com/bcb51/p/17069129.html

相关文章