Skip to content

Instantly share code, notes, and snippets.

View weidagang's full-sized avatar

Dagang Wei weidagang

  • Bay Area, California
View GitHub Profile
@weidagang
weidagang / io-redirection.md
Last active May 29, 2019 10:46
Linux I/O重定向的原理和实现

在Unix系统中,每个进程都有STDIN、STDOUT和STDERR这3种标准I/O,它们是程序最通用的输入输出方式。几乎所有语言都有相应的标准I/O函数,比如,C语言可以通过scanf从终端输入字符,通过printf向终端输出字符。熟悉Shell的朋友都知道,我们可以方便地对Shell命令进行I/O重定向,比如 find -name "*.java" >testfile.txt 把当前目录下的Java文件列表重定向到testfile.txt。多数情况下,我们只需要了解I/O重定向的使用就够了,但是如果要编程实现类似Shell的I/O重定向以及管道功能,那么就需要清楚它的原理和实现。

下面本文就以Linux系统为具体例子,介绍I/O重定向的原理和实现(文中实验环境为Ubuntu 12.04,Bash 4.2.25,内核版本3.2.0-59)。

文件描述符表

理解I/O重定向的原理需要从Linux内核为进程所维护的关键数据结构入手。对Linux进程来讲,每个打开的文件都是通过文件描述符(File Descriptor)来标识的,内核为每个进程维护了一个文件描述符表,这个表以FD为索引,再进一步指向文件的详细信息。在进程创建时,内核为进程默认创建了0、1、2三个特殊的FD,这就是STDIN、STDOUT和STDERR,如下图所示意:

-- fdt --

#include <cstdio>
#include <vector>
#include <algorithm>
class Solution {
public:
int threeSumClosest(std::vector<int> &num, int target) {
int closest = num[0] + num[1] + num[2];
std::sort(num.begin(), num.end());
/*
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
@weidagang
weidagang / kth-smallest-in-binary-search-tree.cpp
Last active August 29, 2015 13:56
Kth Smallest Number in Binary Search Tree
/*
Find the kth smallest number in the binary search tree.
*/
#include <cstdio>
#include <stack>
#include <queue>
struct TreeNode {
int val;
/*
http://oj.leetcode.com/problems/interleaving-string/
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.
For example,
Given:
s1 = "aabcc",
s2 = "dbbca",
/*
http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7},
return its bottom-up level order traversal as:
/*
http://oj.leetcode.com/problems/trapping-rain-water/
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
*/
@weidagang
weidagang / LCS.java
Last active January 1, 2016 18:09
Longest Common Substring
public class LCS {
public static void main(String... args) throws Exception {
System.out.println(lcs("Milexxs", "xxxxxxxM"));
}
public static String lcs(String a, String b) {
if (null == a || null == b) return "";
String longest = "";
for (int i = 0; i < a.length(); ++i) {
push = (element) -> (stack) ->
newStack = [element].concat stack
{value: element, stack: newStack}
pop = (stack) ->
element = stack[0]
newStack = stack.slice 1
{value: element, stack: newStack}
bind = (stackOperation, continuation) -> (stack) ->
/*
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
*/