2

I'm trying to understand concept of syscalls in linux; i wrote a simple program which calls Write 1000 time on file:

package main

import (
    "fmt"
    "os"
    "time"
    "math/rand"
)



const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func RandStringBytes(n int) []byte {
    b := make([]byte, n)
    for i := range b {
        b[i] = letterBytes[rand.Intn(len(letterBytes))]
    }
    return b
}


func main(){
    stat, err := os.Stat(os.Args[1])
    fmt.Println(stat, err)
    time.Sleep(time.Second)
    file, _ := os.OpenFile(os.Args[1], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
    for i := 0; i <= 1000; i++{
        a := RandStringBytes(1000)
        file.Write(a)
    }
    file.Close()
}

So, I expect to write syscall to be called 1000 times; But, when i count syscalls via strace strace ./main file.txt 2>&1 | grep 'write' | wc -l I always get random number of write syscalls,sometimes ~200, sometimes ~300. I doubt that randomness of written strings give this effect. But the mystery is: why there is <1000 write syscalls?

I admit that maybe I misunderstood strace utility, maybe it skips some messages.

Why there is different number of syscalls than i expect?

Ilya Boltnev
  • 1,031
  • 3
  • 13
  • 27

0 Answers0