473,805 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on main

who calls main() ?

Aug 5 '06
14 1684
"Chris Torek" <no****@torek.n etwrote in message
news:eb******** @news2.newsguy. com...
In article <11************ **********@b28g 2000cwb.googleg roups.com>
<Su********@gma il.comwrote:
>>hmmm.... 'the system calls main' is precisely the answer I am
finding
in documentation everywhere..... and I am finding it sort of vague...

Probably because it is (deliberately) vague. :-)

As Eric Sosman noted elsethread, the method is up to the
implementation. It might be illustrative (if borderline off-topic,
and
probably on the "off" side of the border-line) to look at two real
implementations , however.

The first implementation to consider is BSD/OS on the i386. Here,
when you compile a program with:

shlicc -o foo foo.c -ljpeg -lm

you get an executable named "foo" that contains:

- your own code,
- some startup code, and
- references to the shared libraries used (libjpeg, libm, and
libc).

The startup code:

- saves argc and argv (actually just one register that points to
a system-provided data structure) and __environ for getenv() etc
- ensures that file descriptors 0, 1, and 2 are open
- locates the shared libraries, opening them and mapping them in
at the desired locations (text and initialized data, with
"uninitiali zed data" mapped to zero-fill space)
- calls any initialization routines in those shared libraries
(this is really for C++; pure C code never has any)
- calls your main(), passing the saved argc and argv (and a
third parameter which you may safely ignore)
- takes whatever value your main() returned and calls exit()

Initialization of static-duration data for your program -- whether
ordinary initialized variables like:

int globalvar = 3;
void f(void) { static int localstatic = 42; ... }

or uninitialized ("bss", Block Started by Symbol) zero-fill data
-- is handled automatically by the system. However, static-duration
data for shared libraries is handled by the startup code (with a
lot of help from the system; it winds up being just three __mmap()
calls).
An example of "who" accomplishes these things (info from Linux, but
any POSIX/ELF system should be similar):

1. The kernel loads the program image into memory when you do an
exec()
2. If there's dynamic objects linked, ld.so is called to load them and
link them into the image
3. The kernel examines the ELF header to find the entry point (usually
called _start())
4. The kernel jumps to that address
5. _start() handles the initialization tasks Chris lists above
6. _start() calls your main()
7. When main() returns or exit() is called, _start() does all the
cleanup tasks listed above
8. Last, _start() executes a syscall to the kernel to let it know the
process is finished

(I might have step 2 in the wrong place; _start() may call ld.so
instead of the kernel)

Another example is DOS COM files. DOS simply loads the file into
memory and jumps to a particular offset in that memory image. That
location is where the linker puts the equivalent of _start(), and then
execution proceeds roughly as above from step 5.

Every implementation is going to be different, but it needs to do
roughly the same things, so they'll all look roughly the same.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Aug 6 '06 #11
<Su********@gma il.comwrote
who calls main() ?
Normally there is rule about where execution of binaries starts.
An obvious one is that the first byte of the file is the first machine
instruction.

However normally programs require a bit of start-up and end code. There
might be a routine to initialise all the zero areas of memory to zero, for
example. Almost certainly there also needs to be code to set up the
command-line arguments.
So the compiler will typically put this start-up code in the start" position
of the program, and it will the call main().

If you look at assembly language programing on your system, you will get a
better idea of the conventions used.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.

Aug 6 '06 #12
"MQ" <mi************ **@gmail.comwri tes:
dbtid wrote:
>Andrew Poelstra wrote:
On 2006-08-05, Su********@gmai l.com <Su********@gma il.comwrote:
who calls main() ?
Your operating system.

I don't have an operating system, but I do have a main...

Well, what do you have? You have a program obviously, whose entry
point is main. An operating system is something like Windows, Linux,
etc that provides some way of loading and executing your program. In
Windows this usually means double-clicking the program's icon. Can you
explain more about what you are trying to do?
Presumably dbtid is using an embedded system, and the program runs on
the "bare metal".

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 6 '06 #13
In article <11************ **********@i3g2 000cwc.googlegr oups.com>,
MQ <mi************ **@gmail.comwro te:
>dbtid wrote:
>Andrew Poelstra wrote:
On 2006-08-05, Su********@gmai l.com <Su********@gma il.comwrote:
who calls main() ?
Your operating system.
>I don't have an operating system, but I do have a main...
>Well, what do you have? You have a program obviously, whose entry
point is main. An operating system is something like Windows, Linux,
etc that provides some way of loading and executing your program.
There have been a number of claims made over the years that,
technically speaking, Windows is not an operating system.
I didn't follow those debates; it is possible they only apply to
the versions not based upon NT.
--
All is vanity. -- Ecclesiastes
Aug 6 '06 #14
Walter Roberson said:

<snip>
There have been a number of claims made over the years that,
technically speaking, Windows is not an operating system.
I suppose that depends on your definition of "operating system". I tend to
think of "operating system" as meaning "system that is operating". And
Windows systems have always been a bit shaky when it comes to operating.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 6 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1798
by: Scott Berry | last post by:
This is a little frustrating, because it should be easy. I have an application that has been converted to SQL Server 2000 from MS Access 97. New data is loaded each week and one of my old queries will no longer work to assist with formatting the data. I have a Phone Number field that is Varchar 10 The numbers have imported without the leading zero.
16
637
by: ^_^ | last post by:
conversion from: a="a"; to a=0x????; If there are many unicode strings to convert, how can I do batch-conversion?
38
2342
by: aaronfude | last post by:
I'm working on a scientific computing application. I need a class called Element which is no more than a collection of integers, or "nodes" and has only on method int getNode(int i). I would like to implement in the most efficient was possible. So I summoned up my programming intellect and asked myself: Do I want to have members such as int a, b, c, d, e or a single member such as int a. So I wrote the following snippet and compiled it...
11
6921
by: Sweety | last post by:
hello to all members, i have strange question in C. main() { printf("%d",main) ; } here o/p is same for all m/c in TC++ version 3.0 i.e 657. I think this is not garbage.
24
1696
by: Tweaxor | last post by:
This has been puzzling me all this week. This is actually a homework assignment from last semesmter. But the teacher wouldn't tell us why certain things didn't work, but it didn't just work. My thing was what actually turn this while loop into an endless loop instead of waiting for user response it'll would skip right over it. Could someone with the time explain this to me what would make it behave like this int pts1, pts2, pts3,...
7
1286
by: Trev | last post by:
Hey, I just want to know (i know i should know this) how can i show up another form? I have 2 forms and the first one has a button on it that when i click the button i want the second form to show up (and the first form disappear), and i also want another button on the second form to return to the first form.
7
1392
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
29
3586
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
4
1515
by: Doug Handler | last post by:
Hi, I've developed a plug-in application that basically is constructed of 2 pieces 1). the "main framework" 2). channels. Using Reflection in the Main Framework, i dynamically load Channels. This works fine and great. My problem is w/ menus. Each Channel returns a Popup Menu (based on an interface requirement) that gets placed in the Main Framework's main menubar. Again, works fine. However, what i'm unsure of is the eventhandler...
6
6244
by: Padhu Vinirs | last post by:
JDK 1.4 on WinXP. I have 2 threads started from the main thread. I would like to print some status of the child threads from the main thread periodically. But I dont see the main thread to print out anything when the child threads are running. The child threads do sleep after some work. Instead should I make the child threads wait() and make the main thread notifyAll() after printing ? public class MainThread { WorkerThread t1, t2;...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10360
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.