Sponsored Content
Full Discussion: fork in perl
Top Forums Shell Programming and Scripting fork in perl Post 302602419 by Anjan1 on Monday 27th of February 2012 10:24:08 AM
Old 02-27-2012
fork in perl

Hi,

I have a file which has some 50 hosts, I want to login to each host using ssh and execute any command. I am using fork function in perl. I am able to login to each host and execute the command in the hosts paralelly, but it spawing/forking 50 processes at a time.

It will consume the cpu utilization if so many processes open at a time. Can someone tell me how to limit the number of process.

For example, open 10 processes and execute the command on 10 hosts, after completion of those 10 hosts, open next 10 processes and so on.

I know that there is a perl module "Parallel::ForkManager" to limit the number of processes.
But I dont want to use it, as that module is not there in my system and I cannot install it.

Hence, I am looking how to do it using fork in perl.

Can someone help me please..
 

10 More Discussions You Might Find Interesting

1. Programming

fork()

#include <stdio.h> #include <string.h> #include <sys/types.h> #define MAX_COUNT 200 #define BUF_SIZE 100 void main(void) { pid_t pid; int i; char buf; fork(); pid = getpid(); for (i = 1; i <= MAX_COUNT; i++) { sprintf(buf,... (2 Replies)
Discussion started by: MKSRaja
2 Replies

2. Programming

Fork or what?

Hello all. I'm developing a filetransfer application, which is supposed to work sort of like dcc, with multiple transfers etc. Now i wonder what the best way to manage the transfers is. Should i fork() for each new transfer, hogging loads of memory or use pthreads? Maybe I can use select to see... (0 Replies)
Discussion started by: crippe
0 Replies

3. Programming

Fork ()

hi all About this code for (i = 1; i < n; i++) if ((childpid = fork()) <= 0) break; I really can't understand the output . and the way fork () return the value . how about the process Id ,the child process Id and the parent ID in this case so please answer me soon (5 Replies)
Discussion started by: iwbasts
5 Replies

4. Programming

fork() help

Hi everybody, I wanna write a code to understand how fork works. my target -------------- -Parent creates a file(called temp) and writes into this file "1".Then it closes the file. -Then parent creates a child and wait until execution of this child ends. -Then child opens the same... (3 Replies)
Discussion started by: alexicopax
3 Replies

5. Shell Programming and Scripting

new to perl.. how do I do this ? fork/ threads ?

I have never coded in perl before (just started today morning :). I need to write a perl program to automate a task. Here is how I do it manually: Start a program in my home dir. Now if I want to execute another program while this one is still running, what I would do is go to another... (6 Replies)
Discussion started by: the_learner
6 Replies

6. Shell Programming and Scripting

Perl fork

Hi, I want to exec three different functions in perl one per fork(); How can I determine that this it the third fork and I should use third function in it. if ($pid = 0) { first();} else ( #parent second(); ) How to run third function? (1 Reply)
Discussion started by: mirusnet
1 Replies

7. UNIX for Dummies Questions & Answers

fork()

I'm trying to run a simple test on how to use fork(), i'm able to execute the child process first then the parent, but how can I execute parent then child..? Thanks! (1 Reply)
Discussion started by: l flipboi l
1 Replies

8. Shell Programming and Scripting

fork in perl

Can someone tell me perl fork example please. I tried online but could not get proper documentation. (2 Replies)
Discussion started by: Anjan1
2 Replies

9. Programming

Fork!

I understand that fork create a child but I need very simple example that make child useful.... I mean how will make the program faster anyone explain with code plz using C plz (2 Replies)
Discussion started by: fwrlfo
2 Replies

10. Shell Programming and Scripting

Need explanation on Perl Fork

Hi, I am breaking my head to understand the below line of code in perl: next if $pid = fork; # Parent goes to next server. die "fork failed: $!" unless defined $pid; Can anyone please explain me in detail as I am a slow learner? Thanks, Ali. (2 Replies)
Discussion started by: liyakathali
2 Replies
Util(3pm)						User Contributed Perl Documentation						 Util(3pm)

NAME
Coro::Util - various utility functions. SYNOPSIS
use Coro::Util; DESCRIPTION
This module implements various utility functions, mostly replacing perl functions by non-blocking counterparts. Many of these functions exist for the sole purpose of emulating existing interfaces, no matter how bad or limited they are (e.g. no IPv6 support). This module is an AnyEvent user. Refer to the AnyEvent documentation to see how to integrate it into your own programs. $ipn = Coro::Util::inet_aton $hostname || $ip Works almost exactly like its "Socket::inet_aton" counterpart, except that it does not block other coroutines. Does not handle multihomed hosts or IPv6 - consider using "AnyEvent::Socket::resolve_sockaddr" with the Coro rouse functions instead. gethostbyname, gethostbyaddr Work similarly to their Perl counterparts, but do not block. Uses "AnyEvent::Util::inet_aton" internally. Does not handle multihomed hosts or IPv6 - consider using "AnyEvent::Socket::resolve_sockaddr" or "AnyEvent::DNS::reverse_lookup" with the Coro rouse functions instead. @result = Coro::Util::fork_eval { ... }, @args Executes the given code block or code reference with the given arguments in a separate process, returning the results. The return values must be serialisable with Coro::Storable. It may, of course, block. Note that using event handling in the sub is not usually a good idea as you will inherit a mixed set of watchers from the parent. Exceptions will be correctly forwarded to the caller. This function is useful for pushing cpu-intensive computations into a different process, for example to take advantage of multiple CPU's. Its also useful if you want to simply run some blocking functions (such as "system()") and do not care about the overhead enough to code your own pid watcher etc. This function might keep a pool of processes in some future version, as fork can be rather slow in large processes. You should also look at "AnyEvent::Util::fork_eval", which is newer and more compatible to totally broken Perl implementations such as the one from ActiveState. Example: execute some external program (convert image to rgba raw form) and add a long computation (extract the alpha channel) in a separate process, making sure that never more then $NUMCPUS processes are being run. my $cpulock = new Coro::Semaphore $NUMCPUS; sub do_it { my ($path) = @_; my $guard = $cpulock->guard; Coro::Util::fork_eval { open my $fh, "convert -depth 8 Q$pathE rgba:" or die "$path: $!"; local $/; # make my eyes hurt pack "C*", unpack "(xxxC)*", <$fh> } } my $alphachannel = do_it "/tmp/img.png"; AUTHOR
Marc Lehmann <schmorp@schmorp.de> http://home.schmorp.de/ perl v5.14.2 2012-04-13 Util(3pm)
All times are GMT -4. The time now is 05:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy