System Call

Page content

I wathed the following videos.

Here is my note about system call.

POSIX

From Wikipedia:

The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines the application programming interface (API), along with command line shells and utility interfaces, for software compatibility with variants of Unix and other operating systems.

On RAM

There are a lot of programs (processes) on RAM. The OS process is known as kernel. For the user program to access devices, like a disk or network, the kernel expose some functions. In most modern operating systems, kernel code is in the process memory, in the stack space.

Process state

https://en.wikipedia.org/wiki/Process_state

A process could be blocked when it call I/O, because the I/O speed is slow than CPU clock.

Groups of System calls

  • processes: managing process.
  • files: CRUD files.
  • sockets: TCP/UDP or local socket.
  • signals: send status of process from kernel/process to process.
  • threads: each thread has own code pointer and own stack, but they share address spaces.
  • I/O devices

How to system call with assembly?

https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm

Page table

https://en.wikipedia.org/wiki/Page_table

  • The CPU’s memory management unit (MMU) stores a cache of recently used mappings from the operating system’s page table.

https://stackoverflow.com/questions/55415778/do-pointers-refer-to-physical-or-to-virtual-memories

  • In all modern OSes (Windows, Linux, BSD, etc), all addresses in an userspace application are virtual addresses.

https://stackoverflow.com/questions/56619431/where-is-page-table-located

  • In x86 systems, page tables are structures used by the CPU, but they are too large to be hold in registers, so they are kept in RAM.

The size of page table

https://www.javatpoint.com/os-page-table-size

Logical Address = 24 bits Logical Address space = 2 ^ 24 bytes Let’s say, Page size = 4 KB = 2 ^ 12 Bytes Page offset = 12 Number of bits in a page = Logical Address - Page Offset = 24 - 12 = 12 bits Number of pages = 2 ^ 12 = 2 X 2 X 10 ^ 10 = 4 KB Let’s say, Page table entry = 1 Byte Therefore, the size of the page table = 4 KB X 1 Byte = 4 KB

Translation Lookaside Buffer (TLB)

https://en.wikipedia.org/wiki/Translation_lookaside_buffer

It is a part of the chip’s memory-management unit (MMU). … Referencing the physical memory addresses, a TLB may reside between the CPU and the CPU cache, between the CPU cache and primary storage memory, or between levels of a multi-level cache.

Examples of system calls

  • mmap: memory map pages to the process address space (heap)
  • munmap: unmap pages from the process address space (heap)
  • fork: fork a process.

fork()

Many web pages say, fork is a system call, but it is just a function, who wraps clone() system call.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/fork.c#n2514

In this post, I wrote fork as system call.

https://github.com/torvalds/linux/blob/d0a231f01e5b25bacd23e6edc7c979a18a517b2b/arch/x86/entry/syscalls/syscall_64.tbl

  • The only way to create a new process in UNIX is the fork() system call.
  • fork() will copy the own process, and the new process is called “child process”.
  • During the forking, memory spaces are not copied all, and the memory address of the child process points to the same virsual memory address space (pages) as parent process.
  • Only when one of the parent/child process try to write to a shared page, Linux kernel will copy the value of the address to a new RAM page. This is called Copy-On-Write (a.k.a shadowing).
    • This “shadowing” is different from Rust’s shadowing.

Sidenote: Cache ans Slab

By Wikipedia:

  1. Cache: cache represents a small amount of very fast memory. A cache is a storage for a specific type of object, such as semaphores, process descriptors, file objects, etc.
  2. Slab: slab represents a contiguous piece of memory, usually made of several physically contiguous pages. The slab is the actual container of data associated with objects of the specific kind of the containing cache.

Environment variables

His explanation was wrong

Please refer to this page or check Blackmesh’s comment on the video.

About thread

I need to review this Medium article. It was simple and nice.