Saturday, October 25, 2008
What is asynchronous logic?
However, to build a state machine (e.g. computer), the change of state must be synchronized with some master signal (clock).
Color
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 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?
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.