DevNewz News Archives About Us Feedback
Learn More about our dedicated servers

Recent Articles

Microsoft Talks Money In NYC
The Financial Services Developer Conference in New York will promote the .Net Framework as the place to develop next generation financial applications.

Google Summer of Code Search for Python...
Google is looking for students from undergrad through the Ph.D levels to work on new open source code and to act as project mentors.

Google Summer Of Code 2006
Google has announced their second annual Summer of Code, with 49 organizations mentoring student developers who want to work together on some cool open source projects.

But is it Programming?
Apple has put out a tutorial introducing programming with Core Data, Xcode and Interface Builder...

Two Faces of Microsoft
I'm in a lot better mood lately cause I am back home doing the Channel 9 thing. Interviewing Microsoft folks about what they do.

Cool Stuff for Developers Over on Scott’s Blog
Scott Guthrie is a general manager at Microsoft. One thing I like is his blog. It's been getting more and more popular with developers over the past few months.

Intuitive Software Design: The gift that keeps on giving
Since the beginning of computers and software development, the user has wanted ease of use and an intuitive interface.

Microsoft Shaking Up Windows Division Management
According to the Wall Street Journal, Microsoft is reorganizing some of its managers, bringing Steve Sinofsky to the Windows Division over from Office, in an attempt to instill the same reliability Office development has on Windows development.

Don’s $100 API challenge
Back when I was an MVP product groups would bring us into a room and hand us some cash and say "spend this on the features you want..."

Google Draws A Check For SketchUp
The maker of 3D creation package SketchUp, @Last Software of Boulder, CO, have been acquired for an undisclosed sum by Google.

04.26.06


Race Conditions

By A. P. Lawrence

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.

Free to Develop, Deploy. Oracle Database XE.

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


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

About DevNewz
DevNewz has assembled experts around the world to deliver helpful advice to application developers. Our in-house news staff focuses on keeping you updated with the latest new software and trends in application development. DevNewz provides Knowlege For Application Developers.

DevNewz is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITmanagementNews.com


-- DevNewz is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2006 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article

Knowlege For Application Developers DevNewz Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact