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 / cpp_producer_consumer.cpp
Last active April 4, 2022 03:21
Implement producer/consumer (multiple producers, multiple consumers) problem with buffer size = 10.
/**
C++ Producer Consumer using C++11 thread facilities
To compile: g++ -std=c++11 <program name> -pthread -lpthread -o pc
*/
#include <iostream>
#include <sstream>
#include <vector>
#include <stack>
#include <thread>
#include <mutex>
@weidagang
weidagang / least_square_approximation.m
Last active January 4, 2022 12:44
MATLAB - least square approximation
% MATLAB code for finding the best fit line using least squares method.
% input in the form of matrix, each row is a (x, y).
input = [...
1, 2;...
2, 4.5;...
3, 5.9;...
4, 8.1;...
5, 9.8;...
6, 12.3];
@weidagang
weidagang / distributed_mvcc_cross_row_transaction.py
Last active December 29, 2020 11:44
Distributed MVCC based cross-row transaction
#!/usr/bin/python3.5
# Author: Dagang Wei (github.com/weidagang)
# Created: 2016-11-19
# Last modified: 2016-11-27
# License: MIT
# Self link: https://gist.github.com/weidagang/1b001d0e55c4eff15ad34cc469fafb84
#
# This code demonstrates the core algorithm for distributed MVCC based cross-row
# transactions. The algorithm is built on top of a distributed key-value database
@weidagang
weidagang / free-monad.js
Last active December 21, 2020 08:55 — forked from DrBoolean/free-er.js
Free Monad in JavaScript
// Forked and modified from https://gist.github.com/DrBoolean/34c1d3651d1338b1b187
const daggy = require('daggy')
const compose = (f, g) => x => f(g(x))
const id = x => x
const kleisli_compose = (f, g) => x => f(x).flatMap(g)
//=============FREE=============
const Free = daggy.taggedSum({Suspend: ['x', 'f'], Pure: ['x']})
@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 --

@weidagang
weidagang / circular_queue.c
Last active April 27, 2019 22:40
Problem: Implement a circular queue of integers of user-specified size using a simple array. Provide routines to initialize(), enqueue() and dequeue() the queue. Make it thread safe. Please implement this with your own code and do not use any class libraries or library calls.
/*
Problem
Implement a circular queue of integers of user-specified size using a simple array.
Provide routines to initialize(), enqueue() and dequeue() the queue. Make it thread safe.
Please implement this with your own code and do not use any class libraries or library calls.
*/
#include <malloc.h>
@weidagang
weidagang / mvcc_read_committed.py
Last active February 13, 2018 14:49
MVCC transaction
# In-memory MVCC transaction of read-committed isolation level.
next_xid = 1
active_xids = set()
records = []
def new_transaction():
global next_xid
print('Txn %d: start' % next_xid)
xid = next_xid
@weidagang
weidagang / go.js
Created October 27, 2017 14:34 — forked from Gozala/go.js
Go routines for JS
// Utility function for detecting generators.
let isGenerator = x => {
return Function.isGenerator &&
Function.isGenerator.call(x)
}
// Data type represents channel into which values
// can be `put`, or `received` from. Channel is
// very much like queue where reads and writes are
// synchronized via continuation passing.
import Data.IORef
main :: IO ()
main = do
putStrLn "Create a mutable state 0."
ref <- newIORef (0 :: Int)
putStrLn "Increase by 1."
modifyIORef ref (+1)
result <- readIORef ref
print result
@weidagang
weidagang / io.hs
Created February 3, 2017 07:26
Haskell IO
readInt :: IO Int
readInt = readLn
main = do
print "Press any key to start:"
line <- getLine
print "Type the first integer x:"
x <- readInt
print "Type the second integer y:"
y <- readInt