httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ben Laurie <...@gonzo.ben.algroup.co.uk>
Subject Re: help with a concurrency locking problem?
Date Fri, 24 May 1996 10:18:47 GMT
sameer@c2.org wrote:
> 
> 	I have a module which uses a shared anonymous mmap memory
> segment. It is an array of structures, one entry per user on my site,
> with max_bytes and bytes_sent for each user's daily bandwidth limit. 
> 	I think that I am having a concurrency problem where if two
> increments are happening at the same time then only one increment
> takes place. (So users who are only paying for say 500MB/day of
> traffic end up getting 800MB of traffic or something, because all the
> hits don't increment the counter properly.)
> 
> 	I attempted fixing it by replacing:
> 
>   if(orig->bytes_sent > 0)
>     {
>       /* Do the increment */
>       cls->tableptr[orig->finfo.st_uid].bytes_sent += orig->bytes_sent;
>     }
> 
> 
> 	With:
> 
>   if(orig->bytes_sent > 0)
>     {
>       /* Do the increment */
> 
>       /* Wait for the lock to free */
>       while(cls->tableptr[orig->finfo.st_uid].lock);
> 
>       /* Set the lock */
>       cls->tableptr[orig->finfo.st_uid].lock = 1;
> 
>       /* Do the increment */
>       cls->tableptr[orig->finfo.st_uid].bytes_sent += orig->bytes_sent;
> 
>       /* Free the lock */
>       cls->tableptr[orig->finfo.st_uid].lock = 0;
>     }
> 
> 
> 	Now anyone have any ideas on how I can check to see if:
> 
> A) This was a problem in the first place?

It clearly is (unless the += operation happens to be atomic, which I doubt).

> B) This fixes the problem?

Nope ... two processes can both get the lock.

> C) This makes things worse (that scary-looking while statement up there)?

Well, spin locks are OK for stuff that's short lived - but they only work if
you have an atomic test-and-set. Because you don't have control of scheduling,
spin locks are inadvisable in user programs (as opposed to the depths of the
kernel, where they are very appropriate, especially for multiprocessor
systems).

> 
> 
> 	Any ideas on a better way to do locking? I could use fcntl
> locking but that means I need one fd per user, right? (I need
> record-level locking, and I have one record per user)

Use semaphores? Since the operation is very quick you can share a lock between
all users if resource shortage is a problem.

Cheers,

Ben.

> 
> -- 
> Sameer Parekh					Voice:   510-601-9777x3
> Community ConneXion, Inc.			FAX:     510-601-9734
> The Internet Privacy Provider			Dialin:  510-658-6376
> http://www.c2.net/ (or login as "guest")		sameer@c2.net

-- 
Ben Laurie                  Phone: +44 (181) 994 6435
Freelance Consultant and    Fax:   +44 (181) 994 6472
Technical Director          Email: ben@algroup.co.uk
A.L. Digital Ltd,           URL: http://www.algroup.co.uk
London, England.

Mime
View raw message