Back on track, iPod and OpenBSD meeting

After a vacation week on Olhos de Água, Algarve, I’m back on track. The week was completely off computers and work, being the photos download from the digital camera the only allowed task.

Now I’m in Coimbra, after picking up Marc Balmer at the Lisbon airport for the OpenBSD-PT meeting that will start today.

Bought a 1Gb black iPod nano and I’m using [[http://gtkpod.sourceforge.net/|gtkpod]] for managing it under Linux. It works pretty well till now. I also tryed [[http://amarok.kde.org/|amarok]] and its pretty good also.

Humorous comics feeds

Most of my web feeds are on technical issues or from friends that blog about work. That’s ok but sometimes gets really boring. I decided to make reading my feeds more fun and went on search for humorous comics feeds.

Tapestry Comics, a web app built on top of Ruby on Rails, is a directory of web comics with feeds from which I glady pulled out dilbert, Every One Loves Eric Raymond, Sev Wars and Snap-o-Mania for now. They are so many that its difficult to choose :)

I wanted of course a feed for PhD comics and Userfriendly, but couldn’t find any. It’s a pitty, but I understand their bussiness decision, most of these sites earn money from advertisement and page views, a web feed would danger that (or so they seem to think).

If you have more fun stuff let me know. Thanks.

Note: If you don’t know what a ‘feed’ is go to this page and use Bloglines, for example, to track your favorite feeds.

Multiple installations of gcc

Bumped into a problem with multiple gcc installations. I wanted to force the use of gcc-2.95 but I couldn’t use the normal ‘update-alternatives’ script because the package wasn’t being managed by it. I tryed another approach that was to put a copy of the gcc binary I wanted, together with ‘cpp0′ and ‘cc1′, on ~/bin and added it to PATH. I then tryed to compile a minimal C file:


main(){}

But then I was having this error:

ld: crtbegin.o: No such file or directory

GCC searches the PATH for an assembler and a loader, but it only does so after searching a directory list hard-coded in the GCC executables. By issuing ‘gcc –print-search-dirs’ you can check that list. By doing this I found that I needed to include yet another directory and by exporting LIBRARY_PATH to that directory I was able to make it work.

This problem eated most of my day so I hope this saves someone (or me next time) some time.

**Note:** If after this you have problems with the headers you may need to pass to gcc in the compilation command the directory of the headers you want with the option -I.

Linux clone(2) syscall

I’ve been trying out Linux clone(2). What follows is a small example I’ve assembled.

From the man page, “the main use of clone is to implement threads: multiple threads of control in a program that run concurrently in a shared memory space”. Also have in mind clone(2) is Linux-specific.

One example of a real-usage is this short program I wrote for running several prolog engines at the same time.

This is the main file:

cfs.c

#include
#include

#include

my_pred() {
FILE *fd = fopen("./teste", "w");
fwrite("ola", 1, sizeof("ola"), fd);
fclose(fd);
return TRUE;
}

thread() {
int retval;
Start_Prolog(0, 0); /* start prolog engine */
_exit(0);
}

main() {
void **child_stack;
child_stack = (void **) malloc(65536);
clone(thread, child_stack, CLONE_VM|CLONE_FILES, NULL);
}

In the main routine I just create one process with clone(2) but one could create as many as we would like of course. Unlike fork(2), clone allow the child process to share parts of its execution context with the calling process, such as the memory space, the table of file descriptors, and the table of signal handlers. On this example I choosed to share the file descriptor table with the option CLONE_FILES. The CLONE_VM option will make the calling process and the child process run in the same memory space. This is good to make communication happen between the processes we will create and use.

The child process will begin by executing the function called ‘thread’ in this program. The ‘child_stack’ argument specifies the location of the stack used by the child process. Since the child and calling process may share memory, it is not possible for the child process to execute in the same stack as the calling process.

In the Makefile make sure to link with gprolog libraries. This is the Makefile I used:

Makefile

PLL=/usr/lib/gprolog-iso
LIBS=

all: cfs

cfs.o: cfs.c
gplc -c cfs.c

%.o: %.pl
gplc -c $+

cfs: cfs.o prolog.o
gcc -static -ggdb -Wall -o cfs \
$(PLL)/obj_begin.o \
$+ \
-L$(PLL) \
-lbips_fd \
-lengine_fd \
-lbips_pl \
$(PLL)/obj_end.o $(LIBS) -lengine_pl -llinedit -lm

clean:
rm -f *.o *~ cfs

And the prolog source code I tested this example with:

prolog.pl

:- foreign(my_pred, [fct_name(my_pred)]).
:- initialization(my_pred).

The ‘foreign’ call in prolog declares that a C function with that name exists.

This short example shows how to use clone(2) to create a multi-process program with gprolog. You could of course use it for creating processes that would run anykind of program in Linux.