- Forward


UNIX System Calls
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Background Definitions (The Narrow View)
Back SMYC Forward
  • Operating System:
    • A "layer" of software between application programs and the hardware (that manages/allocates computer resources)
  • Kernel:
    • The portion of the operating system that is always resident in memory
New Definitions
Back SMYC Forward
  • One Definition:
    • System calls are the interface between an application and the kernel
  • Another Definition:
    • A system call is a controlled entry point into the kernel
Error Handling
Back SMYC Forward
  • Some Observations:
    • System calls almost always return a status value
    • When a system call fails it sets the global int named errno to a positive value (described in <errno.h> errno.h )
  • An Important Practice:
    • Always check the status value
  • An Unfortunate Reality:
    • For brevity, examples used in courses often do not handle errors
Error Handling (cont.)
Back SMYC Forward
A Handy Library Function: perror() perror
void perror(const char *prefix);
Purpose:
Write a message describing the last error encountered to standard error
Details:
prefix The prefix to the message
#include <stdio.h> stdio.h
Error Handling (cont.)
Back SMYC Forward

A Rudimentary Example

fd = open(path, flags, mode); if (fd == -1) { perror("Error"); exit(EXIT_FAILURE); }
Error Handling: (cont.)
Back SMYC Forward
Another Handy Library Function: strerror() strerror
char *strerror(int n);
Purpose:
Return a string describing an error
Details:
n The error number (e.g., from errno)
#include <string.h> string.h
Some Details of System Calls
Back SMYC Forward
  • Processor State:
    • A system call temporarily changes the processor state from user mode to kernel mode so that the CPU can access protected kernel memory and perform certain operations
  • Identification:
    • The set of system calls is fixed (and each is identified internally by a unique number that corresponds to an offset in a jump table in the kernel)
  • Atomicity:
    • Many system calls are executed atomically (i.e., all of the steps are completed with no chance of interruption)
Some Details of System Calls (cont.)
Back SMYC Forward
  • Arguments:
    • System calls can have arguments (which must be transferred from user space to kernel space and vice versa)
  • Invocation:
    • System calls are normally not invoked directly (i.e., using syscall() syscall ) but through wrapper functions (e.g., in glibc), often with the same name, that essentially: (1) copy arguments to registers; (2) invoke the system call; and (3) set errno
System Data Types
Back SMYC Forward
  • Shortcomings of Using C Fundamental Types:
    • The sizes of fundamental types vary across implementations (e.g., a long may be 4 or 8 bytes)
    • Types may vary between releases of a single implementation (e.g., 16 bits versus 32 bits)
  • Avoiding Potential Problems:
    • Use standard system data types (that typically end in _t and are defined in <sys/types.h> sys/types.h
  • Printing System Data Type Values when Debugging:
    • Use %ld and cast the value using (long)
Learning About System Calls
Back SMYC Forward
  • Two Reminders:
    • man man is an interface to the on-line reference manual
    • The on-line reference manual is divided into sections - system calls are in section 2
  • Getting a List of System Calls:
    • man syscalls
  • Getting the Pages for a Specific System Call:
    • man 2 term
Learning About System Calls (cont.)
Back SMYC Forward
  • The Synposis:
    • What to #include
    • What to #define
    • Syntax
  • The Description:
    • The purpose
  • The Return Value:
    • What is returned
  • Errors:
    • Possible values of errno
Library Functions
Back SMYC Forward
  • Purposes:
    • The C standard library contains functions for multiple purposes (e.g., working with strings, working with times)
  • An Observation:
    • Some library functions are layered on top of system calls (e.g., fopen() fopen and some are not (e.g., strncmp() strncmp )
  • Implementations:
    • The GNU C Library glibc
    • Others (e.g., with smaller memory requirements)
There's Always More to Learn
Back -