Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a command that I'm calling from code:

execvp('generate', ...)

which is an executable program that my code communicates with via stdin, stdout and stderr. This works fine, not a single problem.

I want to change this so I can set resource limits on generate. So I've tried calling:

ulimit -t 1 && generate

But I get a broken pipe when I try to communicate with it.

So I put the line above in a shell script generate_wrapper:

#!/bin/bash
ulimit -t 1 && generate

And I get a broken pipe when I try to communicate with it.

But

$ echo "foo" | generate_wrapper
$ echo "foo" | generate

both give me the correct, identical output. I figured it might be the &&, so I tried just the bare command:

#!/bin/bash
generate

But it still works from the CL, and I still get a broken pipe when I try to communicate with it from code.

I tried to explicitly route the fds, and got:

#!/bin/bash
generate >&1 2>&2 <&0

But no, I still get a broken pipe when I try to communicate with it from code.

So obviously I haven't a clue what I'm doing. Can you help? How do I write a wrapper so I can ulimit a spawned subprocess (rlimit isn't available for pids in my host language) and still communicate with it?

share|improve this question
    
I think this should either either get moved to Unix&Linux or StackOverflow since it is about programming and access to Unix resources, not a general computer user question. Don't re-ask, just wait and see. I've flagged it for a moderator to migrate. –  Caleb Jun 29 '11 at 11:44
    
Oh, the broken pipe in all cases is an EPIPE, rather than a SIGPIPE: "Error: EPIPE, Broken pipe" –  Ian Jun 29 '11 at 11:44
    
Caleb, wasn't sure which way to go, but here did seem to be more questions on piping stuff around bash. –  Ian Jun 29 '11 at 11:45
2  
@lan: That's because new unix users frequently don't understand how the basics of pipes work and on the unix.SE that's kind of assumed knowledge. However your question is a little more advanced than that and involves access from another language and ulimit stuff. That's time for Unix expert advice ... it's more of a programmer question than a usage question. –  Caleb Jun 29 '11 at 11:47
    
pipes, shells scripts and ulimit. O my! –  sealz Jun 29 '11 at 11:52

2 Answers 2

Any reason you don't want to call ulimit (2) in the generating code before execvp?

From the man page:

SYNOPSIS
#include <ulimit.h>
long ulimit (int cmd, ...);

DESCRIPTION
The ulimit() function will get and set process limits.

share|improve this answer

execvp does not handle system commands but only direct binaries. Also script files are only masked system commands in this way.

I think you need to use system() if you wish to run commands instead of binaries.

dmckee's idea about calling ulimit() before execvp() is also a sound one. This will however affect everything the process does from here on.

share|improve this answer
    
Unfortunately I neither have rlimit or ulimit available. So the question stands. Can it be done? –  Ian Jul 13 '11 at 22:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.