当前位置:Linux教程 - 编程技术 - 编程技术 - Linux程式设计之四

编程技术 - Linux程式设计之四

Linux程式设计之四
2004-04-23 15:18 pm
来自:Linux文档
现载:Www.8s8s.coM
地址:无名

Linux程式设计-23.共享记忆体(Shared Memory)
http://www.openchess.org/noitatsko/programming/ (2001-05-27 14:08:00)
共享记忆体是指同一块记忆体区段被一个以上的行程所分享。这是我们所知速度最快的行程间通讯方式。使用共享记忆体在使用多CPU的机器上,会使机器发挥较佳的效能。

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

#include
#include
int shmget(key_t key, int size, int shmflg);
char *shmat ( int shmid, char *shmaddr, int shmflg )
int shmdt ( char *shmaddr)
int shmctl(int shmid, int cmd, struct shmid_ds *buf);

struct shmid_ds {
struct ipc_perm shm_perm; /* operation perms */
int shm_segsz; /* size of segment (bytes) */
time_t shm_atime; /* last attach time */
time_t shm_dtime; /* last detach time */
time_t shm_ctime; /* last change time */
unsigned short shm_cpid; /* pid of creator */
unsigned short shm_lpid; /* pid of last operator */
short shm_nattch; /* no. of current attaches */

/* the following are private */

unsigned short shm_npages; /* size of segment (pages) */
unsigned long *shm_pages; /* array of ptrs to frames -> SHMMAX */
struct vm_area_struct *attaches; /* descriptors for attaches */
};

shm_perm

This is an instance of the ipc_perm structure, which is defined for us in
linux/ipc.h. This holds the permission information for the segment,
including the access permissions, and information about the creator of the
segment (uid, etc).

shm_segsz

Size of the segment (measured in bytes).

shm_atime

Time the last process attached the segment.

shm_dtime

Time the last process detached the segment.

shm_ctime

Time of the last change to this structure (mode change, etc).

shm_cpid

The PID of the creating process.

shm_lpid

The PID of the last process to operate on the segment.

shm_nattch

Number of processes currently attached to the segment.
(http://www.fanqiang.com/) 进入【UNIX论坛】


Linux程式设计-24.Semaphores
http://www.openchess.org/noitatsko/programming/ (2001-05-27 15:00:00)

semget
semop
semctl



Linux程式设计-25.Message Queues
http://www.openchess.org/noitatsko/programming/ (2001-05-27 16:10:00)
msgget
msgsnd
msgctl (http://www.fanqiang.com/) 进入【UNIX论坛】


Linux程式设计-26.PIPE
http://www.openchess.org/noitatsko/programming/ (2001-05-27 17:04:00)
a pipe is a method of connecting the standard output of one process to the standard input of another.

They are half-duplex. Data flows only in one direction.

They can be used only between processes that have a common ancestor. Normally a pipe is created by a process, that process calls fork, and the pipe is used between the parent and child. (http://www.fanqiang.com/) 进入【UNIX论坛】

Linux程式设计-27.GNU Debugger
http://www.openchess.org/noitatsko/programming/ (2001-05-27 18:08:01)
gdb/xxgdb


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

启动方式
你可以单独启动gdb,不过一般来说,启动方式都会带一两个参数。

「gdb program」:指定要除错的程式。
「gdb program core」:指定要除错的程式及其coredump档。
「gdb program pid」:指定要除错的程式及目前其正在执行的process id。

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

命令说明
attach pid
at pid
接上一个已经在执行的行程pid。这会使pid暂停,中断任何sleep及可中断的可系统呼叫。

backtrace, bt, where, w
显示追踪堆叠。

break [filename:]function | line | address
b [filename:]function | line | address
设定中断点。您可以指定函数名称、行数、甚至记忆体位址。

c
中断点後,继续执行程式。

clear [filename:]function | line | address
清除中断点。

condition breakid expression
根据中断点号码来设定中断状况。

delete breakid
清除中断点breakid。

detach pid
解除目前接上的行程。

display expression
每次中断时,显示expression的值。

help [name]
辅助说明

jump address
跳到指定的位址,并开始执行。

list (address)
l (address)
列出位置附近的10行。

next, n
执行到下一行。

nexti
执行下一个处理器指令。

print expression
p expression
列出详细的expression值。

run, r
从头开始执行目前程式。

set expression
设定参数值。

step, s
执行一个程式指令。

stepi
执行一个处理器指令,遇到函数时,追踪进去。

undisplay
取消display。没有参数的话,取消全部。

whatis
显示expression的资料型态。

quit
离开。

x
与print类似,不过仅显示位址内容的简约格式。



Linux程式设计-28.GNU Make
http://www.openchess.org/noitatsko/programming/ (2001-05-27 19:00:00)

dependency, target, rule




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

变数(Variable)
OBJS =

OBJS :=
OBJS +=



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

字尾法则(Suffix Rules)



Linux程式设计-29.时间处理
http://www.openchess.org/noitatsko/programming/ (2001-05-27 20:10:01)
UNIX及Linux的时间系统是由「新纪元时间」Epoch开始计算起,单位为秒,Epoch则是指定为1970年一月一日凌晨零点零分零秒,格林威治时间。
目前大部份的UNIX系统都是用32位元来记录时间,正值表示为1970以後,负值则表示1970年以前。我们可以很简单地计算出其时间领域:

2^31/86400(s) = 24855.13481(天) ~ 68.0958(年)

1970+68.0958 = 2038.0958
1970-68.0958 = 1901.9042

时间领域为[1901.9042,2038.0958]。

准确的时间为2038年一月十八日星期一晚上十点十四分七秒。那一刻,时间将会转为负数,变成1901年十二月十三日黑色星期五下午三点四十五分五十二秒,然後Jason就会跑出来用斧头砸掉您的电脑。

这就是所谓的UNIX 2038 BUG,或者您也可戏称为Jason hatchet bug。在大部份的UNIX上,并没有所谓Y2K问题,不过都有2038年问题。

在一些64位元的平台上,例如Digital Alpha、SGI、Sparc等等,则用64位元来表示时间。

2^63/86400 ~ 1E14(天) ~ 2.92E11(年)

大约是292亿年。

因此,使用64位元的电脑可能会有Armageddon bug的问题。届时位於猎户座旋臂的太阳,已经是黑矮星或暗黑物质,猎户座旋臂大概也已经被重力波震断,银河系大概则已经变成小型似星体了。

虽然许多人认为UNIX的2038年问题会随着科技的进步,而将电脑逐步汰换成64位元电脑,因此无须担心。但我个人相信,在2038年,依然会有许多状况出现。因为,就事实而言,目前许多UNIX系统都有足够的能力服役到2038年而毫无问题。因此,如果有意添购电脑主机,而且有预期会使用到那个时候,最好是选购64位元电脑,确认只有世界末日问题(除非您想要把资料流传给下一个宇宙,那就要另当别论了)。



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

取得目前时间
在所有的UNIX下,都有个time()的函数
#include
time_t time(time_t *t);

这个函数会传回从epoch开始计算起的秒数,如果t是non-null,它将会把时间值填入t中。

对某些需要较高精准度的需求,Linux提供了gettimeofday()。
#include
#include
int gettimeofday(struct timeval * tv,struct timezone *tz);
int settimeofday(const struct timeval * tv,const struct timezone *tz);

struct timeval {
int tv_sec;
int tv_usec;
};

其中tv_sec是由凌晨开始算起的秒数,tv_usec则是微秒(10E-6 second)。

struct timezone {
int tv_minuteswest;
int tv_dsttime;
};

tv_minuteswest是格林威治时间往西方的时差,tv_dsttime则是时间的修正方式。

在Linux下timezone的使用已经废除而不再使用。因为有许多地区都有日光节约时间,日光节约时间的使用与否,往往与无可预测的政治因素相关,没有简单的方法来实作这项设计。

在sys/time.h中,有三个有用的巨集用於操作timeval:
#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
#define timercmp(tvp, uvp, cmp)
((tvp)->tv_sec cmp (uvp)->tv_sec ||
(tvp)->tv_sec == (uvp)->tv_sec &&
(tvp)->tv_usec cmp (uvp)->tv_usec)
#define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)

timerisset检查tvp是否有被设定值进去,timercmp比较时间,timerclear设tvp为零。
cmp为比较操作子如">"、"<"、"=="等等。

在POSIX.1b的即时处理标准中允许较高的时间解析度。

struct timespec
{
long int tv_sec;
long int tv_nsec;
};

tv_nsec是nano second(10E-9 second)。



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

时间表述
电脑使用秒及epoch来表示其时间,但对人脑来说实在太残忍一点,大概没有人可以用人脑来计算。因此,UNIX下提供了其它两种基本方式来表述时间,struct tm及文字格式时间。

struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};

tm_sec表「秒」数,在[0,61]之间,多出来的两秒是用来处理跳秒问题用的。
tm_min表「分」数,在[0,59]之间。
tm_hour表「时」数,在[0,23]之间。
tm_mday表「本月第几日」,在[1,31]之间。
tm_mon表「本年第几月」,在[0,11]之间。
tm_year要加1900表示那一年。
tm_wday表「本第几日」,在[0,6]之间。
tm_yday表「本年第几日」,在[0,365]之间,闰年有366日。
tm_isdst表是否为「日光节约时间」。

struct tm格式时间函数

struct tm * gmtime(const time_t * t);
转换成格林威治时间。有时称为GMT或UTC。

struct tm * localtime(const time_t *t);
转换成本地时间。它可以透过修改TZ环境变数来在一台机器中,不同使用者表示不同时间。

time_t mktime(struct tm *tp);
转换tm成为time_t格式,使用本地时间。

tme_t timegm(strut tm *tp);
转换tm成为time_t格式,使用UTC时间。

double difftime(time_t t2,time_t t1);
计算秒差。

文字时间格式函数

char * asctime(struct tm *tp);
char * ctime(struct tm *tp);
这两个函数都转换时间格式为标准UNIX时间格式。
Mon May 3 08:23:35 1999

ctime一率使用当地时间,asctime则用tm结构内的timezone资讯来表示。

size_t strftime(char *str,size_t max,char *fmt,struct tm *tp);
strftime有点像sprintf,其格式由fmt来指定。

%a : 本第几天名称,缩写。
%A : 本第几天名称,全称。
%b : 月份名称,缩写。
%B : 月份名称,全称。
%c : 与ctime/asctime格式相同。
%d : 本月第几日名称,由零算起。
%H : 当天第几个小时,24小时制,由零算起。
%I : 当天第几个小时,12小时制,由零算起。
%j : 当年第几天,由零算起。
%m : 当年第几月,由零算起。
%M : 该小时的第几分,由零算起。
%p : AM或PM。
%S : 该分钟的第几秒,由零算起。
%U : 当年第几,由第一个日开始计算。
%W : 当年第几,由第一个一开始计算。
%w : 当第几日,由零算起。
%x : 当地日期。
%X : 当地时间。
%y : 两位数的年份。
%Y : 四位数的年份。
%Z : 时区名称的缩写。
%% : %符号。

char * strptime(char *s,char *fmt,struct tm *tp);
如同scanf一样,解译字串成为tm格式。

%h : 与%b及%B同。
%c : 读取%x及%X格式。
%C : 读取%C格式。
%e : 与%d同。
%D : 读取%m/%d/%y格式。
%k : 与%H同。
%l : 与%I同。
%r : 读取"%I:%M:%S %p"格式。
%R : 读取"%H:%M"格式。
%T : 读取"%H:%M:%S"格式。
%y : 读取两位数年份。
%Y : 读取四位数年份。



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

进入「冬眠状态」:Sleeping
unsigned int sleep(unsigned int seconds);
sleep()会使目前程式陷入「冬眠」seconds秒,除非收到「不可抵」的信号。
如果sleep()没睡饱,它将会返回还需要补眠的时间,否则一般返回零。

void usleep(unsigned long usec);
usleep与sleep()类同,不同之处在於秒的单位为10E-6秒。

int select(0,NULL,NULL,NULL,struct timeval *tv);
可以利用select的实作sleep()的功能,它将不会等待任何事件发生。

int nanosleep(struct timespec *req,struct timespec *rem);
nanosleep会沉睡req所指定的时间,若rem为non-null,而且没睡饱,将会把要补眠的时间放在rem上。



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

定时闹钟:Interval Timers
定时闹钟一但启动後,会定期送信号给行程,读者最好要解一下signal的处理。

struct itimerval {
struct timeval * it_interval;
struct timeval * it_value;
};

unsigned int alarm(unsigned int seconds);
alarm()会在seconds时,送出SIGALRM信号,这不是「定期」的。

int getitimer(int which,struct itimerval *val);
读取which指定的Timer目前状态。

int setitimer(int which,struct itimerval *val,struct itimerval *old);
设定which指定的Timer目前状态。

每个行程都有三个定期闹钟(which参数):
ITIMER_REAL :
以系统真实的时间来计算,它送出SIGALRM信号。

ITIMER_VIRTUAL :
以该行程真正有执行的时间来计算,它送出SIGVTALRM信号。

ITIMER_PROF :
以行程真正有执行及在核心中所费的时间来计算,它送出SIGPROF信号。



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

人类的极限:由於太空充斥各种辐射线,太空飞行会无可避免地,造成太空人每年约1%的脑细胞坏死,在他完成任务之前,已经变成白了。因此,人类无法长期进行太空探险。
解决方式:派道家炼气士出太空任务。

Linux程式设计-30.使用者资讯管理(pwd)
http://www.openchess.org/noitatsko/programming/ (2001-05-27 21:04:00)
pwd.h

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

要存取使用者帐号资讯,可以使用pwd.h来取用/etc/passwd内的资讯。
POSIX.1中把/etc/passwd称为user database file.

在POSIX.1中其实仅定义了五个栏位,不过在SVR4及4.3+BSD中,都支援七个栏位。



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

struct passwd
{
char * pw_name; /* Username, POSIX.1 */
char * pw_passwd; /* Password */
__uid_t pw_uid; /* User ID, POSIX.1 */
__gid_t pw_gid; /* Group ID, POSIX.1 */
char * pw_gecos; /* Real Name or Comment field */
char * pw_dir; /* Home directory, POSIX.1 */
char * pw_shell; /* Shell Program, POSIX.1 */
};


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

当您需要取得有关某个使用者的资讯时,大致上有以下几个函数可以使用:

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

struct passwd * getpwuid(uid_t uid);
当您知道使用者的uid(user id)时,可以透过getpwuid来得知所有关於该使用者的相关资讯。



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

struct passwd * getpwnam(char * name);
当您知道使用者名称时,可以透过getpwnam来得知所有关於该使用者的相关资讯。



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

int getpw(uid_t uid, char *buf);
当您仅需要取得使用者的密码进行比对时,可以使用getpw。



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

另外,有存取一系列使用者资讯的方法。


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

FILE * pwdopen(void);
开启password档案。



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

struct passwd * pwdread(FILE * stream,struct passwd *p);
读取一个使用者资讯进来,填到p中,返回p为成功,NULL为失败。



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

void setpwent(void);
将读取资料流重设到起点。



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

void endpwent(void);
关闭password档案资料流。



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

struct passwd * getpwent(void);
读取一个使用者资讯进来,有必要的话,则将进行开档动作。



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

struct passwd * fgetpwent(FILE * stream);
从档案中读取一个使用者资讯进来。



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

int putpwent(struct passwd *p,FILE *f);
将一个使用者资讯写入档案中。



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

struct passwd * pwdalloc(void);
配置一个记忆体区块给passwd用。



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


Linux程式设计-31.工作群资讯管理(grp)
http://www.openchess.org/noitatsko/programming/ (2001-05-27 22:08:00)
grp.h

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





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

struct group
{
char * gr_name;
char * gr_passwd;
gid_t gr_gid;
char ** gr_mem;
};


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





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

FILE * grpopen(void);
开启/etc/group档。



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

struct group * grpread(FILE *stream,struct group *grp);
读取一个group资讯。



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

struct group * grpalloc(void);
配置group记忆体。



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

void setgrent(void);
将group档资料流设定到开始处。



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

void endgrent(void);
关闭group档。



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

struct group * getgrent(void);
读取一项group资讯。



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

struct group * fgetgrent(FILE * stream);
从档案中读取一项group资讯。



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

struct group * getgrgid(gid_t gid);
根据gid读取group资讯。



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

struct group * getgrnam(char *name);
根据group name读取group资讯。



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

int setgroups(size_t n,const gid_t * groups);
???



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

int initgroups(const char *user,gid_t group);
???



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





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

struct group * grpscan(...);
???