277
- 收藏
- 点赞
- 分享
- 举报
如何让udhcpc占用更少的内存?
这个问题的表面现象是用system调用的方式执行udhcpc会失败。解释:由于system是通过fork实现的,而子进程会复制父进程的VM空间,当父进程占用较多VM空间,很容易导致system调用失败。其本质是子进程分配VM空间失败导致的。
解决方法:执行:echo 1 > /proc/sys/vm/overcommit_memory即可。
更好的解决办法是不使用system调用方法,而是使用posix_spawn调用,简单的示例如下:
[code]#include
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
pid_t pid;
int err;
char *spawnedArgs[] = {"/bin/ls","-l","/home ",NULL};/* posix_spawn需要指定子进程的命令的全路径(绝对路径) */
char *spawnedEnv[] = {NULL};
printf("Parent process id=%ld\n", getpid());
if( (err=posix_spawn(&pid, spawnedArgs[0], NULL, NULL,
spawnedArgs, spawnedEnv)) !=0 )
{
fprintf(stderr,"posix_spawn() error=%d\n",err), exit(-1);
}
printf("Child process id=%ld\n", pid);
/* Wait for the spawned process to exit */
(void)wait(NULL);
return 0;
}[/code]
posix_spawn的更多用法,请自行上网搜索。
解决方法:执行:echo 1 > /proc/sys/vm/overcommit_memory即可。
更好的解决办法是不使用system调用方法,而是使用posix_spawn调用,简单的示例如下:
[code]#include
#include
#include
#include
#include
#include
int main(int argc, char* argv[])
{
pid_t pid;
int err;
char *spawnedArgs[] = {"/bin/ls","-l","/home ",NULL};/* posix_spawn需要指定子进程的命令的全路径(绝对路径) */
char *spawnedEnv[] = {NULL};
printf("Parent process id=%ld\n", getpid());
if( (err=posix_spawn(&pid, spawnedArgs[0], NULL, NULL,
spawnedArgs, spawnedEnv)) !=0 )
{
fprintf(stderr,"posix_spawn() error=%d\n",err), exit(-1);
}
printf("Child process id=%ld\n", pid);
/* Wait for the spawned process to exit */
(void)wait(NULL);
return 0;
}[/code]
posix_spawn的更多用法,请自行上网搜索。
我来回答
回答0个
时间排序
认可量排序

或将文件直接拖到这里
悬赏:
E币
网盘
* 网盘链接:
* 提取码:
悬赏:
E币
Markdown 语法
- 加粗**内容**
- 斜体*内容*
- 删除线~~内容~~
- 引用> 引用内容
- 代码`代码`
- 代码块```编程语言↵代码```
- 链接[链接标题](url)
- 无序列表- 内容
- 有序列表1. 内容
- 缩进内容
- 图片
相关问答
-
2014-03-14 16:46:49
-
2020-02-29 18:20:36
-
2016-09-28 22:34:06
-
2019-01-21 16:17:11
-
2020-02-29 18:19:07
-
2013-12-02 00:00:27
-
2018-12-11 10:49:33
-
2018-02-03 03:19:30
-
2012-12-05 10:58:49
-
2019-12-03 15:04:50
-
2016-09-12 11:07:06
-
2013-11-28 22:21:04
-
2019-01-11 10:28:02
-
2019-01-16 13:49:15
-
2018-12-27 11:11:38
-
2019-01-07 14:08:17
-
2019-01-11 16:24:37
-
2016-11-10 17:28:57
-
2015-02-03 10:46:53
无更多相似问答 去提问

点击登录
-- 积分
-- E币
提问
—
收益
—
被采纳
—
我要提问
切换马甲
上一页
下一页
举报反馈
举报类型
- 内容涉黄/赌/毒
- 内容侵权/抄袭
- 政治相关
- 涉嫌广告
- 侮辱谩骂
- 其他
详细说明
提醒
你的问题还没有最佳答案,是否结题,结题后将扣除20%的悬赏金
取消
确认
提醒
你的问题还没有最佳答案,是否结题,结题后将根据回答情况扣除相应悬赏金(1回答=1E币)
取消
确认