Hi Guys
i met a problem when use popen and pclose, the child command process
"sh -c ifconfig | sed -n '/eth1/,$p' | sed -n '1,8p' | grep 'inet addr' | awk '{print $2}' will never exit and cost CPU high usage after several days. i think two possible reason may lead to this problem:
1) pclose may not terminate the child process successfully and hanged.
2) command "sh -c ifconfig |..."is not executed successfully. but i try this command again, it works normal.
i perfer to the first reason, but i am not sure it and don't know why.
Could anyone can give some advice? Thank you!
the code is as follows:
Code:
sprintf(command, "ifconfig | sed -n '/s/,$p' | sed -n '1,8p' | grep 'inet addr' | awk '{print $2}'", "eth1");
if ((fd = popen(command, "r")) == NULL)
{
ret = -1;
goto END;
}
while (fgets(value, 1024, fd))
{
value[strlen(value) - 1] = 0;
ret = 0;
.....
goto END;
}
END:
if (fd != NULL)
{
pclose(fd); }
top commands information: please note the STAT is RUN.
Code:
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
10183 2664 root R 3388 0.3 1 49.7 sh -c ifconfig | sed -n '/eth1/,$p' | sed -n '1,8p' | grep 'inet addr' | awk '{print $2}'