Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-30-2011, 09:56 AM   #1
LQ Newbie
Registered: Feb 2009
Posts: 13

Rep: Reputation: 0
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.

Old 01-31-2011, 04:20 PM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 01-31-2011 at 04:31 PM.
Old 02-22-2011, 02:36 PM   #3
LQ Newbie
Registered: Feb 2009
Posts: 13

Original Poster
Rep: Reputation: 0
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,


programing, ulimit, [c]

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
ulimit -s 40960 vs ulimit ulimit -s 10240 centguy Linux - Software 2 10-23-2016 12:38 AM
catching output and errors with popen c++ mshindo Programming 2 02-04-2009 05:09 AM
Need to extract certain values from kudzu-output. MheAd Linux - Newbie 3 07-02-2008 06:02 AM
popen output not appear dimsh Programming 4 10-22-2005 03:56 AM
C Popen gold5angel Programming 2 04-17-2004 08:07 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 PM.

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