Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

What is the race condition?

This is a discussion on What is the race condition? within the OOPS forums, part of the Software Development category; I think it is related to multi-threading......

Go Back   Geeks Talk > Software Development > OOPS
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

OOPS Object-Oriented Programming Concepts

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-20-2007
Junior Member
 
Join Date: Dec 2007
Location: Miami
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ABSMITH is on a distinguished road
What is the race condition?

I think it is related to multi-threading...
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-11-2008
Expert Member
 
Join Date: Oct 2005
Location: india,cochi,bangalore
Posts: 395
Thanks: 2
Thanked 9 Times in 8 Posts
vmshenoy is on a distinguished road
Re: What is the race condition?

Race condition occurs when two threads acces same instance variable, at same time.i.e both threads may get wrong data.

unlike local variables,Remeber instance variables are shared by threads

in order to prevent race condition, you need to synchronize methods
__________________
:)
NEVER SAY DIE.
Reply With Quote
  #3 (permalink)  
Old 05-25-2008
Junior Member
 
Join Date: Jun 2007
Location: Redmond, WA, USA
Posts: 18
Thanks: 2
Thanked 6 Times in 5 Posts
SomGollakota is on a distinguished road
Re: What is the race condition?

Quote:
Originally Posted by ABSMITH View Post
I think it is related to multi-threading...
Race Conditions: When two threads (with same or different execution paths) race with each other to acquire the same resource (be it memory variable or any other programmable resource). An important fact to be kept in mind is that the Operating Systems schedules threads of equal priority arbitrarily. For example,

Thread1 - As soon as writes data to MemVar1, OS puts it to sleep and schedules Thread2 for execution
Thread2 - Does a few operations and Writes data to MemVar1. OS puts Thread2 to sleep and schedules/resumes Thread1 for execution again
Thread1 resumes and uses data it previously wrote to MemVar1. However, since Thread2 overwrote Thread1's data in MemVar1, the data is now dirty for Thread1.

This condition can be mitigated using synchronization objects for data manipulation. However, ineffective/improper use of synchronization objects result in other (and very painful to resolve) problems such as deadlocks and slowing down of program execution. Consider the following example.

Thread1:
Acquire SyncObject 1;
Modify MemVar1;
Acquire SyncObject2;
Modify MemVar2;
Verify MemVar1 and MemVar2;
Release SyncObject1 and SyncObject2;
Exit Thread;

Thread2:
Acquire SyncObject2;
Modify MemVar2;
Acquire SyncObject1;
Modify MemVar1;
Verify MemVar2 and MemVar1;
Release SyncObject2 and SyncObject1;
Exit Thread;
------------------
In the above two scnerios, consider that after Thread1 acquiring SyncObject1 and modifying MemVar1, the OS puts it to sleep and schedules/resumes Thread2 for execution. Thread2 resumes, acquires SyncObject2, modifies MemVar2, and tries to Acquire SyncObject1. Since SyncObject1 is held by Thread1, Thread2 goes into wait mode until SyncObject1 is released by Thread1. OS puts the waiting thread to sleep and resumes Thread1. Thread1 now tries to acquire SyncObject2. Since SyncObject2 is held by Thread2, Thread1 goes into wait mode until Thread2 releases SyncObject2.

Since Thread2 will not release SyncObject2 until Thread1 releases SyncObject1, and Thread1 does not release SyncObject1 until Thread2 releases SyncObject2, this results in a deadlock. Both threads lock up and will not execute any further resulting in the program hanging.

Last edited by SomGollakota; 05-25-2008 at 01:53 PM.
Reply With Quote
  #4 (permalink)  
Old 05-28-2008
Junior Member
 
Join Date: May 2008
Location: mumbai
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
poo_roh is on a distinguished road
Re: What is the race condition?

Race condition occur when 2 or more thread try to access the same resource at same time.
here the resources can be a local varialble,memory,file etc.
Reply With Quote
  #5 (permalink)  
Old 06-20-2008
Expert Member
 
Join Date: Dec 2007
Location: Pune
Posts: 142
Thanks: 0
Thanked 8 Times in 8 Posts
peeyush_jain is on a distinguished road
Re: What is the race condition?

Race condition errors can occur when the outcome of a program dependant which of the two thread complete a process first.
PLease let me know if you want more details .
Reply With Quote
  #6 (permalink)  
Old 10-16-2008
Contributing Member
 
Join Date: Oct 2008
Location: tamilnadu,india
Posts: 74
Thanks: 2
Thanked 6 Times in 6 Posts
rakesh trichy is on a distinguished road
Re: What is the race condition?

A race condition or race hazard is a flaw in a system or process whereby the output and/or result of the process is unexpectedly and critically dependent on the sequence or timing of other events. The term originates with the idea of two signals racing each other to influence the output first.

Race conditions can occur in electronics systems, especially logic circuits, and in computer software, especially multithreaded or distributed programs.
Reply With Quote
Reply

  Geeks Talk > Software Development > OOPS

« oops | interfaces »
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Where Condition krishnaindia2007 Oracle 3 11-30-2007 01:54 AM
Swimming Race.... psuresh1982 Brainteasers 7 07-14-2007 08:55 AM
race psuresh1982 Brainteasers 9 01-30-2007 10:00 AM
Formula 1 race.. psuresh1982 Brainteasers 6 12-06-2006 02:14 AM
Race jamesravid Brainteasers 4 11-27-2006 03:49 AM


All times are GMT -4. The time now is 07:50 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved