473,231 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Problem with autoflush on Semaphore-threads for Windows

Dear community,

having written a working example for using Semphores on a windows
client, I created a little "server-application" that does the following:

A Server continously "listens/tails" a command-file for new commands.
If a new commands arrives a split of the workload to threads is under-
taken that "control themselves" via Semaphore and are joined finally.

It works.

However, the aspects of logging "I am done" into a log-file does
mysteriously not work synchronised although:
- all threads are definitely joined (see below)
- autoflush is active ($|=1;)

What I observe NOW (see code below) is that the new log-line is written
when a further command arrives OR when the server is interrupted.

Any ideas are highly appreciated. THank you in advance.

Andreas, Paris / France
#----------------------------------------------------------------
use threads;
use Win32::Semaphore;
#replaces the http://www.perldoc.com/perl5.8.0/pod/perlthrtut.html
# example of Semaphore (for UNIX)

my $endFlag = undef;
my $logFile = 'D:/www/eclipse/workspace/Perltest/status.log';
my $cmdFile = 'D:/www/eclipse/workspace/Perltest/cmd.log';
my $noThreads = 2; #two threads work at the same time
my $noTasks = 8; #our static workload is 8 tasks
$|=1; #autoflush

open (CMD, "<$cmdFile") || die "error in open:$!";
#seek Handle, Offset_in_bytes, Whence (0=start, 1=cursor, 2=EOF)
seek CMD, 0, 2; #wait at end of file for new incoming commands

open (LOG, ">>$logFile") || die "error in open:$!";
# ">>" positions already automatically to EOF

#note that the position of the sig-handler routine is important !!
$SIG{INT} = sub { print "\n time to say goodbye!\n";
$endFlag = 1; };

while (!defined $endFlag) {
while (<CMD>) {
print "New Command: $_\n";
processCmd($_);
} #while
print "sleeping for some seconds\n";
sleep 5;
#magic "hopping on the same spot" / Huepfen auf der Stelle
seek CMD, 0, 1; #reposition to "cursor" -> clear EOF-Flag
}#for
sub processCmd {
my $para = shift; #para is the command of CMD-file
print LOG "Processing $para"; #write some useful log-info
@th = (); #initialize array of threads

my $s = Win32::Semaphore->new($noThreads,$noThreads);

#We generate all threads
for (1..$noTasks) {
push @th, threads->new(\&myThread, $_, $s);
}

#..and join them afterwards
for (0..($noTasks - 1)) {
print "join task $_...";
$th[$_]->join();
print "done\n";
}

#xxxxxxxxxx - this log-entry is only written
#xxxxxxxxxx ..after we start the next command OR
#xxxxxxxxxx ..the server is stopped by CTRL-C (INT)
print LOG "Finished $para";
} #sub
# just another dummy thread to do some time-consuming stuff
sub myThread {
my ($p, $s) = @_;

$s->wait();
$w = int (rand($p)/3) + 2;
print "i am a thread $p wait for $w seconds\n";
sleep $w;
$s->release();
} #sub

1;
#----------------------------------------------------------------
Jul 19 '05 #1
0 3874

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gianluca Trombetta | last post by:
How can i do the autoflush effect in a python cgi? In perl i know how do that but not in python... Can anyone help me? Thanks in advance Gianluca Trombetta
3
by: Vanchau Nguyen | last post by:
Hi all, Hoping someone can help us with this strange Innodb problem. At about 5:15 AM 2/2/04 (this morning), our slave server stopped accepting connections. The machine was pingable, but you...
9
by: john smile | last post by:
Hi All, I want to lock 2 tables on 2 servers using TABLOCKX hint. These tables function as semaphores in my application. It means when the tables are locked then other users will not be able to...
1
by: cranfic | last post by:
Please help... To make it simple, I have a simple test C program which calls my semphore library functions only. (It doesn't call any db2 function at all). And the following is the test...
0
by: m.gelosa | last post by:
Dear all, I wrote some weeks ago about a problem with all the committing transactions blocked for many seconds in a Commit Active state. Well, finally I solved it and I just would like to share...
2
by: iSina | last post by:
Hello, I have written a program that reads pdb files (protein databank) interprets some values and prints back data into a file. Because I need my data without closing the program I use autoflush....
0
by: sukasa | last post by:
Greetings. After first noticing a malfunction in my application, I ran some tests which have given me the following insights on the code performance: -The " infinite" loop which waits on...
0
by: perdoname | last post by:
Hello, I urgently need help, im trying to complete a class which will be able of multiple - clients but is not doing the required puprose. Can anyone be assistance of what is needed to add for...
0
by: Samuel R. Neff | last post by:
I'm having trouble creating a Semaphore with read-access rights for everyone. Originally I was trying to use this code: semaphore = new Semaphore(maxLocks, maxLocks, "RwLock#" + name); but...
3
by: karthikchittur | last post by:
Hello scripters Requirement: I am trying to write a perl script that manipulate a html file. Both the html file and the script are placed at central server. I have scheduled one more script in...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.