zero-copy: when exchanging data
- within a user space process(between two threads)
- between two/more process(producer-consumer problem)
- data accessed/copied/moved inside kernel space or between user space process and kernel space portions of OS
I/O related system calls: when user space process has to execute system operations like reading/writing data from/to device(i.e. disk, NIC) through their high level SW interfaces. it has to perform one or moresyustems calls, which are then executed in kernel space
advantages:
- less data copy: CPU can do other tasks
- less context switch between user space and kernel space.
while((n = read(diskfd, buf, BUF_SIZE)) > 0)
write(sockfd, buf , n);
- read:
-
- read() system call, context switch from user space -> kernel space
-
- hw -> kernel buffer: DMA
-
- kernel buffer -> user buffer: cpu copy: context switch from kernel space to user space
-
- write:
-
- write() system call, context switch from kernel space -> user space
-
- user buffer -> socket buffer: cpu copy
-
- socket buffer -> nic: DMA; finish writing context switch from kernel space to user space.
-
basic prerequisite:
3.1. user space and kernel space: virtual mem for each process: -> user space + kernel space
- user space: cannot access to kernel space, when user program needs kernel resouce, need to make system call, context switch: from user -> kernel -> user
- kernel space: process scheudling, mem allocation, HW resouce
3.2 context switch
the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point, which allows multiple processes to share a single central processing unit (CPU).
in the Linux kernel, context switching involves switching registers, stack pointer (it’s typical stack-pointer register), program counter, flushing the translation lookaside buffer (TLB) and loading the page table of the next process to run
3.3. virtual memory: virtual address from different space can point to the same physical memory -> reduce data copy
3.4 DMA:
- user read() system call for IO, user blocking wait for return
- CPU get the system call -> request DMA to IO request
- by IO request, DMA request to disk
- DMA copy data from disk to kernel buffer
- DMA -> cpu: finished
- cpu copy data from kernel buffer to user buffer
- user has data unblocking
Realization:
three ways:
- mmap + write
- sendfile
- sendfile with DMA
in this page, only look at how mmap + write works together
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
mmap can map the buffer between user space and buffer in kernel space;