GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Oct-2004, 06:51
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
Question

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  
Old 26-Oct-2004, 08:02
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by Poolan
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.

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:
  1. Main is the entry point for your program.
  2. The return value in main is returned to the calling (parent) process.
  3. Most of the time this value indicates success/failure.
  #3  
Old 26-Oct-2004, 22:00
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
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  
Old 26-Oct-2004, 23:44
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Poolan
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
No, the "C Runtime" is used by the program. When you compile and link, parts of the C Runtime are copied into your code from the Runtime Library. After that, the program is completely self contained, the runtime lib is no longer needed.

As for your second question, I didn't understand what you are asking. Please restate.
__________________

Age is unimportant -- except in cheese
  #5  
Old 27-Oct-2004, 00:10
Poolan Poolan is offline
New Member
 
Join Date: Oct 2004
Posts: 19
Poolan is on a distinguished road
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  
Old 27-Oct-2004, 10:00
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by Poolan
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


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:
Processes marked <defunct> are dead processes (so-called
"zombies") that remain because their parent has not
destroyed them properly. These processes will be destroyed
by init( 8 ) if the parent process exits.
So again, it is possible to kill a parent process and have a child going along, but the exit will be sloppy if not handled properly.

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.
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.