#include < stdio.h >
#include < sys/time.h >
#include < signal.h >
#include < setjmp.h >
typedef void (*sighandler_t) (int);
sigjmp_buf saved_state;
struct data
{
unsigned int count;
unsigned int epoch;
double watt;
} d;
/*
* This value (in seconds) defines how often an
* attempt is made to read the serial port.
*/
unsigned int sleep_time = 10;
unsigned int sleep_start = 0; // in epoch seconds
/*
* Function to write into SQLite/RRD database.
*/
int write_db()
{
printf("[*] write_db()\n");
return 0;
}
/*
* Function to serve on-demand query.
*/
void on_demand()
{
printf("[*] on_demand() :: ");
printf("count %u now %u watt %.2f\n", d.count, d.epoch, d.watt);
fflush(stdout);
siglongjmp(saved_state, 1);
}
/*
* Function to read from serial port.
*/
int blocking_read()
{
struct timeval timeout;
printf("[*] blocking_read() :: ");
/* simulate the read() blocking-time */
timeout.tv_sec = 0;
timeout.tv_usec = 500;
select(0, NULL, NULL, NULL, &timeout);
d.count = d.count + 1;
d.epoch = time(NULL);
d.watt = random();
printf("count %u now %u watt %.2f\n", d.count, d.epoch, d.watt);
fflush(stdout);
return 0;
}
int main()
{
printf("[*] main() -- pid: %u\n\n", getpid());
fflush(stdout);
signal(SIGUSR1, (sighandler_t) on_demand);
while (1)
{
/*
* If we have just came out from a
* signal, perform the pending delay.
*/
sigsetjmp(saved_state, 1);
if ((int)(sleep_start + sleep_time - time(NULL)) > 0) {
printf("[-] pending delay %u secs\n",
(int)(sleep_start + sleep_time - time(NULL)));
fflush(stdout);
sleep((sleep_start + sleep_time) - time(NULL));
}
blocking_read();
write_db();
printf("\n");
fflush(stdout);
/*
* collect serial port data at
* each 'sleep_time' seconds.
*/
sleep_start = time(NULL);
sleep(sleep_time);
}
return 0;
}
skip to main |
skip to sidebar
Thursday, January 27, 2011
No need to multi-thread if one dones't have to ...
I like spend time writing, re-writing, reversing and hacking into codes. I consider myself a learner. Regarding multi-threaded codes, I keep a simple belief that almost all of them could be written using a single thread doing effectively the same thing. I realize the world isn't going to change because of what I believe. I wrote the following little piece to demonstrate a friend of mine that a multi-threaded version of one of his little projects which has a functions that blocks and responsible for a few other things (eg. write_db(), on_demand() ) could also have been written using a single-thread. For whatever it is worth, here is my little demo code:
Friday, January 14, 2011
Keyboard num-keys not working in VirtualBox Ubuntu guest
The solution that works for me is by disabling 'Mouse Keys'.
- Go to System > Preferences > Keyboard.
- In Mouse Keys tab, un-tick 'Pointer can be controlled using keypad'
Labels
- amarok (2)
- android (4)
- antitaint (1)
- apache (1)
- apple trailers (1)
- apple tv (5)
- ati (2)
- avimerge (1)
- awk (8)
- bash (29)
- batch (1)
- bios (1)
- bittorrent (2)
- blah (1)
- bug (2)
- c (3)
- c++ (1)
- captcha (3)
- chromium (1)
- cityrail (3)
- clipboard (1)
- compiz (3)
- console (1)
- crystal hd (1)
- dccp (2)
- debian (18)
- dhcp (1)
- dictionary (2)
- dns (2)
- dokuwiki (1)
- dpkg (4)
- elitebook (1)
- epoch-time (1)
- epstopdf (1)
- evince (1)
- extract (1)
- fat32 (1)
- ffmpeg (2)
- fileserve (1)
- firefox (9)
- flurry (1)
- font (2)
- foxtel (1)
- funny (1)
- gdb (2)
- gmail (1)
- gnome (1)
- gnuplot (4)
- google-chrome (1)
- greasemonkey (1)
- grep (1)
- gtk (1)
- h264 (2)
- highlight (2)
- hotfile (1)
- hotkey (1)
- hotmail (1)
- hotspotter (1)
- hp (1)
- image-manipulation (3)
- initrd (1)
- iso (1)
- java (3)
- jelly bean (1)
- kernel (1)
- keyboard (1)
- klog (1)
- konqueror (1)
- ktorrent (1)
- laptop (1)
- latex (7)
- lcd (1)
- lightdm (1)
- linux (39)
- lubuntu (3)
- lxde (1)
- lyrics (4)
- lyx (2)
- mac (1)
- mangafox (1)
- miktex (2)
- mkv (2)
- monitor (1)
- mplayer (3)
- netgear (1)
- ns2 (2)
- onemanga (1)
- open-office (1)
- partition (1)
- pastebin (1)
- pdroid (1)
- pennytel (2)
- perl (1)
- php (1)
- pop (1)
- popup (1)
- premiumforfree (1)
- printk (2)
- programming (3)
- prothom-alo (2)
- python (3)
- rapidshare (1)
- real-audio (1)
- reverse-engineering (3)
- screensaver (1)
- sed (6)
- skype (2)
- sms (1)
- ssh (1)
- strace (1)
- swap (1)
- synergy (1)
- taint (1)
- taintdroid (1)
- tcl (1)
- tcpdump (1)
- teleport (1)
- theme (1)
- tshark (3)
- tvshark (1)
- ubuntu (35)
- uuid (1)
- vim (5)
- virtual-box (2)
- vnc (1)
- webmail (2)
- windows (7)
- windows8 (2)
- wine (4)
- wireshark (1)
- word-list (1)
- x11 (3)
- yahoo (1)