Skip to main content

· 3 min read
Kundan Kumar

System calls, as the name suggests, are requests made by a user program to the operating system to perform certain tasks on behalf of the program. They resemble regular function calls initiated by the user program. Whether it's writing to or reading from a file, making network calls, or other operations, system calls work behind the scenes to facilitate these actions. Today, we'll delve into the read and write system calls and explore the intricacies surrounding them.

Read System Call

Golang provides more abstracted os package to do file operation. But you can directly use syscall package if needed.

func main() {
fd, err := syscall.Open("example.txt", syscall.O_RDONLY, 0)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer syscall.Close(fd)

buffer := make([]byte, 1024) // buffer to store read data
bytesRead, err := syscall.Read(fd, buffer)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
fmt.Printf("Read %d bytes from file:\n%s", bytesRead, string(buffer[:bytesRead]))
}

Above code opens a file using syscall.Open call which returns a file descriptor. Kernel checks if requested data is present in buffer cache, if data is present it copies the data into user buffer. Otherwise, kernel makes a disk read to fetch the data and store it in buffer. Common pattern is to read more than what is required so next time read syscall will result in cache hit.

read-sys.png

Write System Call

func main() {
fd, err := syscall.Open("example.txt", syscall.O_WRONLY | syscall.O_CREAT | syscall.O_TRUNC, 0644)
if err != nil {
fmt.Println("Error opening or creating file:", err)
return
}
defer syscall.Close(fd)

data := []byte("Hello, World!\n")
bytesWritten, err := syscall.Write(fd, data)
if err != nil {
fmt.Println("Error writing to file:", err)
return
}
fmt.Printf("Wrote %d bytes to file\n", bytesWritten)
}

Here syscall.Write writes the data in os write buffer & returns. It's the OS responsibility to write the data to storage layer. fsync syscall can be used to synchronize file's data & metadata (permissions, timestamps & directory) to storage medium, ensuring durability & system integrity.

write-sys.png

File Description Offset

  • File Descriptor: a number to uniquely identify a open file. When file is opened, syscall returns a file descriptor not the file path. This file descriptor points to the file description (stores metadata).

All read/write in a file happens at an offset maintained by the file description which is referenced by file descriptor. If write bytes are more than the file size, it simply expands to accommodate the bytes to be written.

img_2.png

img_3.png

  • truncate syscall: shrinks or expands the file size based on the given input. Expanding the file will add zeroed out bytes
  • lseek syscall: moves the marker to given byte offset. You can define offset greater than file size which will expand the file.

· One min read
Kundan Kumar

Fueled by my midnight coffee & desperate need to stop procrastinating. Finally starting my tech blog - spent a good solid 30 minutes finding a good blogging site and copying its vibe.

So, here is my Hello World!! moment

I am Kundan Kumar, a software engineer since 2020. JAVA is my coding jam for work. Lately, obsessed with Distributed Systems & Database Internals and also trying to convince myself Golang is my best friend. My blog might not be the best ones you will find in the chaotic internet sea, but I will try to give some good tech tidbits & inspire you a bit. My goal is to learn, share & maybe, just maybe, make this blog worth your time. Wish me luck!.

· 10 min read
Kundan Kumar

Cracking CKAD examination requires not only a solid comprehension of Kubernetes concepts but also proficiency in executing K8 commands. In this following, I have shared notes which I took during my CKAD preparation. I faced difficulty memorizing all Kubernetes object YAMLs and imperative commands, I consolidate all configurations in a single location as a comprehensive revision guide before the exam. I hope these notes can help you out too!

· 2 min read
Kundan Kumar

I got tired of deploying my Docusaurus website to GitHub Pages manually, and decided to do something about it using GitHub Action.

Initially, I was planning to follow the official guide on doing so. However, it was actually much more complicated than I liked. I did not really want to generate and store a SSH key on GitHub. Too much effort man.

I decided it was better off for me to write my own script. Here it is: