Computer Scientist

Monday, 22 October 2012

How to use and tweak Emacs Code Browser

Emacs is one of the most featured text editor in Linux/Unix like systems. The users don't need to move over the fingers to achieve almost all operations to edit text files or to code. It would be a quite efficient multi-function text editor if the horrible beginning start time of using it is neglected. Some keyboard companies produce specific customised keyboard such as HHKB for the ease of Emacs' using.

One of the Emacs' feature is to browser a program project with a large number of source files. More windows and tools are provided to navigate through the whole project. However, the installation and settings of ECB (Emacs Code Browser) is very complicated with a lot of works involved in its long manual. Here I record my short settings of a usable ECB. Not all of the features of either Emacs or ECB are exploited according to my settings, because I am also a newbie of Emacs and ECB. All of my efforts contribute on the usable version of working Emacs and ECB.

Setting the ECB to work in Emacs.

Find out the location of the installed ECB. I installed it by using yum tools. In this case, the location is /usr/share/emacs/site-lisp/ecb. Put this path into the list of emacs' load-path. Then require the ecb after that. However, an error message would be shown up saying that something wrong with the stack-trace-on-error. Then toggle on the stack-trace-on-error before load the ecb to the load-path in .emacs, the configuration file of Emacs.

ECB would ask you to insert some other setting to Emacs' configuration file. Just accept it is fine.

How to use the ECB:

1. navigation among different windows:
    use the C-c . g (x) command, where (x) can be:
        d: directory window
        s: source files window
        m: class and function names window
        h: history window

2. Show any working directory in directory window.
    When I open the ECB mode by using M-x ecb-activate, the directory window have nothing displayed. In order to set the working directory in the directory window the following steps are required:
    M-x customize-option RET ecb-source-path RET
    Choose the INS button in the opening custom buffer in order to insert a directory as a source path.
    Choose to save for current session or for future sessions by type a number for specific type saving. Then the working directory should be shown in the directory window.

3. Customise the layout of ECB
    There are several predefined layout available to users in ECB. The method used to change to these layouts is to set the option "ecb-layout-name". This is quite similar with the method used to set the working path of current session. The local tool of Emacs: M-x customize-option is employed to accomplish these sort of tasks.
    The command M-x ecb-show-layout-help can be used to check the details of a predefined layout through it text-type picture.
 

Sunday, 21 October 2012

Thread-local storage

Thread-local storage (TLS) is a programming method that is used to make static and global variables be local to a thread. The '"errno" is a canonical example of TLS method. Actually, a "errno" should be a static variable used by all functions to determine the error status. However, in each thread of a running process, there should be a separated one in order to prevent the infection from other threads in the same process.

The comparing method to achieve the similar function with the TLS is the thread lock for a static or global variable. However, a carelessness of requiring the lock in any threads in the process would affect other threads.

Saturday, 1 September 2012

Code counting program

For some reasons, I just want to count the number of lines of code which I have already completed locally. There are several methods to achieve this task, such as the Doxygen and the Git tools. They can all produce a rather good report on the program's physical statistics, for example the lines of code. What I was looking for, however, is a lightweight solution which could only count the number of code lines.

SLOCCount is just this kind of tool which is available in the Fedora's package repository. It can also convert the number of code into a financial metric base according to some unknown theory. I am not expect how accurate it would be. It is just quite interesting.

If someone have the similar demands with mine, just have a try on this tool.

Wednesday, 15 August 2012

Deploy Latex writing environment in Fedora 17

At the end of my PhD, a thesis writing with Latex is inevitable. However the Latex is not my primary writing tool during the whole process of my PhD. The Microsoft word seems more easy and intuitive when some small pieces of work are required. In this case, I am going to construct the Latex writing environment before any composing are produced. The last time when I used the Latex for writing was three years ago for the first year long report. It was all accomplished in an apple Mac environment with simple installation process and intuitive user interface. As for this time, I will complete all works in Linux environment. Here I will review how to produce a work using Latex in Linux firstly.

Simple work process:
  1. Composing the latex document.
  2. Compile the latex document: latex <filename.tex>
  3. Review the dvi file: xdvi <filename.dvi>
  4. Produce the pdf file: pdflatex <filename.tex>
The work process mentioned above is just a fairly simple one without other excessive works. It is enough if it is treated as an hello world testing.

In order to use the Latex in Fedora, some packages should be installed at the first place. Here is a collection of install packages which are available in Fedora 17 's yum package installation tool:
  • texlive: for latex environment.
  • texlive-latex: for latex command for front end program.
  • xdvik: for dvi viewer.
This is all the packages that can handle most situations of latex writing. At least those are all needed by the hello work one mentioned.

Using a light-weight text editor to manipulate a latex document is really painful, at least for me. So I found another package available in yum repository:
  • texmaker
This is a convenient IDE to edit latex document.

Monday, 13 August 2012

Dual Monitors on Fedora 17 with Nvidia

I just too excited about my new Nvidia Graphic Card, to take the consideration of Linux driver installation into account. So, here is my updated specification in my little white box:
  • CPU: i5-3570K
  • GPU: EVGA gtx670 ftw
  • MEM: 8G 1600Mhz
  • Hard_DISK:  WD-Black 1T, WD-Blue 500G x 2
  • PSU: Corsair AX850
  • Motherboard: ASUS P8Z77-V-Deluxe
  • Displays: 24" & 19" Samsung

The problem was shown up when I switched from the i5 integrated Graphic Unit HD4000 to the new external EVGA gtx670 ftw Graphic Card, without changing on my original Operating Systems: Fedora 17 and Windows 7 dual system setting.

On the Windows 7, the EVGA graphic card works fine after installing the card driver software. On the Fedora 17, however, only one display (with smaller resolution) is recognised and the other one (with bigger resolution) is mirrored from that one with the same display resolution. (Sometimes another thing happens. The bigger resolution display is recognised and the smaller resolution display cannot display in that high resolution and then it is totally black.) There is no way to modify display settings like the control panel in Windows to add another display by using the provided display setting GUI tools. In this case, I decide to install the official Nvidia Linux driver to make the new graphic card work in Fedora 17.

Unfortunately, I was failed to install official Nvidia Linux driver.  At first, the driver require me to disable the Nouveau driver before the installation of Nvidia driver. After that some other messages are shown on the screen. I cannot remember them. Actually, I didn't know what is really wrong with my installation procedure. But the problem persists after my installation. I assume my installation is unsuccessful at the first place. But I have found some others have already accomplished some work for installing the Nvidia driver in Linux environment.

According to an article on the Internet, the RPM fusion provide the kernel module of the Nvidia driver. This is quite similar with the wireless driver for ASUS's P8Z77-V-Deluxe motherboard which is composed in another my blog. The command used for installing is:

yum install akmod-nvidia xorg-x11-drv-nvidia-libs

By using the akmod package make me not concerned with the kernel update problem. The last step is to disable the Nouveau driver by:

## Backup old initramfs nouveau image ##
mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r)-nouveau.img
 
## Create new initramfs image ##
dracut /boot/initramfs-$(uname -r).img $(uname -r)
 
reboot 

Tuesday, 26 June 2012

Install MySQL and Development environment in Fedora 17

The installation of MySQL in Fedora is easy and trivial task because the MySQL server package is available in official yum repository. It can be installed both by yum and GUI installer.

The most important task is to configure the MySQL into a usable basic status. Because the Fedora 16/17 introduced another system service manipulating program, "systemctl", which is recommended by Fedora 16/17 in their deployment documentations, the configuration procedure is a little different from the previous Fedora systems that use "service" and "chkconfig" commands. These old programs, however, are still accessible in Fedora 16/17. Here is an example of "systemctl" usage:

systemctl status mysqld.service

Notice that the service name is followed by a word "service".


Enabling a service to start automatically at boot time:

systemctl enable mysqld.service


Disabling a service to start at boot time:

systemctl disable mysqld.service


Checking the service status:

systemctl status mysqld.service


Running, Stoping and Restarting a service:

systemctl [start | stop | restart] mysqld.service



After the MySQL service is started and is added to the start queue at boot time, several privilege settings should be done. There are many methods to accomplish this task. I'd like to use the provided program: "mysql_secure_installation". When this step is completed, the password for root is changed, and the anonymous account and test database should be properly set as desired.


In order to access the MySQL from remote machines, do not forget to open the port 3306 for the MySQL server.

More configurations and database set up can be done by using the GUI workbench of MySQL.

Friday, 22 June 2012

Linux driver for the wifi on P8Z77-V-Deluxe

On Asus P8Z77 motherboard, a powerful wireless network card is provided. It is a dual wireless 802.11 a/b/g/n network card and is more powerful than that is provided by the pro version board, which is a single one with only one antenna without blue tooth. A fabulous set of drivers and utilities is included in the ASUS driver disk for Windows system. However, there is nothing coming with the package even a simple driver to Linux. I cannot found any thing in ASUS website, either.

It is not impossible to drive the wifi on P8Z77 in Linux. I got a box with the latest Fedora 17 32-bit installed. The kernel is 3.4.3 .

I installed a USB wireless card from Cisco Linksys AE1000 on my previous Linux box without too much difficulties. It is not hard to find some articles on the Internet to teach you to install it because the Linux driver is available to the public. The only troublesome task is to identify the correct chipset of the device, which is usually also the most basic and important task to install a device driver in Linux. The AE1000 uses the Ralink's chipset RT3572 according to a website. The Internet is a good tool to save me a lot of time and works to disassemble the network card, which would be a really massive task. Fortunately, the Ralink's official Linux driver is all over the Internet. Downloading, Unpacking and Installing will easily accomplish the task.

My sanguine about the Linux driver of the ASUS wifi go's card keeps until I saw someone's thread saying that the Deluxe version of p8z77 got a wireless network card from broadcom with very limited Linux drivers available on the Internet, especially the official Linux driver. I happened to find an official Linux version driver for broadcom chipset in the official website of the broadcom. However, for some reasons, it cannot be installed correctly because the compilation process is ending with an error message. Fortunately, I found another forum thread representing another method to install this driver.

The chip in the ASUS's board is BCM43228 according to the 'lspci' command in Linux. The only available method to drive this wireless card is to install the official Linux driver which is mentioned above. But, if the RPM fusion is installed in the system, the installation process can be accomplished by a yum installation command, which would be much more reliable. But bear in mind that, the non free repository of RPM fusion should also be installed. The name of package is 'kmod-wl'. However, it cannot be used in my case because I have upgraded my Linux kernel to 3.4.3. The kernel version of both kmod-wl and kmod-wl-PAE require some specific kernel version such as 3.4.2. So I have to use the last one 'akmod-wl', which is kernel version independent. I have successfully installed the driver of the network card by using this method.

In order to find much more information for the wireless network card with broadcom chip installation on Linux you should refer to:
http://forums.fedoraforum.org/showthread.php?t=239922
This guy maintained a very comprehensive article for broadcom chip's Linux installation.

Tuesday, 12 June 2012

How to estimate the order of magnitude of Factorial product

The Factorial of a number usually denotes a very big order of magnitude, especially for the computer world. In order to approximate the magnitude of the factorial of a number, it is necessary to find a method to represent the factorial of a number and to keep it into the computer memory. In my example, I am estimating the factorial of 200 which exceeds much more than the capability of any type of C programming language to keep it. An uniform method to represent this big number is important during the estimation. The only way to represent this gigantic big number is by using some smaller numbers that are with in the capability of a C compiler.

My method to represent a number is to use the prime numbers that is smaller that this number. The big number can be factorised into these prime number, so the big number can easily represented the some powers of these prime numbers. For example:

10! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10
      = 1 x 2 x 3 x ( 2 x 2 ) x 5 x ( 2 x 3 ) x 7 x ( 2 x 2 x 2 ) x ( 3 x 3 ) x ( 2 x 5 )
      = 28 x 34 x 52 x 7

In this case, the factorial of 10 can be represented by these four much smaller prime numbers: 2, 3, 5, 7 with their exponential numbers. Although it is difficult to reproduce the similar procedure for the factorial of 200 by sketching on the paper, the result of the factorial of 200 is not difficult to estimate, because the only difference of the factorial of 200 and that of 10 is the number of prime numbers involved. According to the prime number list referring to the wikipedia page, there are only 47 prime numbers smaller than 200. The factorial of 200 is easily represented by these prime numbers with their exponential numbers. The problem of finding all of the prime numbers smaller than a number is out of discussion of this thread. I will discuss it in more details in another thread.

When we've got the prime-numbers representation of the factorial number, the next task should be concerned is how to estimate its magnitude using this prime_number representation.

As the scientific representation is used to represent a big number, what we concern is the number zeros following the number if the first radix point is placed immediately after the first significant digit, for example, 1.23456 x 1020. The magnitude of this number is ten raised to power of 20. In other words, we are counting the number of zeros when we are talking about the magnitude of a number. The example mentioned above is for decimal number. As for the binary number, zero should still be the digit to be counted, but with different means.

I will give the method for decimal number magnitude estimation, firstly. In order to count the number zero which means a ten (10), we need to make a logarithm to the original number. Using the prime-number representation will make a more convenient way to calculate.

log10(10!) = log10(28 x 34 x 52 x 7)
                  = log(28) + log10(34) + log10(52) + log10(7)
                  = 8 x log10(2) + 4 x log10(3) + 2 x log10(5) + log10(7)

These much smaller prime-numbers logarithm is easily to be calculated. Then the number of zeros ( x 10) can be evaluated form this method.

As for a binary number the log2() should be used to calculate the number twos.

Tuesday, 8 May 2012

Using boost regular expression

It is really painful if you have tested the POSIX's regular expression functions with quite a lot of limitations. The good news is that I am not going to walk on the pure C programming for a further long time. Some C++ library's may save me at this point. The performance of my experiment monitor program is my most concern in the first place. It is not any more when I move to C++. There are two candidates that can be used as the regular experiment tool in my C++ programs: boost regex and TR1's regex. Maybe they share the same sources which is not my concerned; and I did not investigate that. According to my initial testing, they have different features during the development by using them.

Boost have a good compatibility across different present platforms: Windows, Linux and so on. The bad feature is that it requires to be linked against the boost_regex library which is different from most boost tools.

I have not looked into more details of TR1's regular expression. My insight told me that maybe it is still not exist in my system yet because it is a module of cxx11 standard. Really advanced and just don't use it.

If I got time, I will go back to do some investigation on these two tools with more sophisticated testing !!!

Friday, 27 April 2012

Chaos of the 'Places' menu

Today, the items in the menu of 'Places' on the above title bar of Fedora 14 are almost all lost from where they are, after I deleted everything in my home directory. Even after I copy back them from somewhere else, the items seems are lost completely without any recovery.

Google is the best way to solve problems. The config file to control these items are in the home directory named, '.config/user-dirs.dirs'. Each items are changed to be $HOME/. After the manual modifications on these incorrect config, Everything is back.

Monday, 16 April 2012

BE CAUTIONS: when you are using 'strncpy'

In my experiment results, an random error happened quite frequently. But it is not show itself in each time's experiment. It makes me be really frustrated as it is shown in a experiment. The PROBLEM is: an additional extra number always suffix the real number. Due to it happened randomly, which means that no patterns can be found from the situations it is shown, it is difficult to spot the problem's root cause.

What can I do at this point is to skim through the code where it probably happened from. Because this code is written with pure C instead of C++, the most possible reason should be the wrong memory pointer.

After the searching of man docs of the Linux, the reason is found --- the using of 'strncpy'.

When we copy a group of characters to a destination C-type string, a null-termination will be added to the destination automatically as 'strcpy' (without the 'n') is used. There are no problems at all.

However, the 'strncpy' is used, we need to think about the null-termination problem. As the statement of warning in the man doc, if the first 'n' characters of source C-type string have not null-termination in it. This function will not append the '\0' termination to the end of destination string automatically. This is not related to the size of destination string. We only need to be bare in mind the size of n and the first n characters of source string.

SOLUTION: add the '\0' termination to the destination string manually.

Wednesday, 28 March 2012

Stop auto clean the Fedora's /tmp directory

In Fedora, the files that stored in /tmp directory seems would be removed after a certain period of time without operation on it. Which kind of time (last modification time, create time or read time) can be set using different parameters when invoke a program named tmpwatch. Beware in mind, this works only in Fedora series. I am not sure for other Linux distributions.

The tmpwatch is a watching program that is invoked daily-based by cron. A script is also called tmpwatch is in /etc/cron.daily/ directory to invoke the program in /sbin directory.

In my case, I want to keep my file in /tmp directory without bothering with the tmpwatch's auto-removing. So I am able to set myself to be a exclude user by -U parameter for tmpwatch in shell scripts in cron directory.

And it should be work.

Friday, 2 March 2012

Add column to existing table

mysql how to add columnMany ways to add column to existing table in MySQL. If you r using GUI tools, add column to table in MySQL is easy. If you are using CLI, then here’s the command to add column in MySQL table.
To add column in existing table, you may refer to the 2 examples below:-
Example I: Add new varchar column to the end of the table
ALTER TABLE `tablename_here` ADD `new_column_name` VARCHAR( 255 ) NOT NULL ;
Example II: Add new integer column after an existing column in table
ALTER TABLE `tablename_here` ADD `new_column_name` INT NOT NULL AFTER `existing_column` ;
It’s simple to add column to existing table right? :)


Monday, 16 January 2012

Terminal Colors

Here's a list of different colors:
30 black foreground
31 red foreground
32 green foreground
33 brown foreground
34 blue foreground
35 magenta (purple) foreground
36 cyan (light blue) foreground
37 gray foreground

40 black background
41 red background
42 green background
43 brown background
44 blue background
45 magenta background
46 cyan background
47 white background

Commands can also be combined using a semicolon, like so:


printf("\033[45;37mGrey on purple.\033[0m");



Finally here is a list of other neat commands that go at the end (where the '0' is):
0 reset all attributes to their defaults
1 set bold
5 set blink
7 set reverse video
22 set normal intensity
25 blink off
27 reverse video off


=====================================================
Another one:


\033[22;30m - black
\033[22;31m - red
\033[22;32m - green
\033[22;33m - brown
\033[22;34m - blue
\033[22;35m - magenta
\033[22;36m - cyan
\033[22;37m - gray
\033[01;30m - dark gray
\033[01;31m - light red
\033[01;32m - light green
\033[01;33m - yellow
\033[01;34m - light blue
\033[01;35m - light magenta
\033[01;36m - light cyan
\033[01;37m - white