0

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.

Shrikanth N
  • 652
  • 3
  • 17
Beginner
  • 5
  • 4

2 Answers2

0

execlp takes char *const argv[] as rest of arguments ...

So try to convert "mainPID" to char[]

This worked in my case.

B. Caesar
  • 395
  • 3
  • 9
-1

you should convert mainPID to string or char * and then try this.

  • 1
    This would be better served as a comment rather than an answer. Please review https://stackoverflow.com/help/how-to-answer – mitchellJ Nov 22 '19 at 16:36