C sh:系统调用的内存不足
我正在基于ARM的板上运行以下代码:C sh:系统调用的内存不足,c,linux,segmentation-fault,arm,sh,C,Linux,Segmentation Fault,Arm,Sh,我正在基于ARM的板上运行以下代码: void MainLoop() { char command[256]; int ret = 0; int loopCount = 0; while(1) { memset(command, '\0', sizeof(command)); sprintf(command, "/usr/bin/gst-launch-0.10 imxv4l2src ! imxv4l2sink &")
void MainLoop()
{
char command[256];
int ret = 0;
int loopCount = 0;
while(1)
{
memset(command, '\0', sizeof(command));
sprintf(command, "/usr/bin/gst-launch-0.10 imxv4l2src ! imxv4l2sink &");
ret = system(command);
printf("StartStreamer: command=[%s], status:%d\n", command, ret);
if ( ret != 0 )
{
ret = system("reboot");
printf("Rebooting:%d\n", ret);
}
sleep(15);
memset(command, '\0', sizeof(command));
sprintf(command, "killall gst-launch-0.10");
ret = system(command);
printf("StopStreamer: command=[%s], status = %d\n", command, ret);
if ( ret != 0 )
{
ret = system("reboot");
printf("Rebooting:%d\n", ret);
}
sleep(15);
loopCount++;
printf("Loop Count:%d\n", loopCount);
}
}
运行一些随机循环后,我得到以下错误:
sh: out of memory
StartStreamer: command=[/usr/bin/gst-launch-0.10 imxv4l2src ! imxv4l2sink &], status:256
Segmentation fault
Rebooting:35584
StopStreamer: command=[killall gst-launch-0.10], status = 11
Rebooting:11
Loop Count:26
sh: relocation error: sh: symbol free, version GLIBC_2.4 not defined in file libc.so.6 with link time reference
sh: relocation error: sh: symbol free, version GLIBC_2.4 not defined in file libc.so.6 with link time reference
StartStreamer: command=[/usr/bin/gst-launch-0.10 imxv4l2src ! imxv4l2sink &], status:32512
Rebooting:11
killall: gst-launch-0.10: no process killed
StopStreamer: command=[killall gst-launch-0.10], status = 11
Rebooting:11
Loop Count:27
StartStreamer: command=[/usr/bin/gst-launch-0.10 imxv4l2src ! imxv4l2sink &], status:32512
Rebooting:32512
你能告诉我“sh:内存不足”是什么意思吗?是不是因为太多的系统调用。。而且,我在glibc中发现了重新定位错误
我已将C应用程序修改为Bash脚本:
#!/bin/ash
count=0
while [ true ];do
echo "Starting Streamer"
/usr/bin/gst-launch-0.10 imxv4l2src ! imxv4l2sink &
sleep 15
echo "Stopping Streamer"
killall gst-launch-0.10
sleep 15
count=$((count+1))
echo $count
done
在运行一些循环之后,我得到以下错误:
*在/bin/sh中出错:双重释放或损坏(输出):0x0028ebf8***
***
/bin/sh:malloc()中出错:内存损坏:0x0028edf8*
你能告诉我“sh:内存不足”是什么意思吗?是不是因为太多的系统调用
从系统
手册页:
The system() library function uses fork(2) to create a child
process that executes the shell command specified in command using
execl(3) as follows:
execl("/bin/sh", "sh", "-c", command, (char *) 0);
system() returns after the command has been completed.
我想说这就是你看到这条信息的原因。显然,fork
部分的资源已经用完了。很难说这是遇到了rlimit还是仅仅是内存不足(更有可能)
所以基本上你有一个主循环,它调用system
,它“在引擎盖下”创建一个进程的副本,并将二进制文件(execl部分)复制到副本中。哦,您尝试终止第一个进程(再次通过系统
)。不幸的是,信号不能保证是可靠的,但通常是被接收到的。另一个复杂性是,您受调度程序的支配,调度程序可能不会在您认为会运行该进程时运行该进程
最好使用更原始的fork
/exec
函数来访问此功能——如果不是您的程序滥用内存,至少您可以有PID来发送信号,而不会产生killall
的开销,那么一定是你启动的某个程序导致电路板内存不足。你能告诉我检查程序内存使用情况的命令吗?这与使用PID的killall和killing不一样。在前一种情况下,killall正在检索PID..不是真的kill
在这种情况下,是另一个关闭了fork
的进程(使用更多内存),它读取进程表,搜索匹配项以向PID发送信号(例如kill
)kill
不会fork
,避免开销和内存使用。你是说将kill与system()一起使用会创建两个进程,一个由system创建,另一个由kill创建,而使用kill与system()一起使用时,只会创建一个进程。。。请确认。我不是在说,我是在说(它向进程发送信号)。使用system
调用的任何东西都将进入fork
(创建执行过程的副本)。