简体   繁体   English

Popen导致程序死机

[英]Popen causes program to freeze

I have a function that returns the strength of my Wifi signal on a certain interval. 我有一个函数,可以在一定间隔内返回Wifi信号的强度。

FILE *sig;
int sigstr=0, max;
sig = popen("iwconfig wlan0 | awk '/Link/ {print $2}'","r");
while(fgetc(sig)!='=');
fscanf(sig,"%d/%d", &sigstr, &max);
pclose(sig);
return(((sigstr*4)+(max/2))/max);

The problem I am running into is that my program freezes up after a period of time depending on how long the interval is for getting the Wifi signal. 我遇到的问题是程序在一段时间后会冻结,具体取决于获取Wifi信号的间隔时间。 The shorter the interval the faster my program freezes. 时间间隔越短,程序冻结的速度就越快。 Now correct me if I am wrong but this seems to be a problem with memory. 现在纠正我,如果我错了,但这似乎是内存问题。 Where as time progresses some item(s) are not properly removed from the stack or deallocated, leading to memory being filled up and eventually reaching a level where the program can not continue to run. 随着时间的流逝,某些项目没有正确从堆栈中移出或重新分配,导致内存被填满,并最终达到程序无法继续运行的水平。 So my question is apart from what I have up top do I need to include another statement to free file sig or does pclose take care of that? 所以我的问题除了我的专长之外,我还需要包括另一条语句来释放文件sig还是pclose可以解决这个问题?

Thanks! 谢谢!

Its because fscanf can block. 这是因为fscanf可以阻止。 Set your file descriptor obtained from popen to nonblocking using fcntl. 使用fcntl将从popen获得的文件描述符设置为非阻塞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM