Sponsored Content
Top Forums Programming Shelling Out to Give a System Command Drains Free Memory Post 302553495 by BrandonShw on Wednesday 7th of September 2011 06:45:16 PM
Old 09-07-2011
Shelling Out to Give a System Command Drains Free Memory

I am working on a large program which is always up and must run in as many Linux flavors as possible, i.e. be portable. I have created a number of general utility functions for common tasks it needs to do, and one of these utility functions goes out to the shell to give a Linux command and return the output from the command. Depending on the exact Linux command being performed, occasionally it can drain free memory and not free it immediately.

In one case, I need to call this function to give a shell command and get data back a hundred or so times in a loop, which executes in just a moment. I find that during that loop my free memory decreases dramatically. I presume that it's freed at some point, but during execution of the loop, it can get quite low, and I think it may have caused a program abend in one environment. If you have read this far and are wondering, the command being given is to determine if a service is up, and it does this for every service which has script in /etc/init.d. The command, for bluetooth, for example, is:

service bluetooth status

My question is specifically this, is there anything I can do to my code in this function so that it either uses less memory of frees it better. Here is the code:

Code:
  int GeneralUtilities::ExecSystemCmd(string cmd, vector<string>& commandOutput)
  {
  FILE *fp = NULL;
  char commandOutputLine[5000];
  int numLines = 0;
  try
    {
    cmd += " 2>&1";  // Get stderr.  This may not be sufficiently portable.
    /* Open the command for reading. */
    fp = popen(cmd.c_str(), "r");
    if (fp == NULL)
    return(0);
    /* Read the output a line at a time - output it. */
    for (numLines = 0; (fgets(commandOutputLine, sizeof(commandOutputLine)-1, fp) != NULL); numLines++)
      commandOutput.push_back(commandOutputLine);
    /* close */
    pclose(fp);
    }
  catch(...)
    {
    if (fp != NULL)
     pclose(fp);
    }
  return numLines;
  }

I have checked and the character array commandOutputLine[] has no bearing on this problem. How might I re-write this function to make it less of a memory hog or to make it free its memory at once? Thanks in advance.

Brandon

Last edited by pludi; 09-07-2011 at 08:20 PM..
 

10 More Discussions You Might Find Interesting

1. News, Links, Events and Announcements

Sun to Give Out Operating System for Free

Reference: http://story.news.yahoo.com/news?tmpl=story&ncid=738&e=1&u=/ap/20041115/ap_on_hi_te/sun_solaris10 Sun to Give Out Operating System for Free Mon Nov 15, 7:31 AM ET By MATTHEW FORDAHL, AP Technology Writer SAN JOSE, Calif. - After investing roughly $500 million and... (1 Reply)
Discussion started by: Neo
1 Replies

2. Programming

How to free the memory?

For example if i have the piece of code as follows: CountryName = (char *)malloc((strlen(CountryName)+1)*sizeof(char)); memset(CountryName, 0, strlen(CountryName)+1); CountryName = SOME VALUE Now how do i free the memory after use of this code???? :confused: (3 Replies)
Discussion started by: jazz
3 Replies

3. AIX

Free Memory

Hi, how to find free memory in aix? for installing oracle,I have used svmon but not getting proper output (1 Reply)
Discussion started by: manoj.solaris
1 Replies

4. UNIX for Advanced & Expert Users

Can anyone give more details about the system calls?

Hello, Please can any one explain about the parameters to the write systemcalls?? How are they passed?? and how is the address of the user buffer is handled by the kenel?? for ex: write(fd,buf,count); How does the kernel handles this user buffer address?? After write does the kernel write... (1 Reply)
Discussion started by: prakash.kudreka
1 Replies

5. Solaris

How to find Total and Free Physical Memory and Logical Memory in SOLARIS 9

Hi, Im working on Solaris 9 on SPARC-32 bit running on an Ultra-80, and I have to find out the following:- 1. Total Physical Memory in the system(total RAM). 2. Available Physical Memory(i.e. RAM Usage) 3. Total (Logical) Memory in the system 4. Available (Logical) Memory. I know... (4 Replies)
Discussion started by: 0ktalmagik
4 Replies

6. Solaris

how to get the more memory free space (see memory free column)

Hi all, Could please let me know how to get the more memory free space (not added the RAM) in local zone. -bash-3.00# vmstat 2 5 kthr memory page disk faults cpu r b w swap free re mf pi po fr de sr s0 s1 s1 s1 in sy cs us sy... (3 Replies)
Discussion started by: murthy76
3 Replies

7. Solaris

Free memory in top and prstat command

Hi Export, i execute 'top' command to show the free memory in Solaris host, but the read is much lower than the RSS value shown in prstat command. Which one can reflect the real status and it is possible the difference caused by any patch of OS? Top command (only 883 memory is free)... (3 Replies)
Discussion started by: eiga
3 Replies

8. UNIX for Advanced & Expert Users

Out of Memory error when free memory size is large

I was running a program and it stopped and showed "Out of Memory!". at that time, the RAM used by this process is around 4G and the free memory size of the machine is around 30G. Does anybody know what maybe the reason? this program is written with Perl. the OS of the machine is Solaris U8. And I... (1 Reply)
Discussion started by: lilili07
1 Replies

9. Programming

Memory free() in C

Hi guys. I've a question, if we are using a syscall that receives a string allocated dynamicaly to a determined size, or NUL and it will allocate the apropriate size. We should free the memory or the OS will do it for us? If a function returns a pointer we should free that poiter when we are done... (7 Replies)
Discussion started by: pharaoh
7 Replies

10. Solaris

Is there a way to free up memory manually ?

Hi, I am wondering if there is a way to free up memory in Solaris manually ? the way we can do it in Linux for example : echo `/bin/date` "************* Memory Info Before *************" free -m sync echo 1 > /proc/sys/vm/drop_caches echo 2 > /proc/sys/vm/drop_caches echo 3 >... (13 Replies)
Discussion started by: terrykhatri531
13 Replies
POPEN(3P)						     POSIX Programmer's Manual							 POPEN(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
popen -- initiate pipe streams to or from a process SYNOPSIS
#include <stdio.h> FILE *popen(const char *command, const char *mode); DESCRIPTION
The popen() function shall execute the command specified by the string command. It shall create a pipe between the calling program and the executed command, and shall return a pointer to a stream that can be used to either read from or write to the pipe. The environment of the executed command shall be as if a child process were created within the popen() call using the fork() function, and the child invoked the sh utility using the call: execl(shell path, "sh", "-c", command, (char *)0); where shell path is an unspecified pathname for the sh utility. The popen() function shall ensure that any streams from previous popen() calls that remain open in the parent process are closed in the new child process. The mode argument to popen() is a string that specifies I/O mode: 1. If mode is r, when the child process is started, its file descriptor STDOUT_FILENO shall be the writable end of the pipe, and the file descriptor fileno(stream) in the calling process, where stream is the stream pointer returned by popen(), shall be the readable end of the pipe. 2. If mode is w, when the child process is started its file descriptor STDIN_FILENO shall be the readable end of the pipe, and the file descriptor fileno(stream) in the calling process, where stream is the stream pointer returned by popen(), shall be the writable end of the pipe. 3. If mode is any other value, the result is unspecified. After popen(), both the parent and the child process shall be capable of executing independently before either terminates. Pipe streams are byte-oriented. RETURN VALUE
Upon successful completion, popen() shall return a pointer to an open stream that can be used to read or write to the pipe. Otherwise, it shall return a null pointer and may set errno to indicate the error. ERRORS
The popen() function shall fail if: EMFILE {STREAM_MAX} streams are currently open in the calling process. The popen() function may fail if: EMFILE {FOPEN_MAX} streams are currently open in the calling process. EINVAL The mode argument is invalid. The popen() function may also set errno values as described by fork() or pipe(). The following sections are informative. EXAMPLES
Using popen() to Obtain a List of Files from the ls Utility The following example demonstrates the use of popen() and pclose() to execute the command ls* in order to obtain a list of files in the current directory: #include <stdio.h> ... FILE *fp; int status; char path[PATH_MAX]; fp = popen("ls *", "r"); if (fp == NULL) /* Handle error */; while (fgets(path, PATH_MAX, fp) != NULL) printf("%s", path); status = pclose(fp); if (status == -1) { /* Error reported by pclose() */ ... } else { /* Use macros described under wait() to inspect `status' in order to determine success/failure of command executed by popen() */ ... } APPLICATION USAGE
Since open files are shared, a mode r command can be used as an input filter and a mode w command as an output filter. Buffered reading before opening an input filter may leave the standard input of that filter mispositioned. Similar problems with an output filter may be prevented by careful buffer flushing; for example, with fflush(). A stream opened by popen() should be closed by pclose(). The behavior of popen() is specified for values of mode of r and w. Other modes such as rb and wb might be supported by specific implemen- tations, but these would not be portable features. Note that historical implementations of popen() only check to see if the first character of mode is r. Thus, a mode of robert the robot would be treated as mode r, and a mode of anything else would be treated as mode w. If the application calls waitpid() or waitid() with a pid argument greater than 0, and it still has a stream that was called with popen() open, it must ensure that pid does not refer to the process started by popen(). To determine whether or not the environment specified in the Shell and Utilities volume of POSIX.1-2008 is present, use the function call: sysconf(_SC_2_VERSION) (See sysconf()). RATIONALE
The popen() function should not be used by programs that have set user (or group) ID privileges. The fork() and exec family of functions (except execlp() and execvp()), should be used instead. This prevents any unforeseen manipulation of the environment of the user that could cause execution of commands not anticipated by the calling program. If the original and popen()ed processes both intend to read or write or read and write a common file, and either will be using FILE-type C functions (fread(), fwrite(), and so on), the rules for sharing file handles must be observed (see Section 2.5.1, Interaction of File Descriptors and Standard I/O Streams). FUTURE DIRECTIONS
None. SEE ALSO
Section 2.5, Standard I/O Streams, fork(), pclose(), pipe(), sysconf(), system(), wait(), waitid() The Base Definitions volume of POSIX.1-2008, <stdio.h> The Shell and Utilities volume of POSIX.1-2008, sh COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Stan- dard is the referee document. The original Standard can be obtained online at http://www.unix.org/online.html . Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . IEEE
/The Open Group 2013 POPEN(3P)
All times are GMT -4. The time now is 11:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy