Skip to content

Instantly share code, notes, and snippets.

View weidagang's full-sized avatar

Dagang Wei weidagang

  • Bay Area, California
View GitHub Profile
#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());
@weidagang
weidagang / io-redirection.md
Last active July 31, 2024 10:20
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 --

@weidagang
weidagang / bash_aliases.sh
Last active August 29, 2015 14:01
Useful bash aliases
# Bash command aliases and utils
# code format
function astyle-java {
astyle --mode=java --style=linux -s4 $@
}
function astyle-c {
astyle --mode=c --style=linux -s4 $@
}
@weidagang
weidagang / Boolean.scala
Last active August 29, 2015 14:03
Make True and False as subtypes of Boolean in Scala (Note that the "object" keyword is the Scala way to declare a singleton class)
abstract class Boolean {
def == (other: Boolean): Boolean;
def != (other: Boolean): Boolean;
def && (other: Boolean): Boolean;
def || (other: Boolean): Boolean;
def unary_! (): Boolean;
}
object True extends Boolean {
def == (other: Boolean) = other;
@weidagang
weidagang / timespan.pl
Created July 1, 2014 02:46
Perl: timespan between 2 keywords
#!/usr/bin/env perl
use File::Basename
$nargs = @ARGV;
sub usage() {
my $prog = basename($0);
print "Usage: $prog <start-text> <end-text>\n";
}
@weidagang
weidagang / ListLength.scala
Created July 1, 2014 08:18
HackerRank: List Length
package list.length
object Main {
def f(arr: List[Int]): Int = arr match {
case x :: xs => 1 + f(xs)
case _ => 0
}
def main(args: Array[String]) {
val len = f(List(1, 2, 3));
@weidagang
weidagang / ListReplication.scala
Created July 1, 2014 08:19
HackerRank: List Replication
package list.replication
object Main {
def repeat[T](num: Int, e: T): List[T] = if (num <= 0) List[T]() else e :: repeat(num - 1, e);
def f(num: Int, arr: List[Int]): List[Int] = arr.flatMap(e => repeat(num, e));
def main(args: Array[String]) {
val lst1 = f(4, List(1, 2, 3));
lst1.map(e => println(e))
@weidagang
weidagang / FilterArray.scala
Created July 1, 2014 10:52
HackerRank: Filter Array
package filter.array
object Main {
def filter[T](arr: List[T], cond: T => Boolean): List[T] = arr match {
case x :: xs => if (cond(x)) x :: filter(xs, cond) else filter(xs, cond)
case _ => List[T]()
}
def f(delim: Int,arr: List[Int]): List[Int] = filter(arr, (x: Int) => x < delim)
@weidagang
weidagang / SumOdd.scala
Created July 1, 2014 14:30
HackerRank: Sum of Odd Numbers
package sum.odd
object Main {
def f(arr: List[Int]): Int = arr.map(x => (if (0 == x % 2) 0 else x)).reduce((x, y) => x + y)
def main(args: Array[String]) {
val r = f(List(2, 3, 4, 5, -7));
println(r);
}
}
@weidagang
weidagang / StateMonad.cs
Created July 20, 2014 06:29
Label a Binary Tree with State Monad
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// We demonstrate three ways of labeling a binary tree with unique
// integer node numbers: (1) by hand, (2) non-monadically, but
// functionally, by threading an updating counter state variable
// through function arguments, and (3) monadically, by using a
// partially generalized state-monad implementation to handle the