Thread: Process manipulation questions

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    50

    Process manipulation questions

    Hello!

    I have written a small program with the purpose of benchmarking other programs. That is, run them, get execution time, memory usage, report if a program terminated succesfully, if it had an error, and also automatically terminate a program if it exceeds a preset time and memory limit. I have implemented all these functions, however I'm not sure if my approaches are the best ones, even if they do seem to work, so I have a few questions:

    How can I efficiently monitor a running process' memory usage (created with CreateProcess())? Right now, after I create the process, I run a thread that uses GetProcessMemoryInfo in a while loop, and compares the memory usage with the preset limit. If it exceeds this limit, it terminates the process and returns. This however seems very inefficent, and it even slows down my program a lot, making it return much higher running times for the launched programs (up to 2 seconds more). So, my question is: is there a better way to terminate a process once it exceeds a preset memory limit and somehow return a message that the termination reason is exceeding the allocated memory? If yes, how?

    I have added a Sleep(10) in my while loop, which seemed to cut the slowdown completely, but this seems like a very ugly hack, and I would rather not stick to it.

    Second, right now I report "Runtime error" if the process exitcode is not 0. Is there a way to report more precise stuff, as in, make sense of the exit codes returned? Like, exitcode X means division by 0, Y means segmentation fault etc...

    Thank you for any help.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Jobs can do this sort of thing: http://www.codeproject.com/KB/dotnet...ory_Limit.aspx

    gg

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    50
    I'm using C, I'll have to look on MSDN and see if I can manage to come up with an implementation.

    Thanks!

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    50
    Well, I have read up on MSDN and I have an idea of what I should do, however, I got stuck right at the beginning. I have this in a void function:

    Code:
    void run()
    {
        HANDLE obj = CreateJobObject(NULL, NULL);
    
       // ... more code
    }
    This gives the error "CreateJobObject was not declared in this scope". I'm using windows XP pro, code::blocks IDE with GNU GCC compiler. I have also linked the libkernel32.a file and included windows.h but the error won't go away. Any ideas?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I checked my MinGW headers and the function was in there - but you have to make sure _WIN32_WINNT is defined to be at least 0x0500. The job object functions are wrapped in
    Code:
    #if (_WIN32_WINNT >= 0x0500)
    #endif
    gg

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    50
    Thank you, that did it.

    Now, I've managed to come up with something. I assumed all the memory limits need to be set in bytes, is that correct?

    Second, after I create my job object like this:

    Code:
        HANDLE Job = CreateJobObject(NULL, NULL);
        JOBOBJECT_EXTENDED_LIMIT_INFORMATION info;
    
        info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_PROCESS_MEMORY;
        info.ProcessMemoryLimit = mem * 1024;
    
        SetInformationJobObject(Job, JobObjectExtendedLimitInformation, &info, sizeof(info));
    And create a process in suspended mode, assign it to the job object then resume it and wait for it to end, I want to know the peak memory that it used. For this I do:

    Code:
    int maxmem = info.PeakProcessMemoryUsed / 1024; // I need this in KB
    Now, this maxmem is different than what I get by using GetProcessMemoryInfo, and what I get by calculating the memory used by hand (I have the source to the program). Not to mention it's the same for multiple runs of the same process, even if I know for sure the used memory changes (increases) for each run (again, due to GetProcessMemoryInfo and hand calculations).

    Also, I know the same way that the program can end up using up to 6 megs of memory. If I set mem to, say, 4 megs / 1024, the program does not end as it should due to insufficient memory.

    So, I'm obviously doing something wrong, any suggestions? Maybe I should use WorkingSetSize instead? Is that reliable though, I read there are ways around it, or that it's just a suggestion to the OS, not a hard limit.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    50
    Ok, I was wrong about what I wrote before. This method limits the memory just fine. There's just one more thing I would like to know:

    is there any way to detect that the process has been terminated because it exceeded its allocated memory, and not for another reason?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Not that I know of. If the code doesn't check the return value of an allocation function, it may just die by dereferencing a null-pointer.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One process with two console windows
    By siavoshkc in forum Windows Programming
    Replies: 8
    Last Post: 01-30-2009, 04:13 PM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  4. Almost finished with Round Robin algorithm
    By Shinobi-wan in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2004, 03:00 PM
  5. Linux: Send keyboard input to background process
    By xErath in forum Tech Board
    Replies: 2
    Last Post: 12-09-2004, 07:02 PM