Tuesday, 25 December 2007

the royal

白金汉宫表示今年圣诞节,81岁的英国女王伊丽莎白二世将按照惯例发表圣诞致辞,除了通过电视直播,还将首次通过Youtube网站播放视频。同时 YouTube网站已经推出一个新的皇家频道的,让网友观看女王在1957年的第一个圣诞节讲话以及其他一些昔日王室及其活动的片段。该视频所在频道为 www.youtube.com/theroyalchannel

Friday, 21 December 2007

Google map (gps) for UK on Mobile

Google has launched the UK-specific version of its popular Java mobile app - Google Maps mobile. Users in Britain can now see Tube stations, and use the local search. If you have a Java-enabled handset (almost all new phones support Java), you can download the UK version of Google Maps mobile from google.co.uk/gmm.

In the other news, Google finally added support for GPS to the latest version (v1.5.1) of Google Maps mobile. In the application press “0″ to quickly pin-point your location with a blue dot. Originally, Google promoted this feature as being exclusive for the Helio Drift. Now they’ve quietly released an update that should work on all major platforms. (via: Crave, WebWorkerDaily)




http://www.esato.com/board/viewtopic.php?topic=146481

http://amazegps.com/welcome.php?language=uk

Saturday, 8 December 2007

Troubleshooting Sudo



I happen to much prefer Ubuntu and Mac OS X's sudo model to the root/user one that's typical of most Linux distributions. You can read all about why Ubuntu uses sudo and all the pros and cons of that model at help.ubuntu.com/community/RootSudo.


The one thing I don't like about sudo is how fragile it is. If you don't know what you're doing (especially at the command-line), sometimes sudo can get broken. It doesn't happen very often, but it does happen. That's what this page is for.


If your sudo is "broken," meaning that you can't use the sudo command to temporarily gain administrative privileges, there are two files you should be aware of:


/etc/sudoers and /etc/group


The /etc/sudoers file should look the same for every Ubuntu user who hasn't fiddled with it:

# /etc/sudoers

#

# This file MUST be edited with the 'visudo' command as root.

#

# See the man page for details on how to write a sudoers file.

#



# Host alias specification


# User alias specification


# Cmnd alias specification


# Defaults


Defaults !lecture,tty_tickets,!fqdn


# User privilege specification

root ALL=(ALL) ALL


# Members of the admin group may gain root privileges

%admin ALL=(ALL) ALL



It basically says anyone who is root can do anything, and anyone in the administrative group (people who can sudo) can do anything (with a password).


Now, the /etc/group file will look different for every Ubuntu
installation. It specifies which groups each user belongs to. An
example of how it might look is here:

root:x:0:

daemon:x:1:

bin:x:2:

sys:x:3:

adm:x:4:firstuser

tty:x:5:

disk:x:6:

lp:x:7:cupsys

mail:x:8:

news:x:9:

uucp:x:10:

man:x:12:

proxy:x:13:

kmem:x:15:

dialout:x:20:firstuser,cupsys

fax:x:21:

voice:x:22:

cdrom:x:24:firstuser,haldaemon

floppy:x:25:firstuser,haldaemon

tape:x:26:

sudo:x:27:

audio:x:29:firstuser

dip:x:30:firstuser

www-data:x:33:

backup:x:34:

operator:x:37:

list:x:38:

irc:x:39:

src:x:40:

gnats:x:41:

shadow:x:42:

utmp:x:43:

video:x:44:firstuser

sasl:x:45:

plugdev:x:46:firstuser,haldaemon

staff:x:50:

games:x:60:

users:x:100:

nogroup:x:65534:

dhcp:x:101:

syslog:x:102:

klog:x:103:

firstuser:x:1000:

lpadmin:x:104:firstuser

scanner:x:105:firstuser,cupsys

admin:x:106:firstuser

crontab:x:107:

ssh:x:108:

messagebus:x:109:

haldaemon:x:110:

slocate:x:111:


For troubleshooting purposes, the most important line in the /etc/group file is the one in bold, which specifies who is in the admin group, and hence who has sudo privileges. Substitute your actual username for firstuser, of course.


Now, this begs the question, "How can I edit the /etc/group file if I don't have sudo permissions?"


The answer is something called recovery mode.



Free Image Hosting at www.ImageShack.us Free Image Hosting at www.ImageShack.us

You know when you boot up, you get several options for how you want to
boot up? There's usually a kernel, recovery mode, and memtest at the
very least.

After you boot into recovery mode, you should be logged in as
root. Or, if you set a root password in your installation, you'll be
prompted for your root password. Either way--password or not--you'll
end up logged in as root.



Once
you're there, before you make any changes, it's a good idea to make
backup copies of your two corrupt files. Sure, they're incorrect, but
they're better than nothing, especially if you accidentally delete the
contents of the original files. To back them up, type

cp /etc/group /etc/group.old

cp /etc/sudoers /etc/sudoers.old


Then, to edit the files, use these commands:


sudo visudo


This command edits the /etc/sudoers file.


nano /etc/group

This command edits the /etc/group file.




To save in nano, you press Control-X (save), Y (confirm), and Enter (exit).


If you don't want to bother editing the /etc/group file, you can also issue this command:


adduser username admin


That one command will add user username to the admin group so you can sudo


If you are trying to fix the error where it says sudo is mode _____, should be 0440, then you'll want to type


chmod 0440 /etc/sudoers


When you're done, reboot, and you should be able to sudo again.



Powered by ScribeFire.

two ways to make a user sudo

$sudo adduser laser admin
sudo adduser laser sudo, here 'sudo' can not make the user run sudo ability.

===================

if group admin doesn't exist, you should edit /etc/sudoers

login with root, if no root, run "passwd root" to create one.

$vi /etc/sudoers

cp the last line of "root ALL......", paste it at the next line and change the root to your name. Done.



######################################

If you've used Linux for any amount of time, you might be used to
running programs as root directly whenever you need to install
packages, modify your system's configuration, and so on. Ubuntu employs
a different model, however. The Ubuntu installer doesn't set up a root
user -- a root account still exists, but it's set with a random
password. Users are meant to do administration tasks using sudo and
gksudo.



You probably already know how to use sudo -- just run sudo commandname .
But what about running GUI apps that you want to run as root (or
another user)? Simple -- use gksudo instead of sudo. For instance, if
you'd like to run Ethereal as root, just pop open a run dialog box (Alt-F2) and use gksudo ethereal.



By the way, if you really must do work as root, you can use sudo su -,
which will log you in as root. If you really, really want to have a
root password that you know, so that you can log in as root directly
(i.e., without using sudo), then run passwd when logged
in as root, and set the password to whatever you want. I'd recommend
using the pwgen package to create a secure password not only for root
but for all your user accounts.



Powered by ScribeFire.

Wednesday, 5 December 2007

Top/free的使用及参数详解

1.作用

top命令用来显示执行中的程序进程,使用权限是所有用户。

2.格式

top [-] [d delay] [q] [c] [S] [s] [i] [n]

3.主要参数

d:指定更新的间隔,以秒计算。

q:没有任何延迟的更新。如果使用者有超级用户,则top命令将会以最高的优先序执行。

c:显示进程完整的路径与名称。

S:累积模式,会将己完成或消失的子行程的CPU时间累积起来。

s:安全模式。

i:不显示任何闲置(Idle)或无用(Zombie)的行程。

n:显示更新的次数,完成后将会退出top。

4.说明

top命令是Linux系统管理的一个主要命令,通过它可以获得许多信息。这里我们结合图1来说明它给出的信息。

top命令的显示 (图略)

第一行表示的项目依次为当前时间、系统运行时间、当前系统登录用户数目、1/5/10分钟系统平均负载(一般来说,这个负载值应该不太可能超过 1 才对,除非您的系统很忙碌。 如果持续高于 5 的话,那么.....仔细的看看到底是那个程序在影响整体系统吧!)。

第二行显示的是所有启动的进程、目前运行、挂起 (Sleeping)的和无用(Zombie)的进程。(比较需要注意的是最后的 zombie 那个数值,如果不是 0 ,嘿嘿!好好看看到底是那个 process 变成疆尸了吧?!)(stop模式:与sleep进程应区别,sleep会主动放弃cpu,而stop是被动放弃cpu ,例单步跟踪,stop(暂停)的进程是无法自己回到运行状态的)

第三行显示的是目前CPU的使用情况,包括us用户空间占用CPU百分比、sy 内核空间占用CPU百分比、ni 用户进程空间内改变过优先级的进程占用CPU百分比(中断处理占用)、id 空闲CPU百分比、wa 等待输入输出的CPU时间百分比、hi,si,st 三者的意思目录还不清楚 :)

第四行显示物理内存的使用情况,包括总的可以使用的内存、已用内存、空闲内存、缓冲区占用的内存。

第五行显示交换分区使用情况,包括总的交换分区、使用的、空闲的和用于高速缓存的大小。

第六行显示的项目最多,下面列出了详细解释。

PID(Process ID):进程标示号 ( 每个 process 的 ID )

USER:进程所有者的用户名 ( 该 process 所属的使用者 )

PR:进程的优先级别 ( Priority 的简写,程序的优先执行顺序,越小越早被执行 )

NI:进程的优先级别数值 ( Nice 的简写,与 Priority 有关,也是越小越早被执行 )

VIRT:进程占用的虚拟内存值。

RES:进程占用的物理内存值。

SHR:进程使用的共享内存值。

S:进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。

%CPU:该进程占用的CPU使用率。

%MEM:该进程占用的物理内存和总内存的百分比。

TIME+:该进程启动后占用的总的CPU时间 ( CPU 使用时间的累加 )

Command:进程启动的启动命令名称,如果这一行显示不下,进程会有一个完整的命令行。

top命令使用过程中,还可以使用一些交互的命令来完成其它参数的功能。这些命令是通过快捷键启动的。

<空格>:立刻刷新。

P:根据CPU使用大小进行排序。

T:根据时间、累计时间排序。

q:退出top命令。

m:切换显示内存信息。

t:切换显示进程和CPU状态信息。

c:切换显示命令名称和完整命令行。

M:根据使用内存大小进行排序。

W:将当前设置写入~/.toprc文件中。这是写top配置文件的推荐方法。

可以看到,top命令是一个功能十分强大的监控系统的工具,对于系统管理员而言尤其重要。但是,它的缺点是会消耗很多系统资源。

5.应用实例

使用top命令可以监视指定用户,缺省情况是监视所有用户的进程。如果想查看指定用户的情况,在终端中按“U”键,然后输入用户名,系统就会切换为指定用户的进程运行界面,见图2所示。

a.作用

free命令用来显示内存的使用情况,使用权限是所有用户。

b.格式

free [-b|-k|-m] [-o] [-s delay] [-t] [-V]

c.主要参数

-b -k -m:分别以字节(KB、MB)为单位显示内存使用情况。

-s delay:显示每隔多少秒数来显示一次内存使用情况。

-t:显示内存总和列。

-o:不显示缓冲区调节列。

d.应用实例

free命令是用来查看内存使用情况的主要命令。和top命令相比,它的优点是使用简单,并且只占用很少的系统资源。通过-S参数可以使用free命令不间断地监视有多少内存在使用,这样可以把它当作一个方便实时监控器。

#free -b -s5

使用这个命令后终端会连续不断地报告内存使用情况(以字节为单位),每5秒更新一次。

解读Linux操作系统内核源码的好方法

针对好多Linux 爱好者对内核很有兴趣却无从下口,本文旨在介绍一种解读linux内核源码的入门方法,而不是解说linux复杂的内核机制;

一.核心源程序的文件组织:

1.Linux核心源程序通常都安装在/usr/src/linux下,而且它有一个非常简单的编号约定:任何偶数的核心(例如2.0.30)都是一个稳定地发行的核心,而任何奇数的核心(例如2.1.42)都是一个开发中的核心。

本文基于稳定的2.2.5源代码,第二部分的实现平台为 Redhat Linux 6.0。

2.核心源程序的文件按树形结构进行组织,在源程序树的最上层你会看到这样一些目录:

●Arch :arch子目录包括了所有和体系结构相关的核心代码。它的每一个子目录都代表一种支持的体系结构,例如i386就是关于intel

cpu及与之相兼容体系结构的子目录。PC机一般都基于此目录;

●Include: include子目录包括编译核心所需要的大部分头文件。与平台无关的头文件在 include/linux子目录下,与 intel

cpu相关的头文件在include/asm-i386子目录下,而include/scsi目录则是有关scsi设备的头文件目录;

●Init: 这个目录包含核心的初始化代码(注:不是系统的引导代码),包含两个文件main.c和Version.c,这是研究核心如何工作的一个非常好的起点。

●Mm :这个目录包括所有独立于 cpu

体系结构的内存管理代码,如页式存储管理内存的分配和释放等;而和体系结构相关的内存管理代码则位于arch/*/mm/,例如arch/i386/mm/Fault.c

●Kernel:主要的核心代码,此目录下的文件实现了大多数linux系统的内核函数,其中最重要的文件当属sched.c;同样,和体系结构相关的代码在arch/*/kernel中;

●Drivers: 放置系统所有的设备驱动程序;每种驱动程序又各占用一个子目录:如,/block

下为块设备驱动程序,比如ide(ide.c)。如果你希望查看所有可能包含文件系 统的设备是如何初始化的,你可以看drivers/block/genhd.c中的device_setup()。它不仅初始化硬盘,也初始化网络,因为 安装nfs文件系统的时候需要网络其他:

如, Lib放置核心的库代码; Net,核心与网络相关的代码; Ipc,这个目录包含核心的进程间通讯的代码; Fs

,所有的文件系统代码和各种类型的文件操作代码,它的每一个子目录支持一个文件系统,例如fat和ext2;

Scripts, 此目录包含用于配置核心的脚本文件等。

一般,在每个目录下,都有一个 .depend 文件和一个 Makefile

文件,这两个文件都是编译时使用的辅助文件,仔细阅读这两个文件对弄清各个文件这间的联系和依托关系很有帮助;而且,在有的目录下还有Readme

文件,它是对该目录下的文件的一些说明,同样有利于我们对内核源码的理解;

二.解读实战:为你的内核增加一个系统调用

虽然,Linux 的内核源码用树形结构组织得非常合理、科学,把功能相关联的文件都放在同一个子目录下,这样使得程序更具可读性。然而,Linux

的内核源码实在是太大而且非常复杂,即便采用了很合理的文件组织方法,在不同目录下的文件之间还是有很多的关联,分析核心的一部分代码通常会要查看其它的几个相关的文件,而且可能这些文件还不在同一个子目录下。

体系的庞大复杂和文件之间关联的错综复杂,可能就是很多人对其望而生畏的主要原因。 当然,这种令人生畏的劳动所带来的回报也是非常令人着迷的:你不仅可以从中学到很多的计算机的底层的知识(如下面将讲到的系统的引导),体会到整个操作系 统体系结构的精妙和在解决某个具体细节问题时,算法的巧妙;而且更重要的是:在源码的分析过程中,你就会被一点一点地、潜移默化地专业化;甚至,只要分析 十分之一的代码后,你就会深刻地体会到,什么样的代码才是一个专业的程序员写的,什么样的代码是一个业余爱好者写的。

为了使读者能更好的体会到这一特点,下面举了一个具体的内核分析实例,希望能通过这个实例,使读者对Linux的内核的组织有些具体的认识,从中读者也可以学到一些对内核的分析方法。

以下即为分析实例:

A、操作平台:

硬件:cpu intel Pentium II ;

软件:Redhat Linux 6.0; 内核版本2.2.5

B、相关内核源代码分析:

1.系统的引导和初始化:Linux 系统的引导有好几种方式:常见的有 Lilo,

Loadin引导和Linux的自举引导(bootsect-loader),而后者所对应源程序为arch/i386/boot/bootsect.S,它为实模式的汇编程序,限于篇幅在此不做分析;无论是哪种引导方式,最后都要跳转到

arch/i386/Kernel/setup.S, setup.S主要是进行时模式下的初始化,为系统进入保护模式做准备;此后,系统执行

arch/i386/kernel/head.S (对经压缩后存放的内核要先执行 arch/i386/boot/compressed/head.S);

head.S 中定义的一段汇编程序setup_idt ,它负责建立一张256项的 idt 表(Interrupt Descriptor

Table),此表保存着所有自陷和中断的入口地址;其中包括系统调用总控程序 system_call

的入口地址;当然,除此之外,head.S还要做一些其他的初始化工作;

2.系统初始化后运行的第一个内核程序asmlinkage void __init start_kernel(void) 定义在

/usr/src/linux/init/main.c中,它通过调用usr/src/linux/arch/i386/kernel/traps.c 中的一个函数

void __init trap_init(void) 把各自陷和中断服务程序的入口地址设置到 idt

表中,其中系统调用总控程序system_cal就是中断服务程序之一;void __init trap_init(void) 函数则通过调用一个宏

set_system_gate(SYSCALL_VECTOR,&system_call); 把系统调用总控程序的入口挂在中断0x80上;

其中SYSCALL_VECTOR是定义在 /usr/src/linux/arch/i386/kernel/irq.h中的一个常量0x80; 而

system_call

即为中断总控程序的入口地址;中断总控程序用汇编语言定义在/usr/src/linux/arch/i386/kernel/entry.S中;

3.中断总控程序主要负责保存处理机执行系统调用前的状态,检验当前调用是否合法, 并根据系统调用向量,使处理机跳转到保存在 sys_call_table

表中的相应系统服务例程的入口; 从系统服务例程返回后恢复处理机状态退回用户程序;

而系统调用向量则定义在/usr/src/linux/include/asm-386/unistd.h 中;sys_call_table

表定义在/usr/src/linux/arch/i386/kernel/entry.S 中; 同时在

/usr/src/linux/include/asm-386/unistd.h 中也定义了系统调用的用户编程接口;

4.由此可见 , linux 的系统调用也象 dos 系统的 int 21h 中断服务, 它把0x80 中断作为总的入口, 然后转到保存在

sys_call_table 表中的各种中断服务例程的入口地址 , 形成各种不同的中断服务;

由以上源代码分析可知, 要增加一个系统调用就必须在 sys_call_table 表中增加一项 ,

并在其中保存好自己的系统服务例程的入口地址,然后重新编译内核,当然,系统服务例程是必不可少的。

由此可知在此版linux内核源程序中,与系统调用相关的源程序文件就包括以下这些:

arch/i386/boot/bootsect.S

arch/i386/Kernel/setup.S

arch/i386/boot/compressed/head.S

arch/i386/kernel/head.S

init/main.c

arch/i386/kernel/traps.c

arch/i386/kernel/entry.S

arch/i386/kernel/irq.h

include/asm-386/unistd.h

当然,这只是涉及到的几个主要文件。而事实上,增加系统调用真正要修改文件只有include/asm-386/unistd.h和arch/i386/kernel/entry.S两个。

C、对内核源码的修改:

1.在kernel/sys.c中增加系统服务例程如下:

asmlinkage int sys_addtotal(int numdata)

{

int i=0,enddata=0;

while(i<=numdata)

enddata+=i++;

return enddata;

}

该函数有一个 int 型入口参数 numdata , 并返回从 0 到 numdata 的累加值;

当然也可以把系统服务例程放在一个自己定义的文件或其他文件中,只是要在相应文件中作必要的说明;

2.把 asmlinkage int sys_addtotal( int) 的入口地址加到sys_call_table表中:

arch/i386/kernel/entry.S 中的最后几行源代码修改前为:

... ...

.long SYMBOL_NAME(sys_sendfile)

.long SYMBOL_NAME(sys_ni_syscall) /* streams1 */

.long SYMBOL_NAME(sys_ni_syscall) /* streams2 */

.long SYMBOL_NAME(sys_vfork) /* 190 */

.rept NR_syscalls-190

.long SYMBOL_NAME(sys_ni_syscall)

.endr

修改后为: ... ...

.long SYMBOL_NAME(sys_sendfile)

.long SYMBOL_NAME(sys_ni_syscall) /* streams1 */

.long SYMBOL_NAME(sys_ni_syscall) /* streams2 */

.long SYMBOL_NAME(sys_vfork) /* 190 */

/* add by I */

.long SYMBOL_NAME(sys_addtotal)

.rept NR_syscalls-191

.long SYMBOL_NAME(sys_ni_syscall)

.endr

3. 把增加的 sys_call_table 表项所对应的向量,在include/asm-386/unistd.h

中进行必要申明,以供用户进程和其他系统进程查询或调用:

增加后的部分 /usr/src/linux/include/asm-386/unistd.h 文件如下:

... ...

#define __NR_sendfile 187

#define __NR_getpmsg 188

#define __NR_putpmsg 189

#define __NR_vfork 190

/* add by I */

#define __NR_addtotal 191

4.测试程序(test.c)如下:

#include

#include

_syscall1(int,addtotal,int, num)

main()

{

int i,j;

do

printf("Please input a number\n");

while(scanf("%d",&i)==EOF);

if((j=addtotal(i))==-1)

printf("Error occurred in syscall-addtotal();\n");

printf("Total from 0 to %d is %d \n",i,j);

}

对修改后的新的内核进行编译,并引导它作为新的操作系统,运行几个程序后可以发现一切正常;在新的系统下对测试程序进行编译(*注:由于原内核并未提供此系统调用,所以只有在编译后的新内核下,此测试程序才能可能被编译通过),运行情况如下:

$gcc -o test test.c

$./test

Please input a number

36

Total from 0 to 36 is 666

可见,修改成功;

而且,对相关源码的进一步分析可知,在此版本的内核中,从/usr/src/linux/arch/i386/kernel/entry.S

文件中对 sys_call_table 表的设置可以看出,有好几个系统调用的服务例程都是定义在/usr/src/linux/kernel/sys.c

中的同一个函数:

asmlinkage int sys_ni_syscall(void)

{

return -ENOSYS;

}

例如第188项和第189项就是如此:

... ...

.long SYMBOL_NAME(sys_sendfile)

.long SYMBOL_NAME(sys_ni_syscall) /* streams1 */

.long SYMBOL_NAME(sys_ni_syscall) /* streams2 */

.long SYMBOL_NAME(sys_vfork) /* 190 */

... ...

而这两项在文件 /usr/src/linux/include/asm-386/unistd.h 中却申明如下:

... ...

#define __NR_sendfile 187

#define __NR_getpmsg 188 /* some people actually want streams */

#define __NR_putpmsg 189 /* some people actually want streams */

#define __NR_vfork 190

由此可见,在此版本的内核源代码中,由于asmlinkage int sys_ni_syscall(void) 函数并不进行任何操作,所以包括 getpmsg,

putpmsg 在内的好几个系统调用都是不进行任何操作的,即有待扩充的空调用;

但它们却仍然占用着sys_call_table表项,估计这是设计者们为了方便扩充系统调用而安排的;

所以只需增加相应服务例程(如增加服务例程getmsg或putpmsg),就可以达到增加系统调用的作用。

Tuesday, 4 December 2007

Comand Line Process Bar for Java

As we all know python can print without change line. Now the following codes shows how to do it in Java.

###############################################
"""sc.py
This is a simple class to demonstrate the use of jythonc.
"""

import java.lang.String

class sc(java.lang.Object):
def __init__(self):
"""public sc() # constructor is not forced to add'@sig'
"""

def tprint(self,c='hello'):
"""@sig public void tprint(String c)
"""
print c,

if __name__ == '__main__':
sc1 = sc()
sc1.tprint('zhengxin')


################################################
// Simpleclasstest.java

import java.lang.*;

public class Simpleclasstest {
public static void main(String[] args) {
sc sc1 = new sc();
for (int i = 1;i<100; i++)
{ try { Thread.sleep(200); } catch (InterruptedException e) {};
sc1.tprint("--");
}
System.out.println('\n');
}
}
#########################################
What to do now:
$jythonc sc.py # to generate java class.
$javac -cp /usr/share/jython/jython.jar:./jpywork Simpleclasstest.java # using Simpleclasstest.java to call the jython generated class.
$java -cp /usr/share/jython/jython.jar:.:./jpywork Simpleclasstest # run it.
=========================================================
Here is the way to achieve it in pure Java. and this java Class can be called by matlab to show process bar.
#########################################
//tryoutput.java
public class tryoutput {
String s;
public void setStr(String s)
{ this.s = s; }
public void tPrint()
{ try { Thread.sleep(200); } catch (InterruptedException e) {};
System.out.print(this.s);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
tryoutput op = new tryoutput();
op.setStr("=");
for ( int i=0; i<100; i++) {
op.tPrint();
} } }
#####################################

Comments:
Here for both java and python, the loop can be put in the tprint methods. Same effect.

python: import and Class and dir()

Python:
No matter what the current directory is, Python must import the things it need.
---------------------------------------------------------
Java:
If the class or jar is in the CLASSPATH, import will cause errors like: "import abc; something is expected"

############################
Import for Python:

  • What is imported is just the module name. If in the module a class is defined, to get a instance of the class, you must
    >>>import abc # here abc is the module file name
    >>>instance=abc.Cons() # here Cons is the class constructor; for jython, abc=Cons, it will looks like >>>instance=abc.abc()
  • >>>dir(instance)
    [] # this is just what you got when you want to check what methods is available for the object. If you want to achieve it, you should run:
    >>>dir(abc.Cons) # in Jython it should be: >>>dir(abc.abc)

Monday, 3 December 2007

Notes for Unix Course

Commands:

  • ls -R: to recursively descend subdirectory tress

    ls -i: to get inode number for each file.

    ls -l: can list 'link count'. This shows how many files are linked to one file ( same inode number)
  • find / -inum XXXXX -ls: here ls is similar to -print; using this way we can find what files are linked to same file (for hard link).

    $touch -m -t 200712030900 timewanted # generate a file with specified time stamp.
    $find / -newer timewanted -ls # show the files modified after the specified time.


    find / -mount -name "XXXXXXX" -ls: here -mount means NOT scan mounted file system. Especially useful for NOT scan mounted iso files, mounted Windows file system and some network or mirrors storage.
  • 2> /dev/null: this always follows shell command to redirect standard error output. while "1>" for standard output redirection. /dev/null doesn't generate any file, just get rid of error messages. For example: $ls -il /bin/ls # we will get the inode number XXXand link count, if the ls has been hard linked to other files, use the following command find what they are; $find / -mount -inum XXX 2> /dev/null. # using this command we will not see the permission error messages.
  • ps ax | head -123 # head is used to show first 123 lines.
  • tail +123 # to show lines from 123 lines
    tail -123 # to show the last 123 liens
    tail -f /var/log/messages # to monitor the messages file in real time.
  • cp *.[ch] ~ # use regular expression to specify what kind of file you want to copy. Here means copy .c and .h files to home folder.
  • umask: displays or changes the permissions that are taken away, i.e., masked out, from the defaults requested at file creation.
  • ps:
    ps -e: processes of everybody
    ps e: command arguments in detail
    ps l: in long format, similar as ls -l.
    ps -o pid,state,args: to assign what information to output, here -o means options.
    ps -O pri: preloaded -o, pri is added to show basing on the default output.
    state: S=sleeping,R=running,T=stop(because S has assigned to sleeping, so use the second letter T for STOP)
    ppid: parent pid. from ps -l , the ppid can be got. it means all the process with same ppid are the child process of the process, whose pid is ppid. If kill this parent process, all the child process will be killed as well.
  • kill -SIG pid: totally33 kinds of Signal for pid.
  • job control:
    prog & # run a program in the background.
    jobs -l # list current background jobs. -l can return pids as well as job numbers.
    : move the current foreground job into the background and STOP it.
    bg %2: RESUME job number 2 in the background.
    fg %2: Resume job number 2 in the foreground.
    wait %2: wait job number 2 to complete.
    kill %2: terminate job number 2.
  • tar -tvf /the/tar/file.tar: list the contents in the tar file while not decompress it.
  • vi: ":file": return the some file information, including total lines.
  • awk: Command line text tool (especially edit file or texts with columns)
    $awk -F':' '{ printf "%-10s%5d\n", $1, $3 }' /etc/passwd
    Here, printf "%-10s%5d\n", $1, $3 -- the action, it's the format string of two arguments to printf, in the case,$1 is the username column in the password file, '-' means left justified. ':' is the separator.
    $fs=$(mount | awk ' $5 =="ext3" { print $3 }')
    Here, shows mounted file systems and we are interested in those whose file system typ is ext3, in which case we print the mount point ($3).


Regular Expression:


symbols meaning example matches
---------------------------------------------------------------
. any 1 char pean.t peanut
[] 1 character set waln[aeiou]t walnet
[-] 1 character range pec[a-l]n pecen
[^] not in set alm[^Oo] almend
* 0 or more instances penut peeeeenut
^ begin line ^begin beginning of line
$ end line end$ line end
\ quote next n\-\[\$ n-[$

.* any string n.*s newts;ns
[]* string from set ba[na]* banana
[ - - ] multiple ranges nu[m-os-u]s nuns

USING WITH "grep","sed"
--------------------------------
\{m\} m occurrences [m-os-u]\{4\} stun
\{m,\} m or more occurrences x[0-9]\{3,\}y x121345y
\{m,n\} m to n occurrences x[0-9]\{3,7\}y x1213458y

USING WITH "egrep","awk"
--------------------------------
? 0 or 1 instance chest?nut chesnut;chestnut
+ 1 or more instance p[ae]+nut paenut;peeenut
| alternate RE pattern Wal|Pea Wal or Pea
( ) RE group (Wal|Pea|Coco)nut walnut;Peanut;Coconut

REPLACEMENT STRING USING WITH "ex","sed"
--------------------------------

\( \) save match \(..\)\(.*\)
\n nth saved match s/\(..\)/\2--\1/

Examples:
$echo TUNUTS | sed 's/\(..\)/\2--\1/'
NUTS--TU

$sed -e 's/abc/def/g' -e 's/hij/klm/g' ./where/the/file # In the file, use def to replace abc, and use klm to replace hij.


Shell Programming:

  • '${varible}' is good practice
  • Judge statement:
    [[ ]] for string compare, including "abc" and numeric string "12", here x=9 and x="9" are same.
    (( )) for numerical compare.
    • Be careful of space in the judge statement. [[ $x = 9 ]]. there are " space " around [ ] =, same with (( )).
    • but when you assign x=9, there can not be space!!!!
    • [[ $x -eq 9 ]] , here -eq just for [[]].
  • $x=9
    $[[ $x == 9 ]]
    $echo $? # here $? shows correct or not. or the command runs successfully or not.
  • shell scripts: exit can be put at the last line. if 'exit 9' at the last line. In the shell, $echo $? will return 9.
  • A shell script called 'test', after it runs, $./run , you can not get any variables generated in the script. Because it runs in the sub shell. $. ./run will give you the variable, because, '.' means run the script in the current shell.
  • Subshell. in scripts
    for f in $(ls | grep -v 'gz$')
    do
    ls -l ${f)
    done
    ###this script just shows the files except .gz files. Here sub shell similar to pipe.

C header definition and declaration

Structuring header files

  > In the past, I've been told that for every .c file, you should
> have a corresponding .h file which will contain all the necessary
> definitions, prototypes, etc... for the functions in the file.
> Is this the case, or should we just use one global header file
> that contains all the prototypes, externs, defines, etc...

The rules are not carved in stone, and should be followed with a grain of
salt in all specific cases, but here's the usual story:

a) It's always a good idea to play safe against possible multiple inclusions
of the same .h file. This can happen if you #include "foo.h" ,"bar.h", and
bar.h has an "#include "foo.h" line itself.
The canonical way to prevent multiple inclusions is to wrap each header
with an #ifndef CONSTANT directive, and #define that constant in the file
(immediately after). Example:

/* This is myheader.h */
#ifndef _MYHEADER_
#define _MYHEADER_

/* All the rest of the header here */

#endif /* _MYHEADER_ */
/* End of myheader.h */

The preprocessor will access the "rest of the header" only the first time
the header is included. The second time the _MYHEADER_ constant will be
already defined, causing #ifndef to be false, and the "rest" to be skipped
up to the #endif /* _MYHEADER_ */ line, that is up to the end.

b) .c files should contain relatively small sets of tightly related
functions. Their declarations go in a .h file with the same name, to
be included by all the other .c files that use those functions.

c) Type definitions (typedef, struct) must go in .h files, as soon as they are
used by more than one .c file. Defined type names should visually suggest
that they are not base types (like int or float). An often used convention
is to write their names using caps:
typedef int* Foo;
typedef float GlobeTrotter[3];
typedef struct _BarfBeer {
int a,b;
struct _BarfBeer* next; /* For a linked list */
} BarfBeer;

d) Preprocessor directives defining constants and macros go in .h files as
soon as the constants and macros are used by more than one function (it
annoys me to have to fish through a .c file for the definition of a macro,
and I'd much rather see it in a short .h). Constants used by just one
function are #define-d right before it in the .c file, and #undef-ined
immediately after. Defined constant names should be ALL_IN_CAPS

e) Global variables are declared with the "extern" storage qualifier in .h
files, which are included by all the .c files that need those
globals. There must be only ONE definition for each global variable (i.e. a
declaration without "extern"), and it MUST be in a .c file. If you put it
in a header, you get a multiple definition error as soon as that header is
included by more than one .c file. It doesn't hurt, and greatly helps
code readability, to redeclare at the beginning of a function all extern
variables used by that funciton (of course using the "extern" storage
qualifier).

f) If at all possible, do not include header files in header files, and let
only the .c guys do all the #include-ing.
A non ``flat'', hyerarchical inclusion structure is more suited to C++.

P.S. A "storage qualifier" is a keyword specifying the type of storage needed
for a certain variable. Example of C storage qualifiers are: static,

extern, register, volatile.

#######################################################

Extern Variables and Functions

Because a global variable may be defined in one file and referred to in other files, some means of telling the compiler that the variable is defined elsewhere may be needed. Otherwise, the compiler may object to the variable as undefined. This is facilitated by an extern declaration. For example, the declaration

extern int size; // variable declaration

informs the compiler that size is actually defined somewhere (may be later in this file or in another file). This is called a variable declaration (not definition) because it does not lead to any storage being allocated for size.

It is a poor programming practice to include an initializer for an extern variable, since this causes it to become a variable definition and have storage allocated for it:

extern int size = 10; // no longer a declaration!

If there is another definition for size elsewhere in the program, it will eventually clash with this one.

Function prototypes may also be declared as extern, but this has no effect when a prototype appears at the global scope. It is more useful for declaring function prototypes inside a function. For example:

double Tangent (double angle)

{

extern double sin(double); // defined elsewhere

extern double cos(double); // defined elsewhere

return sin(angle) / cos(angle);

}

The best place for extern declarations is usually in header files so that they can be easily included and shared by source files.



Sunday, 2 December 2007

jython call 3rd party java class file

------------------------------
Accessible Class
------------------------------
Every functions (including constructor) and the class declaration must start with public.

------------------------------
Jython call class Notes
------------------------------
For instance, if you want to initialize an object form your class, you should run
>>>gc = GetConf();
the '()' is needed! Just remember, python loves (), the basic function dir() must followed ().

------------------------------
About Path
------------------------------
1. in terminal, go to the folder where the class file is. then run jython,and import the name of the calss. Now you can get a instance of that.

2. at runtime, you could do:

>>> import sys
>>> sys.path.append('/path/to/module')


A few rules about CLASSPATH and python.path:

  • sys.path in the registry file -- Add here to enable importing from Java classes (.java), Java class libraries (.jar), and Jython/Python (.py).
  • CLASSPATH -- Add here to enable importing from Java classes (.java) and Java class libraries (.jar), but not Jython/Python (.py).

Saturday, 1 December 2007

Matlab call java "class"

1. Every methods should be public, especially the constructor. Otherwise the class can not be called.
Programming only within java, not so strict. Even you don't need put any identifier in front classes and functions.

2. Matlab and Sun Java are not compatible completely, though 99% do, especially when Sun Java or Matlab update, issues always come out. But I found Matlab call the java classes generated by gcc-java(gcj) works much better.
When Matlab updates, it almost work on or for latest java jdk. Matlab 2007a can not call the java class generated by jdk 1.6.03, while Matlab 2007b can.

3. Remember the javaclasspath variable in Matlab. To use javaaddpath(pwd) for you Matlab functions. If new class file is added, run clear;clc; again to initialize the new class.

3.2. About compiling
Put the class file in the static javaclasspath (which is set in /toolbox/local/classpath.txt), in this way it will much faster than javaaddpath(pwd). But you much also put the class file in the classpath of MCR, /toolbox/local/classpath.txt, in this file, there still saying "$matlabroot" things, but here $matlabroot is mean . Just put the class in the mcr classpath folder, no need to be same with .

4.Can not get the attributes from the returned object directly, you need to compose a getXXXX(){return XXXXXX} method by yourself.

5. mcc -m XX.m -a YY.txt
If the XX.m doesn't call java class (including Matlab embeded java functions, here try to use import less, which always cause error when the m-file is compiled), the YY.txt added by mcc without format changed will be able to be accessed, it is stored in XX_mcr/XX/. If not, the YY.txt can not be accessed. You must copy the YY.txt manually to the XX.exe folder.
Just be careful the added files when you mcc Matlab programs using java.

The attached code is to read config file abc.txt.
##################################
// GetConf.java
// GetConf.java
import java.util.*;
import java.io.*;

public class GetConf {
//the public is not necessary for Matlab, but for jython, this is essential.

String para;
Properties conf;

public GetConf() throws IOException{
conf = new Properties();
para = new String();
File file_name = new File("abc.txt");

FileInputStream conf_input;

try {
conf_input = new FileInputStream(file_name);
conf.load(conf_input);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

}

public String getOnePara(String pnm){
para = conf.getProperty(pnm);
para = para.trim();
return para;
}

public static void main(String[] args) throws IOException{
String pa = (new GetConf()).getOnePara("a");
System.out.println(pa);
}

}
##################################
Here is the config file abc.txt
-------------------------------------------------
# a comment
! a comment

a = a string
b = a string with escape sequences \t \\ \" \' \ (space) \u0123
c = a string with a continuation line \
continuation line
d.e = another string
------------------------------------------------

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter