Saturday, October 25, 2008

What is asynchronous logic?

Asynchronous logic (circuit) means the output value depends on the input logic and the combinatorial logic that links them. There is no delay in the process except the propagation delay through the logic gates.

However, to build a state machine (e.g. computer), the change of state must be synchronized with some master signal (clock).

Color

Hue is the name of the color. While there are many color, there is far less hue. Pink and crimson are color but the base hue of these color is red.

Saturation (Chroma) is the strength or purity of the color. It is simply the amount of white in the color. Brightness (Value) is the lightness or darkness of the color. It is determined by the amount of black in the color.

Combination of hue, saturation and brightness create infinite number of color that we see in the world.

Sunday, October 5, 2008

What is a pipe?

A pipe is a communication buffer defined as 2 file descriptor. One descriptor allows write to the pipe and the other allows one to read from the pipe. A pipe has no external name and so the only way to access it is via the file descriptor. Pipe is also temporary such that they will be deallocated when no process has them open.

A pipe is typically used by a parent process to communicate with its child. It is achieve by creating the pipe before the fork call. The file dscriptors of the pipe will be then be inherited by the child process. Read/write to the pipe is not atomic. In other words, the read could return partial data if the read and write call is in a race condition.

The pipe "|" used in shell (e.g. ls -al | sort -n 4) can be implemented using a combination of pipe and I/O redirection. The pipe-read file descriptor replaces the stdin for one process, and the pipe-write file descriptor replaces the stdout for the second process.

Saturday, October 4, 2008

How is I/O redirection realized?

The command "cat > test.txt" will echo terminal input into a text file instead of stdout.

I/O redirection is implemented using the dup2 function.  dup2 copies one file descriptor to another in the file descriptor table.  To realize redirection in the cat command above, first open test.txt which result in the creation of a new file descriptor in the table.  Then use dup2 to copy this new entry to file descriptor 2 (stdout).  After the call, stdout is now pointing to test.txt.  When cat writes to stdout, the data will be written to test.txt.

What is a file pointer?

The ISO C standard I/O functions use file pointer instead of file descriptor.

File pointer is the address of a FILE structure which in turn contains the file descriptor number. The FILE structure also contains the buffer used in buffered I/O.  Data will be written out from the buffer to the destination devices when the buffer is filled up (for disk I/O) or when the new line character is encountered (for terminal I/O).  The I/O subsystem performs the write using file descriptor.

The fflush call force-writes the buffer out immediately.

What is a file descriptor?

A file descriptor is an integer corresponding to the index into the file descriptor table.  The file descriptor table exists in user address space.  Each process has its own copy of the file descriptor table. Direct access to the table is not possible except via functions that uses file descriptor.

File descriptor table entry points to am entry in the system file table.  The system file table exists in the kernal space and the same table is shared by all processes.  Each entry contain the current offset, access mode and number of file descriptor pointing to it.  Each system table entry points to an entry in the in-memory inode table which represents the physical file.  Similarly, more than one entry of the system table may point to the same in-memory inode table entry.

Friday, October 3, 2008

What is the benefit of representing device as file?

Devices are represented as special files in UNIX under /dev.  Representing devices as files simplifies the programming model such that all devices can be controlled consistently by 5 system calls - open, close, read, write and ioctl.

Block devices are those that have characteristics similar to disk.  They typically allow random access.  Data are access in unit of blocks.

On the other hands, charcter devices have characteristics similar to terminal.  Access is sequential and data is represented as a stream of bytes.

Difference between stdout and stderr

stdout and stderr is defaulted to the console.  It means both error messages and normal output will be shown on the same device.  Why do we differentiate stdout and stderr in the first place?

Output to stdout is buffered.  In other words, output may take some time before it will appear in the destination device (console typically).  In contrast, stderr is unbuffered.  It means output will appear in the destination device immediately after the write call.  That's why error message should be written to stderr instead of stdout.

Why are there zombie process?

The fork call will create a child process which is independent from its parent. UNIX allows the parent process to synchronize its process with the child process via the wait call. As the system cannot predict if the parent process will execute wait call for the child or not, UNIX will not release all the resources when a process terminates in case its parent executes wait later on.  The termination status (normal or abnormal) of the child process and its resource statistics will be kept for the wait call.

Terminated process which has not been waited for becomes a zombie process. If the parent terminates without executing the wait call for its child, the child zombie process becomes an orphan process. Orphan process will be adopted by the init process (PID=1). init will call wait periodically and this is the mechanism UNIX uses to clean up zombie processes.