Skip to content

Instantly share code, notes, and snippets.

@zhaoyao
zhaoyao / VariableByteCode.java
Created September 24, 2011 17:35
Variable Byte Code Algorithm
import java.nio.ByteBuffer;
import java.util.*;
import static java.lang.Math.log;
/**
* User: zhaoyao
* Date: 11-9-24
* Time: PM10:48
*/
@zhaoyao
zhaoyao / install-gmond.sh
Created January 18, 2012 04:51
gmond install script
########################################## expat #############################################
cd /usr/local/src
wget http://downloads.sourceforge.net/project/expat/expat/2.0.1/expat-2.0.1.tar.gz?use_mirror=cdnetworks-kr-2
tar zxvf expat-2.0.1.tar.gz·
cd expat-2.0.1
./configure --prefix=/usr/local/expat
make && make install
mkdir /usr/local/expat/lib64
cp -a /usr/local/expat/lib/* /usr/local/expat/lib64/
@zhaoyao
zhaoyao / tail.c
Created April 9, 2012 05:35
tail.c
/* tail -- output the last part of file(s)
Copyright (C) 1989-2012 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@zhaoyao
zhaoyao / double_fork_trick.md
Last active April 2, 2021 16:18
double fork trick

避免僵尸进程出现.

如果fork后不进行waitwaitpid,那么子进程将会成会僵尸进程。double fork后, second child成为孤儿进程,由init负责后续的清理工作。

#include <stdio.h>
#include <unistd.h> 
#include <stdlib.h>
#include <sys/wait.h>
@zhaoyao
zhaoyao / gist:3965128
Created October 27, 2012 15:47
Endiness test
int
is_big_endian()
{
int test = 1;
unsigned char *testp = (unsigned char*) &test;
// test if the high bits is on the left
return testp[0] == 0;
}
@zhaoyao
zhaoyao / gist:3965951
Created October 27, 2012 20:05
lua string
lua_pushstring与lua_pushlstring
push的string会被memcpy到lua malloc出来的TString中去,会产生一次memcpy
@zhaoyao
zhaoyao / Typoglycemia.java
Created November 5, 2012 17:24
Typoglycemia
import java.util.*;
/**
* User: zhaoyao
* Date: 11/5/12
* Time: 11:56 PM
*/
public class Typoglycemia {
private static final Random rnd = new Random();
@zhaoyao
zhaoyao / Huffman.java
Last active December 10, 2015 17:18
Huffman Encoding
package snip;
import java.io.EOFException;
import java.util.Arrays;
import java.util.PriorityQueue;
/**
* User: zhaoyao
* Date: 1/5/13
* Time: 10:53 PM
<?xml version="1.0" encoding="UTF-8"?>
<scheme name="SonOfObsidian" version="1" parent_scheme="Default">
<option name="LINE_SPACING" value="1.0" />
<option name="EDITOR_FONT_SIZE" value="16" />
<option name="CONSOLE_FONT_NAME" value="Monospaced" />
<option name="CONSOLE_FONT_SIZE" value="12" />
<option name="EDITOR_FONT_NAME" value="Consolas" />
<colors>
<option name="CARET_COLOR" value="ffffff" />
<option name="CARET_ROW_COLOR" value="333333" />
@zhaoyao
zhaoyao / sp.sh
Last active December 30, 2015 09:29
integrate sshpass and keychain
U=$(echo "$1" | cut -d@ -f1 | cut -d: -f1)
HOSTNAME=$(echo "$1" | cut -d@ -f2 | cut -d: -f1)
PORT=$(echo "$1" | cut -d: -f2)
if [[ $U = $1 ]]; then #no user specified
U=$(USER)
fi
if [[ $PORT = $1 ]]; then #no port specified
PORT="22"
fi