Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting a process using posix_spawn

Tags:

c++

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";

Application did launches but says no command line argument. What is the error in posix_spawn arguments?

like image 514
Banti Kumar Avatar asked Dec 19 '22 22:12

Banti Kumar


1 Answers

From the posix_spawn manual page:

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.

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.

so change:

 char *argV[] = {"192.168.1.40",(char *) 0};

to:

char *argV[] = {"/home/user/application", "192.168.1.40",(char *) 0};

The behaviour of the argv array is mentioned more explicitly later on:

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;

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.

like image 162
Petesh Avatar answered Jan 04 '23 16:01

Petesh