Jun
11
2009
0

system in C++ like perl’s system

Small tricks, haven’t tested these yet, perhaps a bit of more error handling in fork or exec. I don’t like system (3) shell interpolation. In this case one can do system_exec(”ls”,”/tmp”,NULL) or special chars in args that won’t get interpolated by the shell.


    int system_exec(char* cmd, ...)
    {
        va_list ap;
        vector arglist;
        arglist.push_back(cmd);
        char* cur;
        va_start(ap,cmd);
        while( (cur=va_arg(ap,char*)) != NULL)
            arglist.push_back(strdup(cur));
        va_end(ap);
        arglist.push_back(NULL);
        int res;
        pid_t chld;
        if( (chld = fork()) ) {
            for(vector::iterator i = arglist.begin()+1; i != arglist.end(); ++i)
                free(*i);
            wait(&res);
        } else {
            execvp(cmd, &arglist[0]);
        }
        return res;
    }
#define W 1
#define R 0

    int stdout2stream_system_exec(std::ostream& os, char* cmd, …)
    {
        va_list ap;
        vector arglist;
        arglist.push_back(cmd);
        char* cur;
        va_start(ap,cmd);
        while( (cur=va_arg(ap,char*)) != NULL)
            arglist.push_back(strdup(cur));
        va_end(ap);
        arglist.push_back(NULL);

        int fd[2];
        pipe(fd);

        int res;
        pid_t chld;
        if( (chld = fork()) ) {
            for(vector::iterator i = arglist.begin()+1; i != arglist.end(); ++i)
                free(*i);
            close(fd[W]);
            char buf[1024];
            ssize_t nread;
            while( (nread=read(fd[R],buf,1024)) > 0 && os.good() ) {
                os.write(buf,nread);

            }
            // TODO errors
            close(fd[R]);
            wait(&res);
        } else {
            close(fd[R]);
            // no need according to manpage
            //close(STDOUT_FILENO);
            if( dup2(fd[W], STDOUT_FILENO) != STDOUT_FILENO) {
                perror(”dup2″);
                exit(EXIT_FAILURE);
            }
            close(fd[W]);
            execvp(cmd, &arglist[0]);
                perror(”execvp”);
        }
        return res;
    }


Written by piotr in: computers | Tags: , ,
Jun
03
2009
0
May
05
2009
0

Amazing Bach


Written by piotr in: art, computers, entertainment |
Apr
02
2009
0

1000% performance increase in flex parser

Or how an unexpected bug improved performance a lot.

I have a flex C++ parser that I was feeding an igzstream.
At some point I refactored some code and added io exceptions like:

is.exceptions(ios::eofbit | ios::failbit | ios::badbit);

Which caused the yylex() member function throwing a ios::clear exception at EOF, well, the parser checks the bits of the stream by itself, setting exceptions is not good with a flex parser. Looking at the generated parser and with the nice advice from DrNick of setting a breakpoint on __cxa_throw to backtrace the exception point, I saw accidentally that YY_INTERACTIVE was defined on my parser, which can be disabled by setting:

%option batch

in the .ll file. This caused a huge unexpected performance boost that is going to be appreciated given the cpu intensive task that parsing is.

The numbers are: from an average parsing of 2882ms to 290 ms

Written by piotr in: Uncategorized |
Mar
12
2009
0

El baño

Doblo mi cuerpo y me hago un ovillo
el líquido me rodea pero no soy el mismo
mi ser en el tiempo se disuelve
como el agua al polvo cuando llueve
a mi mismo me digo… ¡He venido!, !He venido!
mas ya nada me conmueve.

Written by piotr in: art |
Mar
12
2009
0

Two different ways to visualize hard disk usage

My two favorite programs to manage hard disk usage are based in two very different visualization techniques. One is gdmap which uses a treemap and the other is filelight which uses concentric circles which work like multiple piecharts.

Written by piotr in: computers | Tags: ,
Jan
26
2009
0

Tightening yokes

Looks like this year is the year of the Ox in the chinese calendar. Last year was the year of the rat which strikes me as a very descriptive allegory of what happened in 2008. Like how the rat crossed the river above the Ox, the financos and the brickers got a free ride on the back of the hardworking people. Now, what to expect from the year of the Ox? Probably a lot of sweat that I hope serves to fulfill projects and illusions. Investing effort, money and time on ideas that make a difference might give its fruits.

Written by piotr in: chinese, entertainment, social | Tags: , ,
Jan
12
2009
0

Some random music!

Just about to buy some groceries and putting some fresh music in my small mp3 player. I made a small perl script to get random mp3 from a list of directories, more fun to hear surprising stuff!  It would have been a little long for an one-liner. One cool thing to add would be random to be biased by votes on music wanted to be hear more often. Probably theres a very complete GTK/KDE app to do this crap in a much better and user friendly way, but I couldn’t care less. I love small scripts.

Written by piotr in: computers | Tags: ,
Nov
21
2008
0

What happened to the value of my assets?

If you hold stocks, real state, a vehicle, oil wells or almost any asset, you probably have noticed how it has decreased in value. I keep an eye on the price of commodities, and although I was fully aware of the price drop of oil, reality struck me bluntly when I was refilling my small 50cc motorbike gas tank with the usual 5€, only to see a lot of gasoline spilled on my motorbike seat and to be reminded that volume is measured in liters, not in euros. I think sometimes we fail to act on things even when we know about them.

I think I can give an hypothesis of what have happened with the money supply and how economic regulation not only doesn’t prevent bubbles and bursts from occurring but it most likely reinforces them.

(more…)

Written by piotr in: Uncategorized, investment & finance | Tags: ,
Nov
10
2008
0

India’s NPTEL lectures online

India follows MIT opencourseware, with those great lectures on a wide range of topics. I’m viewiing the ones by professor Dasgupta on Artificial Intelligence.

Written by piotr in: computers, research | Tags: , ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes