全球第一免费源码下载基地!
首 页文章中心下载中心繁體中文
首页 ASP源码 | PHP源码 | CGI源码 | JSP源码 | .NET源码 | flash源码 | 其它源码 | 网页模板 | 常用软件 | 字体下载 | 视频教程
站长学院 | 网络编程 | 网页设计 | 图形图像 | 操作系统 | 数据库 | 多媒体 | 黑客攻防 | 编程开发 | Seo专区 | 小说连载
您当前的位置:中文源码之家 -> 站长学院 -> 软件教学 -> 文章内容 退出登录 用户管理
栏目导航
· 软件报道 · 软件教学
· 游戏娱乐 · 小说连载
热门文章
· [推荐] 卡巴斯基v6.0..
· [新闻] 超级解霸3500..
· [推荐] 冰点还原精灵..
· [组图] 一键GHOST硬盘..
· [组图] PQMagic──硬..
· [新闻] 2006年世界顶..
· [图文] 一键GHOST 优..
· [推荐] 征途小宝外挂..
· [新闻] 下载:IE 7 简..
· ASP自动解压RAR文件..
相关文章
· [图文] 你会这样远程..
· Windows XP操作系统..
精彩激情游戏推荐
这样学习Linux下C语言编程的(设置工作环境避免死机)
作者:本站  来源:不详  发布时间:2007-2-14 14:41:07  发布人:hncj

减小字体 增大字体

在编程的过程中,不可避免地会有一误操作,当然这些误操作编译器是不知道的,你运行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文件的大小。
[] [返回上一页] [打 印] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图
Copyright 2005-2006 Chcodes.Com.中文源码之家 All Rights Reserved .
Powered by:NewAsp SiteManageSystem Version 2.1 SP1 1030