2005年11月28日 星期一

Social Network Theory

Social Network Theory
Theory Used

由 beat 发表于 下午08点59分 | 回复 (0)

2005年11月26日 星期六

PostgreSQL扩展C函数需要注意的几个问题

要包含的头文件:
"postgres.h"
"fmgr.h"

动态内存分配,必须使用其自带的palloc,pfree进行内存分配
对于text*等动态类型,
text * p=(text*)palloc(n);之后还需要
VARATT_SIZEP(p)=n;
否则,用VARSIZE(p)看到的大小将是可用内存的最大值。

对于基本类型,如char *等,则不存在此问题

可变长度类型的结构包含两部分VARHDR和VARDATA
一般情况下,VARHDRSZ为4个字节长度。

由 beat 发表于 上午05点25分 | 回复 (56)

2005年11月23日 星期三

被CIA盯上了,好怕怕!

发件人: Department@cia.gov
收件人: ThisAccount@eyou.com
主题: You_visit_illegal_websites
日期: Wed, 23 Nov 2005 00:08:08 GMT (08:08 CST)

Dear Sir/Madam,

we have logged your IP-address on more than 30 illegal Websites.

Important:
Please answer our questions!
The list of questions are attached.


Yours faithfully,
Steven Allison

++++ Central Intelligence Agency -CIA-
++++ Office of Public Affairs
++++ Washington, D.C. 20505

++++ phone: (703) 482-0623
++++ 7:00 a.m. to 5:00 p.m., US Eastern time

由 beat 发表于 下午07点56分 | 回复 (0)

2005年11月22日 星期二

中奖啦,哈哈

发件人: staatslotery.promotions@virgilio.it
收件人:
抄送:
邮件主题: We Are Pleased To Inform You!!!
发件日期: Mon, 21 Nov 2005 10:44:59 +0100 (GMT+01:00)

来信内容:


We Are Pleased To Inform You!!!

STAATSLOTERIJ INTERNATIONAL
PROMO.
Adres:Paleisstraat 52515 JA Den Haag.

from: the
DIRECTOR,
INTERNATIONAL PROMOTIONS DEPT.
E-mail; staatswinclaim@walla.
com
Website: www.staatsloterij.nl


REF Number: NM/BC921245/KY14,
BATCH No: NM/207161/WOP.
TICKET No: 46939
Serial No: 472-9768
lUCKY No: W-91237-H°67/B4


ATTN: WINNING NOTIFICATION!!!!!!!


We
happily announce to you the draw of the Netherlands Loterij
International programs held on the 19th of November 2005 in Netherlands.
Your e-mail address attached to ticket number: 46939 with Serial number
472-9768 drew the lucky numbers: W-91237-H°67/B4, which
subsequently
won you the lottery in the B category.You have therefore been approved
to claim a total sum of 1,000,000.00 (One million Euros in cash
credited to file KPC/9080118308/02/TCA. This is from a total cash
prize of 10,000,000.00 Million Euros shared amongst the first Ten (10)
lucky winners in this category B + (Plus).

This lottery is a
promotional program by Staatsloterij , (Biggest lottery Organization in
the Netherlands) to advertise to the world its existence. All
participants were selected through a computer ballot system drawn from
over 50,000 companies and 2,000,000 individual email addresses from all
over the world, as part of our international promotions program,This
promotion takes place annually. For security reasons, you are advised
to keep your winning information confidential till your claims is
processed and your money remitted to you in whatever manner you deem
fit to claim your prize. This is a part of our precautionary measure to
avoid double claiming and unwarranted abuse of this program by some
unscrupulous elements.

NOTE: All winnings must be notarized and a
certificate of award must be obtained from the Netherlands Gaming
Control Board to complete the claims process, this certificate can only
be obtained through legal representation so winners will be referred to
our Regional Director to
assist in this process. Winners are to cover
the legal charges for the notarization of the claims form and the
acquisition of the certificate of award not STAATSLOTERIJ.
To file for
your claim, please contact our Regional Office with the details below
for processing and release of your winning.
***********************************************************
Name;Mr.
MARK JEFFERSON
TEL: +31-643- 522-754
Fax: +31-847-220-937,
Fax: +31-
847-566-395.
Email:staatswinclaim@walla.com

STAATSLOTERIJ REDEMPTION
CENTRE, AMSTERDAM.THE NETHERLANDS.
***********************************************************
Note that
all claims process and clearance procedures must be duly completed not
later than 10th of December 2005 to avoid impersonation arising to the
issue of double claim. To avoid unnecessary delays and complications,
please quote your reference/batch numbers in any correspondences with
us or our designated agent.

In order to avoid unnecessary delays and
complications, provide the following when replying:
Name:
Nationality:
Country of residence:
Tel number:

Delays will compel us to disperse
your funds, in other words, we shall divert your funds as stated above.
Kindly remember to quote your reference number in all correspondence.
Finally, we call on you to make sure that you save a copy of this mail
and note every letter clearly as stated for we will not be held
responsible should there be any complications in this transaction due
to laxity on your part.

Congratulations once more from our members
of staff and thank you for being part of our promotional program.


Yours truly,
Wendy Marriott.
Staatsloterij International.

NOTICE: You have received this message from Staatsloterij prize dept
at your e-mail address, because you have visited our sponsored sites
and have voluntarily given your email address to receive mails from
their sponsors.If you wish to be taken out of this list do not reply to
this mail, reply to the agent with the words remove. If you are not
the intended recipient, you must not, directly or indirectly, use,
disclose, distribute, print or copy any part of this message. If you
believe you have received this message in error, please delete it and
all copies of it from your system and notify the sender immediately by
reply e-mail. Thank you.



由 beat 发表于 下午11点14分 | 回复 (0)

2005年11月15日 星期二

Linux下常用压缩格式的压缩与解压

.tar
解包:tar xvf FileName.tar
打包:tar cvf FileName.tar DirName
(注:tar是打包,不是压缩!)
---------------------------------------------
.gz
解压1:gunzip FileName.gz
解压2:gzip -d FileName.gz
压缩:gzip FileName
.tar.gz 和 .tgz
解压:tar zxvf FileName.tar.gz
压缩:tar zcvf FileName.tar.gz DirName
---------------------------------------------
.bz2
解压1:bzip2 -d FileName.bz2
解压2:bunzip2 FileName.bz2
压缩: bzip2 -z FileName
.tar.bz2
解压:tar jxvf FileName.tar.bz2
压缩:tar jcvf FileName.tar.bz2 DirName
---------------------------------------------
.bz
解压1:bzip2 -d FileName.bz
解压2:bunzip2 FileName.bz
压缩:未知
.tar.bz
解压:tar jxvf FileName.tar.bz
压缩:未知
---------------------------------------------
.Z
解压:uncompress FileName.Z
压缩:compress FileName
.tar.Z
解压:tar Zxvf FileName.tar.Z
压缩:tar Zcvf FileName.tar.Z DirName
---------------------------------------------
.zip
解压:unzip FileName.zip
压缩:zip FileName.zip DirName
---------------------------------------------
.rar
解压:rar x FileName.rar
压缩:rar a FileName.rar DirName

rar请到:http://www.rarsoft.com/download.htm 下载!
解压后请将rar_static拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp rar_static /usr/bin/rar
---------------------------------------------
.lha
解压:lha -e FileName.lha
压缩:lha -a FileName.lha FileName

lha请到:http://www.infor.kanazawa-it.ac.jp/~ishii/lhaunix/下载!
>解压后请将lha拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp lha /usr/bin/
---------------------------------------------
.rpm
解包:rpm2cpio FileName.rpm | cpio -div
---------------------------------------------
.deb
解包:ar p FileName.deb data.tar.gz | tar zxf -
---------------------------------------------
.tar .tgz .tar.gz .tar.Z .tar.bz .tar.bz2 .zip .cpio .rpm .deb .slp
.arj .rar .ace .lha .lzh .lzx .lzs .arc .sda .sfx .lnx .zoo .cab .kar .cpt .pit .sit .sea
解压:sEx x FileName.*
压缩:sEx a FileName.* FileName

sEx只是调用相关程序,本身并无压缩、解压功能,请注意!
sEx请到: http://sourceforge.net/projects/sex下载!
解压后请将sEx拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp sEx /usr/bin/

由 beat 发表于 上午10点40分 | 回复 (0)

2005年11月13日 星期日

编写Linux下的Daemon程序

编写Linux下的Daemon程序 作者: dagger 无崖阁 xyg.ods.org
版本: 1.0 2003-04-08 初始版本
一、引言 Daemon程序是一直运行的服务端程序,又称为守护进程。
本文介绍了在Linux下编写Daemon程序的步骤,并给出了例子程序。
二、Daemon程序简介 Daemon是长时间运行的进程,通常在系统启动后就运行,
在系统关闭时才结束。一般说Daemon程序在后台运行,是因为它没有控制终端,
无法和前台的用户交互。Daemon程序一般都作为服务程序使用,等待客户端程序
与它通信。我们也把运行的Daemon程序称作守护进程。
三、Daemon程序编写规则
编写Daemon程序有一些基本的规则,以避免不必要的麻烦。
1、首先是程序运行后调用fork,并让父进程退出。子进程获得一个新的进程ID,
但继承了父进程的进程组ID。
2、调用setsid创建一个新的session,使自己成为新session和新进程组的
leader,并使进程没有控制终端(tty)。
3、改变当前工作目录至根目录,以免影响可加载文件系统。或者也可以改变到
某些特定的目录。
4、设置文件创建mask为0,避免创建文件时权限的影响。
5、关闭不需要的打开文件描述符。因为Daemon程序在后台执行,不需要于终端
交互,通常就关闭STDIN、STDOUT和STDERR。其它根据实际情况处理。
另一个问题是Daemon程序不能和终端交互,也就无法使用printf方法输出信息
了。我们可以使用syslog机制来实现信息的输出,方便程序的调试。在使用
syslog前需要首先启动syslogd程序,关于syslogd程序的使用请参考它
的man page,或相关文档,我们就不在这里讨论了。
四、一个Daemon程序的例子 编译运行环境为Redhat Linux 8.0。
我们新建一个daemontest.c程序,文件内容如下:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <signal.h>

int daemon_init(void)
{ pid_t pid;
if((pid = fork()) < 0) return(-1);
else if(pid != 0) exit(0); /* parent exit */
/* child continues */
setsid(); /* become session leader */
chdir("/"); /* change working directory */
umask(0); /* clear file mode creation mask */
close(0); /* close stdin */
close(1); /* close stdout */
close(2); /* close stderr */
return(0); }
void sig_term(int signo)
{ if(signo == SIGTERM)
/* catched signal sent by kill(1) command */
{ syslog(LOG_INFO, "program terminated.");
closelog(); exit(0); }
}
int main(void)
{ if(daemon_init() == -1)
{ printf("can't fork self\n"); exit(0); }
openlog("daemontest", LOG_PID, LOG_USER);
syslog(LOG_INFO, "program started.");
signal(SIGTERM, sig_term); /* arrange to catch the signal */
while(1) { sleep(1); /* put your main program here */ }
return(0); }
使用如下命令编译该程序: gcc -Wall -o daemontest daemontest.c
编译完成后生成名为daemontest的程序,执行./daemontest来测试程序的运行。
使用ps axj命令可以显示系统中已运行的daemon程序的信息,包括进程ID、
session ID、控制终端等内容。
部分显示内容:
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
1098 1101 1101 1074 pts/1 1101 S 0 0:00 -bash 1 1581 777 777 ? -1 S 500 0:13 gedit 1 1650 1650 1650 ? -1 S 500 0:00 ./daemontest 794 1654 1654 794 pts/0 1654 R 500 0:00
ps axj 从中可以看到daemontest程序运行的进程号为1650。
我们再来看看/var/log/messages文件中的信息: Apr 7 22:00:32 localhost
daemontest[1650]: program started.
显示了我们在程序中希望输出的信息。
我们再使用kill 1650命令来杀死这个进程,
/var/log/messages文件中就会有如下的信息:
Apr 7 22:11:10 localhost daemontest[1650]: program terminated.
使用ps axj命令检查,发现系统中daemontest进程已经没有了。
五、参考资料
Advanced Programming in the UNIX Environment W.Richard Stevens

由 beat 发表于 下午06点44分 | 回复 (0)

Daemon是怎样练成的

守护进程

守护进程是生存期长的一种进程。它们独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。他们常常在系统引导装入时启动,在系统关闭时终止。unix系统有很多守护进程,大多数服务器都是用守护进程实现的。比如,网络服务inetd、Web服务http等。同时,守护进程完成许多系统任务。比如,作业规划进程crond、打印进程lqd等。

这里主要说明守护进程的进程结构,以及如何编写守护进程程序。因为守护进程没有控制终端,所以我们还要介绍在守护进程运行时错误输出的方法。

守护进程及其特性

守护进程最重要的特性是后台运行。在这一点上,DOS下的常驻内存程序TSR与之相似。

其次,守护进程必须与其运行前的环境隔离开来。这些环境包括未关闭的文件描述符、控制终端、会话和进程组、工作目录以及文件创建掩码等。这些环境通常是守护进程从执行它的父进程(特别是shell)中继承下来的。

最后,守护进程的启动方式有其特殊之处。它可以在系统启动时从启动脚本/etc/rc.d中启动,可以由inetd守护进程启动,可以有作业规划进程crond启动,还可以由用户终端(通常是shell)执行。

总之,除开这些特殊性以外,守护进程与普通进程基本上没有什么区别。因此,编写守护进程实际上是把一个普通进程按照上述的守护进程的特性改造成为守护进程。如果大家对进程的认识比较深入,就对守护进程容易理解和编程了。

首先我们来察看一些常用的系统守护进程,看一下他们和几个概念:进程组、控制终端和对话期有什么联系。p s命令打印系统中各个进程的状态。该命令有多个选择项,有关细节请参考系统手册。为了察看所需的信息,执行:
ps -axj

PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
0 1 0 0 ? -1 S 0 0:04 init
1 2 1 1 ? -1 SW 0 0:00 [keventd]
1 3 1 1 ? -1 SW 0 0:00 [kapm-idled]
0 4 1 1 ? -1 SWN 0 0:00 [ksoftirqd_CPU0]
0 5 1 1 ? -1 SW 0 0:00 [kswapd]
0 6 1 1 ? -1 SW 0 0:00 [kreclaimd]
0 7 1 1 ? -1 SW 0 0:00 [bdflush]
0 8 1 1 ? -1 SW 0 0:00 [kupdated]
1 9 1 1 ? -1 SW< 0 0:00 [mdrecoveryd]
1 17 1 1 ? -1 SW 0 0:02 [kjournald]
1 92 1 1 ? -1 SW 0 0:00 [khubd]
1 573 573 573 ? -1 S 0 0:03 syslogd -r -x
1 578 578 578 ? -1 S 0 0:00 klogd -2
1 598 598 598 ? -1 S 32 0:00 portmap

进程号为1、2的这些进程非常特殊,存在于系统的整个生命期中。它们没有父进程ID ,没有组进程ID ,也没有对话期ID 。syslogd 守护进程可用于任何为操作人员记录系统消息的程序中。可以在一台实际的控制台上打印这些消息,也可将它们写到一个文件中。sendmail 是标准邮递守护进程。update 程序定期将内核缓存中的内容写到硬盘上(通常是每隔30 秒)。为了做到这一点,该程序每隔30 秒调用sync(2 )函数一次。cron 守护进程在指定的日期和时间执行指定的命令。许多系统管理任务是由cron 定期地使相关程序执行而得以实现的。inetd进程监听系统的网络界面,以输入对各种网络服务器的请求。最后一个守护进程,lpd 处理对系统提出的各个打印请求。

注意,所有守护进程都以超级用户(用户ID为0)的优先权运行。没有一个守护进程具有控制终端,终端名称设置为问号(?)、终端前台进程组ID设置为-1。缺少控制终端是守护进程调用了setsid的结果。除update以外的所有守护进程都是进程组的首进程,对话期的首进程,而且是这些进程组和对话期中的唯一进程。最后,应当引起注意的是所有这些守护进程的父进程都是init进程。

在接触实际编程前,我们来看看编写守护进程要碰到的概念:进程组合会话期。

进程组

每个进程除了有一进程ID之外,还属于一个进程组(在讨论信号时就会涉及进程组)进程组是一个或多个进程的集合。每个进程有一个唯一的进程组ID。进程组ID类似于进程ID——它是一个正整数,并可存放在pid_t数据类型中。

每个进程组有一个组长进程。组长进程的标识是,其进程组ID等于其进程ID,进程组组长可以创建一个进程组,创建该组中的进程,然后终止,只要在某个进程组中有一个进程存在,则该进程就存在,这与其组长进程是否终止无关。从进程组创建开始到其中最后一个进程离开为止的时间区间称为进程组的生命期。某个进程组中的最后一个进程可以终止,也可以参加另一进程组。

前面已经提到进程调用setgid可以参加一个现存的组或者创建一个新进程组(setsid也可以创建一个新的进程组,后面将用到)

会话期

会话期(session)是一个或多个进程组的集合。其中,在一个会话期中有3个进程组,通常是有shell的管道线将几个进程编成一组的。

下面说明有关会话期和进程组的一些特性:

一个会话期可以有一个单独的控制终端(controlling terminal),这一般是我们在其上登录的终端设备(终端登录)或伪终端设备(网络登录),但这个控制终端并不是必需的。

建立与控制终端连接的会话期首进程,被称之为控制进程(contronlling process)。以及一个会话期中的几个进程组可被分为一个前台进程组(foreground process group)以及一个或几个后台进程组(background process group)

如果一个会话期有一个控制终端,则它有一个前台进程组,其他进程组为后台进程组。无论何时键入中断键(常常是delete或ctrl-c)或退出键(通常是ctrl-\),就会造成将中断信号或退出信号送至前途进程组的所有进程。

守护进程的编程规则

在不同Unix环境下,守护进程的具体编程细节并不一致。但所幸的是,守护进程的编程原则其实都一样,区别仅在于具体的实现细节不同,这个原则就是要满足守护进程的特性。编程规则如下:

1、在后台运行

为避免挂起控制终端,要将daemon放入后台执行,其方法是,在进程中调用fork使父进程终止,让daemon在子进程中后台执行。具体就是调用f o r k ,然后使父进程e x i t 。这样做实现了下面几点:
第一,如果该精灵进程是由一条简单s h e l l 命令起动的,那么使父进程终止使得s h e l l 认为这条命令已经执行完成。
第二,子进程继承了父进程的进程组I D ,但具有一个新的进程I D ,这就保证了子进程不是一个进程组的首进程。这对于下面就要做的s e t s i d 调用是必要的前提条件。

2、脱离控制终端,登录会话和进程组

登录会话可以包含多个进程组,这些进程组共享一个控制终端,这个控制终端通常是创建进程的登录终端、控制终端,登录会话和进程组通常是从父进程继承下来的。我们的目的就是要摆脱它们,使之不受它们的影响。

其方法是在第一点的基础上,调用setsid()使进程成为会话组长:

需要说明的是,当进程是会话组长时,setsid()调用会失败,但第一点已经保证进程不是会话组长。setsid()调用成功后,进程成为新的会话组长和新的进程组长,并与原来的登录会话和进程组脱离,由于会话过程对控制终端的独占性,进程同时与控制终端脱离。
具体是操作就是:
(a )成为新对话期的首进程
(b )成为一个新进程组的首进程
(c )没有控制终端。

3、禁止进程重新打开控制终端

现在,进程已经成为无终端的会话组长,但它可以重新申请打开一个控制终端。可以通过使进程不再成为会话组长来禁止进程重新打开控制终端:

4、关闭打开的文件描述符

进程从创建它的父进程那里继承了打开的文件描述符。如不关闭,将会浪费系统资源,造成进程所在地文件系统无法卸下以及无法预料的错误。一般来说,必要的是关闭0、1、2三个文件描述符,即标准输入、标准输出、标准错误。因为我们一般希望守护进程自己有一套信息输出、输入的体系,而不是把所有的东西都发送到终端屏幕上。调用fclose();

5、改变当前工作目录

将当前工作目录更改为根目录。从父进程继承过来的当前工作目录可能在一个装配的文件系统中。因为精灵进程通常在系统再引导之前是一直存在的,所以如果精灵进程的当前工作目录在一个装配文件系统中,那么该文件系统就不能被拆卸。

另外,某些精灵进程可能会把当前工作目录更改到某个指定位置,在此位置做它们的工作。例如,行式打印机假脱机精灵进程常常将其工作目录更改到它们的s p o o l 目录上。
可以调用chdir(“目录”);

6、重设文件创建掩码

将文件方式创建屏蔽字设置为0 。由继承得来的文件方式创建屏蔽字可能会拒绝设置某些许可权。例如,若精灵进程要创建一个组可读、写的文件,而继承的文件方式创建屏蔽字,屏蔽了这两种许可权,则所要求的组可读、写就不能起作用。

7、处理SIGCHLD 信号

处理SIGCHLD信号并不是必需的。但对于某些进程,特别是服务器进程往往在请求到来时生产子进程出来请求。如果父进程不等待子进程结束,子进程将成为僵尸进程,(zombie)而仍占用系统资源。如果父进程等待子进程结束,将增加父进程的负担,影响服务器进程的并发性能。在系统V下可以简单的将SIGCHLD信号的操作设为SIG-IGN:

signal(SIGCHLD,SIG_IGN);

这样,内核在子进程结束时不会产生僵尸进程,这一点与BSD4不同,在BSD4下必须显示等 待子进程结束才能释放僵尸进程。

守护进程实例

守护进程实例包括两部分:主程序test.c和初始化程序init.c。主程序每隔一分钟向/tmp
目录中的日志test.log 报告运行状态。初始化程序中的init_daemon 函数负责生成守护进程

void make_daemon(void)
{
pid_t pid;
FILE * lockfd;
sigset_t sighup;
int i;

extern pid_t getsid(pid_t);
pid = fork();//第一个子进程生成
if (pid < 0) {
printinfo(”fork error!”,INFOERROR);
exit(FAILEXIT);
}else if (pid > 0) {
printinfo(”fork 1 ok! “, INFOSCREEN);
exit(OKEXIT);//退出父进程,摆脱shell的控制
}
pid = getpid();//获得子进程自身的id
lockfd = fopen(PIDFILE, “w”);//以下是将pid写入文件
if (lockfd != NULL) {
fprintf(lockfd, “%d\n”, pid);
fclose(lockfd);
}//写入pid
if (getsid(0) != pid) {//创建新的会话期
if (setsid() < 0) {
printinfo(”backupdaemon setsid error!”,INFOERROR);
perror(”setsid”);
}
}
if(pid=fork()){//再次生成子进程,这时候是孙子进程
exit(0);//退出上一代进程
}else if(pid<0){
exit(1);
}
close(1);//关闭文件
close(2);
chdir(rundir);//改变运行的目录
umask(022);//改变文件权限
}

守护进程的错误输出

守护进程不属于任何终端,所以当需要输出某些信息时,它无法像一般程序那样将信息直接输出到标准输出和标准错误输出中。我们很大时候也不希望每个守护进程将它自己的出错消息写到一个单独的文件中。因为对于系统管理人员而言,要记住哪一个守护进程写到哪一个记录文件中,并定期的检查这些文件,他一定会为此感到头疼的。所以,我们需要有一个集中的守护进程出错记录机制。目前很多系统都引入了syslog记录进程来实现这一目的。

自伯克利开发了BSD syslog并广泛应用以来,BSD syslog 机制被大多数守护进程所使用。我们下面介绍BSD syslog 的用法。有三种方法产生记录消息:

1 内核例程可以调用log函数。任何一个用户进程通过打开和读/dev/klog设备就可以读取这些消息。因为我们无意编写内核中的例程,所以不再进一步说明此函数。

2 大多数用户进程(守护进程)调用syslog函数以产生记录消息。我们将在下面说明其调用序列。这使消息发送至Unix域数据报套接口/dev/log。

3 在此主机上,或通过TCP/IP网络连接到此主机的某一其他主机上的一个用户进程可将记录消息发向UDP端口514。

注意:syslog 函数并不产生这些UDP数据报——它们要求产生此记录消息的进程具有显式的网络编程。

通常,syslog守护进程读取三种格式的记录消息。此守护进程在启动时读一个配置文件。一般来说,其文件名为/etc/syslog.conf,该文件决定了不同种类的消息应送向何处。例如,紧急消息可被送向系统管理员(若已登录),并在控制台上显示,而警告消息则可记录到一个文件中。该机制提供了syslog函数,其调用格式如下

#include <syslog.h>
void openlog (char*ident,int option ,int facility);
void syslog(int priority,char*format,……)
void closelog();

调用openlog是可选择的。如果不调用openlog,则在第一次调用syslog时,自动调用openlog。调用closelog也是可选择的,它只是关闭被用于与syslog守护进程通信的描述符。调用openlog 使我们可以指定一个ident,以后, 此ident 将被加至每则记录消息中。ident 一般是程序的名称(例如 ,cron ,inetd 等)。option 有4种可能:

LOG_CONS 若日志消息不能通过Unix域数据报发送至syslog,则将该消息写至控制台。

LOG_NDELAY1 立即打开Unix域数据报套接口至syslog守护进程,而不要等到记录第一消息。通常,在记录第一条消息之前,该套接口不打开。

LOG_PERROR 除将日志消息发送给syslog 外,还将它至标准出错。此选项仅由4.3BSDReno及以后版本支持。

LOG_PID 每条消息都包含进程ID。此选项可供对每个请求都fork一个子进程的守护进程使用。

在openlog中设置facility参数的目的是让配置文件可以说明,来自不同设施的消息以不同的方式进行处理。如果不调用openlog,或者以facility 为0来调用它,那么在调用syslog 时,可将facility作为priority参数的一个部分进行说明。调用syslog产生一个记录消息。其priority参数是facility和level的组合,它们可选取的值分别列于下面。level值按优先级从高级到最低按序排列。

由 beat 发表于 下午06点41分 | 回复 (0)

2005年11月12日 星期六

An introduction to Portable Batch System

1. Introduction
2. User commands
3. Submitting MPI parallel jobs
4. System configuration

由 beat 发表于 下午05点32分 | 回复 (0)

PORTABLE BATCH SYSTEM (PBS) MINI-HOWTO

Introduction
The Portable Batch System (PBS) is available as Open Source software from http://www.OpenPbs.org/. A commercial version can be bought from http://www.PBSPro.com/. The PBSPro also offer support for OpenPBS, and at a decent price for academic institutions.

There exists a very useful collection of user-contributed software/patches for Open PBS at http://www-unix.mcs.anl.gov/openpbs/.

This HowTo document outlines all the steps required to compile and install the Portable Batch System (PBS) version 2.1, 2.2 and 2.3. Most likely the steps will be the same for the PBSPro software.

The latest version of PBS is available from http://www.OpenPbs.org/. The PBS documentation available at the Web-site should be handy for in-depth discussion of the points covered in this HowTo.

We also discuss how to create a PBS script for parallel or serial jobs. The cleanup in an epilogue script may be required for parallel jobs.

Accounting Reports may be generated from PBS' accounting files. We provide a simple tool pbsacct that processes and formats the accounting into a useful report. Download the latest version of pbsacct from the ftp://ftp.fysik.dtu.dk/pub/PBS/ directory.

This page is at http://www.fysik.dtu.dk/CAMP/pbs.html.
Feedback to this document was kindly provided by:

Tim Mattson, timothy.g.mattson (at) intel.com.

HowTo steps
The following steps are what we use to install PBS from scratch on our systems. Please send corrections and additions to Ole.H.Nielsen (at) fysik.dtu.dk.


Ensure that tcl8.0 and tk8.0 are installed on the system. Look into the PBS docs to find out about these packages. The homepage is at http://www.scriptics.com/products/tcltk/. Get Linux RPMs from your favorite distribution, or build it yourself on other UNIXes.
If you installed the PBS binary RPMs on Linux, skip to step 4.

Configure PBS for your choice of spool-directory and the central server machine (named "zeise" in our examples):
./configure --set-server-home=/var/spool/PBS --set-default-server=zeise

On Compaq Tru64 UNIX make sure that you use the Compaq C-compiler in stead of the GNU gcc by doing "setenv CC cc". You should add these flags to the above configure command: --set-cflags="-g3 -O2". It is also important that the /var/spool/PBS does not include any soft-links, such as /var -> /usr/var, since this triggers a bug in the PBS code.
If you compiled PBS for a different architecture before, make sure to clean up before running configure:

gmake distclean


Run a GNU-compatible make in order to build PBS.
On AIX 4.1.5 edit src/tools/Makefile to add a library: LIBS = -lld

On Compaq Tru64 UNIX use the native Compaq C-compiler:

gmake CC=cc

The default CFLAGS are "-g -O2", but the Compaq compiler requires "-g3 -O2" for optimization. Set this with:
./configure (flags) --set-cflags="-g3 -O2"

After the make has completed, install the PBS files as the root superuser:
gmake install


Create the "nodes" file in the central server's (zeise) directory /var/spool/PBS/server_priv containing hostnames, see the PBS 2.2 Admin Guide p.8 (Sec. 2.2 "Installation Overview" point 8.). Substitute the spool-directory name /var/spool/PBS by your own choice (the Linux RPM uses /var/spool/pbs). Check the file /var/spool/PBS/pbs_environment and ensure that important environment variables (such as the TZ timezone variable) have been included by the installation process. Add any required variables in this file.

Initialize the PBS server daemon and scheduler:
/usr/local/sbin/pbs_server -t create
/usr/local/sbin/pbs_sched

The "-t create" should only be executed once, at the time of installation !!
The pbs_server and pbs_sched should be started at boot time: On Linux this is done automatically by /etc/rc.d/init.d/pbs. Otherwise use your UNIX's standard method (e.g. /etc/rc.local) to run the following commands at boot time:

/usr/local/sbin/pbs_server -a true
/usr/local/sbin/pbs_sched

The "-a true" sets the scheduling attribute to True, so that jobs may start running.

Create queues using the "qmgr" command, see the manual page for "pbs_server_attributes" and "pbs_queue_attributes": List the server configuration by the print server command. The output can be used as input to qmgr, so this is a way to make a backup of your server setup. You may stick the output of qmgr (for example, you may use the setup listed below) into a file (removing the first 2 lines which are actually not valid commands). Pipe this file into qmgr like this: cat file | qmgr and everything is configured in a couple of seconds !
Our current configuration is:

# qmgr
Max open servers: 4
Qmgr: print server
#
# Create queues and set their attributes.
#
#
# Create and define queue verylong
#
create queue verylong
set queue verylong queue_type = Execution
set queue verylong Priority = 40
set queue verylong max_running = 10
set queue verylong resources_max.cput = 72:00:00
set queue verylong resources_min.cput = 12:00:01
set queue verylong resources_default.cput = 72:00:00
set queue verylong enabled = True
set queue verylong started = True
#
# Create and define queue long
#
create queue long
set queue long queue_type = Execution
set queue long Priority = 60
set queue long max_running = 10
set queue long resources_max.cput = 12:00:00
set queue long resources_min.cput = 02:00:01
set queue long resources_default.cput = 12:00:00
set queue long enabled = True
set queue long started = True
#
# Create and define queue medium
#
create queue medium
set queue medium queue_type = Execution
set queue medium Priority = 80
set queue medium max_running = 10
set queue medium resources_max.cput = 02:00:00
set queue medium resources_min.cput = 00:20:01
set queue medium resources_default.cput = 02:00:00
set queue medium enabled = True
set queue medium started = True
#
# Create and define queue small
#
create queue small
set queue small queue_type = Execution
set queue small Priority = 100
set queue small max_running = 10
set queue small resources_max.cput = 00:20:00
set queue small resources_default.cput = 00:20:00
set queue small enabled = True
set queue small started = True
#
# Create and define queue default
#
create queue default
set queue default queue_type = Route
set queue default max_running = 10
set queue default route_destinations = small
set queue default route_destinations += medium
set queue default route_destinations += long
set queue default route_destinations += verylong
set queue default enabled = True
set queue default started = True
#
# Set server attributes.
#
set server scheduling = True
set server max_user_run = 6
set server acl_host_enable = True
set server acl_hosts = *.fysik.dtu.dk
set server acl_hosts = *.alpha.fysik.dtu.dk
set server default_queue = default
set server log_events = 63
set server mail_from = adm
set server query_other_jobs = True
set server resources_default.cput = 01:00:00
set server resources_default.neednodes = 1
set server resources_default.nodect = 1
set server resources_default.nodes = 1
set server scheduler_iteration = 60
set server default_node = 1#shared


Install the PBS software on the client nodes, repeating steps 1-3 above.

Configure the PBS nodes so that they know the server: Check that the file /var/spool/PBS/server_name contains the name of the PBS server (zeise in this example), and edit it if appropriate. Also make sure that this hostname resolves correctly (with or without the domain-name), otherwise the pbs_server may refuse connections from the qmgr command.
Create the file /var/spool/PBS/mom_priv/config on all PBS nodes (server and clients) with the contents:

# The central server must be listed:
$clienthost zeise

where the correct servername must replace "zeise". You may add other relevant lines as recommended in the manual, for example for restricting access and for logging:
$logevent 0x1ff
$restricted *.your.domain.name

(list the domain names that you want to give access).
For maintenance of the configuration file, we use rdist to duplicate /var/spool/PBS/mom_priv/config from the server to all PBS nodes.


Start the MOM mini-servers on both the server and the client nodes:
/usr/local/sbin/pbs_mom

or "/etc/rc.d/init.d/pbs start" on Linux. Make sure that MOM is started at boot time. See discussion under point 5.
On Compaq Tru64 UNIX 4.0E+F there may be a problem with starting pbs_mom too soon. Some network problem makes pbs_mom report errors in an infinite loop, which fills up the logfiles' filesystem within a short time ! Several people told me that they don't have this problem, so it's not understood at present.
The following section is only relevant if you have this problem on Tru64 UNIX.

On Tru64 UNIX start pbs_mom from the last entry in /etc/inittab:

# Portable Batch System batch execution mini-server
pbsmom::once:/etc/rc.pbs > /dev/console 2>&1

The file /etc/rc.pbs delays the startup of pbs_mom:
#!/bin/sh
#
# Portable Batch System (PBS) startup
#
# On Digital UNIX, pbs_mom fills up the mom_logs directory
# within minutes after reboot. Try to sleep at startup
# in order to avoid this.
PBSDIR=/usr/local/sbin
if [ -x ${PBSDIR}/pbs_mom ]; then
echo PBS startup.
# Sleep for a while
sleep 120
${PBSDIR}/pbs_mom # MOM
echo Done.
else
echo Could not execute PBS commands !
fi


Queues defined above do not work until you start them:
qstart default small medium long verylong
qenable default small medium long verylong

This needs to be done only once and for all, at the time when you install PBS.

Make sure that the PBS server has all nodes correctly defined. Use the pbsnodes -a command to list all nodes.
Add nodes using the qmgr command:

# qmgr
Max open servers: 4
Qmgr: create node node99 properties=ev67

where the node-name is node99 with the properties=ev67. Alternatively, you may simply list the nodes in the file /var/spool/PBS/server_priv/nodes:
server:ts ev67
node99 ev67

The :ts indicates a time-shared node; nodes without :ts are cluster nodes where batch jobs may execute. The second column lists the properties that you associate with the node. Restart the pbs_server after editing manually the nodes file.

After you first setup your system, to get the jobs to actually run you need to set the server scheduling attribute to true. This will normally be done for you at boot time (see point 5 in this file), but for this first time, you will need to do this by hand using the qmgr command:
# qmgr
Max open servers: 4
Qmgr: set server scheduling=true

--------------------------------------------------------------------------------

Batch job scripts
Your PBS batch system ought to be fully functional at this point so that you can submit batch jobs using the qsub command. For debugging purposes, PBS offers you an "interactive batch job" by using the command qsub -I.

As an example, you may use the following PBS batch script as a template for creating your own batch scripts. The present script runs an MPI parallel job on the available processors:

#!/bin/sh
### Job name
#PBS -N test
### Declare job non-rerunable
#PBS -r n
### Output files
#PBS -e test.err
#PBS -o test.log
### Mail to user
#PBS -m ae
### Queue name (small, medium, long, verylong)
#PBS -q long
### Number of nodes (node property ev67 wanted)
#PBS -l nodes=8:ev67

# This job's working directory
echo Working directory is $PBS_O_WORKDIR
cd $PBS_O_WORKDIR

echo Running on host `hostname`
echo Time is `date`
echo Directory is `pwd`
echo This jobs runs on the following processors:
echo `cat $PBS_NODEFILE`
# Define number of processors
NPROCS=`wc -l < $PBS_NODEFILE`
echo This job has allocated $NPROCS nodes

# Run the parallel MPI executable "a.out"
mpirun -v -machinefile $PBS_NODEFILE -np $NPROCS a.out

If you specify #PBS -l nodes=1 in the script, you will be running a non-parallel (or serial) batch job:

#!/bin/sh
### Job name
#PBS -N test
### Declare job non-rerunable
#PBS -r n
### Output files
#PBS -e test.err
#PBS -o test.log
### Mail to user
#PBS -m ae
### Queue name (small, medium, long, verylong)
#PBS -q long
### Number of nodes (node property ev6 wanted)
#PBS -l nodes=1:ev6

# This job's working directory
echo Working directory is $PBS_O_WORKDIR
cd $PBS_O_WORKDIR

echo Running on host `hostname`
echo Time is `date`
echo Directory is `pwd`

# Run your executable
a.out

--------------------------------------------------------------------------------

Clean-up after parallel jobs
If a parallel job dies prematurely for any reason, PBS will clean up user processes on the master-node only. We (and others) have found that often MPI slave-processes are lingering on all of the slave-nodes waiting for communication from the (dead) master-process.

At present the only generally applicable way to clean up user processes on the nodes allocated to a PBS job is to use the PBS epilogue capability (see the PBS documentation). The epilogue is executed on the job's master-node, only.

An epilogue script /var/spool/PBS/mom_priv/epilogue should be created on every node, containing for example this:

#!/bin/sh
echo '--------------------------------------'
echo Running PBS epilogue script

# Set key variables
USER=$2
NODEFILE=/var/spool/PBS/aux/$1

echo
echo Killing processes of user $USER on the batch nodes
for node in `cat $NODEFILE`
do
echo Doing node $node
su $USER -c "ssh -a -k -n -x $node skill -v -9 -u $USER"
done
echo Done.

由 beat 发表于 上午10点06分 | 回复 (0)

PBS/SCore(Open PBS/SCore) Administrator's Guide

1. Introduction
PBS/SCore must be configired independently from the SCore configuration. This document describes hot to configure PBS/SCore. For further deials, please refer to the Administrator Guide attached to PBS(/SCore). In this document, it is assumed that SCore is already installed and configured.

If SCore system has been installed by using EIT, all the configuration procedure described in this document has been automatically done by EIT.

2. Host Configuration
There are 4 host types in PBS/SCore. An administrator must decide which host is which type.

PBS server host
One of the hosts must be a PBS server host on which PBS server (pbs_server) and PBS scheduler (pbs_sched) are running.

It is possible that msgbserv and/or scoreboard server programs of SCore can also run on the PBS server host.

Compute host
Parallel jobs run on compute hosts. On each compute host, pbs_mom server must be running to monitor host resource and invoke user parallel jobs. To run SCore jobs, compute hosts must be registered in scorehosts.db file, and scoreboard server is up and running on some host.

User hosts
To submit parallel jobs from user hosts, PBS commands, such as qsub and qdel, must be installed on the user hosts.

Host to invoke SCore jobs
An SCore administrator may specify a host from which SCore jobs are invoked via the scout command. This host should not be a compute host. Like a computer host, pbs_mom must be initiated on this host too. If this host is not allocated, one of compute hosts which are allocated to a submitted job is used.

In this document, it is assumed that above hosts are cofigured like the following.

PBS server host
server.pccluster.org
Compute hosts
comp0.pccluster.org
comp1.pccluster.org
comp2.pccluster.org
comp3.pccluster.org
User hosts
server.pccluster.org
Host to invoke SCore jobs
server.pccluster.org
3. File setup on all hosts
Every (possible) host must be configured as in the following.

/opt/score/etc/pbs_server_name
Default PBS server host name must be specified in the /opt/score/etc/pbs_server_name file.

server.pccluster.org

4. File setup on PBS server host
/var/scored/pbs/ directory
Execute the following command to setup the /var/scored/pbs/. This operation is not needed if SCore system has been installed by using the binary rpms or EIT.

# cd /opt/score/install
# ./setup -pbs_server

/var/scored/pbs/server_priv/nodes
All compute hosts must be listed in the /var/scored/pbs/server_priv/nodes file. If scorehosts.db file is already created and scoreboard server is up and running, then the scbd2pbs command creates the nodes file via the scoreboard database.

% /opt/score/sbin/scbd2pbs pcc > /var/scored/pbs/server_priv/nodes

In this example, all hosts belong to pcc host group are listed, and then the nodes file is created. The follwoing is an example of the created nodes file.

comp0.pccluster.org pcc score
comp1.pccluster.org pcc score
comp2.pccluster.org pcc score
comp3.pccluster.org pcc score

Each line consists of hostname and property names of the host. As a PBS property name, host group name, "pcc" in this example, is used. In scorehosts.db file, if a host record has an attribute named "pbs", then its associated value(a) is added to the line as property name(s).

For more detail on the nodes file, please refer to Section 3.3.2. Declaring Nodes of PBS Administrator Guide

5. File setup on compute hosts
/var/scored/pbs/ directory
Type the following command to setup the /var/scored/pbs/. This operation is not needed if SCore system has been installed by using the binary rpms or EIT.

# cd /opt/score/install
# ./setup -pbs_comp

/var/scored/pbs/mom_priv/config
In the /var/scored/pbs/mom_priv/config file, PBS server hostname must be included as in the following.

$logevent 0x1ff
$clienthost server.pccluster.org

For more detail on the config file, please refer to Section 9.2. pbs_mom of PBS Administrator Guide

6. File setup on user hosts
There is nothing to do with user hosts.

7. File setup on the host to submit SCore jobs
/var/scored/pbs/ directory
Type the following command to setup the /var/scored/pbs/. This operation is not needed if SCore system has been installed by using the binary rpms or EIT.

# cd /opt/score/install/
# ./setup -pbs_comp

/var/scored/pbs/mom_priv/config
In the /var/scored/pbs/mom_priv/config file, PBS server hostname and all compute hostnames must be listed.

$logevent 0x1ff
$clienthost server.pccluster.org
$clienthost comp0.pccluster.org
$clienthost comp1.pccluster.org
$clienthost comp2.pccluster.org
$clienthost comp3.pccluster.org

For more detail on the config file, please refer to Section 9.2. pbs_mom of PBS Administrator Guide

8. Initiating PBS Servers
pbs_mom
Initiate the pbs_mom program on all compute host. If there is a host to submit SCore jobs, then the pbs_mom program must be initiate on the host too.

Red Hat or Turbo Linux:
% su
# /etc/rc.d/init.d/pbs_mom start

SuSE Linux:
% su
# /etc/init.d/pbs_mom start

pbs_sched
Initiate PBS scheduler on PBS server host.

Red Hat or Turbo Linux:
% su
# /etc/rc.d/init.d/pbs_sched start

SuSE Linux:
% su
# /etc/init.d/pbs_sched start

pbs_server
Initiate the pbs_server program on the server host. If there is a host to submit SCore jobs, add -m option followed by the name of the host. If this is the first time to initiate PBS server, add -t option to setup databse.

For the first time,
% su
# /opt/score/sbin/pbs_server -t create -m server.pccluster.org

Otherwise,
Red Hat or Turbo Linux:
% su
# /etc/rc.d/init.d/pbs_server start

SuSE Linux:
% su
# /etc/init.d/pbs_server start -m

Confirmation
To confirm if the PBS server processes are running, invoke the following command.

% su
# /opt/score/bin/qmgr -c 'p n @active'

If they are running properly, then the following output is obtained.

#
# Create nodes and set their properties.
#
#
# Create and define comp0.pccluster.org
#
# create node comp0.pccluster.org # unsuppored operation
set node comp0.pccluster.org state = free
set node comp0.pccluster.org properties = any
set node comp0.pccluster.org properties += score
set node comp0.pccluster.org properties += score-pcc
set node comp0.pccluster.org ntype = cluster
#
# Create and define comp1.pccluster.org
#
# create node comp1.pccluster.org # unsuppored operation
set node comp1.pccluster.org state = free
set node comp1.pccluster.org properties = any
set node comp1.pccluster.org properties += score
set node comp1.pccluster.org properties += score-pcc
set node comp1.pccluster.org ntype = cluster
#
# Create and define comp2.pccluster.org
#
# create node comp2.pccluster.org # unsuppored operation
set node comp2.pccluster.org state = free
set node comp2.pccluster.org properties = any
set node comp2.pccluster.org properties += score
set node comp2.pccluster.org properties += score-pcc
set node comp2.pccluster.org ntype = cluster
#
# Create and define comp3.pccluster.org
#
# create node comp3.pccluster.org # unsuppored operation
set node comp3.pccluster.org state = free
set node comp3.pccluster.org properties = any
set node comp3.pccluster.org properties += score
set node comp3.pccluster.org properties += score-pcc
set node comp3.pccluster.org ntype = cluster

Creation of default queue
Create a default queue. In the following example, the created queue is named as "default."

# qmgr -c "create queue default queue_type=execution"
# qmgr -c "set queue default enabled=true"
# qmgr -c "set queue default started=true"
# qmgr -c "set server default_queue=default"

For more detail on the config file, please refer to Section 3.5.2. Queue Configuration of PBS Administrator Guide

Start of scheduling
Finally, activate PBS scheduling queue.

# qmgr -c "set server scheduling=true"

If PBS/SCore is installed from a binary package, then C based scheduler is implemented. For more detail on the C based scheduler, please refer to Section 9.9. C Based Scheduler of PBS Administrator Guide

9. The difference between PBS and PBS/SCore
score property
In PBS/SCore, the property name, score is treated as a special name to distinguish not-SCore jobs and SCore jobs.

Host to invoke SCore jobs
Any SCore jobs must be invoked via the scout and scrun programs. The scout program creates a parallel execution environment in a cluster, and the scrun program invokes user parallel progam in the parallel environment. The scout program can be invoked outside of a cluster.

In contrast, parallel program invokation scheme of PBS is different. PBS assumed user parallel program is firstly invoked on a compute host. And then the program itself spawns remote processes on the other compute hosts.

"-m" option is added to pbs_server to specify the host to invoke SCore jobs. As described above, pbs_mon daemon process must be running on this host. However, this host cannot be allocated to a designated compute host. By default, the host is allocated dynamically one of the compute hosts.

pbs_server -m <hostname>[:]

由 beat 发表于 上午10点02分 | 回复 (0)