Skip to content

Instantly share code, notes, and snippets.

@styxyang
styxyang / test_pool.c
Last active December 18, 2015 22:29
It's not a good idea to pass a stack temporary variable(structure) to thread routine: 1. we don't know the execution sequence of created threads 2. the temporary variable(structure) may get cleared before the sub threads access it
/* definition of thread argument passed to pool wrapper*/
typedef struct {
thread_pool *pool;
uint64_t id;
} thread_argument;
//////////////////////////////////////////////////////////////
// thread_pool_wrapper: thread start routine waiting for tasks
// to execute
/////////////////////////////////////////////////////////////
@styxyang
styxyang / randstr.sh
Created July 18, 2013 05:33
I tried my best to use one-line perl script to reach this... But I actually want to achieve the effects in comments, though failed.
perl -e '@r = ("A".."Z", 0..9); foreach (1..shift) { print join "", @r[ map { rand @r } 1..16 ], "\n" }' -- 8
# perl -e 'print join "", ("A".."Z")[ map { rand @("A".."Z") } 1..shift ], "\n"' -- 16
@styxyang
styxyang / proc-port.sh
Last active December 20, 2015 00:49
find out which process has the connection on one port
# `sudo' is needed on Debian
lsof -n -i4TCP:7777

On naming and cache

There are only two hard things in Computer Science: cache invalidation and naming things.

-- Phil Karlton

On reusing code

There are two "rules of three" in [software] reuse:

a. It is three times as difficult to build reusable components as single use components, and
b. a reusable component should be tried out in three different applications before it will be sufficiently general to accept into a reuse library.

@styxyang
styxyang / grade-breakpoint.tcl
Created October 22, 2013 08:49
tcl script to test gdb-like breakpoint function for jos lab
#!/usr/bin/expect -f
set timeout 10; # set timeout to -1 to wait forever
set hda "obj/kern/kernel.img"
# spawn qemu -nographic -hda $hda -serial "file:jos.out" -monitor null -no-reboot
spawn qemu -nographic -hda $hda -monitor null -no-reboot
# interact
expect {
@styxyang
styxyang / numlock_osx.xml
Last active July 26, 2022 03:36
This is a Karabiner configuration to get NumLock work on OS X the same way as PC
<!-- Get NumLock on keypad to work on osx for G80-3494, Realforce and so on -->
<!-- By https://groups.google.com/d/msg/osx-karabiner/-2ns5XVXXdQ/kDGIKVSztjwJ -->
<item>
<name>Map NumLock to OSX NumLock function</name>
<identifier>private.pc_numlock_to_mac_numlock</identifier>
<autogen>__KeyToKey__ KeyCode::KEYPAD_CLEAR, KeyCode::VK_IOHIKEYBOARD_TOGGLE_NUMLOCK</autogen>
</item>
/*
* 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.
*/
#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
package main
import (
"errors"
"log"
"runtime"
"sync/atomic"
"time"
)