南方Linux论坛   首页 | 行业 | 下载 | Blog | 桌面应用 | 数据库 | 电子商务 | 文摘 | 网络服务 | 开源 | 系统管理 | 内核代码 | 教程

返回   南方Linux论坛 > Linux基础技术支持区 > 命令行使用与SHELL编程
注册账号 Blog 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记讨论区已读

发表新主题 回复
 
主题工具 显示模式
旧 2007-02-22, 11:55 AM   #1
TOM
级别:10 | 在线时长:143小时 | 升级还需:22小时级别:10 | 在线时长:143小时 | 升级还需:22小时级别:10 | 在线时长:143小时 | 升级还需:22小时级别:10 | 在线时长:143小时 | 升级还需:22小时
论坛义工
 
注册日期: 2006-07-05
帖子: 477
精华: 0
现金: 1632 金币
资产: 1632 金币
声望: 16 TOM 正向着好的方向发展
默认 我是这样学习Linux下C语言编程的-设置工作环境避免死机

在编程的过程中,不可避免地会有一误操作,当然这些误操作编译器是不知道的,你运行gcc命令编译程序,编译通过了生成可运行程序了。这个程序虽然可以运行了,但可能是不正常的。 比如有的程序只顾着打开文件却不关闭文件,因为操作系统会为每个打开的文件分配一个句柄(file descriptor),这样的程序长久运行下去必然导致打开的文件句柄超出系统资源限制。超出系统资源限制的后果可能不仅仅影响到当前用户,整个系统都可能会受影响。
又比如有些程序,比如下面的代码:

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
while(1) if(fork() < 0) {perror("fork"); break;}
return 0;
}

这段代码虽然编译完全正确,也能运行。但它会导致系统死机,因为它不断产生进程,每产生一个进程,系统必然耗费一些资源,最终导致系统资源耗尽而死机。

作为一个初学者,编程过程中发生这些失误再所难免,但如果因为这样的错误就导致我们要不断重新启动计算机,甚至不小心丢失一些还未来得及保存的数据,那就可惜了。

Linux系统中提供了一些保护机制,我们可以避免不必要的麻烦。其中bash提供这样一个ulimit命令就可以用来帮助我们实现资源配置的目的。查看bash手册可以看到这些:

ulimit [-SHacdefilmnpqrstuvx [limit]]
Provides control over the resources available to the shell and to processes started by it, on systems that allow such con‐
trol. The -H and -S options specify that the hard or soft limit is set for the given resource. A hard limit cannot be
increased once it is set; a soft limit may be increased up to the value of the hard limit. If neither -H nor -S is speci‐
fied, both the soft and hard limits are set. The value of limit can be a number in the unit specified for the resource or
one of the special values hard, soft, or unlimited, which stand for the current hard limit, the current soft limit, and no
limit, respectively. If limit is omitted, the current value of the soft limit of the resource is printed, unless the -H
option is given. When more than one resource is specified, the limit name and unit are printed before the value. Other
options are interpreted as follows:
-a All current limits are reported
-c The maximum size of core files created
-d The maximum size of a process’s data segment
-e The maximum scheduling priority (‘nice’)
-f The maximum size of files created by the shell
-i The maximum number of pending signals
-l The maximum size that may be locked into memory
-m The maximum resident set size
-n The maximum number of open file descriptors (most systems do not allow this value to be set)
-p The pipe size in 512-byte blocks (this may not be set)
-q The maximum number of bytes in POSIX message queues
-r The maximum rt priority
-s The maximum stack size
-t The maximum amount of cpu time in seconds
-u The maximum number of processes available to a single user
-v The maximum amount of virtual memory available to the shell
-x The maximum number of file locks

If limit is given, it is the new value of the specified resource (the -a option is display only). If no option is given,
then -f is assumed. Values are in 1024-byte increments, except for -t, which is in seconds, -p, which is in units of
512-byte blocks, and -n and -u, which are unscaled values. The return status is 0 unless an invalid option or argument is
supplied, or an error occurs while setting a new limit.


这是一个bash内嵌命令(要通过man bash命令才能查看到)。用来设置当前shell及其开启的进程可使用的资源的。 你可以用下列命令试一下:

ulimit -a

这个命令显示所有当前资源限制情况。
限制有两种,一种是软限制(soft limit),另一种是硬限制(hard limit)。比如:

ulimit -S -f 1024

这个命令将限制当前shell创建的文件大小为1024K,所以你可能没办法产生一个超过1M的文件。但这是软限制。又比如:

ulimit -H -u 100

这个命令将设置系统中当前用户能够开启的进程最多为100个,所以这一设置上面那个fork程序就不会让系统死机了。

软限制是可以突破的,而硬限制是不能突破的,软限制还可以突破到硬限制设定的值来。

用ulimit的另外一个好处就是设置系统资源以满足进行特定测试。比如我们想要调试程序,可能有些系统默认是限制产生core dump文件的,而我们知道,core dump文件是我们调试程序的一个很有用的帮助文件,根据core dump文件提供的信息,我们可以比较快地定位到程序的bug。
如果用命令:

ulimit -c

看到的是0,则说明系统限制了产生core dump,我们可以设定一个文件大小以产生core dump文件。比如:

ulimit -c 1024

将设置允许产生的core dump文件最大为1024K
当然我们也可以设置为

ulimit -c unlimited

表示不限制产生的core dump文件的大小。

转贴:广东Linux中心 - 作者: 周立发

此帖于 2007-02-22 12:05 PM 被 TOM 编辑.
TOM 当前离线  
回复时引用此帖
发表新主题 回复


当前查看此主题的会员: 1 (0 位会员和 1 位游客)
 
主题工具
显示模式

发帖规则
不可以发表新主题
不可以回复主题
不可以上传附件
不可以编辑您的帖子

论坛启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码


所有时间均为北京时间。现在的时间是 04:40 AM


vBulletin 3.6.8 Powered by 南方Linux联盟
版权所有 ©2004 - 2009, bbs.linuxunion.net