使用posix_spawn启动进程 [英] Starting a process using posix_spawn
问题描述
我正在使用以下代码在Linux中启动新进程
i am using the folowing code to launch the new process in Linux
pid_t processID;
char *argV[] = {"192.168.1.40",(char *) 0};
int status = -1;
status = posix_spawn(&processID,"/home/user/application",NULL,NULL,argV,environ);
if(status == 0)
std::cout<<"Launched Application";
else
std::cout<<"Launching application Failed";
应用程序确实启动,但是没有命令行参数. posix_spawn参数有什么错误?
Application did launches but says no command line argument. What is the error in posix_spawn arguments?
推荐答案
在posix_spawn
手册页中:
自变量argv是指向以null终止的字符数组的指针,该数组指向以null终止的字符串.这些字符串构成参数列表以供新进程使用.数组中至少必须存在argv [0],并且应包含要生成的程序的文件名,例如路径或文件参数的最后一个组成部分.
The argument argv is a pointer to a null-terminated array of character pointers to null-terminated character strings. These strings construct the argument list to be made available to the new process. At least argv[0] must be present in the array, and should contain the file name of the program being spawned, e.g. the last component of the path or file argument.
正在发生的是,在启动的过程中,argv[0]
将是192.168.1.40
而不是可执行文件的名称,并且此后没有程序的参数.
What's happening is that in the launched process, argv[0]
will be 192.168.1.40
instead of the name of the executable, and there are no arguments to the program after that.
所以改变:
char *argV[] = {"192.168.1.40",(char *) 0};
收件人:
char *argV[] = {"/home/user/application", "192.168.1.40",(char *) 0};
稍后将更明确地提及argv数组的行为:
The behaviour of the argv array is mentioned more explicitly later on:
当通过posix_spawn()或posix_spawnp()调用执行程序时,其输入如下:
When a program is executed as a result of a posix_spawn() or posix_spawnp() call, it is entered as follows:
main(argc, argv, envp)
int argc;
char **argv, **envp;
其中argc是argv中的元素数("arg计数"),而argv指向指向参数本身的字符指针数组.
where argc is the number of elements in argv (the ''arg count'') and argv points to the array of character pointers to the arguments themselves.
这篇关于使用posix_spawn启动进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!