- Before we start discussing system calls, it will be useful to present a command with which you can learn about and debug system calls.
- The strace command traces the execution of another program, listing any system calls the program makes and any signals it receives.
$ strace hostname
- This produces a couple screens of output. Each line corresponds to a single system call. For each call, the system call's name is listed, followed by its arguments and its return value.
- In the output from "strace hostname", the first line shows the execve system call that invokes the hostname program:
execve("/bin/hostname",["hostname"], [/* 49 vars */]) = 0
- The first argument is the name of the program to run; the second is its argument list, consisting of only a single element; and the third is its environment list, which strace omits for brevity.The next 30 or so lines are part of the mechanism that loads the standard C library from a shared library file.
- Toward the end are system calls that actually help do the program's work. The uname system call is used to obtain the system's hostname from the kernel,
uname({sys="Linux",node="myhostname", ...}) = 0
- Finally, the write system call produces output.
write(1,"myhostname\n",11) = 11