![]() |
|
#1
|
|||
|
|||
Who Calls main and where does main returns (C/C++)I would like to know more about main function.(in C/C++)
Will any one help me to answer the questions 1. Who calls main? (in C/C++) 2. Where does main returs? 3. Who uses the return value and for what purpose? Please Don't just answer it as OS. I am looking for a detailed answer. |
|||
|
#2
|
||||
|
||||
|
Quote:
Hello Poolan. Welcome to GIDForums™. My immediate answer is the O/S .Actually, only the lowest level of programs are actually called directly by the O/S. The better answer is by the parent process. Using a linux shell for example, when I execute a program from the linux shell, it spawns a new child process and executes the program with the entry point defined by main. The "parent" process will then patiently wait for the end of the child process. Once the child process is done, the return value of main is returned to the parent process. Most of the times, the return value indicates success or failure. However, there are times when it could be a value that the calling program needs, etc. It must be an integer however. With MS Windows, much of this is obscured by the O/S itself although it is the same principle. When you click on an icon in windows and a process starts, the calling (parent) process is not the O/S. It is called by a process that is probably several layers removed from the actual kernel. It still returns a value and the parent process should check this value for sucess or failure. I am not sure if that helps or not. In summary:
__________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
|
Dear Smith,
I got a general idea on that. Thanks for your reply. But I would like to know weather threre is any role for 'C Runtime' in calling main. And I would like to know weather, in any case main exit before termintion of the progarm. (which multi threading) Poolan |
|
#4
|
||||
|
||||
|
Quote:
As for your second question, I didn't understand what you are asking. Please restate. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#5
|
|||
|
|||
|
Thank you for your reply.
My second question was "Will the main function return before the termination of the program in any case". for eg: "Will there exist any child thread which is created by the main function after the tremination of main function?" Poolan |
|
#6
|
||||
|
||||
|
Quote:
Hi poolan. Again I will attempt to answer this question. I am by no means an expert... In the case of a multi-threaded process, by killing the parent thread, all other child threads should be terminated. This is not a definitive, but it is a good idea. That is why you should always call pthread_exit (or its equivalent) when you are done with your program and you are ready to exit. If not, you can get child threads running all over the place and they are messy to clean up. I have first hand experience in this.. ![]() It is kind of the same thing with a child process. A child process needs a parent to return a value to. I believe this is how you get a zombie process. From the Linux man page: Quote:
My best answer is there definitely can be a child process that continues after the parent process. If you are a programmer though, you should think hard about doing this. If you are a *nix user, you should know that an & in a bash command will spawn a process that will not close when you close the shell. In addition, the parent process will not wait for the program to finish. I am not advanced enough though to know the mechanics of this. I hope this rambling helps in some manner. __________________
The best damn Sports Blog period. |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The