Thread: The most silliest question about functions

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    The most silliest question about functions

    Hello,
    Just wanted to know how does the functions work internally? I mean functions are laoded into memory only once. So, if hundred clients request the function at the same time, 100 copies of same function aren't going to be created. It's only 1 function that executes. So how does it get handled? if 1st client requested function and the function is in middle of execution and suddenly 2nd client also requested. So how will the function get executed?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Functions in the same process are only loaded once.
    If multiple threads in the same process access the same function at the same time, I believe they just create their own stack space for the function's local variables and execute as normal. If it's on a single CPU/core system then only one thread is running at a time, but on multi-core systems each core could load the executable code into it's cache and run...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by cpjust View Post
    Functions in the same process are only loaded once.
    Generally, yes. But then there's stuff like Kernel Samepage Merging for linux, and there's probably a windows variant of it as well, that works to reduce duplication of memory.
    Quote Originally Posted by cpjust View Post
    If multiple threads in the same process access the same function at the same time, I believe they just create their own stack space for the function's local variables and execute as normal.
    Correct. And global variables are shared between all threads. There might be some OSes that do it differently but personally I don't know of any.
    Quote Originally Posted by cpjust View Post
    If it's on a single CPU/core system then only one thread is running at a time, but on multi-core systems each core could load the executable code into it's cache and run...
    Yes, but even if you're on a single cpu system the task scheduler could switch out one thread while it's in the middle of a function and then switch in another thread in the same function so you could still run in to race conditions on a single cpu.
    Last edited by _Mike; 01-28-2010 at 08:02 AM. Reason: Adding note about KSM

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Right cp,

    you mean to say if none of the core is free then the client requesting the function has to wait? I was just thinking about a general scenario where a web application is there and 1000's of clients request the same function eg. say to add a record to database. If server is of 64 cores then does it mean after all cores are occupied and busy the request will be in wait state?

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by chottachatri View Post
    Right cp,

    you mean to say if none of the core is free then the client requesting the function has to wait? I was just thinking about a general scenario where a web application is there and 1000's of clients request the same function eg. say to add a record to database. If server is of 64 cores then does it mean after all cores are occupied and busy the request will be in wait state?
    A bit oversimplified; But yes, then they'd have to wait. But depending on how long the database transactions take the wait might be so short that it seems to be instant from the user's point of view.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Cool you people are so responsive and helpful. I wish there was a java category also on this forum since this semester we need to learn JEE. This is my first forum that i ever joined and really never enjoyed so much on any other till date.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about redefining functions in derived class
    By Sharke in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2009, 11:48 AM
  2. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  3. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  4. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  5. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM