LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2015, 03:42 AM   #1
framinghu
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Rep: Reputation: Disabled
Unhappy pclose hangs in multi-thread program


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}'

Last edited by framinghu; 03-18-2015 at 05:58 AM.
 
Old 03-18-2015, 05:28 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
please use [code]here comes your code[/code] to keep formatting
use:
Code:
sprintf(command, "ifconfig eth1 | sed |sed ....")
and finally avoid using chains like sed|sed|grep|awk, because usually that commands can be implemented in one single script
What language did you use?
 
1 members found this post helpful.
Old 03-18-2015, 06:04 AM   #3
framinghu
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
please use [code]here comes your code[/code] to keep formatting
use:
Code:
sprintf(command, "ifconfig eth1 | sed |sed ....")
and finally avoid using chains like sed|sed|grep|awk, because usually that commands can be implemented in one single script
What language did you use?
Thank for your response, pan64.
C language used for this API. Do you mean sed|grep|awk may lead to this problem? but popen already create one process to execute these commands.
 
Old 03-19-2015, 03:16 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
one problem can be you close the socket before eof (looks like you goto end after the first 1024 bytes) and therefore there can be something still running. Also stren(value) may or may not work (depending on the content), but value[strlen(value) - 1] = 0; is a noop, because strlen(value) will search for 0 - what you want to set.
sed|grep|awk itself will not cause that problem.
 
1 members found this post helpful.
Old 03-19-2015, 06:46 AM   #5
framinghu
LQ Newbie
 
Registered: Mar 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
one problem can be you close the socket before eof (looks like you goto end after the first 1024 bytes) and therefore there can be something still running. Also stren(value) may or may not work (depending on the content), but value[strlen(value) - 1] = 0; is a noop, because strlen(value) will search for 0 - what you want to set.
sed|grep|awk itself will not cause that problem.
You give me a good clue for this problem.
because cpu high usage process is "sh -c ifconfig | ..", so i think these commands are kept executing all the time, so popen API is not exited, fget and pclose shouldn't be executed so far. i checked that parent process (PID 2664) is pending now.

i need to check what case will lead the script "sh -c ifconfig | sed -n '/eth1/,$p' | sed -n '1,8p' | grep 'inet addr' | awk '{print $2}'" not return back, ifconfig/awk/sed/grep are got from busybox v1.19.4.

If suggestion, it is very appreciated.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] what's the difference between a multi-cpu , multi-core and a multi-thread system ? entz Linux - Hardware 11 12-20-2011 04:49 PM
pclose() hangs and never return in multi-threaded program zxtole Programming 6 08-30-2011 09:56 PM
problem in multi thread server program naveenisback Programming 1 07-29-2009 06:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration