System calls

System calls allow user programs to communicate with the operating system. Every OS has a calling convention that defines how parameters are passed to the function. On Unix, many syscalls are similar to shell commands.

File descriptors #

File descriptor: integer pointing to a file to use in syscalls

A few important standard file descriptors (used by shells and in IPC) are:

  • Standard input (stdin): 0
  • Standard ouput (stdout): 1
  • Standard error (stderr): 2

Common Unix syscalls #

  • read - reads bytes a file descriptor
  • write - writes bytes to a file descriptor
  • seek - repositions read/write file offset
  • fork - crates a child process identical to the parent
  • exec - replaces the process with a new process
  • kill - sends a signal to a process
  • signal - setup a signal handler for the process

Common Unix Signals #

Signals can be seen as a software version of interrupts.

  • SIGINT (2): Interrupt signal, send by CTRL+C in the terminal.
  • SIGKILL (9): Immediately kills a process, cannot be caught or ignored
  • SIGTERM (15): Termination signal
  • SIGSTOP (19) and SIGCONT (18): signals to stop and continue a process, respectively. On the terminal CTRL+Z sends SIGSTOP and the fg builtin command resumes it.
  • SIGSEGV(11): Segmentation fault, often caused by bugs in C programs :)
  • SIGCHLD (17): Child stopped or terminated