7

According to this question, a good way to learn system programming would be to implement the existing tools. I decided to try to rewrite ls and ps.

My question is how would one proceed to get documentation for such?

  • Where can I find the API of system calls needed to query the kernel, the file system, etc ...

  • I thought that maybe I could take a look at the source code of the GNU tools. Where can I find it?

  • Are there any (hopefully freely available online) good documentation you guys would recommend as to where to start?

rahmu
  • 20,023

2 Answers2

10

You can use the Glibc manual as a reference. It's not for absolute beginners, but if you're reasonably fluent in C you should be able to read a section and write a working program.

You can find the source of the GNU tools on the GNU website; the easiest way to get it is to obtain the source packages on your Linux distribution (e.g. apt-get source coreutils if your distribution uses Debian packaging utilities). I recommend starting with BusyBox rather than the GNU tools. BusyBox is a set of utilities for embedded Linux systems; it's smaller and simpler than the GNU utilities.

You can run strace ls to see what system calls ls is making. Note that when you write a program, you don't call system calls directly, you call functions in the C library which make system calls. For things like file manipulations, the C library functions like open and read are pretty close to the system calls though.

For learning, a book (on paper) is usually helpful. Advanced Programming in the UNIX® Environment (APUE) is a classic. If you get it, make sure to get the second edition, as the first is quite dated. This book is the reference, but other books may be more suitable for beginners; shop around.

1

On linux, commands as ps,free and top are implemented reading information from proc file-system.

   The proc file system is a pseudo-file system which is used as an inter-
   face to kernel data structures.  It is commonly mounted at /proc.  Most
   of  it  is  read-only,  but  some  files  allow  kernel variables to be
   changed.

Start at man 5 proc and do not forget that all these utilities are open-source, so you can also peek they code.

andcoz
  • 17,130