Since part of my Msc work in being written in C I went digging for Test Driven Development in C. The guys at Object Mentor published an article on this very same topic in Octorer 2002 C/C++ Users Journal. They’ve arranjed a neat scheme for emulating a full testing framework in C.
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.
5 minutes
What if you had only 5 minutes to post a blog entry? What would you write? How many errors would you make? What would you talk about? Life? Tech? Threads? :-)
Portugal won against England: Incredible shit by Ricardo on defending those penalties and by Ricardo Carvalho on defense.
I wanted to write something about the reasons on why buddishm continues to attract so many people but the time is out.. maybe next time.
OpenBSD .PT Meeting
Next July 22 and 23 OpenBSD .PT will be holding its 6th meeting in Coimbra. It’s fantastic how I’ve started this group and it has envolved into something much bigger than I and my ambition to be a
hacker.
Last meeting we had developer Pedro Martelletto joining us and this time its Marc Balmer (covered in Kerneltrap) that will be comming all the way from Basel, Switzerland. I first meet Marc in WTH and if you know him you certainly agree he is a great guy. There will be technical presentations and general slacking. Everyone is welcome to come.
http://www.openbsd-pt.com/eventos/coimbra06/
Grandmother
It’s been a while since I’ve write anything here. A lot has happen since my last post, most significantly my grandmother passed away.
About a year ago, she and my grandfather were preparing to leave to Lisbon for a few weeks. As they were leaving the house, she suddenly lost strength in her legs and fell to the ground. She never walked again since.
She had a treatment for a cancer twenty years ago but it got worse and caused pressure on the medula that didn’t allow the commands from the brain to go through anymore. She did a whole year and a few weeks without leaving the bed, depending on the good faith and love of family, friends and doctors, prior to succumbing to the illness. Her last hours were of agony and pain.
But life isn’t about your last year is it? Everyone who have seen my grandparents together knows they were pretty close. I’m know they lived great moments of happiness and had a wonderful life trip together.
Inevitably, death brings back that life is temporary. Something we know but tend to ignore most of the time. As for me, I tryed as much as I could to be like a lotus flower – in the water but yet untouched by it. Right or wrong, the choice was mine and I take full responsability for it.