where command line arguments are stored?

青春壹個敷衍的年華 提交于 2019-12-30 10:16:30

问题


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



来源:https://stackoverflow.com/questions/2115301/where-command-line-arguments-are-stored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!