Part 5 of 8 · 3 min read
Tracing a program
Think of a courier who must show a pass at every door of a building. A simple delivery crosses many doors, and a file copy needs many system calls in the same way. The analogy stops in one place: passes are checked by people, and system calls are checked by kernel code.
In plain words: Even a simple task takes many system calls. OSC uses a file copy as its example: open the input, create the output, then loop: read a piece, write a piece, until the input ends, then close both files. Each step is a system call, and each can fail.
The key lines.
Here is a short C version. It copies a 200-byte file using a buffer of 64 bytes, and counts its calls:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(void) {
char buf[64];
int in = open("in.txt", O_RDONLY);
int out = open("out.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (in < 0 || out < 0) { perror("open"); return 1; }
int reads = 0, writes = 0;
ssize_t n;
for (;;) {
n = read(in, buf, sizeof buf);
reads++;
if (n <= 0) break;
write(out, buf, (size_t)n);
writes++;
}
close(in);
close(out);
printf("copy done: %d read calls, %d write calls\n", reads, writes);
return 0;
}
Output:
copy done: 5 read calls, 4 write calls
A worked example.
The count follows from the sizes. 200 bytes in chunks of 64 gives 64 + 64 + 64 + 8, which is four reads with data. A fifth read finds the end of the file and returns 0. Each of the four chunks is written once.
The tool strace shows the system calls of any program. Running strace ./a.out on this program prints, among other lines, these:
openat(AT_FDCWD, "in.txt", O_RDONLY) = 3
openat(AT_FDCWD, "out.txt", O_WRONLY|O_CREAT|O_TRUNC, 0644) = 4
read(3, "xxxx..."..., 64) = 64
write(4, "xxxx..."..., 64) = 64
read(3, "xxxxxxxx", 64) = 8
write(4, "xxxxxxxx", 8) = 8
read(3, "", 64) = 0
close(3) = 0
close(4) = 0
The numbers 3 and 4 are file descriptors, small integers that the kernel hands out for open files. The line = 0 after the last read shows the end of the file. A trace like this is the quickest way to see what a program really asks the OS to do.
The common kinds of system calls, with Linux and Windows examples (from OSC):
| Kind | Linux | Windows |
|---|---|---|
| Process control | fork(), exit(), wait() |
CreateProcess(), ExitProcess(), WaitForSingleObject() |
| File management | open(), read(), write(), close() |
CreateFile(), ReadFile(), WriteFile(), CloseHandle() |
| Device management | ioctl(), read(), write() |
SetConsoleMode(), ReadConsole() |
| Information | getpid(), alarm(), sleep() |
GetCurrentProcessID(), SetTimer(), Sleep() |
| Communication | pipe(), mmap() |
CreatePipe(), MapViewOfFile() |
| Protection | chmod(), umask(), chown() |
SetFileSecurity() |
File descriptor
A file descriptor is a small non-negative integer that the kernel gives a process as a handle for something the process has opened, such as a file, pipe, socket or device. The process passes the number back to the kernel in later system calls (read, write, close) to say which open item it means.
Example: after open("in.txt", ...) returns 3, the call read(3, buf, 64) reads from that file.
Check yourself: 1. Why does the copy program make a fifth read call? 2. Work out the calls for a 1,000-byte file with a 100-byte buffer. 3. What does the number 3 in the trace stand for?