Saturday, March 23, 2013

UNIX Files

Regular files contain byte of data, organized in a linear array called a byte stream.  There is no further structure (e.g. record) imposed on the top of the stream.

Special files are kernel onkects that are represented as files.  Linux supports 4 special files - block device files, character device files, named pipes, and UNIX domain sockets.  Special files are a way to let certain abstractions fit into the filesystem.

Device access in UNIX is performed via device files which look like a file residing in filesystem.  Character device is accessexd as a linear queue of bytes.  The device driver places bytes onto the queue for user process to read in same order.  Block devices are accessed as am array of bytes and can be access by random order.

Named pipes (aka FIFO) are IPC mechanism to provide a communication channel over a file descriptor accessed via a special file.

Sockets are an advanced form of IPC that allow communication within a machine or across machines.  UNIX domain sockets ises a special file (called socket file) residing in filesystem.

UNIX uses a unified name space (root = /) while other OS use separate name space (e.g. C:, D:)

Linux Filesystem

Inode (Information Node) is a physical object on disk as well as a conceptual object in kernel.  Directory acts as a mapping of user-friendly name to inode.  A name and an inode pair is called a link.

Kernel resolve a directory path by walking each directory entry (dentry) in the pathname.  Each dentry will map to a inode allowing the kernel to get to the next level.  A cache called dentry cache is used to speed up resolution.

Hard link map multiple filenames to a same inode.  Hard link cannot cross filesystem as there will be a collision of inode.  Symbolic link (symlink) allows this.  Symbolic link is like a regular file that comprises its own inode and data chunk.  Symlink can point to either directory or link.  It can even points to something nonexisting (called broken link). 

Using hard link is entire transparent.  Manipulating symlink requires special system calls.  Symlink is more like a shortcut than a fulesystem internal links.

Saturday, March 9, 2013

Process Group and Session

When a new user logs in, the login process creates a new session containa single process (login shell), which is also the process group leader.  The process leader's PID is used as the process group ID as well as the session ID.

Process group is used to facilitate job control in UNIX.  It simply the task to send singal  to all process in group.  For example, when a user exit a terminal, SIGQUIT is sent to all processes in the foreground group.  SIGHUP is sent when a network disconnect is detected by a terminal.  SIGNIT is sent when a user presses Control-C (interrupt key).

The command "cat file.text | grep error | more" create a process group with 3 processes.

Session arrange a user's activities and associate that user with a controlling terminal which handle the terminal I/O. 

daemon

Daemon is a process running at background without a control terminal.  The name is derived from physicist James Maxwell's demon (a thought experiement for 2nd law of thermodynamics).  Daemons are also superbeing in Greek's mythology.  Daemons are not god (kernel) but perform tasks supplementing gods.

A daemon has 2 requirements: (1) it must run as a child of init, (2) it must not have a control terminal.  In linux, a process performs the following steps to become a daemon:

1. Call fork() to spawn a child process
2. In the parent, call exit().  The parent, which is the process group leader terminates.  The child process is not the process group leader and thus able to perform setsid() to create a session.  This ensure the child process will be associate with a control terminal create any any other porcess in the same process group.
3. Change working directory to root.  This allows administrator to unmount non-root filesystems or otherwise will be held by daemons.
4. Close all file descriptor to release all files.  Open only standard in, out and err.  Redirect them to /dev/null