Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

Race Conditions

A.P. Lawrence
Expert Author
Published: 2006-04-25

WebProNews RSS Feed


A "race condition" is a circumstance where you have two things that really need to happen sequentially, but don't.

Usually if there is an error (the programmer didn't provide any method of enforcing the sequence), it's because the programmer didn't notice the problem under "ordinary" circumstances. It then becomes a "bug", where something unexpected happens sometimes but not always. It comes up a lot in system level programming, but also well above that.

A good example of that came up with regard to How can I make a device that will print to a network printer?. That article suggests a method to create a "device" that transfers data to a network printer. That's useful for ancient software that can't work with spooled printers.

The solution presented works, but does have a potential race condition. I'd never noticed it, but this comment pointed it out:
I needed to migrate from serial printers to network printers using (D-Link and Netgear) print server devices. Unlike a Unix system, those devices have no understanding of printer capabilities. I therefore needed to retain the interface mechanisms used with local printers. I fell upon your suggestion of diverting printing to a remote printer using a named pipe and a perpetual script with enthusiasm, and at first it appeared to work brilliantly. However, disappointment set in when garbled print came out of busy printers. This is, I think, caused by a rapid succession of lp jobs for the same printer that write concurrently to the same named pipe. It seems that the lp system relies on the ability of a process to lock a tty for exclusive writing, and to wait if it finds a locked tty; I am not aware of a similar feature for named pipes. The remedy seems to be to amend the interface scripts to invoke the remote print instead, as I believe you and others have suggested elsewhere, and abandon the neat but, it seems, flawed approach of using a named pipe.
Actually, print jobs would never write concurrently to the same pipe; they would always be sequential, so that's not where the problem lies. It's the process that is reading the pipe that has the problem.

Here's what happens: the reader process is reading data from the pipe. When it hits eof, it picks up and sends that data to the print server. Normally, that's all very quick, but if the jobs are large and the system is busy, there may be data from a new print job written to the pipe before the reader is quite through. That can confuse things.

A simple solution is to add a sleep at the end of the writer interface script. If the sleep is longer than the time it takes to transfer data to the print server, that solves the problem, but of course the race condition still exists: you've simply handicapped one of the runners. However, that may be "good enough" for most circumstances.

If it isn't, you need to institute some form of cooperative locking scheme. Your writer process would not send new data until the reader process had cleared the lock. That's very simple to do in Perl. In a shell script, it's not as neat but can still be accomplished with a "mkdir": creating a directory is always atomic, so if you can't (because it exists), your other process has the lock. That scheme does have the problem of stale locks (the directory) being left over from a crash, so I'd rather use Perl's "lockf".

*Originally published at APLawrence.com

Add to | DiggThis | Yahoo! My Web

Receive Our Daily Email of Breaking eBusiness News


About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

WebProNews RSS Feed

More Expert Articles Articles

Contact WebProNews
Advertisement





TOP NEWS

Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.
eBUSINESS RESOURCES






 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.


WebProWorld
Ten most recent posts.

NetworkingFiles
Featured Software

WebProNews in the News
View all recent mentions of WebProNews from around the world!

Recent Articles On ...
Google eBusiness
Yahoo Ask Jeeves
MSN Blogs
Search Engines Blogging
Affiliate Programs Marketing
eCommerce Advertising
eBay Sun Microsystems
AOL Adsense
Microsoft Adwords
Oracle IBM
Amazon Apple
SEM Mac
SEO iPod
Adsense XBox
PR Adobe



iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us