Configuring Ubuntu locale – This time THE right way
I am using Linux for some time now and after each reinstall I configure locale different way. Some of the methods I use works well, some does not. Maybe it is because I am too lazy to configure locale on servers I install and too lazy to install different version of Ubuntu on my laptop every time Canonical releases one, so I never remember the way that worked. Also In my course of Linux use I realized early that THE ONLY RIGHT WAY TO PARTITION HDD IS WITH /HOME FOLDER MOUNTED TO SEPARATE PARTITION, so after reinstall my all documents remain where they should be and in the same directory structure.
So this time after upgrading from 9.04 to 10.04 LTS I have decided to find a way how to configure locale so the configuration stay in my ~/ and is shell independent (so bash_profile is not an option!)!
- Add desired locale to list of supported profiles in
/var/lib/locales/supported.d/local
file. Here we use ALLCAPS UTF-8 (lt_LT.UTF-8 UTF-8)
- Generate required locales
sudo dpkg-reconfigure locales
- Edit file
~/.pam_environment
and add list of environment variables you want to set. My list look like this:
EDITOR=/usr/bin/vim LC_NUMERIC=lt_LT.utf8 LC_TIME=lt_LT.utf8 LC_COLATE=lt_LT.utf8 LC_MONETARY=lt_LT.utf8 LC_PAPER=lt_LT.utf8
Note the here utf8 is not capitalized and without a dash between "utf" and "8".
- Reboot.
- Run
printenv
to make sure that changes took effect
When configuring locale this way, your configuration become shell independent and will remain there every time you reinstall distribution while keeping your home folder unformatted. More about environment variables can be found here.
Random Pic #3 – MIG-21 in a backyard
What a nice thing to have in your backyard! Click on picture for a full sized version.
Also - stay tuned - more frequent blogging to resume next month!
Configuring Eclipse IDE and lightty for FastCGI C development
You might want to skip my thoughts and go to configuration steps...
During a discussion with a programmer in my workplace I came to a conclusion that I don't know anything about FastCGI. Prehistory was that we were discussing ways how to make Internet application scale to N servers. Let's say we know(...hope...) that our application will be interesting to general public, so we expect high load on server. On the other hand, our application might not be so popular, so we will waste our money if we invest to many servers at time=0. So what we need is a solution, how to scale out easily and efficiently. One way to do this is FastCGI. We can start with Web server, Apps server and DB server in one physical machine (or even in one virtual machine) and later on scale out depending on our workload. To try this out I decided to learn a little bit about FastCGI. For underlying programming language I chose C because I know how to program it and it should be as fast as possible without dipping into assembler world.
Configuration - Eclipse IDE.
- I assume that you have installed and configured C/C++ perspective in Eclipse.
- Get the latest FastCGI release. At the time of writing it was "2.4.1-SNAP-0910052249" and was available for download from here
- Unpack FastCGI and do the
- ./configure
- make
- make install
- Check that installation went well. /usr/local/include should have FastCGI headers.
- Add libfcgi to linker libraries list in Eclipse project properties.
- In case your web server does not have libfcgi installed, be sure to click "No shared libraries" checkbox in GCC C Linker General options screen
- Compile the program from the example at the end of this post.
- If everything goes well - copy this program to your development server
Configuration - lighttpd server
- Open lighttpd.conf file in any text editor as long as it is vi(m)
- Make sure that "mod_fastcgi" is in server.modules list.
- Configure the fastcgi.server section. Example configuration:
fastcgi.server =
(
".php" =>
( "localhost" =>
(
"socket" => "/var/run/lighttpd/php-fastcgi.socket",
"bin-path" => "/usr/bin/php-cgi"
)
)
,
"/texas-cgi" =>
( "texas-cgi.handler" =>
(
"socket" => "/tmp/texas-cgi.socket",
"bin-path" => "/srv/www/htdocs/cgi-bin/texas-cgi",
"check-local" => "disable",
"min-proc" => 1,
"max-proc" => 1,
"idle-timeout" => 100
)
)
)Edit this part to suit your needs. Having socket in /tmp folder might not be the best idea. Be sure to change it...
- Restart the lightty /etc/init.d/lighttpd restart
- Check the lightty error log if something goes wrong
- This is how processes tree looks like in CentOS server with lightty and php
- Access the cgi page. My anonymised output can be seen in screenshot
That's it folks. Get into programming C and running your programs as web pages! And stay tuned for updates!
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <syslog.h> #include <alloca.h> #include <sys/types.h> #include <unistd.h> #include <fcgiapp.h> #define LISTENSOCK_FILENO 0 #define LISTENSOCK_FLAGS 0 int times_run; int main(int argc, char** argv) { openlog ("testfastcgi", LOG_CONS|LOG_NDELAY, LOG_USER); int err = FCGX_Init (); /* call before Accept in multithreaded apps */ if (err) { syslog (LOG_INFO, "FCGX_Init failed: %d", err); return 1; } times_run=0; FCGX_Request cgi; err = FCGX_InitRequest(&cgi, LISTENSOCK_FILENO, LISTENSOCK_FLAGS); if (err) { syslog (LOG_INFO, "FCGX_InitRequest failed: %d", err); return 2; } while (1) { err = FCGX_Accept_r(&cgi); if (err) { syslog (LOG_INFO, "FCGX_Accept_r stopped: %d", err); break; } char** envp; int size = 200; for (envp = cgi.envp; *envp; ++envp) size += strlen(*envp) + 11; char* result = (char*) alloca(size); strcpy (result, "Status: 200 OK\r\nContent-Type: text/html\r\n\r\n"); strcat (result, "<html><head><title>testcgi</title></head><body><ul>\r\n"); for (envp = cgi.envp; *envp; ++envp) { strcat(result, "<li>"); strcat(result, *envp); strcat(result, "</li>\r\n"); } strcat(result, "</ul>\r\n"); int pid; char str[10]; pid = getpid(); strcat(result,"<p><b>My PID is: "); sprintf(str, "%d", pid); strcat(result, str); strcat(result, " And I've been run "); sprintf(str, "%d", times_run); times_run++; strcat(result, str); strcat(result, "times</b></p>\r\n"); strcat(result, "</body></html>\r\n"); FCGX_PutStr(result, strlen(result), cgi.out); FCGX_Finish_r(&cgi); } FCGX_ShutdownPending(); return 0; }
(c) Whoever owns it. I made this program by combining several examples from different web sites.
Knight tour in C and with dynamic memorry allocation
Knight tour is an interesting problem. To summarize it:
Given an a x b sized board find the way that chess knight can visit all the squares landing in each square only once. There might be a situation without a valid way.
What makes this problem interesting in computer science is multiple ways it can be solved. Brute-forcing, heuristics, neuron networks to name a few. Also testing is made more difficult, because unless you know a priori that a way exists, there is no way to test the "There is no knight tour!" answer from your program.
To write a knight tour solving program first thing one should know is how knight can move. To illustrate available moves I took a picture from wikipedia.
As you can see knight can move by 2 squares in one direction plus additional one square perpendicular to its 2 squares movement. Since it always can move to only 8 positions, calculating legal moves is not very difficult (compared let's say to queen).
Then the other thing is to choose method for finding knight tour. I went for straight forward wall head-banging (brute force). Also this way I had a chance to remember recursive programming. Algorithm is a really simple one - given starting position calculate all possible moves. Then narrow down the list by removing places you have already visited. From remaining list try first available. If after trying you did not finish knight tour, try other available. If you want to reuse my legal moves calculating function - heres the sequence I use for trying out moves.
Some things to remember:
- There might be a board without a knight tour.
- If there is a knight tour, there always will always be one in counter direction.
- Starting position does not have any difference. You are visiting all places anyway.
Next step - rewrite of this code using POSIX threads and adding some heuristics. Also - I promise to add results of benchmarks in the nearest future.
IMPORTANT: Add some sanity checking to user input!
Click "Continue reading" to get source code: Continue to knight tour source code






