LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-04-2011, 07:01 PM   #1
systemlordanubis
Member
 
Registered: Jun 2010
Distribution: Debian, Ubuntu, Win
Posts: 143

Rep: Reputation: 16
Preventing duplicate threads from performing actions at the same time.


Hi All,

We have a linux server designed as an authentication gateway for our network.

One of our perl scripts which process authentications uses a lockfile (Lockfile::Simple) to prevent duplicate logins from occuring over the top of one another.

Each login attempt/request is processed in a separate thread so that one request doesn't hold up another.

However, I'm getting issues with the lock, completely locking up and crashing the server.

Is there another way (aside from using lockfiles) to prevent duplicate request from being processed?

Any help on this would be much appreciated.
Thanks
Anubis.
 
Old 05-04-2011, 08:51 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426

Rep: Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786
If you really are using Perl Threads, use a Threads Shared hash of current logins and check each new login to see if an entry already exists in the hash. If it does refuse login.
Much faster as this is all in RAM.
This assumes that any thr can accept a login. If you have a dedicated thr (I use the master/startup thr for similar) then just a local hash is sufficient.
 
Old 05-04-2011, 11:47 PM   #3
systemlordanubis
Member
 
Registered: Jun 2010
Distribution: Debian, Ubuntu, Win
Posts: 143

Original Poster
Rep: Reputation: 16
Hi Chris,

Thanks for the reply.

To clarify, I have a master process (perl script) running which forks client requests to their own process.

I'm not familiar with Shared Hash's, do you have any reference points on this?

Thanks
Anubis.
 
Old 05-05-2011, 01:11 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426

Rep: Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786
Sounds like you are using multiple processes, not Threads; 2 different things.
In that case, you could just use a hash in the master process to keep a list of current logins. However, you'd have to find a method for the client procs to communicate back when they're done, so releasing that entry.
Methods include writing to a file (can be tricky), 1 table DB (handles locking for you), shared mem, etc.

For lock files, I use bare bones code
Code:
use Fcntl qw(:flock);

open(LOG) or (error handler)
flock(LOG, LOCK_EX) or 
flock(LOG, LOCK_UN) or ..
close(LOG) or  ...
Strictly speaking you only need a LOCK_EX for writing (& will block until other writer process with LOCK_EX lets go), LOCK_SH will do for read-only without blocking eg 'test if login in this file' only requires read access.

http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials

Threads are useful for co-operating 'processes' (ie threads not forked processes), but hard to learn first time around.


Have a look at Chap 16.19 in Perl Cookbook (worth its weight in gold imho) for uses of wait(), waitpid() and also those links, as another option.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Duplicate Syndicated News threads Kenny_Strawn LQ Suggestions & Feedback 1 12-22-2010 04:15 PM
When repling to threads on LQ it submits duplicate replies and can't edit EDDY1 LQ Suggestions & Feedback 7 11-23-2010 05:52 AM
[SOLVED] Preventing bots from posting in threads Aquarius_Girl LQ Suggestions & Feedback 7 04-13-2010 10:31 AM
A better way to handle duplicate threads? Cogar LQ Suggestions & Feedback 7 07-14-2006 09:22 AM
performing multiple actions with procmail Red Squirrel Linux - Software 1 05-28-2005 04:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration