To the chagrin of the kibitzers, this quickly revealed that one of the operations was being done in the
wrong place!
Moral: Don't try to do too many things in one statement.
It won't make the generated code any more efficient, and it will ruin your chances of debugging the
code. As Kernighan and Plauger pointed out, "Everyone knows that debugging is twice as hard as
writing a program in the first place. So if you're as clever as you can be when you write it, how will
you ever debug it?" [2]
[2] Brian W. Kernighan and P.J. Plauger, The Elements of Programming Style, Second Edition,
p.10, New York, McGraw-Hill, 1978, p. 10.
How Is a Library Call Different from a System Call?
One question we sometimes use to see if a candidate knows his or her way around programming is
simply, "What is the difference between a library call and a system call?" It's amazing how many
people never figured it out. We haven't seen many books that describe the difference, so this is a good
way to determine if a candidate has done much programming and has the intellectual curiosity to find
out about issues like this.
The short answer is that library calls are part of the language or application, and system calls are part
of the operating system. Make sure you say the keyword "trap". A system call gets into the kernel by
issuing a "trap" or interrupt. A comprehensive answer will cover the points listed inTable A-1.
Table A-1. Library Call versus System Call
Library Call System Call
The C library is the same on every ANSI C
implementation
The systems calls are different in each OS
Is a call to a routine in a library Is a call to the kernel for a service
Linked with the user program Is an entry point to the OS
Executes in the user address space Executes in the kernel address space
Counts as part of the "user" time Counts as part of the "system" time
Has the lower overhead of a procedure call Has high overhead context switch to kernel and
back
There are about 300 routines in the C library libc There are about 90 system calls in UNIX, (fewer in
MS-DOS)
Documented in Section 3 of the UNIX OS
manual
Documented in Section 2 of the UNIX OS manual
Typical C library calls: system, fprintf, malloc Typical system calls: chdir, fork, write, brk
Library routines are usually slower than in-line code because of the subroutine call overhead, but
system calls are much slower still because of the context switch to the kernel. On a SPARCstation 1,
we timed the overhead of a library call (i.e., how fast a procedure call is made) at about half a
microsecond. A system call took seventy times longer to establish (35 microseconds). For raw
performance, minimize the number of system calls wherever possible, but remember, many routines in
the C library do their work by making system calls. Finally, people who believe that crop circles are
the work of aliens will have trouble with the concept that the system() call is actually a library call.