where command line arguments are stored?

2019-02-18 04:50发布

问题:

I have a doubt @ the storage of command line arguments.

myprog.exe -cfgfile myconfig.cfg

commandline args are passed when process gets created so are they strored outside the process?

where OS stores it?

回答1:

For WIndows, the command line arguments are kept in the process environment block (PEB), which is allocated in the user process address space when the process is created.

You can read Windows Internals for a lot more details. Here's a snippet from Chapter 5 - Processes, Threads, and Jobs.

I would assume that it's the same for the Unix flavors. This data needs to be in the process memory, so that it can be accessed by the process itself.



回答2:

It depends on the OS and possibly the language. A good C-centric answer is that the OS creates the process space (including loading the code, creating the heap and stack, etc). Then it puts the command line argument vector in a location, and then copies the address of the argument vector to 'argv' on the stack, and the count of words to 'argc'.

Only after these tasks are done does the OS allow the process to execute.



回答3:

The command line arguments are stored in the application's memory space. Exactly where differs from OS to OS, my guess is it usually goes at the bottom of the heap. The code which puts it there is in the kernel source code for exec on Unix-like OSs, not sure where it would be in Windows (not that you can see the source anyway). The C runtime code (this is where "crt" comes from) takes argv and argc from the stack and then calls main. If you are interested in learning more how an executable starts up in Linux, this paper by Ulrich Drepper (glibc maintainer) may be of value: http://people.redhat.com/drepper/dsohowto.pdf



回答4:

in Linux : command line arguments will get stored in stack. dont confuse environmental variable with command line args, process address space have separate memory area for environmental variables