Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How to limit resources ?

by zenmaster (Beadle)
on Oct 24, 2001 at 13:51 UTC ( [id://121055]=perlquestion: print w/replies, xml ) Need Help??

zenmaster has asked for the wisdom of the Perl Monks concerning the following question:

In brief, I'd like to fork several processes that I won't control and I like to avoid one of them sucking up all my resources.

I'd like to know if it's possible to limit the resources (CPU, Mem) used by child processes.
If it's possible, I'd also like to know how ;-)

TIA

Replies are listed 'Best First'.
Re: How to limit resources ?
by broquaint (Abbot) on Oct 24, 2001 at 14:17 UTC
    Well if you're fortunate enough to be running this on a Unix system then you can re-schedule the priority of a process by being 'nice' to the process. If you have permissions you can use the renice command (man renice(8)) which should lower it's CPU usage significantly (assuming you give it a positive value). This isn't part of perl's functionality, so you'll probably have to run the command through system() or something of that ilk, as I don't think there's a CPAN module for it (now would be a good time to write one though ;o)
    HTH

    broquaint

      There is also the perl function setpriority to renice a process. From the docs, it might cause a fatal error on a system that doesn't support setpriority. Maybe WinNT or siblings support it? I can't check right now.
Re: How to limit resources ?
by jeroenes (Priest) on Oct 24, 2001 at 14:03 UTC
    Process resource control is something completely done by the operating system. So your mileage may vary. You can program yourself a form of control by monitoring the system and kill your childs when they got too greedy.

    There are several modules on CPAN for process monitoring/control. Do a CPAN search on 'proces' than 'proc' to find one that fits your OS.

    Jeroen
    "We are not alone"(FZ)

    Update: Did that search on CPAN and found: Proc::ProcessTable and Win32::Process. There are modules for Solaris and Mac as well.

Re: How to limit resources ?
by Anarion (Hermit) on Oct 24, 2001 at 15:35 UTC
    In Linux/Unix its easy to limit the resources of one user, you have to modify 2 files to do it, or just one if you already include limits.conf in pam.

    There two files are:
    In /etc/pam.d/login or ssh, etc
    put this line

    session required pam_limits.so


    Then edit:
    /etc/security/limits.conf

    #<domain> <type> <item> <value> # #Where: #<domain> can be: # - an user name # - a group name, with @group syntax # - the wildcard *, for default entry # #<type> can have the two values: # - "soft" for enforcing the soft limits # - "hard" for enforcing hard limits # #<item> can be one of the following: # - core - limits the core file size (KB) # - data - max data size (KB) # - fsize - maximum filesize (KB) # - memlock - max locked-in-memory address space (KB) # - nofile - max number of open files # - rss - max resident set size (KB) # - stack - max stack size (KB) # - cpu - max CPU time (MIN) # - nproc - max number of processes # - as - address space limit # - maxlogins - max number of logins for this user # - priority - the priority to run user process with # #<domain> <type> <item> <value>

    $anarion=\$anarion;

    s==q^QBY_^=,$_^=$[x7,print

Re: How to limit resources ?
by MZSanford (Curate) on Oct 24, 2001 at 14:01 UTC
    In Unix, you can limit per-process resources for a given user, but the only way i could think of right off the top of my head in perl would be to use the times() function, and have the child die if the number is greater than it's limit. But, this is really not the best way, and it does not cover memory usage. I will be keeping an eye on this thread to see if anyone else knows.
    i had a memory leak once, and it ruined my favorite shirt.
Re: How to limit resources ?
by Zaxo (Archbishop) on Oct 24, 2001 at 14:28 UTC

    On *nix you can parse the output of ps, or somebody may have an xs interface to a 'top' lib. Can you do something with this?

    $ perl -e 'print `ps -l p $$`,$/' F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME C +MD 000 S 501 5150 561 0 69 0 - 614 pipe_w pts/2 0:00 p +erl -e pri
    The ps command has a terrific variety of options for the information and format returned. You can throttle CPU by sleeping, memory footprint will be trickier.

    After Compline,
    Zaxo

Re: How to limit resources ?
by echo (Pilgrim) on Oct 24, 2001 at 15:05 UTC
    GTop is a Perl interface to LibGTop, a library which fetches information on system resource utilization by running processes.
Re: How to limit resources ?
by claree0 (Hermit) on Oct 24, 2001 at 14:12 UTC

    I'm not sure how to limit memory usage, but it may be worth investigating whether the unix 'nice' command will carry over to the children

Re: How to limit resources ?
by Fletch (Bishop) on Oct 24, 2001 at 19:23 UTC

    Check out the BSD::Resource module. It provides a perl interface to the getrlimit() family of system calls for systems that implement them (don't let the name fool you; according to the Linux manpages it's required for SVr4 as well).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://121055]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-25 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found