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.

Static variables in multiuser environment

This is a discussion on Static variables in multiuser environment within the C and C++ forums, part of the Software Development category; If in a multiuser system a static variable is initialized as 10 and the first user increments its value by 1 and then the second user increments its value by ...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 06-22-2006
Moderator
 
Join Date: Oct 2005
Posts: 1,037
Blog Entries: 75
Thanks: 3
Thanked 168 Times in 136 Posts
Lokesh M has a spectacular aura aboutLokesh M has a spectacular aura about
Static variables in multiuser environment

If in a multiuser system a static variable is initialized as 10 and the first user increments its value by 1 and then the second user increments its value by one.
What is the value of the variable now?

(This question was asked in Accenture Technical Interview)
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-17-2007
Contributing Member
 
Join Date: Jul 2007
Location: India
Posts: 31
Thanks: 6
Thanked 4 Times in 4 Posts
kamanianil is on a distinguished road
Re: Static variables in multiuser environment

It is be 12 ...
Reply With Quote
  #3 (permalink)  
Old 12-25-2007
Contributing Member
 
Join Date: Nov 2007
Location: bangalore
Posts: 54
Thanks: 6
Thanked 6 Times in 4 Posts
rahulvegi is on a distinguished road
Re: Static variables in multiuser environment

i aggred to the above answer
Reply With Quote
  #4 (permalink)  
Old 12-26-2007
Contributing Member
 
Join Date: Dec 2007
Posts: 48
Thanks: 1
Thanked 8 Times in 7 Posts
sk_seeker is on a distinguished road
Re: Static variables in multiuser environment

Quote:
Originally Posted by Lokesh M View Post
If in a multiuser system a static variable is initialized as 10 and the first user increments its value by 1 and then the second user increments its value by one.
What is the value of the variable now?

(This question was asked in Accenture Technical Interview)
This question is a trap. Be careful how you answer this. The answer is that the value is non-deterministic due to the possibility of a race condition.
Details below.

- Lets take an example. Let us say that the static variable is x and its value is 10. Lets say that two processes/threads are doing the increment: let us name them as P1 and P2.
- a increment operation in C (or in any other programming language) is actually three operations at the machine level i.e. a load, modify and a store.
- So, since there is no locking implemented to access the static variable, the load, modify and store can be started by P1 and P2 at any time.
- For example, lets say P1 and P2 started the load instruction at the same time. Both will read a value of 10 from x, both will increment to 11 and both will write 11 back. So, in this case, an increment is lost.
- Another example, lets say that P1 started its load first, and read 10, incremented and then stored 11 back to memory. If P2 now starts the load, the value will be 12.
- Since the order/timing of the instructions determine the outcome, this is a perfect example of a race condition. This is why we need locking to protect access to global scope variables.

Hope that helps.
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

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



All times are GMT -4. The time now is 11:16 PM.


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