Sponsored Content
Full Discussion: using threads in perl
Top Forums Shell Programming and Scripting using threads in perl Post 302629951 by free2rhyme2k on Wednesday 25th of April 2012 05:38:49 PM
Old 04-25-2012
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 continue indefinitely.

Note that the functionality of the file transfer and file size monitoring all works as required, the problem is only with the timer not continuing after the file has been completely sent. I want to add subsequent files so it is important that the timer continues.

Here is some code:
Code:
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Time::HiRes qw(usleep nanosleep);

my @downloadingFiles = ();
my $fileNumber = 0;
my $overallTimer = 0;
my $fileSize = 0;
my $temp1 = 0;
my $temp2 = 0;

#this is my timer which should run until 1000, but it actually stops when setupSenderAndReceiver function has terminated. It is monitoring the file size of the file which is being transferred through the socket.
sub GlobalTimer	{
	while($overallTimer <= 1000)	{
	$overallTimer++;
	sleep(1);
	$fileSize = -s "out$fileNumber";
	@downloadingFiles[$fileNumber] = $fileSize;
        $print "$overallTimer";
					}
		}

#this function uses two functions inside it to establish a server and client connection and send a file through a socket.
sub setupSenderAndReceiver{
$temp1 = $_[0];
$temp2 = $_[0];

#this script opens the server port
sub first {
system("/usr/bin/perl myPerlScript2.pl $temp1");
}

#this sends a command to a remote machine to start sending data.
sub second {
my @x = ("bash", "-c", "ssh ip_address_and_details_removed $temp2");
system(@x);
}

#this should instruction both processes to run at the same time and exit the thread upon completion.
my $firstThread = threads->create(\&first);
my $secondThread = threads->create(\&second);
firstThread->join();
secondThread->join();
}

#this is the thread I used to start the overall timer
my $globalT = threads->create(\&GlobalTimer);
$globalT->detach();
#this is the thread I used to create the file transfer
my $start = threads->create(\&setupSenderAndReceiver, $fileNumber);
$start->join();

Thanks in advance.

---------- Post updated at 04:38 PM ---------- Previous update was at 07:26 AM ----------

Uppies
 

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

old threads

Neo, if I have an old thread that is a few months old, and a few pages back in the forum it was posted in, is it ok to 'bump' it back to the front? or, would you rather i deleted the old thread, and just create a new one? btw the thread has no replies. (2 Replies)
Discussion started by: norsk hedensk
2 Replies

2. UNIX for Dummies Questions & Answers

threads

i am tring to sort lots of data thats in many columns by just one column but, if I use sort +16 inputfile the column fluctuates because some of the rows have spaces etc within the text, so the end result is just a mess as it jumps around the columns depending whether it has spaces or not ....ie... (2 Replies)
Discussion started by: Gerry405
2 Replies

3. Solaris

threads

Hi all! 1)Is there a way to write a program that will work on both solaris and intel based machines. 2)How can I achive this for a program that creates and synchronizes three threads. Thank you. vij. (3 Replies)
Discussion started by: vijlak
3 Replies

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

5. UNIX for Advanced & Expert Users

How many threads do I use ?

Hi, I have a program that has two types of threads: 1) Reader threads 2) Worker Threads Readers: Their only job is to read files. They just read data from the files and put them into a buffer. They are obviously I/O intensive. Workers: These are CPU intensive. They do some computation... (5 Replies)
Discussion started by: the_learner
5 Replies

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

7. Programming

Threads help

Hello! I started studying studying about POSIX Threads a few days ago... so I am a little confused and I would appreciate some help! I isolated this code... and I wonder if I could use threads in it! #include <unistd.h> #endif #include <math.h> //#include "main.h" #include <sys/time.h>... (1 Reply)
Discussion started by: smurf2
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. 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

10. 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
iv_timer(3)						    ivykis programmer's manual						       iv_timer(3)

NAME
iv_timer_register, iv_timer_unregister, iv_timer_registered - deal with ivykis timers SYNOPSIS
#include <iv.h> struct iv_timer { struct timespec expires; void *cookie; void (*handler)(void *); }; void IV_TIMER_INIT(struct iv_timer *timer); void iv_timer_register(struct iv_timer *timer); void iv_timer_unregister(struct iv_timer *timer); int iv_timer_registered(struct iv_timer *timer); DESCRIPTION
The functions iv_timer_register and iv_timer_unregister register, respectively unregister, a timer with the current thread's ivykis event loop. iv_timer_registered on a timer returns true if that timer is currently registered with ivykis. When a timer that is registered becomes 'ready', due to the current system clock value becoming greater than or equal to the timer's ->expires member field, the callback function specified by ->handler is called in the thread that the timer was registered in, with ->cookie as its first and sole argument. When this happens, the timer is transparently unregistered. The application is allowed to change the ->cookie and ->handler members at any time. The application is not allowed to change the ->expires member while the timer is registered. A given struct iv_timer can only be registered in one thread at a time, and a timer can only be unregistered in the thread that it was reg- istered from. There is no limit on the number of timers registered at once. See iv_examples(3) for programming examples. SEE ALSO
ivykis(3), iv_examples(3) ivykis 2010-08-15 iv_timer(3)
All times are GMT -4. The time now is 08:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy