Skip navigation.

西邮Linux兴趣小组

Posts tagged with "Linux"

Linux, Clocks, and Time

, ,

文章来源:http://www.linuxsa.org.au/tips/time.html

Introduction

This document explains how to set your computer's clock from Linux, how to set your timezone, and other stuff related to Linux and how it does its time-keeping.

Your computer has two timepieces; a battery-backed one that is always running (the ``hardware'', ``BIOS'', or ``CMOS'' clock), and another that is maintained by the operating system currently running on your computer (the ``system'' clock). The hardware clock is generally only used to set the system clock when your operating system boots, and then from that point until you reboot or turn off your system, the system clock is the one used to keep track of time.

On Linux systems, you have a choice of keeping the hardware clock in UTC/GMT time or local time. The preferred option is to keep it in UTC because then daylight savings can be automatically accounted for. The only disadvantage with keeping the hardware clock in UTC is that if you dual boot with an operating system (such as DOS) that expects the hardware clock to be set to local time, the time will always be wrong in that operating system.

Setting your timezone

The timezone under Linux is set by a symbolic link from /etc/localtime[1] to a file in the /usr/share/zoneinfo[2] directory that corresponds with what timezone you are in. For example, since I'm in South Australia, /etc/localtime is a symlink to /usr/share/zoneinfo/Australia/South. To set this link, type:
ln -sf ../usr/share/zoneinfo/your/zone /etc/localtime

Replace your/zone with something like Australia/NSW or Australia/Perth. Have a look in the directories under /usr/share/zoneinfo to see what timezones are available.

[1] This assumes that /usr/share/zoneinfo is linked to /etc/localtime as it is under Red Hat Linux.

[2] On older systems, you'll find that /usr/lib/zoneinfo is used instead of /usr/share/zoneinfo. See also the later section ``The time in some applications is wrong''.

Setting UTC or local time

When Linux boots, one of the initialisation scripts will run the /sbin/hwclock program to copy the current hardware clock time to the system clock. hwclock will assume the hardware clock is set to local time unless it is run with the --utc switch. Rather than editing the startup script, under Red Hat Linux you should edit the /etc/sysconfig/clock file and change the ``UTC'' line to either ``UTC=true'' or ``UTC=false'' as appropriate.

Setting the system clock

To set the system clock under Linux, use the date command. As an example, to set the current time and date to July 31, 11:16pm, type ``date 07312316'' (note that the time is given in 24 hour notation). If you wanted to change the year as well, you could type ``date 073123161998''. To set the seconds as well, type ``date 07312316.30'' or ``date 073123161998.30''. To see what Linux thinks the current local time is, run date with no arguments.

Setting the hardware clock

To set the hardware clock, my favourite way is to set the system clock first, and then set the hardware clock to the current system clock by typing ``/sbin/hwclock --systohc'' (or ``/sbin/hwclock --systohc --utc'' if you are keeping the hardware clock in UTC). To see what the hardware clock is currently set to, run hwclock with no arguments. If the hardware clock is in UTC and you want to see the local equivalent, type ``/sbin/hwclock --utc''

The time in some applications is wrong

If some applications (such as date) display the correct time, but others don't, and you are running Red Hat Linux 5.0 or 5.1, you most likely have run into a bug caused by a move of the timezone information from /usr/lib/zoneinfo to /usr/share/zoneinfo. The fix is to create a symbolic link from /usr/lib/zoneinfo to /usr/share/zoneinfo: ``ln -s ../share/zoneinfo /usr/lib/zoneinfo''.

Summary

* /etc/sysconfig/clock sets whether the hardware clock is stored as UTC or local time.

* Symlink /etc/localtime to /usr/share/zoneinfo/... to set your timezone.

* Run ``date MMDDhhmm'' to set the current system date/time.

* Type ``/sbin/hwclock --systohc [--utc]'' to set the hardware clock.

Other interesting notes

The Linux kernel always stores and calculates time as the number of seconds since midnight of the 1st of January 1970 UTC regardless of whether your hardware clock is stored as UTC or not. Conversions to your local time are done at run-time. One neat thing about this is that if someone is using your computer from a different timezone, they can set the TZ environment variable and all dates and times will appear correct for their timezone.

If the number of seconds since the 1st of January 1970 UTC is stored as an signed 32-bit integer (as it is on your Linux/Intel system), your clock will stop working sometime on the year 2038. Linux has no inherent Y2K problem, but it does have a year 2038 problem. Hopefully we'll all be running Linux on 64-bit systems by then. 64-bit integers will keep our clocks running quite well until aproximately the year 292271-million.

Other programs worth looking at

* rdate - get the current time from a remote machine; can be used to set the system time.

* xntpd - like rdate, but it's extremely accurate and you need a permanent 'net connection. xntpd runs continuously and accounts for things like network delay and clock drift, but there's also a program (ntpdate) included that just sets the current time like rdate does.

Further information

* date(1)

* hwclock(8)

* /usr/doc/HOWTO/mini/Clock

[C代码示例]mmap的使用和Linux下的错误处理

, ,

下面一段代码的作用是用mmap实现文件的拷贝,它可以给你演示mmap的使用,同时也告诉你在Linux上如何正确有效地处理错误。(代码可以在这里下载:http://wangcong.org/src/mmcpy.c

读完这段代码,请试着回答后面的几个问题。
/*
 * Copyright (C) WANG Cong, Apr. 2007.
 * GPLv2 applies.
 */
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
int infd, outfd;
void *from, *to;
struct stat infstat;
int ret = 0;
if(argc != 3){
fprintf(stderr, "Bad usage!\n");
ret = -1;
goto fail;
}
infd = open(argv[1], O_RDWR| O_CREAT, 0600);
if(infd == -1){
perror("Open failed");
ret = -1;
goto fail;
}
outfd = open(argv[2], O_RDWR| O_CREAT, 0600);
if(outfd == -1){
perror("Open failed");
ret = -1;
goto close_fail1;
}
if(fstat(infd, &infstat)){
perror("fstat failed");
ret = -1;
goto close_fail2;
}
if(lseek(outfd, infstat.st_size-1, SEEK_SET) == (off_t) -1){
perror("lseek failed");
ret = -1;
goto close_fail2;
}
if(write(outfd, "", 1) != 1){
perror("write failed");
ret = -1;
goto close_fail2;
}
if(lseek(outfd, 0, SEEK_SET) == (off_t) -1){
perror("lseek failed");
ret = -1;
goto close_fail2;
}
from = mmap(0, (size_t)infstat.st_size, PROT_READ,  MAP_PRIVATE | MAP_NORESERVE, infd, 0);
if(from == (void*) -1){
perror("mmap failed");
ret = -1;
goto close_fail2;
}
to = mmap(0, (size_t)infstat.st_size, PROT_WRITE, MAP_SHARED, outfd, 0);
if(to == (void*) -1){
perror("mmap failed");
ret = -1;
goto unmap_fail1;
}
memcpy(to, from, infstat.st_size);
if( -1 == msync(to, (size_t)infstat.st_size, MS_SYNC)){
perror("msync failed");
ret = -1;
}
if (-1 == munmap(to, infstat.st_size)){
perror("munmap failed");
ret = -1;
}

unmap_fail1:
if( -1 == munmap(from, infstat.st_size)){
perror("munmap failed");
ret = -1;
}

close_fail2:
if( -1 == close(outfd)){
perror("close failed");
ret = -1;
}

close_fail1:
if( -1 == close(infd)){
perror("close failed");
ret = -1;
}
fail:
exit(ret);
}
问题:
1. mmap利用了操作系统中的什么原理?
2. 我们知道好的程序应该避免goto的使用,这里的goto没有问题吗?
3. 为什么这种情况下goto比单纯的return要更好?
4. 这种情况下的错误处理有什么特点?

GNOME 操作快捷键

,

Alt+F1 打开开始菜单
Alt+F2 打开运行对话框
Print Screen 整个桌面截图
Alt+Print Screen 当前窗口截图
Ctrl+Alt+Arrow keys 在不同工作区跳转
Ctrl+Alt+D 显示桌面
Alt+Tab 不同的窗口之间跳转
Ctrl+Alt+Tab 在不同窗口之间跳转,含启动工具栏,Shift可反向
Alt+F4 关闭当前窗口
Alt+F5 恢复当前窗口,如果它被最大化的话
Alt+F8 改变当前窗口大小,方向键操作大小
Alt+F9 最小化当前窗口
Alt+F10 最大化当前窗口
Alt+Spacebar 打开窗口菜单
Shift+Ctrl+Alt+Arrow keys 把当前窗口移到别的工作区
Shift+F10 打开上下文菜单

写给初学者:Linux下的软件安装

, ,

作者:西邮 王聪

如果你获得的是普通的源代码压缩包(*.tar/*.tar.gz/*.tar.bz2),请先使用相应的命令把它解压到一个临时目录。具体命令如下:

对于*.tar文件: tar -xvf package_name.tar
对于*.tar.gz文件: tar -xvzf package_name.tar.gz
对于*.tar.bz2文件: tar -xvjf package_name.tar.bz2
对于*.bz2文件: bzip2 -d package_name.bz2
对于*.bz文件: bzip -d package_name.bz
对于*.gz文件: gzip -d package_name.gz
对于*.zip文件: unzip package_name.zip
对于*.rar文件: rar x package_name.rar

然后进入该目录(cd your-path),在此目录下依次执行如下命令(一般情况都如此,特殊情况见后面的解释):

./configure
make
make install(<-- 此命令可能需要root权限,请注意)
我解释一下各个命令的作用:

./configure是执行当前目录中的名为configure的脚本(已经存在,注意"."代表当前目录,而".."代表当前目录的父目录──即上一层),它的作用是检查你的机器配置(比如:你使用的是什么操作系统,是Unix还是Linux;你使用的是什么芯片,是Intel还是PowerPC;你的机器上有没有安装gcc编译器,它用来编译C/C++/Java代码;你的机器上有没有安装这个软件需要的库,比如glib等),它会根据实际情况生成相应的makefile(什么是makefile下面有解释)。如果有不符合安装条件的情况,它会停止。这时请检查错误提示。

make是在当前目录中查找一个名为makefile(上一步生成的)的文件,此文件指定了安装这个软件需要的执行的一些命令,比如用什么编译,用什么选项编译,编译哪些文件等等(如果你想学习Linux开发你肯定要学习makefile的书写规则,其实一些优秀的Windows程序员也是自己写makefile的)。这一步完成后它会生成一些相应的目标文件和二进制文件(这些在安装完成后可以清除,见下面),为下一步的安装(make install)做准备。除非编译过程中出现问题(很少出现),它不会失败。

make install其实也是在执行make命令,它和上一步的相同之处是都是执行的当前目录中文件makefile中的命令,不同之处是它是执行的名为“install”的动作(动作就是一系列的命令,当然也是在makefile文件中,动作可以有不同的名称,比如install,clean,uninstall)。它的作用一般是进行进一步的编译,把编译好的二进制文件或其它配置文件放到特定目录(比如:/bin,/usr/bin,/etc,因为这些目录的写入需要root权限,所以一般要用root权限来运行此命令)或其它动作。这一步执行完这个软件就算是安装完成了。

如果你注意观察,你会发现,执行了上面的命令后此目录中还有一些不再使用的目标文件或其它一些临时文件,是的,你可以删除它们。执行make clean即可。同理,如果你想卸载此软件,执行make uninstall。

Linux的安装程序也有二进制形式,比如Redhat/Suse上的*.rpm格式和Debian/Ubuntu上的*.deb格式,它们可以直接像在Windows上那样在图形界面下安装。在命令行下请使用下面的命令:

对于rpm包:rpm -ivh package_name.rpm
对于deb包:dpkg -i package_name.deb

当然会有例外!Linux上的realplayer的安装程序是.bin的二进制形式,它的安装很简单直接运行即可(./realplayer.bin)。还有一些软件提供的安装程序是.sh(shell脚本),它的作用是代替makefile来执行相应的动作。这类安装方式的缺点也是显然的,灵活性太差,它们只能适应一种构架,所以如果软件提供商提供这种方式,它一般会为各个平台提供一种安装程序(所以在下载时请注意是否是你使用的平台上的安装包),而且这种发布软件的方式//一般//表明此软件是封闭源代码的。

可能还会有一些安装方式和上面所提的方式不同,遇到此类情况请仔细查看此软件目录中的名为README或INSTALL等的说明文件,作者会在里面说明相应的安装方法。

写给像我一样的初学者

,

作者:西邮 梅延涛

很高兴在西邮能遇到你们这些有共同爱好(Linux)的朋友们!

我已经大三了,虽然学习Linux已有一年多的,但是,我在学习上走了太多弯路。写这篇感想的目的不是想教你们什么――我没有这个份量――但是,看完我这篇感想,一定能让你们少走许多弯路,至少我不愿意看到你们再走我的老路。

1. 想学好Linux(包括其他计算机知识)要做的第一件事,就是学好English。如果你们看到我现在读资料的窘况,你们就明白英语是何等重要。Linux的大部分有价值的文档都是英文的,没有翻译。等翻译出来了,这些资料也就该过时了。我买了一本Qt3的翻译本,但是,略有常识的人都会知道,Qt4已经出来很久了。Qt3和Qt4在细节上有很大的差别,所以,我不得不把我的翻译本作为参考,硬着头皮去看QT4的下载文档。我的英语很差,虽然借助翻译工具,也能勉强看懂,但是印象不深。所以,我只能用一个本子,从电脑上看一句,再把它默写下来。只有如此,才能既记单词,又对文档内容有一个教深的印象,不至于掰一个玉米就扔一个。其速度,可想而知。

2. 学Linux就一定要学习编程(别担心,编程是对思维的良好训练,即使你以后不从事编程工作学习编程对你也是很有好处的),不然,你大可只用Window XP了。Linux可以更好的完成XP下的一切任务(游戏除外),而且不用太伤神。反着说一句,如果你学习编程,那就一定要学习Linux,不然,你可能永远只会停留在API这些抽象接口上,不会进入操作系统的本质,因为Windows是不开源的。

3. 学习Linux除了要会C这样的高级语言外,你必须熟悉两种格式的汇编语言AT&T和Intel(用nasm汇编)。关于这两种汇编的教材,我有两本非常好,都在我留给本论坛的一个链接里下载,分别是《Professional Assembly Language》(书店有翻译版)和《Guide to Assembly Language Programming in Linux》。请相信,只有熟悉了32位汇编,才能真正理解硬件的基本原理,在计算机体系里学的8086汇编是16位的,而且是基于masm的,基本上对Linux没有太大帮助。

4. 学习一门除C外的能够夸平台的、且有良好库支持的高级语言(这是最低要求),你可以选择Java,它的第三方支持很好,在网络方面很多支持都可能会成为未来的标准,而且这门语言也比较简单。我选择的是C++,这门语言功能十分强大,而且有Qt这种强大的库支持,在各种平台上都非常实用。但是,正如王聪曾经说过的那样,C++太庞大了,如果你选择了它,就不得不花费5倍于Java或C的时间去学习和实践(这个倍数可能有些夸张,但它确实很难)。有人曾经打过比方,C++是一辆驰骋在荒野上的跑车,你不仅要熟悉车的驾驶(熟悉其基本语法和常见支持),也要能辨别驾驶的方向(有较高的软件设计水平),这是技能和思想上的统一。一个意大利的大学老师曾经在ICQ上告诉我:"C++ is only for good programmer.“想必也是这个道理吧。

5. 要习惯阅读电子版的教程,因为Linux的文档不仅翻译的少,就连英文原版也很少在书店里及时找到。我就是不习惯阅读电子版,所以比别人的学习效率差了很多。

6. 学习Linux你要有淡泊名利的精神。因为在中国,人们的思想是以Windows为正统的,所以,你可能不会因为学习Linux而被周围的同学所看好。他们会告诉你Vista是如何的优秀,如何之漂亮,他们会把那些能帮他们杀毒的人最为偶像,而你,不过是一个搞些无用之事的闲散人。其实,如果你能淡泊一切,闲散会是另一种忙碌,在这种闲散的忙碌中,你的计算机水平将在不知不觉中得到提高。

7. Linux是一门复杂的技术,更是一种崇高的文化。Linux中有许多命令,死记硬背是背不完的。多动手,一次记不住,下次只要你知道怎么去查找这个问题的答案了就行了。我是很少去记各种命令的,因为记不住,而且也没必要。Linux的帮助文档如此之多,学习开源的朋友如此之众,随便找个论坛发个帖子,都会有许多人帮你的,这一点个我深有体会。当你有一些适合你的朋友时,你就会知道这种文化是什么了:Linux的世界里没有分数的束缚,没有名次的高低,没有那么多鄙视与功利――只有一声为了理想和兴趣而共同进步的号角。万一有一天你发现有人在你的帖子下鄙视你的话,那也不必介意,他只是一个刚刚入门的菜鸟,因为Linux高手都是有很深的黑客文化修养的人(关于什么是黑客文化,你可以在王聪的blog上找到这个链接),不会这么浮浅。放下所有的负担,去接受这种伟大文化的熏陶吧。

8. 不要在乎你的考试成绩。我一直再考虑要不要在这里贴出这条,因为这么写是很危险的,我极可能会遭到考研的或是品学兼优年年得奖学金的同学之围攻。Linux不是你所想的那么简单,他要求你付出比专业课更多的时间去学习,这样,势必会影响到你们的光辉仕途。请不要以为你们计算机的专业课会对Linux有太大帮助,我虽然是文科学生,但我上过计算机辅修,那些课程的教材都是10年(至少也在5年)以前,与Linux乃至于计算机技术的要求相差深远。关于这一点,我只能说到这里,因为我不想牵进太多是非之中。如果你要批评我的看法,那无论你出于什么样的理由,我都会检讨自己,承认自己的错误。子曾经曰过:“神鬼之事,敬而远之。”对考研和走仕途之路的朋友们,我也“敬而远之”……

9. 我眼中Linux的优势。免费这个我就不说了,这不算什么优点。众所周知,XP在中国也是“免费”的,F8四块钱一张,量多从优。但是,如果你要学习C/C++,Linux下有最好的编译器,而且许多技术性的东西都是从Unix/Linux上移植到Windows下的(游戏除外)。如果你喜欢上网看学习资料,而又担心受到病毒袭击,你就用Linux吧。我还没有见过针对Linux的病毒(注:Linux下是有病毒的,但相对于Windows而言,那就太少了)。此外,可能也是你们最关心的一点:Linux人才的市场需求远大于Windows下的普通程序员。因为Linux提供了许多安全性高,价格便宜而且开放源代码的各种工具。这对于不玩游戏的企业单位来说,实在是太有用了,企业需要它。尽管UNIX还是占有很大的市场份额,但是放心,Linux和UNIX使用的是同一套标准,他们在很大程度上是兼容的。此外,时代已经不同了,如果还想像几年前那样依靠会架个网站或是会写个Windows小程序这样的技术来糊口,那几乎是不可能的。普通的Windows应用程序员和会用Word的人一样多,如果你没有一些系统级和硬件级的软件设计能力,做程序员可能还不如摆地摊儿。而Linux提供了大量的文档和工具(大多是免费的),给你提供了学习这些底层设计的机会。比如现在流行的许多智能手机,内嵌的都是以Linux为雏型的系统。如果你熟悉了kernel,这些对你而言将不再是梦想!

好了,我最后再重申一遍,我和你们一样,也是初学者,文章中有不欠当的地方是再所难免的。欢迎大家指出,我们共同学习!

console与X-window自由切换

,

作者:西邮 董溥

有时候我们开机的时候也许没有必要进入X-window界面。比如,我开机只是为了编译一个东西,或者是写一段程序,我可以在console下可以更高效的完成,例如编译软件的时候使用console会大大的提高编译效率。(甚至我用vi写程序的时候仍然可以使用mplayer播放歌曲。)

安装完一个发行版后,一般默认启动的是x-window,我们可以通过更改
/etc/inittab
中的
id:5:initdefault:
这一行来实现开机默认启动X还是console。(3为console,5为X-window)

当然这有点不太现实,因为我们无法在开机的时候决定这个文件的内容,所以最好的办法是如果能在grub中选择。

下面是实现的办法:
更改/etc/grub.conf文件

title Fedora Core (X-window)
        root (hd0,2)
        kernel /boot/vmlinuz-2.6.18-1.2239.fc5 ro root=LABEL=/12 rhgb quiet vga=0x305
        initrd /boot/initrd-2.6.18-1.2239.fc5.img
title Fedora Core (text console)
        root (hd0,2)
        kernel /boot/vmlinuz-2.6.18-1.2239.fc5 ro root=LABEL=/12 rhgb quiet vga=0x305 3
        initrd /boot/initrd-2.6.18-1.2239.fc5.img

其实很简单的,新增加一个启动的项目,在kernel最后一行加一数字3,进入level3模式(多用户console模式)

vga=0x305代表启动vga图形驱动模式,设置分辨率为0x305(1024x768)

觉得1024x768下的console更cool!

Anatomy of A Linux System

,

看到一张不错的图片,共享之~~


January 2010
S M T W T F S
December 2009February 2010
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30