Atomicity


 
Thread Tools Search this Thread
Top Forums Programming Atomicity
# 8  
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.
# 9  
Old 05-05-2004
As I am not confident about a topic and it may sound to be silly, so forgive me for my query.

My question is 'is if atomic' i.e. suppose if I have the following lines of code in a 'C' program:

......;
......;
x=0;
y=8;
u=1;
z=9;
...... ;
.......;

if ( x == 0 && y ==8 && ++u || z = user_func ( ) && ...... )
{
/* Statements to be executed */
}
..........;
..........;

Does atomicity regarding 'if' is implied in 'C'.

Thanks in advance.

Last edited by S.P.Prasad; 05-05-2004 at 10:42 AM..
# 10  
Old 05-05-2004
If you mean 'will the entire if statement be evaluated atomically'
-- the answer is No. It cannot be guaranteed.

Once the quantum expires (your timeslice is used up), some other process can pre-empt the processor. Assuming I understood your question correctly.
# 11  
Old 05-11-2004
Please visit the link within this post.

_check_lock and _clear_lock on AIX


I tested the code implementing the routines and the code behaves fine in Unit testing.

Also if someone can explain me in detail "The word variable must be aligned on a full word boundary." as stated in the page. Please correct me if I am worng - "A group of related bytes that are treated as a single addressable unit or entity in memory is called as a WORD. Hence size of a word varies from one computer to another, depending on the CPU. For computers with a 16-bit CPU, a word is 16 bits (2 bytes)."

Single Word = Single addressable unit by CPU
Word Boundary = Address completely divisible by number of bytes that represents a single addressable unit.

Example Single Word length = 16 bits then a word boundary is any address that is completely divisible by 16, which would mean that the variable referenced here should begin at 0,16,32,48,64 ... and so on memory locations and the variable should consume 16 bits of memory.

Am I correct? What extra care should I take into account during coding?

Please let me know whether these are a proven set of standard 'C' routines. Please let me know your honest opinions before I put it into the production system and have a go.

Thanks in advance

Last edited by S.P.Prasad; 05-11-2004 at 05:11 AM..
# 12  
Old 05-11-2004
Quote:

> Single Word = Single addressable unit

No, as I said, the largest single addressable unit that fits into a GPR.

> Word Boundary = Address divisible by number of bytes that represents a single addressable unit.

With the above addition, correct.

> Example Single Word length = 16 bits then a word boundary is any address divisible by 16.

I think this is a typo, but just to make sure: No, divisible by two
I agree to the modification of the definition of 'Single Word'.

Please provide me arguments as to why you say 'No, divisible by two' with regards to ‘word boundary'. Is it that word boundary depends upon the type of data we are referring to i.e. depends upon the sizeof of the data type. Hence for a four-byte-sized integer the word boundary will be of multiples of 4 and for two-byte-sized integer the word boundary will be multiples of 2 respectively.

Thanks in advance.
# 13  
Old 05-11-2004
Oh ! After reading your reply I realized that I have confused myself regarding 'word boundary' memory alignment. It happens to me at times.

Thanks Driver for your replies.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question