Tuesday, July 21, 2009

C++ Notes

Well, this is my C++ notes collections, might be useful for anyone who want to learning the C++ programming language. Most of these codes have been compiled with GNU C++ Compiler under Linux system, but i think it will also run on different compiler and system.

class-inheritance.cc
type.cc
static-member.cc
overload1.cc
friend-function.cc
friend-class.cc

Tuesday, July 14, 2009

Debian's Repository


I just re-install Debian Lenny into my second development box, have it through USB installtion using first CD image. After installation i need to install additional packages such as development files, yes the compiler, manpages, development libraries etc. I use local repository server http://kambing.ui.ac.id, the apt-get always told me some errors after fetching the repo. So i manually check the repo's server structure and i see this repo is sucks! that's way the aptitude always fail. Now i use VLSM ftp://ftp.vlsm.org repo, editing /etc/apt/sources.list as follow:

...
deb ftp://ftp.vlsm.org/debian/ lenny main contrib
deb ftp://ftp.vlsm.org/debian-security/ lenny/updates main contrib
...


ftp://ftp.vlsm.org/debian/dists/lenny/
+-- contrib
+-- main
+-- non-free


and a debian-security repo

ftp://ftp.vlsm.org/debian-security/dists/lenny/updates/
+-- contrib
+-- main
+-- non-free


aptitude can now fetch the repo's packages correctly.

Monday, July 06, 2009

Linux FIFO



FIFO stands for "First in/First out", a part of interprocess communication mechanism. FIFO actually a named pipe (see manual page about pipe) used for communication between unrelated process. The kernel share it to be a part of file system, so multiple process can use (read/write) it concurrently. When data exchange occurs in a FIFO, the kernel passes all data internally without writing to the file system. Many real-time application use this method because it's unbuffered behavior, drastically reduce read/write process.

To create a FIFO, use mkfifo utility shipped by Linux's coreutils, in C way, we will need use mkfifo system call to create and then use it for read/write as like as working with a regular file.

Example to create FIFO:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int ret = mkfifo("/tmp/myfifo", 0666);
if (ret == -1)
    perror("mkfifo");


If the call success it should create a FIFO named "/tmp/myfifo", then sender or receiver process can open and exchange data via read and write. Normally reading or writing from a FIFO should blocking, but in Linux, opening a FIFO with read and write access always success both in blocking or unblocking mode.

Sender:
int fd;
unsigned char cmd = 0xff;
fd = open("/tmp/myfifo", O_RDWR | O_NONBLOCK, 0666);
if (fd == -1) {
    perror("open");
...
}
ret = write(cmd, &cmd, sizeof(cmd));
if (ret == -1) {
/* When write fails because the receiver has not opening the FIFO for reading, the kernel will send SIGPIPE (broken pipe) signal. */
    perror("write");
...
}


Receiver:
int fd;
unsigned char cmd;
fd = open("/tmp/myfifo", O_RDWR | O_NONBLOCK, 0666);
if (fd == -1) {
    perror("open");
...
}
ret = read(fd, &cmd, sizeof(cmd));
if (ret == -1) {
    perror("read");
...
}


Because this blocking behavior, application programmer should be very careful when write their codes to prevent deadlocks.

Reference:
Linux manual page: mkfifo, FIFO.

Thursday, July 02, 2009

Background image on GtkTextView

I have answer a question in the GTK+ mailing list regarding setting a custom background image on a GtkTextView widget, i already done this in the past, just trying to refresh my memory here a important piece of that code:

...
gtk_widget_show_all (window);
/* Windows are nonexistent before the widget has been realized. */
textv_window = gtk_text_view_get_window (GTK_TEXT_VIEW (textv), GTK_TEXT_WINDOW_TEXT);
pixmap = gdk_pixmap_create_from_xpm ((GdkDrawable *) textv_window, NULL, &color, "back-gimp.xpm");
gdk_window_set_back_pixmap (textv_window, pixmap, FALSE);
...

Crontab

Ok, it's time to refreshing my memory about the Crontab, here is what should remember when adding or editing a crontab.

* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)


Example command are:

0 0 1 * * /usr/bin/rsync -avrt rsync://archive.fedoraproject.org/pub/archive/fedora/linux/updates/8/i386/ --exclude=debug/ /data/repo/fedora/updates/8/i386

This will run rsync for every month: first minute, hour and date in that month.