Sponsored Content
Full Discussion: Atomicity
Top Forums Programming Atomicity Post 50849 by Perderabo on Tuesday 4th of May 2004 08:02:51 AM
Old 05-04-2004
Quote:
Originally posted by Driver
>
Code:
if ( shared_variable_One == 0 ) /* Variable Shared Across Processes */
 {
 shared_variable_One = getpid ( ) ;
 }

Assume two processes execute this code at ``about the same time''.
You're right that's a race condition. But the next line is...

if (shared_variable_One == getpid ( ) )
{

which is (I think) intended to check for the race condition. But there is still a race condition here. Both processes hit the first test and see the variable as zero. Then one process proceeds all the way past the second test. Then the second process picks up right after the first test and rolls right through the code.

This kind of thing is why I'd be afraid to design my own algorithm. You've gotta go with one of the algorithms that were designed by the pros and have been peer reviewed.
 
WAIT_EVENT_INTERRUPT(9) 					   Driver Basics					   WAIT_EVENT_INTERRUPT(9)

NAME
wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true SYNOPSIS
wait_event_interruptible_exclusive_locked_irq(wq, condition); ARGUMENTS
wq the waitqueue to wait on condition a C expression for the event to wait for DESCRIPTION
The process is put to sleep (TASK_INTERRUPTIBLE) until the condition evaluates to true or a signal is received. The condition is checked each time the waitqueue wq is woken up. It must be called with wq.lock being held. This spinlock is unlocked while sleeping but condition testing is done while lock is held and when this macro exits the lock is held. The lock is locked/unlocked using spin_lock_irq/spin_unlock_irq functions which must match the way they are locked/unlocked outside of this macro. The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag set thus when other process waits process on the list if this process is awaken further processes are not considered. wake_up_locked has to be called after changing any variable that could change the result of the wait condition. The function will return -ERESTARTSYS if it was interrupted by a signal and 0 if condition evaluated to true. COPYRIGHT
Kernel Hackers Manual 3.10 June 2014 WAIT_EVENT_INTERRUPT(9)
All times are GMT -4. The time now is 08:59 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy