For this topic, I have found some excellent bloggers.
Wednesday, October 14, 2009
Tuesday, August 25, 2009
gdb debugger introduction
- Compile your program using debug. If you want to use gdb, you have to compile your code using '-g' flag. It will tell the compiler to store the debug information in object file. It describes the data type of each variable or function.
g++ -g test.cxx -o test - Run your program.
Quit your program.
gdb test
quit ; or Ctrl + D - Set up break points.
> b 19 // break at line 19 at a file
> b func1 // break at func1
> b testClass::testFunc(int) // C++ is polymorphic. you must tell break which version of the function you want to break on
> info breakpoints // can print out all the break points - Use backtrace.
backtrace
You can see many frames related the point.
#0 std::string::assign (this=0xd3d704c, __str=@0xfd0d05)
#1 0x00e4e524 in std::string::operator= (this=0xd3d704c, __str=@0xfd0d05)
#2 0x065692d4 in IssueSeverity (this=0xd3d7048, rhs=@0xfd0d01)
#3 0x0656831f in StatusCode::cloneSeverity (sev=0xfd0d01) at ../src/Lib/StatusCode.cpp:28
#4 0x0317eb29 in StatusCode (this=0xbff07888, rhs=@0xbff077a4)
#5 0xb7947250 in ?? ()
#6 0xbff07888 in ?? ()
#7 0xbff077a4 in ?? ()
#8 0x00000000 in ?? ()
You can take a look at the detail of one frame.
frame 1
info frame
info locals
info args
links
Thursday, August 13, 2009
Gmail : How to mark all the unread email to read
Maybe you have many emails you don't care. So you just want to mark these emails to read and don't want to read them. In Gmail, if you just click the button "unread" at the top of mail, it will just select the unread email in this page(less than 50 conversation). You have to go to next page to select unread and mark as read. That is not convenient. What you can do is to type"is:unread" in search email frame.
Then you can see all the unread emails(not in one page). Now you can click "select All". In this case, you just select the unread emails in one page. Gmail will ask you whether you want to select all the unread emails. You choose yes. Then all your unread emails have been selected. The last step is click mark as read.
Then you can see all the unread emails(not in one page). Now you can click "select All". In this case, you just select the unread emails in one page. Gmail will ask you whether you want to select all the unread emails. You choose yes. Then all your unread emails have been selected. The last step is click mark as read.
- Type"is:unread" in search email frame.
- Select All
- Gmail will ask you whether you want to select all the unread emails. You choose yes.
- Click "Mark as read".
Monday, August 10, 2009
xterm
If you use xmanager, maybe you need this setting to make your terminal look better.
/usr/bin/X11/xterm -bg black -fg gray -fn 10x20 +cm +dc -ls -display $DISPLAY
Sunday, July 19, 2009
Cpp : My Test
Integer
If you use integer in C++, you should be careful. Because the integer width is limited. For example, an short int in Cpp is 16bits. An int is no shorter than short integer. A long integer is 32 bits. I just do a test on i686 GNU/Linux. Here is the result.
If you use integer in C++, you should be careful. Because the integer width is limited. For example, an short int in Cpp is 16bits. An int is no shorter than short integer. A long integer is 32 bits. I just do a test on i686 GNU/Linux. Here is the result.
short a ;
> a^14 = 16384
> a^15 = -32768
> a^16 = 0
int a ;
> a^30 = 1073741824
> a^31 = -2147483648
> a^32 = 0
long a ;
> a^30 = 1073741824
> a^31 = -2147483648
> a^32 = 0
unsigned short a ;
> a^14 = 16384
> a^15 = 32768
> a^16 = 0
unsigned int a ;
> a^30 = 1073741824
> a^31 = 2147483648
> a^32 = 0
unsigned long a ;
> a^30 = 1073741824
> a^31 = 2147483648
> a^32 = 0
Tuesday, May 12, 2009
Cron on Linux
If you have many scripts running on a server machine(I have 6 scripts running on different machines). These scripts can be monitoring script or something important to the system. But sometime these script can crash due to some unknown reasons. At this time it's very dangerous to the system. So setting up one mechanism to monitor the condition of these scripts is very important to the system and your work. The CRON job can do this job. CRON is part of Linux and can be used to run a script at some time you want. You can use this to open the cron job setting.
If you want to know the meaning of this sentence, please go to Cron on Linux.
This mcJobMonitor.py is
Add this to this file.
crontab -e
*/5 * * * * /usr/local/bin/mcJobMonitor.py >> /dev/null
If you want to know the meaning of this sentence, please go to Cron on Linux.
This mcJobMonitor.py is
#!/usr/bin/python
#//////////////////////////////////////////////////
# Haifeng Li
#///////////////////////////////////////////////////
import sys, os, time, commands, getopt
def SendMailtoUser(ScriptName ):
hostname = commands.getoutput("uname -n")
timedate = commands.getoutput("date")
pwd = commands.getoutput("pwd")
message = "Your script "+ScriptName+" in machine " +hostname+ " is dead. \n"\
+"We have restarted this script. You can go to this machine to check the reason. \n"
email = "xxxxxxx@gmail.com"
command ="".join(["echo \"", message, \
"\" | mail ", email, " -s \"Cron Monitor for ", ScriptName, "\""])
os.system(command)
if __name__=="__main__" :
#________________________updatedatabase.py________________________________
ScriptName = "/var/www/html/hpeng3/script/updatedatabase.py"
status = commands.getoutput("ps -ef | grep 'updatedatabase.py' | grep -v grep")
status = status.strip()
if status == '' :
pushd = "pushd /var/www/html/hpeng3/script"
restart = "./restart.sh"
popd = "popd"
command = ";".join([pushd, restart, popd])
os.system(command)
SendMailtoUser(ScriptName )
Monday, May 4, 2009
Monday, April 20, 2009
Initial process of Linux
Suppose you set up a sever or a administration machine, where there are many monitoring programs. If the machine reboots, then you have to restart all the programs manually. Maybe sometime you forget some processes. In that case, it's very danger to the system. So you should set up some initialization of Linux system. When the machine reboot, some processes will start automatically. This is what I have done.
You will see.
-----------------------------------------------------------------------------------------------------
Linux Process Control
Linux start scripts
emacs -nw /etc/rc.d/rc.local
You will see.
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/sbin/ntpdate ntp1.cs.wisc.edu
# define by Haifeng Li. For the CHTC machine's jobs scanning
/home/haifeng/chtcKillZombieJobs.py >> /home/haifeng/chtcKillZombieJobs.log &
# For the removing the old directory.
/home/hpeng/chtcclean.py >> /home/hpeng/chtcclean.log &
-----------------------------------------------------------------------------------------------------
Linux Process Control
Linux start scripts
Friday, April 17, 2009
Static library and dynamic library in Linux
A library is a package of code which can be used by other program. First, libraries are compiled. You don't have to compile them when you are using them. Second, because the complied libraries are in machine language, it can prevent other people to know the source code. This is important for business use. But it's not good for open source.
-----------------------------------------------------------------------------------------------------
What is library
Actually, library is just a collection of header(C++) files or sub functions(maybe Fortran).
There are two types library.
Generate library
Use Library
If you want to use the shared library, you have to tell the computer where the library is. Actually you have to how the Linux system to find lib.
My Makefile
This is a Makefile I am using.
--------------------------------------------------------------------------------------------
Program Library HOW TO
Static and Dynamic Libraries
Analysis of static and shared library
Programming of static and shared library
-----------------------------------------------------------------------------------------------------
What is library
Actually, library is just a collection of header(C++) files or sub functions(maybe Fortran).
There are two types library.
- Static library(archive library). In linux, statics lib has a .a extension. When you compile a program using static library, the program will copy all the funcitonally to the program. So your executable file is very big. One advantage of static library is the program does not have to find the lib when it's running. This is very convenient when you compile the code in one machine and run the program in another machine and you don't want to install the lib in that machine.
- Dynamic library(shared library). Dynamic library has a .so extension in Linux. Dynamic lib will be linked when the program is running. So you don't have to copy all the libs to your executable file. That is a advantage. Because the library you are using may be very big(GB).
Generate library
- To create a static library,
This command adds the objects files file1.o and file2.o to the static library.
ar rcs my_library.a file1.o file2.o - Create a Shared library. First , create the object files using the gcc -fPIC of -fpic flag. The -fPIC and -fpic option enable 'position independent code' generation. Here is an example.
-g means debugging information. -Wall will generate warnings. -c is compilation.
gcc -fPIC -g -c -Wall a.c
gcc -fPIC -g -c -Wall b.c
gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc
Use Library
If you want to use the shared library, you have to tell the computer where the library is. Actually you have to how the Linux system to find lib.
- Default shared lib directory. The default dir is /usr/lib, which means the system will first search the libs in /usr/lib. Then you have to run ldconfig.
ldconfig -n directory_with_shared_libraries
- You can use LD_LIBRARY_PATH to tell the system to find the shared libraries before the usual places.
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH my_program
- ldd can display the dependence of a shared library.
ldd /usr/bin/mesg
It will display
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7eaf000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0xb7feb000)
My Makefile
This is a Makefile I am using.
$(shell root-config --libs) is a fantastic bash command. When you run the executable file, this command will be evaluated and will give the all the libs related to root(which is a analysis framework of high energy physic). In my case, the result of this command is
##################################
# root variables
ROOTCFLAGS := $(shell root-config --cflags)
ROOTLIBS := $(shell root-config --libs) -lRooFit -lHtml -lCore -lCint -ldl -lTree \
-lGpad -lGraf -lHist -lMatrix -lPostscript -lMinuit -lMinuit2 -lPhysics
# Programs
CXX = g++
F77 = f77
CXXFLAGS = -g -ggdb -Wall -fPIC -Wno-deprecated
MY_CXXFLAGS = -static
LDFLAGS = -g
SOFLAGS = -shared
# do yourself a favor, use gcc for linking, and not ld
LD = CXX
# local Includes
INCDIR = -Iinclude
# Assign or Add variables
CXXFLAGS += $(ROOTCFLAGS)
CXXFLAGS += $(INCDIR)
# Assigan or add variables
LIBS += $(ROOTLIBS)
validation: src/validation.C clean
$(CXX) $(CXXFLAGS) $(MY_CXXFLAGS) -c src/validation.C -o lib/validation.o
$(CXX) lib/validation.o -o bin/validation $(LDFLAGS) $(LIBS)
clean:
rm -rf bin/* lib/*
-L/afs/cern.ch/sw/lcg/external/root/5.18.00/slc4_ia32_gcc34/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -pthread -lm -ldl -rdynamic
--------------------------------------------------------------------------------------------
Program Library HOW TO
Static and Dynamic Libraries
Analysis of static and shared library
Programming of static and shared library
Thursday, April 16, 2009
Page Size in ps2pdf
ps2pdf is a linux tool, which can convert ps(or eps) file to pdf file. If you convert a ps file to a pdf, the default page size is letter. Some time you want to get different size plots. You can use
This command is working only for eps file, which has a bounding box. If you open a ps file, you can see the
There is no bounding box in ps file. So you can not get the special size pdf file from a ps file.
You also can use ps2eps to convert ps file to eps. This program will calculate the boundary box for ps file and generate new eps file with a boundary box.
PostScript-to-PDF converter
Or you can use epstopdf
ps2pdf -dEPSCrop jet.eps
epstopdf muonDetail.eps
This command is working only for eps file, which has a bounding box. If you open a ps file, you can see the
%!PS-Adobe-2.0 EPSF-2.0
%%BoundingBox: 0 0 567 552
There is no bounding box in ps file. So you can not get the special size pdf file from a ps file.
You also can use ps2eps to convert ps file to eps. This program will calculate the boundary box for ps file and generate new eps file with a boundary box.
ps2eps electron.ps
PostScript-to-PDF converter
Tuesday, April 14, 2009
Image manipulation using Python

You can use Python Image model to manipulate the image. It's interesting because you know that many pixels compose a picture. And the pixels are in different mode. For example, RGB. This is a python script which can print a new image.
This is the picture we get.
#!/usr/bin/python
import sys
import Image, ImageDraw
newpic = Image.new("RGB", (300,300), color=(255,255,255))
xsize, ysize = newpic.size
for x in xrange(xsize) :
for y in xrange(ysize) :
newpic.putpixel((x,y), (int(255*(y/ysize)), int(255*x*y/(xsize*ysize)), int(255*(x/xsize))))
newpic.show()
newpic.save("gen.jpg", "JPEG")
Wednesday, April 8, 2009
Tips for Bash
Bash is a kind of script language. It's not too formatted. But it's the language used by Linux system. So it's the most direct operation command for Linux. Other script languages, for example, python, perl, have to use bash sometimes. In my mind, if you want to write several lines script to finish your work, BASH is the best choice. I just write down something, which is used by myself when I am working.
- tar.
tar -cvzf filename.tgz filename
- untar.
tar xvzf filename.tgz
- check the space of some directorie.
du -h --max-depth=1
- process control.
#!/bin/bash
for each in `ps -ef | grep ” | grep -v PID | awk ‘{ print $3 }’`;
do
for every in `ps -ef | grep $each | grep -v cron | awk ‘{ print $2 }’`;
do kill -9 $every;
done;
done - sed. This command will look at the text in 2.txt and substitute the 'new2' to 'CERN'. The new text will be stored in 2.txt.
If you use
sed -i 's/new2/CERN/g' 2.txtsed -e 's/new2/CERN/g' 2.txt
This command will just substitute the string, print the new text in monitor and will not be stored in file.
You also can use
line="x1.root y1.root x2.root y2.root"
echo $line | sed 's/y[1-9].root//g'
x1.root x2.root - find.
find ./work/ -mindepth 2 -maxdepth 2 | xargs chmod 744 - awk. I don't recommend people to use this awk to get some system information, like the example. Because it's not a good programming way.
#!/bin/bash
ps -ef | grep gnome | grep -v grep | awk '{print $7}'
ps -ef | grep "slot" | awk '{print $7}' | sed -e 's/-.*//g' | grep -v ":" - refresh the desktop
killall nautilus
Monday, April 6, 2009
PS to image.
ps to png
eps to png
pdf to png
One bash script to do that
You can convert a eps to pdf like this.
----------------------------------------------------------------------------------------------------------
Links
How to use Ghostscript
Details of Ghostscript output devices
gs -sDEVICE=png256 -o muon03.png -r800 -dBATCH -dNOPAUS muon.ps
gs -sDEVICE=png256 -sOutputFile=muon03.png -r800 -dNOPAUSE muon.ps -c quit
eps to png
gs -sDEVICE=png256 -dEPSCrop -sOutputFile=output.png -r800 -dNOPAUSE input.eps -c quit
pdf to png
gs -sDEVICE=png256 -o muon03.png -r800 -dBATCH -dNOPAUS muon.pdf
gs -sDEVICE=png256 -sOutputFile=muon03.png -r800 -dNOPAUSE muon.pdf -c quit
One bash script to do that
#!/bin/bash
for i in `ls *.ps`;
do gs -sDEVICE=png256 -sOutputFile=${i//.ps/.png} -r600 -dNOPAUSE ${i} -c quit;
done
You can convert a eps to pdf like this.
gs -dEPSCrop -sDEVICE=pdfwrite -sOutputFile=out.pdf -dNOPAUSE
-dBATCH -dAutoRotatePages=/None -c .setpdfwrite -f input.eps
----------------------------------------------------------------------------------------------------------
Links
How to use Ghostscript
Details of Ghostscript output devices
Sunday, April 5, 2009
CSS centralize problem
It's very difficult to centralize the web page because IE and Firefox have different explanation of CSS code. I wrote a very simple web page these days. But I found my web page is central in Firefox and can not be centralized in IE. We don't like IE. But somebody likes it. So it's better to make it work at IE too.
At first I used one div container.
This setting is working in Firefox because the "margin : 0 auto". But IE can not know this setting. So I looked through the web to get some information. There is one method which is almost good.
Actually this is a good setting if you want to get a fixed width web page. But for my case, this is not very good because I don't want to get a fixed width but a unfixed width. I felt upset about the uncompatibility between IE and Firefox. Then I want to give up. Suddenly I got an idea when I was sitting in a meeting. My idea is this. I use two containers. One of them has 100% width using the first method. Then I use a another container in the first container, which has 88% width. When I reload my web page, it looks great.
At first I used one div container.
div.body{
width: 32cm;
float: center ;
margin: 0 auto ;
padding: 0px;
text-align: center;
border: 0px groove;
}
This setting is working in Firefox because the "margin : 0 auto". But IE can not know this setting. So I looked through the web to get some information. There is one method which is almost good.
div.body{
position:absolute;
top:50%;
left:50%;
margin-top:-240px;
margin-left:-320px;
width:640px;
height:480px;
background:#ECECEC;
}
Actually this is a good setting if you want to get a fixed width web page. But for my case, this is not very good because I don't want to get a fixed width but a unfixed width. I felt upset about the uncompatibility between IE and Firefox. Then I want to give up. Suddenly I got an idea when I was sitting in a meeting. My idea is this. I use two containers. One of them has 100% width using the first method. Then I use a another container in the first container, which has 88% width. When I reload my web page, it looks great.
Tuesday, March 31, 2009
Emacs Usage
Emacs is the bese editor I have ever used. It's very good at speed and language format.
.emacs setting
.emacs is a configure file for emacs. When emacs starts, it will run .emacs first. Usually .emacs is at ~/.emacs. Actually .emacs is something like LISP language.
- (setq inhibit-startup-message t) is used to shut down the welcome massage when you open emacs. its boring to see a welcome page every time you use emacs.
- t means true.
- nil means false.
- (setq-default make-backup-files nil) is used to shut down the backup automatically. But I recommend you open this function.
- (setq require-final-newline nil) will not require a new line in the end of file.
- But sometime, (setq require-final-newline nil) does not work. Because /usr/share/emacs/site-lisp/default.el will run after .emacs. At this time, you should switch off the default.el by using (setq inhibit-default-init t). I spent plenty of time on this stupid new line query. Now I can shut it down. But you have to open one thing[7] in .emacs.
- (when window-system
;; enable wheelmouse support by default
(mwheel-install)
;; make switching frames works properly under the default click- to-focus
(setq focus-follows-mouse nil) )
-----------------------------------------------------------------------------------------------
Find and Replace
- Find a string : C-s
- Find backward : C-r
- Find forward : C-s
- How to replace a string. M-x replace-string
- How to do query replace. M-x query-replace.
- You also can do query replace by. M-%
Rectangular Operations
Sometimes it's very convenient to use the rectangular selection in emacs. When you are learning how to do it, it's painful. But after you get it, it will save you much more time.
- Select a rectangular area and copy(cut) it to another place. Go to the left-top of the rectangular you want to select. Hit C-space. Then this point is mark set. Then go to the right-bottom of your rectangular area, and click C-x r r(C-x r k for cut) and Enter. This rectangular area has been copied to register. Move your cursor to the place where you want to put this area, and click C-x r i and Enter. After click enter, the content in register will be copied to the new place.
- Select a rectangular area and insert some strings. Hit C-space and click C-x r t and Enter. Then you will enter the text you want insert this area. After hit enter, you will see the text has been inserted.
Cursor Movement
- Move cursor to the beginning of a line : C-a
- Move cursor to the and of a line : C-e
- Move cursor to the beginning of this file: C-x [
- Move cursor to the end of this file: C-x ]
Multi-Windows
In emacs, you can split the window in to several windows.
- C-x 3 will split the window in two columns.
- C-x 2 will split the window horizontally.
- C-x 0 will shut down one window.
- C-x o will change the cursor between different windows.
Some very useful links
Ted's Blog for emacs
Emacs Keyboard Shortcuts
JW's Tips and Notes: Emacs Example Tutorial: insert a block of text
emacs-fu: working with rectangular selections
Tuesday, March 24, 2009
A Python function which can combine a list to a string

Sometime you can use str.split(".") to split a string to a list. It's very easy to operate on this list. But sometime you want to combine the list to a string again after the list operation. I just write a simple python function.
ListName : the name of list which you want to combine
StrInsert : the string which you want to insert to this list
def combineStr( ListName , StrInsert ):
out_str = ListName[0]
for item in range(1, len(ListName)):
out_str +=StrInsert+ListName[item]
return out_str
Monday, March 23, 2009
How to take a screenshot in your computer
Using GIMPTo take a screenshot with the GIMP, find the following menu option: File —> Acquire —> Screen Shot. You will then be offered some options for the screenshot such as length of delay and whether you want to take a screenshot of the entire screen, or just a window.
PrintScreen Button
This method is easy. Put the button in you keyboard. Normally, the PrintScrn is at the right side of F12. After you put this button, there will be one window to ask you how to store this picture.

Use Ubuntu(GNOME)
You can open Applications->Accessories->Take Screenshot.
You will see a window which can help you to take a screenshot.
How to add a public calendar to Google Calendar
If you need to print out the meeting schedule, you can just add the public calendar of the meeting to Google Calendar. Google Calender will update the time schedule automatically and I don't need to worry about whether the schedule is the latest.

I have tested the update rate of Google calender. Normally, it will be updated every 24 hours.
If you are familiar with Python script, you can also use the gcalcli.
gcalcli can connect with google calendar by python. First you can get the public calendar by 'wget'. Then you can edit the calendar file and add these files in to google calendar by gcalcli. It's powerful and useful. But in my case, the first method is enough. So I did't dig out the second method.
- First you should get the link of the public calender. For example, http://indico.cern.ch/categoryDisplay.py/ical?categId=3l94
- Then you can go to your google calender.Click the Add->Add by URL. Then you can type the link of you calendar. Enjoy your own calendar.

I have tested the update rate of Google calender. Normally, it will be updated every 24 hours.
If you are familiar with Python script, you can also use the gcalcli.
gcalcli can connect with google calendar by python. First you can get the public calendar by 'wget'. Then you can edit the calendar file and add these files in to google calendar by gcalcli. It's powerful and useful. But in my case, the first method is enough. So I did't dig out the second method.
Delete the contents of your search toolbar
Sometime if you are using a public computer, you maybe want to delete the search record of your google toolbar.


I like the second way. It's more easier and quick.

- If you are using firefox,you can openEdit->Preferences->Privacy->Clear Now.
- You also can delete the contents directly. You just click the contents of search bar and shift+delete.

I like the second way. It's more easier and quick.
Subscribe to:
Posts (Atom)