Thread: Parallel execution

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Utrecht, Netherlands
    Posts
    18

    Parallel execution

    Hey guys,

    I'm still getting used to C, but need to run a really large simulations in it. I have a very n00b question about how C handles memory etc.

    More specifically, is it risky to simultaneously execute the same program in C? As in, if there are some inconsequential (for a single execution) memory leaks, or global variables...if I run other instances of the code at the same time... can they possibly get in each other's way?

    (there is no IO collisions, as in multiple running instances will not try to for example access the same files..that could of course be a problem, but its not the case)

    Sorry if the question is a bit vague, but hope understandable.
    I ran into a problem like this in python , where apparently a global variable's scope carries over into parallel executions of a program, if started by the same script.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    It is like any other language. If you had to run the same code twice, it would effectively be a two different process. Meaning it would have it own process local member allocated like stack and the heap. Which in any case will never collide. However, if your trying access memory which are shared like mmap or any other external resources. Perhaps, you might have to give more thought about it as. The multiple process racing for the same resources at the same time can cause race condition. A locking mechanism should be implemented. This applies for the handling the file. If you're just reading a file, then its probably fine. But any writing operation need to be controlled by locking mechanics.

    ssharish
    Last edited by ssharish2005; 09-11-2012 at 06:41 AM.
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Every process gets its own stack, data and code space. So to answer your question, global variables are "process local".
    On the other hand, if more than one process use the same resource, like an IO operation, eg the same file. There may be problems if you don't design your program properly.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I ran a Sudoku program 8 times (at the same time), on the same 8 core processor, for weeks at a time, with no problems. Each instance of the program, ran from it's own directory, reading from it's own data file, and writing out it's own output file. (Each file having hundreds of thousands of lines of data). In addition, each program called another program to process the data file, every few minutes, so there was a LOT of file IO going on.

    It ran flawlessly, and never locked up or crashed. Of course, if you have a memory leak, it will undoubtedly crash the system, eventually. If you set up any system resources as shared, between the programs, THEN you have to be VERY careful, as mentioned above.

  5. #5
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    No more so than running multiple programs simultaneously is risky.

    If you're really worried, fix the memory leaks.

    If you run lots of threads from a single process, though - that's a different matter depending on what you share between them.

    Which reminds me - I have a new quad-core, hyperthreaded laptop and need to try out some 8-way threading on it for my own amusement.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with two parallel arrays
    By Rockie in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2011, 10:55 PM
  2. Serial to Parallel
    By ssharish2005 in forum Tech Board
    Replies: 11
    Last Post: 09-10-2007, 01:11 PM
  3. Parallel execution
    By Panopticon in forum C++ Programming
    Replies: 2
    Last Post: 01-27-2003, 01:57 AM
  4. Serial-Parallel
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-19-2002, 04:38 AM
  5. !!parallel ports!!
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-30-2001, 11:28 AM