System Calls

Some of the more important system calls are the following. See intro(2). Use truss(1) or Linux's strace to watch the system calls of a process.
Memory management:brk sbrk swapctl mmap
Process management:exec*
exit _exit
fork vfork pipe wait*
kill sig* alarm
getpgrp getpid getppid setpgrp setgid ...
Threads:*lwp*
File class:mount umount sync
chdir chmod chown umask mkdir creat rename rmdir
fcntl stat
I/O:open close ioctl
read write lseek dup
Misc:profil time
acct ulimit
 
System call versus library/function call
In a function call, the code executed is part of the process and came from the program, obtained possibly by linking in libraries at compile time (static libraries: *.a) or at run time (shared libraries: *.so). No other process is directly involved.

In a system call, the major part of the code executed is part of the kernel. The switch is achieved by a software interrupt mechanism. A different process runs. The system calls in intro(2) actually are all C function call front ends to the true system call.

 
Handling failure
Failed library calls typically return EOF if a nonnegative is expected, NULL if a pointer is expected or a non-zero int if 0 is expected. A program simply tests for these.

Failed system calls, with very few exceptions, return NULL if a pointer is expected or -1 for other cases. The external variable errno is set when a failure occurs, but it is not cleared after successful calls and may be set even on success: a multi-part system call, for example, when searching for a file on a path, may fail in some parts but succeed ultimately. errno describes the error in more detail and is not the exit status! Use it as follows:

    #include <errno.h>
    if ( -1 == system_call(...)) /* check if it failed */
        { /* it failed: Only then is errno significant */
          if (errno == ENOENT) fprintf(stderr,"No such file or directory\n");
          /* or */ 
          perror("Function getfile() in program wonderful");

All error numbers have a symbolic names which begin with E, are defined in errno.h and described in intro(2). As an alternative to using errno,

perror(char *namestring)
prints the namestring and the appropriate standard error message on stderr. If the symbolic names are not used, errno.h need not be included.

Last update: 2000 December 30