免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: yaoaiguo
打印 上一主题 下一主题

linux系统的严重问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-11-22 20:44 |只看该作者
只调用fork不调用exec时,只回拷贝堆栈段,数据段,代码段公用,不拷贝。
如果fork后立即调用exec,那么这时才会根据新代码的大小分配代码空间,
不存在比父进程代码小而浪费空间的问题。如果没有用到父进程的数据,可能
数据段根本就不会拷贝,也想代码段那样新分配,堆栈段估计怎么都会先拷贝,
但是堆栈不存在浪费的问题----个人理解。
而且任何系统调用都可能失败,怎么能说是缺陷呢,fork有专门的返回值是
NOMEM,内存不足,不管你怎么实现这个函数,内存都有可能不足。这个错误
无法100%避免。但是现在的电脑都有虚存,这种概率比较小。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
12 [报告]
发表于 2007-11-23 09:04 |只看该作者

回复 #1 yaoaiguo 的帖子

楼主有点想当然了。

论坛徽章:
0
13 [报告]
发表于 2007-11-23 09:39 |只看该作者
这样的问题当然存在,下面是测试的代码,用fork会失败,用vfork会成功,当然我只实验了一两次,因为我觉得这种情况,vfork也有可能失败。
#include <stdlib.h>
#include <unistd.h>
#define LARGE_MEM 1024 * 1024 * 1024
int main()
{
char* p;
int pid;
p = (char*)malloc(LARGE_MEM);
memset(p, 0, LARGE_MEM);
pid = vfork();
if (pid < 0)
{      
printf("memory is not enough\n");
return 0;
}      
if (pid == 0)
{      
execlp("/bin/ls", "ls", "/", NULL);
exit (0);
}      
wait(NULL);
free(p);
}

论坛徽章:
0
14 [报告]
发表于 2007-11-23 09:41 |只看该作者
我测试机器的情况如下:
             total       used       free     shared    buffers     cached
Mem:        507996     504928       3068          0      36548     336428
-/+ buffers/cache:     131952     376044
Swap:      1052248      73136     979112
平常我们的程序是不会出现这种情况的,但是会有变态的java存在。

论坛徽章:
0
15 [报告]
发表于 2007-11-23 11:45 |只看该作者
原帖由 yaoaiguo 于 2007-11-23 09:39 发表
这样的问题当然存在,下面是测试的代码,用fork会失败,用vfork会成功,当然我只实验了一两次,因为我觉得这种情况,vfork也有可能失败。
#include
#include
#define LARGE_MEM 1024 * 1024 * 1024
int m ...


我在机器上运行, fork, vfork都不会出错.
下面是我机器的配置
             total       used       free     shared    buffers     cached
Mem:       3107224     918352    2188872          0     176392     624696
-/+ buffers/cache:     117264    2989960
Swap:      2096472          0    2096472

下面这段话摘自<Linux Kernel Development>, 是讲的写时复制的内容, 您看看.
Copy-on-Write
Traditionally, upon fork() all resources owned by the parent are duplicated and the copy is given to the child. This approach is significantly na&iuml;ve and inefficient in that it copies much data that might otherwise be shared. Worse still, if the new process were to immediately execute a new image, all that copying would go to waste. In Linux, fork() is implemented through the use of copy-on-write pages. Copy-on-write (or COW) is a technique to delay or altogether prevent copying of the data. Rather than duplicate the process address space, the parent and the child can share a single copy. The data, however, is marked in such a way that if it is written to, a duplicate is made and each process receives a unique copy. Consequently, the duplication of resources occurs only when they are written; until then, they are shared read-only. This technique delays the copying of each page in the address space until it is actually written to. In the case that the pages are never writtenfor example, if exec() is called immediately after fork()they never need to be copied. The only overhead incurred by fork() is the duplication of the parent's page tables and the creation of a unique process descriptor for the child. In the common case that a process executes a new executable image immediately after forking, this optimization prevents the wasted copying of large amounts of data (with the address space, easily tens of megabytes). This is an important optimization because the Unix philosophy encourages quick process execution.

论坛徽章:
0
16 [报告]
发表于 2007-11-23 13:25 |只看该作者
楼上的那段英文看起来比较费劲,需要慢慢研究。
不过你的机器没出问题是因为系统太强大了。看样子有3G内存,2G缓存。
而我的机器有512M内存,1G缓存,所以当malloc 1G内存时候还行,但是再fork 1G的话就失败了。
而且,申请出来的内存不用,是没有效果的,所以我加了一个memset。

论坛徽章:
0
17 [报告]
发表于 2007-11-23 15:56 |只看该作者
学习了~~~~~~~~~~~~~

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
18 [报告]
发表于 2007-11-23 21:45 |只看该作者
原帖由 yaoaiguo 于 2007-11-23 13:25 发表
不过你的机器没出问题是因为系统太强大了。看样子有3G内存,2G缓存。

Are you sure?

BTW, 说说你的内核版本吧?

论坛徽章:
0
19 [报告]
发表于 2007-11-24 12:24 |只看该作者
Linux localhost 2.6.9-5.EL #1 Wed Jan 5 19:22:18 EST 2005 i686 i686 i386 GNU/Linux
LSB Version:    1.3
Distributor ID: RedHatEnterpriseAS
Description:    Red Hat Enterprise Linux AS release 4 (Nahant)
Release:        4
Codename:       Nahant
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP