Unix Programming - Why the disk space keep on decreasing,is that due to use popen()?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > February 2007 > Why the disk space keep on decreasing,is that due to use popen()?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Why the disk space keep on decreasing,is that due to use popen()?
CFAN

2007-02-15, 1:27 am

Why the disk space keep on decreasing,is that due to use popen()?
I have wrote a monitor program to ensure my program on working by
using popen to
use 'ps' command, but the disk space keep on decreasing.

And when the disk shrink to near 40K , it seems stop to decrease
anymore.

So Is the popen implementation needs write disk other than using meory
instead?
got wired.
And also , can their be a way to only access the memory for my
program's purpose?

Following is the code :

#define PS_COMMAND "ps -ef | awk '{print $2 , $8;}'| grep %s |grep -v
grep"
main()
{
while (_GetProcessId("programA",&already_start))
{
//do something ... and sleep a minute.

}

}

int _GetProcessId(char *program_name,int* already_start)
{

FILE *fd;
char command[255];
char str[255];
sprintf(command,PS_COMMAND,program_name)
;
if((fd = popen(command, "r")) == NULL) {
printf("call popen failed ");
return APR_EINIT;
} else {
while(fgets(str, 255, fd) != NULL) {
//printf("%s ",str);
*already_start=atoi(str);
pclose( fd ) ;
return APR_SUCCESS;
}
}
pclose( fd ) ;
return APR_EGENERAL;
}

Jens Thoms Toerring

2007-02-15, 1:19 pm

CFAN <eyelinux@gmail.com> wrote:
> Why the disk space keep on decreasing,is that due to use popen()?
> I have wrote a monitor program to ensure my program on working by
> using popen to
> use 'ps' command, but the disk space keep on decreasing.


> And when the disk shrink to near 40K , it seems stop to decrease
> anymore.


> So Is the popen implementation needs write disk other than using meory
> instead?


popen() doesn't need to write to the disk at all (but, of course,
the process you start via popen() may, but that doesn't seem to
be the case here since you just seem to be using ps, grep and awk).
You probably have some other process running that fills up the disk.
Perhaps some process that continuously writes to a log file? Or
one that creates lots of large files in e.g. /tmp and doesn't
remove them when not needed anymore?

> got wired.
> And also , can their be a way to only access the memory for my
> program's purpose?


Sorry, but I don't understand these "sentences".

> Following is the code :


> #define PS_COMMAND "ps -ef | awk '{print $2 , $8;}'| grep %s |grep -v
> grep"
> main()


Better make that

int main( void )

And you seem to be missing a few includes of header files.

> {
> while (_GetProcessId("programA",&already_start))


Variables starting with an underscore are reserved for the
implementation, so better avoid them. And there's no definition
of the variable 'already_start' anywhere, so this can't be your
real code.

> {
> //do something ... and sleep a minute.


> }


> }


> int _GetProcessId(char *program_name,int* already_start)
> {


> FILE *fd;
> char command[255];
> char str[255];
> sprintf(command,PS_COMMAND,program_name)
;


In gneral you would better use strncpy() here - at least if you
don't have control about how long 'program_name' is. If it's
too long you could overflow your 'command' buffer.

> if((fd = popen(command, "r")) == NULL) {
> printf("call popen failed ");


You better add a '\n' or what you try to print out my get stuck
in the internal buffers of printf() for quite some time.

> return APR_EINIT;


What's 'APR_EINIT'?

> } else {
> while(fgets(str, 255, fd) != NULL) {


Why loop here if you return from within the loop? If you loop
because there could be several processes with the same name
then you would have to return something else than a single
int...

> //printf("%s ",str);
> *already_start=atoi(str);


Better use strtol(), it allows you to do reasonable error checking.

> pclose( fd ) ;
> return APR_SUCCESS;


What's 'APR_SUCCESS'?

> }
> }
> pclose( fd ) ;
> return APR_EGENERAL;


What's 'APR_EGENERAL'?

> }


You know, spaces aren't an expensive commodity nowadays, so
you would be better off if you would use some reasonable
indentation in your code. Makes it much easier to read and
spot mistakes.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
CFAN

2007-02-16, 1:19 am

On Feb 15, 9:42 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> CFAN <eyeli...@gmail.com> wrote:
>
> popen() doesn't need to write to thediskat all (but, of course,
> the process you start viapopen() may, but that doesn't seem to
> be the case here since you just seem to be using ps, grep and awk).
> You probably have some other process running that fills up thedisk.
> Perhaps some process that continuously writes to a log file? Or
> one that creates lots of large files in e.g. /tmp and doesn't
> remove them when not needed anymore?
>
>
> Sorry, but I don't understand these "sentences".
>
>
> Better make that
>
> int main( void )
>
> And you seem to be missing a few includes of header files.
>
>
> Variables starting with an underscore are reserved for the
> implementation, so better avoid them. And there's no definition
> of the variable 'already_start' anywhere, so this can't be your
> real code.
>
>
> In gneral you would better use strncpy() here - at least if you
> don't have control about how long 'program_name' is. If it's
> too long you could overflow your 'command' buffer.
>
>
> You better add a '\n' or what you try to print out my get stuck
> in the internal buffers of printf() for quite some time.
>
>
> What's 'APR_EINIT'?
>
>
> Why loop here if you return from within the loop? If you loop
> because there could be several processes with the same name
> then you would have to return something else than a single
> int...
>
>
> Better use strtol(), it allows you to do reasonable error checking.
>
>
> What's 'APR_SUCCESS'?
>
>
> What's 'APR_EGENERAL'?
>
>
> You know, spaces aren't an expensive commodity nowadays, so
> you would be better off if you would use some reasonable
> indentation in your code. Makes it much easier to read and
> spot mistakes.
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ j...@toerring.de
> \__________________________ http://toerring.de


Thanks a lot for your patient marks on my program, any how it is just
a part of my program,
this program has invited the APache Libaray ,APR_EGENERAL is an const
for general error defined in its header files , and I deleted some
part of code to make it concentrated.
Sorry for the ugly indentation ,they are lost when i copy it to
textbox .
here is new one.
again _OS_UNIX__ is defined elsewhere when the program compiled under
unix or linux.
int _GetProcessId(char *program_name,int* already_start)
{
#ifdef _OS_UNIX_
FILE *fd;
char command[255];
char str[255];
sprintf(command,PS_COMMAND,program_name)
;
*already_start=0;
if((fd = popen(command, "r")) == NULL) {
printf("call popen failed ");
return APR_EINIT;
} else {
while(fgets(str, 255, fd) != NULL) {
//printf("%s ",str);
*already_start=atoi(str);
}
if (*already_start) {
pclose( fd ) ;
return APR_SUCCESS;
}
}
return APR_EGENERAL;

#else
return APR_SUCCESS;
#endif
}


Jens Thoms Toerring

2007-02-17, 7:19 am

CFAN <eyelinux@gmail.com> wrote:
> this program has invited the APache Libaray ,APR_EGENERAL is an const
> for general error defined in its header files , and I deleted some
> part of code to make it concentrated.
> Sorry for the ugly indentation ,they are lost when i copy it to
> textbox .
> here is new one.


From which I don't see anything that would eat up disk space, apart
from the fact that it may use up all available file descriptors or
process slots in the worst case.

> again _OS_UNIX__ is defined elsewhere when the program compiled under
> unix or linux.
> int _GetProcessId(char *program_name,int* already_start)
> {
> #ifdef _OS_UNIX_
> FILE *fd;
> char command[255];
> char str[255];
> sprintf(command,PS_COMMAND,program_name)
;


Better use snprintf() here, otherwise, if the command you assemble
here is longer than 254 characters, you write past the end of the
array.

> *already_start=0;
> if((fd = popen(command, "r")) == NULL) {
> printf("call popen failed ");
> return APR_EINIT;
> } else {


This 'else' seems to be a bit useless since you already have returned
if popen() failed.

> while(fgets(str, 255, fd) != NULL) {
> //printf("%s ",str);
> *already_start=atoi(str);
> }
> if (*already_start) {
> pclose( fd ) ;
> return APR_SUCCESS;
> }


If either the command returned nothing or the last line it returned
didn't contain a number (in which case atoi() did return 0, you can
avoid that problem by using strtol()) you don't close the file. And
since pclose() waits for the spawned process' exit value you will
probably end up with a zombie process if it doesn't get called.

> }
> return APR_EGENERAL;


> #else
> return APR_SUCCESS;
> #endif
> }

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com