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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting new to perl.. how do I do this ? fork/ threads ?
# 1  
Old 05-01-2007
new to perl.. how do I do this ? fork/ threads ?

I have never coded in perl before (just started today morning Smilie. 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 terminal and then run it there in my home dir. While these 2 programs are running and I need to run another program then I will simply start another terminal and run the third one there.

But I want to automate this using perl. So I will have a perl script which will have 3 system calls:

$output1 = `./program1`;
$output2 = `./program2`;
$output3 = `./program3`;

# All 3 programs are finished. Do something with these output strings


But here, program2 will not be started until program1 has returned so it will have to unnecessarily wait. I want all 3 programs to run in parallel. And only once when all system calls have returned, go ahead and process the output strings.

So what do I need to do? use forks or threads ?

(note that Im using backticks `` to execute the system command, but I think there are other ways to do it such as system() etc.. I am free to use anything I want. Also, in the example I take the output in a string.. however I will eventually have the system calls direct the output to a file)

Thanks !!!
# 2  
Old 05-01-2007
Use fork() and then exec().
# 3  
Old 05-02-2007
Can I use fork() to start more than 1 child processes ?


What I have so far is:

Quote:
my $pid = fork();

if (not defined $pid) {

print "resources not avilable.\n ";

}
if ($pid == 0) {

#
# child process - Do something here !
#

} else {

#
# parent process - Do something here !
#

waitpid($pid,0);

#Both child and parent has finished.
}
But this will spawn off 1 child and I have 1 parent. But I need 3 processes. ANd all 3 should be doing something else.

Please help !!! Smilie
# 4  
Old 05-02-2007
Quote:
Originally Posted by the_learner
# parent process - Do something here !
"Do something here" can be to fork another process.
# 5  
Old 05-02-2007
Hey when I have the child process run a program, the parent process does not proceed until the child has finished...

Quote:
my $pid = fork();

if ($pid == 0) {

#
# child process
#

my $output1 = `./program1`;

# the above program takes 5 mins to complete.

} else {

#
# parent process
#

print 'Hello, I am parent';

waitpid($pid,0);

#Both child and parent has finished.
}
program1 takes 5 mins to complete. The parent does not print 'Hello, I am parent' until the child has finished.
# 6  
Old 05-02-2007
Quote:
Originally Posted by the_learner
The parent does not print 'Hello, I am parent' until the child has finished.
My guess is that output buffering is getting in the way. Try
Code:
print STDERR 'Hello, I am parent';

# 7  
Old 05-02-2007
For output buffering, either you add a "\n" at the end (as Perl IO is line buffered), or set

$| = 1;

at the top of the program.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Perl Threads Terminating Abnormally.

I've used threads before, but not with Perl. I tried looking up these errors and using 'join' instead of 'detach' with no luck. Here is the code I am currently using: #!/usr/bin/perl -w use warnings; use threads; use threads::shared; $Linux='Linux'; $Greek='Greek'; my... (3 Replies)
Discussion started by: Azrael
3 Replies

3. Shell Programming and Scripting

using threads in perl

Hi everyone, I am trying to create a script which runs a number of processes simultaneously and at the same time use a timer to keep track of what is going on. The problem is that the timer stops and the script exits upon the completion of some of the processes, whereas I want to timer to... (0 Replies)
Discussion started by: free2rhyme2k
0 Replies

4. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: Anjan1
5 Replies

5. 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

6. Shell Programming and Scripting

perl launch threads in an array variable?

Im having a problem launching multiple sub routines as threads. My script seems to stop when the first thread is launched. Im condensing the code for simplification here: #!/usr/bin/perl -w use strict; use threads; srand; my ($cnt,$line,$iprange_rand); my... (2 Replies)
Discussion started by: trey85stang
2 Replies

7. 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

8. Shell Programming and Scripting

Perl v5.8.5 Threads Problem

Hi Unix gurus, I am facing a threading problem in Perl. I have a worker thread in perl in which I am calling a shell script. The shell script echo's output to the Standard Output from time to time as it progresses. In the worker thread, I am unable to display the echo statement of shell... (1 Reply)
Discussion started by: som.nitk
1 Replies

9. UNIX for Advanced & Expert Users

Threads and Threads Count ?

Hi all, How can I get the list of all Threads and the Total count of threads under a particular process ? Do suggest !! Awaiting for the replies !! Thanks Varun:b: (2 Replies)
Discussion started by: varungupta
2 Replies

10. 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
Login or Register to Ask a Question