tags:

views:

144

answers:

4

Does the main function we define in C or C++ run in a process or thread. If it runs in a thread, which process is responsible for spawning it

+3  A: 

It's a process that you spawn when you execute your program. The main function is called at the beginning of the program. It is all a part of the same program (i.e. one process).

Silmaril89
as mentioned by michael does main runs in a thread
siri
It is just one process, even if they call fork(), it spawns a new process. No threads are spawned. Depending on the OS though, it may treat the program as a thread within the process, but there is just one thread doing all the work of the program. So, Michael is still right to a certain degree.
Silmaril89
@Sirish: The platform being run on may or may not have threads. It probably has processes, or at least one process.
David Thornley
+1  A: 

Your whole program is a single process unless it starts fork()ing things, and by default the process has one thread that does everything; main() starts on that thread

Michael Mrozek
+2  A: 

When you ask your OS to start a new process, it initializes data structures for a process and for a single thread inside that process. The initial instruction pointer in that thread context is the process entry point, which is a function provided by your C runtime library. That library-provided entry point converts the environment table and command-line arguments into the format demanded by the C standard, and then calls your main function.

Ben Voigt
None of which is actually necessary to c or c++, of course, this is just what modern, general-purpose OSs do. Either language can be defined to run in a bare-metal, unprotected environment that does not support processes or threads.
dmckee
Bare-metal environments generally don't have a `int main(int argc, char* argv[])` function. The question isn't "Context of power-on interrupt handler".
Ben Voigt
No need to get defensive, the comment was for the OP's benefit and you got my vote for a lucid summary. Certainly the embedded world is different, but there was a c compiler for the Apple II, and that ran programs in the same ring as the "OS": no protection was available. Likewise for early versions of DOS, I believe.
dmckee
+3  A: 

main() is the entry point for your program. C++ (current C++ anyway) doesn't know what a process or thread is. The word 'process' is not even in the index of the standard. What happens before and after main() is mostly implementation defined. So, the answer to your question is also implementation defined.

In general though most operating systems have the concept of process and thread and they have similar meanings (though in Linux, for example, a thread is actually a "light weight process"). You can generally assume that your program will be started in a new process and that main() will then be called by the original thread after the implementation defined initialization.

Since there's plenty of room for the implementation and/or you to start up a whole bunch of threads before main is called though you will probably generally want to consider main() to have been called during the execution of a thread. The best way to think about it though is probably in terms of the standard unless you really have to think about the implementation. The standard doesn't currently know what a process or thread is. C++0x will change that in some way but I'm not sure at this point what the new concepts will be or how they will relate to OS specific constructs.

My answer is specifically addressed at the C++ language part of the question. C is a different language and I haven't used it in a good 10 years so I forget how the globals initialization is specified.

Noah Roberts
why main cannot directly execute in a process, why need to spawn a thread...
siri
@Sirish: It is OS dependant. Some OS's will spawn threads in their processes that do all the work.
Silmaril89