Skip to content

Instantly share code, notes, and snippets.

View khanh101's full-sized avatar
🎯
Focusing

nguyễn ngọc khánh khanh101

🎯
Focusing
View GitHub Profile
@khanh101
khanh101 / catch_segv.cpp
Last active August 11, 2021 15:48 — forked from fairlight1337/catch_segv.cpp
Catching SIGSEGV (Segmentation Faults) in C
// This code installs a custom signal handler for the SIGSEGV signal
// (segmentation fault) and then purposefully creates a segmentation
// fault. The custom handler `handler` is then entered, which now
// increases the instruction pointer by 1, skipping the current byte
// of the faulty instruction. This is done for as long as the faulty
// instruction is still active; in the below case, that's 2 bytes.
// Note: This is for 64 bit systems. If you prefer 32 bit, change
// `REG_RIP` to `REG_EIP`. I didn't bother putting an appropriate
// `#ifdef` here.
@khanh101
khanh101 / spectre.c
Created February 15, 2019 06:08 — forked from ErikAugust/spectre.c
Spectre example code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <intrin.h> /* for rdtscp and clflush */
#pragma optimize("gt",on)
#else
#include <x86intrin.h> /* for rdtscp and clflush */
#endif
const {isMainThread, Worker, workerData, parentPort} = require("worker_threads");
const {safeEval} = require("safe-eval");
if (isMainThread) {
// functionObject must be pure function
module.exports = {executeAsync: function(functionObject = function(...paramsObject) {return undefined;}, ...paramsObject) {
return new Promise(function(resolve, reject) {
const worker = new Worker(__filename, {
workerData: {
functionString: functionObject.toString(),
argumentsObject: paramsObject,
@khanh101
khanh101 / sieve_prime.py
Last active August 29, 2020 03:16
Prime Sieve Algorithm
from typing import Iterator, Any
def natural(start: int = 0) -> Iterator[int]:
n = start
while True:
yield n
n += 1
def sieve_prime() -> Iterator[int]:
def sieve(i: Iterator[int]) -> Iterator[int]:
@khanh101
khanh101 / bayesian_inference.py
Last active September 10, 2020 14:16
Simple Bayesian Inference for Binomial
from typing import Tuple, Iterator, Any
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
# from matplotlib.collections import PathCollection
from matplotlib.figure import Figure
from matplotlib.lines import Line2D
np.seterr(all='raise')
@khanh101
khanh101 / median3x3.cc
Last active September 10, 2020 15:50
median filter
template<typename type>
inline void swap_sort(type& a, type& b) {
if (a > b) {
type temp = a;
a = b;
b = temp;
}
}
#include<array>
// source: http://ndevilla.free.fr/median/median.pdf
@khanh101
khanh101 / fileserver.go
Last active September 10, 2021 11:41
file server
package main
import (
"log"
"net/http"
)
func main() {
fs := http.FileServer(http.Dir("./"))
http.Handle("/", http.StripPrefix("/", fs))
import random
import pygame
from pygame.locals import *
class snake:
def __init__(self, points, velocity):
self.points = points
self.velocity = velocity
self.head = points[-1]
self.tail = points[0]
@khanh101
khanh101 / compressive-sensing.py
Last active November 15, 2020 17:48
compressive sensing sample code
from typing import Callable
import matplotlib.pyplot as plt
import numpy as np
import sparse_uls
np.random.seed(1234)
def forward_transform(N: int, K: int) -> np.ndarray:
@khanh101
khanh101 / fork.c
Created January 8, 2021 13:46 — forked from Cr4sh/fork.c
fork() for Windows
/*
* fork.c
* Experimental fork() on Windows. Requires NT 6 subsystem or
* newer.
*
* Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.