免费注册 查看新帖 |

Chinaunix

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

[进程管理] 线程中fork进程提示【Cannot allocate memory】是否与线程库相关? [复制链接]

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
11 [报告]
发表于 2014-06-03 12:27 |只看该作者
不对,2中考虑的是虚拟内存占用量,而不是物理内存,虚拟内存占用通常要比物理内存大很多,也就是说当设置2时,如果被fork进程的虚拟地址空间占用很大就可能导致失败的情况。
2中主要考虑的是物理内存,只有还有空闲的物理内存,而不管虚拟内存占用多少,应该都能分配到。
1则是另一种极端,如果需要保证一定不会分配失败的话,就设置1,当然当内存确是不足时,这也只是推迟了失败而已。
0是相对折中的方案,也是标准方式。

论坛徽章:
0
12 [报告]
发表于 2014-07-10 14:12 |只看该作者
回复 11# humjb_1983


    不好意思,之前帐号名记错了一直登录不了,今天试了下是之前把下划线记成点了,郁闷

/ #
/ # cat /proc/meminfo
MemTotal:         125172 kB
MemFree:           54036 kB
Buffers:            9156 kB
Cached:            37484 kB
SwapCached:            0 kB
Active:            24592 kB
Inactive:          39216 kB
Active(anon):      17168 kB
Inactive(anon):       32 kB
Active(file):       7424 kB
Inactive(file):    39184 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:             0 kB
SwapFree:              0 kB
Dirty:                 0 kB
Writeback:             0 kB
AnonPages:         17184 kB
Mapped:             5460 kB
Shmem:                32 kB
Slab:               3680 kB
SReclaimable:       1160 kB
SUnreclaim:         2520 kB
KernelStack:         896 kB
PageTables:          876 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:      112652 kB
Committed_AS:      53996 kB
VmallocTotal:     778240 kB
VmallocUsed:        4988 kB
VmallocChunk:     765948 kB
/ #
/ # cat /proc/sys/vm/overcommit_memory
2
/ # cat /proc/sys/vm/overcommit_ratio
90
/ #
/ # free
             total         used         free       shared      buffers
Mem:        125172        71144        54028            0         9156
-/+ buffers:              61988        63184
Swap:            0            0            0
/ #


大富大贵兄,上面红色表示就是设备当前最大可用虚拟内存吧,紫色就是表示已经使用的虚拟内存吧?   目测剩余space还挺多的啊。

论坛徽章:
0
13 [报告]
发表于 2014-07-10 14:15 |只看该作者
回复 11# humjb_1983


    因为当前环境是嵌入式环境,所以很多东西我自己也改不了,没有/etc/sysctl.conf文件。

请问还有什么其他方法来修改这个数值吗?

/etc #
/etc # pwd
/etc
/etc # ls -l
-rwxrwxrwx    1 root     root          2870 Mar  5 10:51 S_udev
-rwxrwxrwx    1 root     root             7 Sep  9  2011 TZ
drwxrwxrwx    2 root     root             0 Mar 26 03:02 dropbear
-rwxrwxrwx    1 root     root            35 Sep  9  2011 group
-rwxrwxrwx    1 root     root             0 Sep  9  2011 hosts
-rwxrwxrwx    1 root     root           265 Jan  6  2014 inetd.conf
-rwxrwxrwx    1 root     root           329 Jan  7  2014 inittab
-rwxrwxrwx    1 root     root            82 Jul 10 03:48 passwd
-rwxrwxrwx    1 root     root            83 Sep  9  2011 passwd_nfs
-rwxrwxrwx    1 root     root          1508 Mar  5 10:51 profile
-rwxrwxrwx    1 root     root            40 Jul 10 03:48 resolv.conf
-rwxrwxrwx    1 root     root           362 Sep  9  2011 services
drwxrwxrwx    3 root     root             0 Jul 25  2012 udev
-rwxrwxrwx    1 root     root           226 Sep  9  2011 udev.conf

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
14 [报告]
发表于 2014-07-10 14:30 |只看该作者
Swayer_Chen 发表于 2014-07-10 14:15
回复 11# humjb_1983

手工echo 就可以
不行的话,把echo加到/etc/profile也行

论坛徽章:
0
15 [报告]
发表于 2014-07-10 15:05 |只看该作者
回复 14# humjb_1983




修改过后,在开启应用程序,ok,没有fork失败。

感谢大富大贵兄的帮助。

1016 root      268m S    {openplatform_de} /home/centaurus
1017 root      1080 S    sh -c openplatform_demo
1018 root       668 S    openplatform_demo
1019 root       668 S    openplatform_demo
1020 root       668 S    openplatform_demo
1021 root       668 S    openplatform_demo
1022 root       668 S    openplatform_demo

程序已经启动!
  1.         if((pid = fork())<0)
  2.         {
  3.                 PSIA_EXP_DBG(SYS_ERROR, "%s fork  [%s]!!!\n", __FUNCTION__, strerror(errno));
  4.                 (void)fresh_app_runstatus(FALSE, appname_tmp);
  5.                 status = -1;
  6.         }
  7.         else if(pid == 0)
  8.         {
  9.                 execl("/bin/sh", "sh", "-c", appname_tmp, (char *)0);
  10.                 exit(127); //子进程正常执行则不会执行此语句
  11.         }
复制代码
上面代码启动进程,为什么还是会出现启动一个shell去执行程序呢(sh -c那个东西)? 难道是execl函数选错了?
      

论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
16 [报告]
发表于 2014-07-10 15:12 |只看该作者
Swayer_Chen 发表于 2014-07-10 15:05
回复 14# humjb_1983

shell脚本执行本来就是这样的,自己会fork子进程执行。
如果是想执行脚本的话,就没有必要自己fork了,直接system不就可以了?

论坛徽章:
0
17 [报告]
发表于 2014-07-10 15:27 |只看该作者
回复 16# humjb_1983


我执行的程序是一个可执行程序,不是shell脚本0,可执行程序放在flash中,通过软件接搞出来的一个命令,就是程序里面的appname_tmp

期望的是直接启动一个进程,而不需要sh -c这个东西。
  1.         if((pid = fork())<0)
  2.         {
  3.                 PSIA_EXP_DBG(SYS_ERROR, "%s fork  [%s]!!!\n", __FUNCTION__, strerror(errno));
  4.                 (void)fresh_app_runstatus(FALSE, appname_tmp);
  5.                 status = -1;
  6.         }
  7.         else if(pid == 0)
  8.         {
  9.                 execvp(appname_tmp, (char *)0);
  10.                 exit(127); //子进程正常执行则不会执行此语句
  11.         }
复制代码
这样搞,把execl改为execvp就行了。

  321 root      268m S    {openplatform_de} /home/centaurus
  322 root       668 S    [openplatform_de]
  323 root       668 S    [openplatform_de]
  324 root       668 S    [openplatform_de]
  325 root       668 S    [openplatform_de]
  326 root       668 S    [openplatform_de]



论坛徽章:
15
射手座
日期:2014-02-26 13:45:082015年迎新春徽章
日期:2015-03-04 09:54:452015年辞旧岁徽章
日期:2015-03-03 16:54:15羊年新春福章
日期:2015-02-26 08:47:552015年亚洲杯之卡塔尔
日期:2015-02-03 08:33:45射手座
日期:2014-12-31 08:36:51水瓶座
日期:2014-06-04 08:33:52天蝎座
日期:2014-05-14 14:30:41天秤座
日期:2014-04-21 08:37:08处女座
日期:2014-04-18 16:57:05戌狗
日期:2014-04-04 12:21:33技术图书徽章
日期:2014-03-25 09:00:29
18 [报告]
发表于 2014-07-10 15:40 |只看该作者
呵呵,这样就对了嘛~

论坛徽章:
0
19 [报告]
发表于 2014-07-10 15:52 |只看该作者
回复 18# humjb_1983


    OK,反正这个问题总算解决了,十分感谢!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP