Process

Process is an instance of a #Program that is being executed. fork System Call# could be used to create a new Process. The process will inherit all the environment variables from the parent process. In #c, this could be accessed via function getenv(), which will either return a pointer to the specified environment variable or a NULL pointer, or third argument to the main function (shown below). The latter will be terminated with a NULL pointer.

int main(int argc, char *argv[], char *envp[]) {
  ...
}

It has the following structure, from top to bottom:

  • stack (where the function call’s address linkage and data elements)
  • heap (for dynamic memory allocation)
  • uninitialised data
  • initialised read-write data
  • initialised read-only data
  • text (contain machine instructions)

Note: In most cases, there will be a space between heap and stack in order to accommodate the growth of these two portions.

Every process has its own unique process ID (PID), which could be obtained via getid() System Call#. We could get its parent’s PID too using getppid(). The process will inherit the user ID and group ID, which could be inspected with getuid() and getgid() or geteuid() and getegid() (getting the effective user ID and group ID, usually is the same with the user ID and group ID).

Links to this page
  • Unix System Call
    exit (to terminate Process#)
    fork (create process by duplicating the calling Process#)
  • TTP3121 Chapter 4: Unix Processes Control
  • TTP3121 Chapter 2: Introduction to Unix Based OS
  • Signal Handling In POSIX

    Signal blocking can be useful in protecting the process from being interrupted while executing a critical task. The signal will be blocked until the Process# has completed its operations. This could be done using the function sigprocmask() with parameters that specify which action to take (SIG_SETMASK, SIG_BLOCK, and SIG_UNBLOCK), what signals are involved, and the returned current mask of blocked signals. A typical usage is shown as follows:

  • Signal

    Signal is a notification sent asynchronously to a #Process that an event has occurred typically used in #unix like operating system.

  • Program

    Program is an executable file in Unix-based system such as Linux# and #macos, which could be a shell script, shell command, compiled file or object code file. It is created by a link editor and stored on a disk, and can be executed by the exec System Call# which will create a Process#.

  • I/O Multiplexing

    There will be the case where asynchronous I/O is desired in order to serve different I/O events at the same time. For example, a Program# might have to serve two read requests from different Processes. However, since serving one read request will always block another, it is unwise to listen just one of the socket and wait for it since the program can’t know which one will arrive first. It is the same concern for asynchronous Socket Programming where the server might need to serve two sockets at the same time while being ignorant about the order of their arrival. I/O Multiplexing using three Unix System Call# : select(), pselect(), and poll().

  • Difference between thread and processes

    In conjunction, even though Process# could create its own child processes, those processes does not necessarily share the same variable and PID. Furthermore, child processes will not be allocated to its parent’s memory space. They sometime will even occupy a separate system resources for themselves.

#operating-system #c