0

I want to launch a new process from a given string of input (including parameter).
I tried to use system() but it appears that in the child process, getppid() is not the same as the caller in some runs (probably because system uses the shell as command interpreter).

However, if I fork and then use exec(), it is complicated to parse the parameter.

Anyone knows a way to get around this problem? Or is there any library that helps parse a string of input into program parameters like shells often do?

Thank you very much in advance.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by nezachem
1

I think this is what you want:

main.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define APP_PATH "/path_to_testit/testit"

int main(int argc, char**argv)
{
	if (fork())
	{
		fprintf(stdout, "parent pid->%d\n", getpid());
	}
	else
	{
		execlp(APP_PATH, APP_PATH, "some value");
	}
	exit(EXIT_SUCCESS);
}

testit.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char**argv)
{
	fprintf(stdout, "we passed->%s\n", argv[1]);
	fprintf(stdout, "client's parent->%d\n", getppid());
	exit(EXIT_SUCCESS);
}
0
execlp(APP_PATH, APP_PATH, "some value");

Don't forget to null-terminate the arglist:

execlp(APP_PATH, APP_PATH, "some value", 0);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.