I need help in printing process tree just right from the code. Thanks to stackoverflow community I wrote a program which creates several process using fork() function and now I need to print on the screen a process tree using execlp() function.
int main()
{
int t = 5;
int mainPID = getpid();
cout << "Main process: " << mainPID << endl << endl;
int pid;
if ((pid = fork()) == -1) return -1;
if (pid == 0)
{
cout << "source for child process ";
}
else{
cout << "source for parent process ";
}
sleep(t);
return 0;
}
While I run program and on another instance of terminal type
pstree /mainPID/
I get tree which starts printing from mainPID. I need to print that tree from the code, but when put in code
execlp("pstree", "pstree", "-c", "-p", (int *)NULL);
I get printed tree from all system
execlp("pstree", "pstree", mainPID, "-c", "-p", (int *)NULL);
prints nothing.