-   Programming (
-   -   how to extract output ulimit under popen (

Ranny 01-30-2011 09:56 AM

how to extract output ulimit under popen
A strange question, I guess.
I'm running processes called from a c main program. The calling is performed (for now) as:

FILE * res=popen(ulimit -t 1; prg args);

So I can read the stdout of the process as a file and analyze it. The time limit is important for me.

2 questions:
1. How do I get to know if the process terminated on its own or by the ulimit?
2. How do I limit to times that are less than 1 sec (I have many of those).

I know that setrlimit exists, just before I change my whole approach I wanted to see if I can deal with these things from the outside.


ta0kira 01-31-2011 04:20 PM

If the process exceeds its CPU time limit, the kernel will end the process with SIGXCPU if the soft (but not hard) limit is reached, otherwise with SIGKILL. If you want to obtain the exit status, you'll need the process ID or process group to use with waitpid, which means the monitoring process needs to be the parent of the monitored process. That means you can't use popen. You'll probably have to do it manually with pipe, fork, dup2, setrlimit, and execvp. Using system instead of execvp might just give you the exit status of the shell; I'm not sure if it passes on the signal.

You cannot specify a non-integer time limit, even with setrlimit.

I'm not sure of a real use for limiting CPU time, other than debugging when you're having a problem with a race condition. What exactly are you using it for? Do you handle SIGXCPU?
Kevin Barry

Ranny 02-22-2011 02:36 PM

Hi Kevin,
Thanks for the answer. I'm still having issues with these things. Sorry for taking so much time to respond (this is a late-night hobby I'm developing, with no extensive background).
First, it's probably best to explain what I try to do.
basically, I have a (main) program that "creates" c-codes, compiles them, runs them with inputs and reads the outputs.
I use popen to get the stdout of these and analyze it.
The results are unexpected a-priory, meaning: infinite run-time, infinite stdout, no output at all etc.
I now measure the time from start to end and if it exceeds something like half a second I consider it "infinite", but I still have problems with reading of infinite outputs, and generally - knowing when the sub-process stopped (I want to analyze the data only after all of it is already given, now it's a FILE *).
the system command is good for that, but can't get access to the stdout...
If you have any ideas how this can be done, I would be very grateful!
Thanks again,

All times are GMT -5. The time now is 12:17 AM.