System Calls

Synopsis:

In programs written for Linux, a system call is an entry point into the Linux kernel, requesting an action for the calling program. The system call changes processor state from user mode to kernel mode, allowing the processor to access protected kernel memory.

System calls usually result from the execution of a C or C++ library function, which internally invokes the Linux syscall entry point. Each system call is identified by a number. syscall places the call number and any supplied parameters into architecture specific registers and traps to kernel mode.

Many system calls interact with devices attached to the system or virtual devices through device drivers. Each device driver has an associated file in /dev.

Linux System Calls:

  1. Sources of information:

  2. Devices:

    Linux devices are classified as block or character devices, depending on whether they are read and written with blocks of characters or a single character per read/write call. You can see all the devices currently on your Linux machine with the command:

    cat /proc/devices

    Devices support a universal interface:

    • int open(const char* path, int flags)
    • ssize_t read(int fd, void* buf, size_t count)
    • ssize_t write(int fd, void* buf, size_t count)
    • off_t lseek(int fd, off_t offset, int whence) - block devices only
    • int close(int fd)

    They also support a universal "custom" interface:

    • int ioctl(int fd, int request, ...)

    The arguments after request, if any, are unique to the device, e.g., the "custom" part. Here's a code fragment from a Windows sockets library I wrote:

        size_t Socket::bytesWaiting()
        {
          unsigned long int ret;
          ::ioctlsocket(socket_, FIONREAD, &ret);
          return (size_t)ret;
        } 
                
    For a Linux program you would replace ioctlsocket with ioctl.

  3. Quick Look at Linux System Programming, ppt
  4. Examples: