Skip to content

Instantly share code, notes, and snippets.

View matutter's full-sized avatar

Mat Utter matutter

View GitHub Profile
@matutter
matutter / basic_parent_child_fork.c
Last active August 29, 2015 13:56
a basic example of parent/child that creates zombies and a race condition
#include <stdio.h>
#include <sys/types.h>
void child(int*);
int parent(int);
void main(void)
{
pid_t pid;
int n=0, i=0;
do {
if (pid == 0)
@matutter
matutter / basic_parent_child_wait.c
Last active August 29, 2015 13:56
using wait() to prevent children from zombiefying
/* Using 'wait();' and an '__EXIT_STATUS' makes sure we don't have zombies or a race condition */
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
void child(int*);
int parent(int);
int main(void)
{
pid_t pid=1;
@matutter
matutter / file_change_event.h
Created February 28, 2014 05:25
The basics of detecting a file change event without excess libraries. Very inefficient.
#include <cstring>
#define _missingFile 0x601DE0
using namespace std;
class fileListener
{
private:
struct stat st;
public:
class file_
@matutter
matutter / thread_race.cpp
Created March 12, 2014 02:55
An example demonstrating c++ threads and how mutex's work to ensure critical operation order.
// the compile args -- gcc version 4.6.3 //
// g++ this.cpp -Wall -fexceptions -std=c++0x -g -lpthread //
//////////////////////////////////////////////////////////////
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mx;
@matutter
matutter / file_event_iNotify.h
Created March 12, 2014 20:28
Detecting file modifications the efficient way using the inotify library
#include <sys/inotify.h> //filesystem events
class tinyListener {
public:
string name, list_name;
void addListener(string s) {
list_name = s;
}
bool good() {
return (list_name == name);
}
@matutter
matutter / gist:9956647
Created April 3, 2014 15:32
nested for loops and running average
for ( y = 0; y < rows ; y++)
{
for ( x = 0; x < cols ; x++)
{
number[y][x] = (rand() % 10001 + 5);
if(hi < number[y][x]) hi= number[y][x];
count++;
avg-=avg/(float)count;
avg+=number[y][x]/(float)count;
}
@matutter
matutter / JSON to form creator
Created May 17, 2014 02:32
dynamic JSON to form creation, much to expland on
function generateTemplate(doc) {
var name = doc.name
var page = doc.page
var content = doc.content
var id = doc._id
var form = ''
form += '<generate>'
form += '<div class="page-header"><h1>' + name + '<small>' + page + '</small></h1><button class="btn btn-gen">Save</div>'
form += '<label>Post title</label><input class="form-control" type="text" name="title">'
@matutter
matutter / gist:12f569954e8132c2c4e8
Created May 31, 2014 18:45
Install sublime with PPA
sudo add-apt-repository ppa:webupd8team/sublime-text-3
sudo apt-get update
sudo apt-get install sublime-text-installer
@matutter
matutter / simple_IPC.js
Created June 13, 2014 03:06
IPC in nodeJS
try {
var proc = cp.spawn('execs/a.out',['param1','param2'], null );
proc.stdout.setEncoding('utf-8');
proc.stdin.setEncoding('utf-8');
var writeBuffer = []
, artificial_latency = 50
, writeCRON = setInterval(function(){
if( /*proc.connected &&*/ writeBuffer.length ) {
proc.stdin.write( writeBuffer[0] + '\n')