Skip to content

Instantly share code, notes, and snippets.

View DanGe42's full-sized avatar

Daniel Ge DanGe42

View GitHub Profile
@DanGe42
DanGe42 / naive.js
Last active December 10, 2015 01:19
A naive way of doing something
var toArray = function(orig) {
var arr = [];
for (var i = 0; i < orig.length; i++) {
arr.push(orig[i]);
}
return arr;
}
var find_child_by_class = function(root, klass) {
var queue = toArray(root.children);
@DanGe42
DanGe42 / server.sh
Last active December 16, 2015 07:39
Python "one-liner" to start a server to serve static files from a directory
# Start an HTTP server from a directory, optionally specifying the port
function server() {
# Default to port 8000
local port="${1:-8000}"
# Since the one-liner blocks, we open the browser beforehand. However, we want to
# wait just a couple of seconds since the server will not be ready just yet.
# Also, I think Linux users should be able to use 'xdg-open' ('open' is for OS X).
( sleep 2; open "http://localhost:${port}/" ) &
@DanGe42
DanGe42 / batman
Last active December 22, 2015 08:39
Script that prints something related to Batman. (Not originally a script that I wrote; I just rewrote it from memory)
#!/bin/bash
trap "echo ' Batman!'; exit" SIGINT SIGQUIT
while (true)
do
echo -n "Na"
sleep 0.1
done
@DanGe42
DanGe42 / context.diff
Created October 23, 2013 23:56
The "hello world" user context example I demonstrated in class on Tuesday. The diff file attached shows one way you could modify it so that the program context switches back to `main` after `f` finishes.
--- context_hello_world.c 2013-10-23 19:47:07.000000000 -0400
+++ context_hello_world1.c 2013-10-23 19:47:13.000000000 -0400
@@ -7,8 +7,11 @@
#define STACKSIZE 4096
+ucontext_t main_context;
+
void f(){
printf("Hello World\n");
#include <stdlib.h>
#include <stdio.h>
int poly1(int x) {
return x*x - 3*x + 4;
}
int poly2(int x) {
return x*(x - 3) + 4;
@DanGe42
DanGe42 / poly_O2.s
Last active December 26, 2015 11:39
.section __TEXT,__text,regular,pure_instructions
.section __TEXT,__literal8,8byte_literals
.align 3
LCPI0_0:
.quad -4609434218613702656 ## double -3
LCPI0_1:
.quad 4616189618054758400 ## double 4
.section __TEXT,__text,regular,pure_instructions
.globl _poly1a
.align 4, 0x90
#include <ucontext.h>
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define STACKSIZE 4096
void f(){
@DanGe42
DanGe42 / context_demo.c
Created October 25, 2013 03:14
A non-trivial example of how to use user contexts for trivial scheduling.
#include <ucontext.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <poll.h>
/* ucontext sample program
@DanGe42
DanGe42 / broken_context.c
Created October 25, 2013 03:16
Why is this one broken? Answer (use a base64 decoder): Zm9vX2NvbnRleHQgbmVlZHMgdG8gYmUgcmVpbml0aWFsaXplZCB3aXRoIG1ha2Vjb250ZXh0IG9uY2UgZm9vIHJldHVybnMgYW5kIGNhdXNlcyB0aGUgY29udGV4dCBzd2l0Y2ggdG8gdWNfbGluaw==
#include <stdlib.h>
#include <stdio.h>
#include <ucontext.h>
#define STACKSIZE 4096
ucontext_t foo_context, main_context;
void foo() {
printf("Executing foo!\n");
@DanGe42
DanGe42 / SyncTest.java
Created February 6, 2014 06:51
A simple program showing Java synchronization behavior.
public class SyncTest {
public void test1(int x) throws InterruptedException {
synchronized (this) {
System.out.println("Entered guarded block");
if (x != 42) {
wait();
}
System.out.println("Done!");
}
}