Skip to content

Instantly share code, notes, and snippets.

@tiebingzhang
tiebingzhang / filewatch.c
Created July 14, 2015 18:49
This is the sample program to notify us when the file /tmp/myinput is modified
/*This is the sample program to notify us when the file /tmp/myinput is modified */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/inotify.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
@tiebingzhang
tiebingzhang / dirwatch.c
Created July 14, 2015 18:50
This is an sample program to notify us for the file creation and file deletion takes place in “/tmp” directory
/*This is the sample program to notify us for the file creation and file deletion takes place in “/tmp” directory*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <linux/inotify.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int readoneline(char *fname){
static char line[256];
FILE *fd=fopen(fname,"r");
if (fd==NULL)
return NULL;
if (fgets(line,sizeof(line),fd)==NULL){
fclose(fd);
retrun NULL;
}
line[sizeof(line) - 1] = '\0';
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
@tiebingzhang
tiebingzhang / getaddrinfo.c
Created August 9, 2016 14:49
getaddrinfo test, returns a list of IP addresses for a particular domain name
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUF_SIZE 500
@tiebingzhang
tiebingzhang / detect.c
Last active August 30, 2016 17:26
C code to detect link connected/disconnected using RTNETLINK
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
int main(){
@tiebingzhang
tiebingzhang / fetch_page.py
Created March 6, 2017 23:35 — forked from Smerity/fetch_page.py
An example of fetching a page from Common Crawl using the Common Crawl Index
import gzip
import json
import requests
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
# Let's fetch the Common Crawl FAQ using the CC index
resp = requests.get('http://index.commoncrawl.org/CC-MAIN-2015-27-index?url=http%3A%2F%2Fcommoncrawl.org%2Ffaqs%2F&output=json')
@tiebingzhang
tiebingzhang / maxmem.sh
Last active September 6, 2017 16:29
script to sort which process uses the most memory
#!/bin/sh
>/tmp/maxmem.out
>/tmp/maxmem2.out
for i in /proc/[1-9]* ; do
grep -q VmSize $i/status || continue
awk '/^VmSize:/{printf("%s ",$2);};' $i/status >> /tmp/maxmem.out
awk '/^VmRSS:/{printf("%s ",$2)};' $i/status >> /tmp/maxmem2.out
echo -n " $i " >> /tmp/maxmem.out
echo -n " $i " >> /tmp/maxmem2.out
echo >> /tmp/maxmem.out
@tiebingzhang
tiebingzhang / simple-tcp-server.c
Created June 7, 2019 20:02
A basic tcp server in C
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
@tiebingzhang
tiebingzhang / simple-tcp-client.c
Last active March 6, 2022 10:12
A simple TCP client in C
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define PORT 8080