pstree in c ++ program

1

The process tree should be shown at the beginning of the execution, at the moment in which all processes are created and at the end of the program. (pstree -s)

This requirement in bold is part of the development of this code below, the code is working, but I do not know how to display the tree in that process. I put the command system (pstree -s pidPai) but it does not show the tree of this process, but the tree of all system processes, test with printf, sprinf, fprintf but none works, does anyone know me tell how to use this command required to display the tree of each process of the program?

#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>

using namespace std;
void
proc_neto1(int dtnasc) {
int num_segundos = 0;
while (true) {
    sleep(1);
    num_segundos ++;
    cout << "Sou o processo neto 1 (PID " << getpid() << "), estou rodando   há " << num_segundos << " segundos." << endl;
}
}

void
proc_neto2(int dtnasc) {
int num_segundos = 0;
while (true) {
    sleep(1);
    num_segundos ++;
    cout << "Sou o processo neto 2 (PID " << getpid() << "), estou rodando há " << num_segundos << " segundos." << endl;
    if (dtnasc + num_segundos == 60) {
        cout << "Neto 2 ficou louco! Matou seu pai (PID " << getppid() << ")" << endl;
        kill(getppid(), SIGKILL);
    }
    if (dtnasc + num_segundos == 63) {
        cout << "Não aguentando a pressão, neto 2 se suicida." << endl;
        kill(getpid(), SIGKILL);
    }
}
}

void
proc_filho1(int dtnasc) {
int num_segundos = 0;
int pid_filho = 0;

while (true) {
    sleep(1);
    num_segundos ++;
    cout << "Sou o processo filho 1 (PID " << getpid() << "), estou rodando há " << num_segundos << " segundos." << endl;
    if (num_segundos == 15 && ! (pid_filho = fork())) proc_neto1(dtnasc + num_segundos);
    if (dtnasc + num_segundos == 50) {
        cout << "Filho 1 ficou louco! Matou seu pai (PID = " << getppid() << ")" << endl;
        kill(getppid(), SIGKILL);
    }
    if (dtnasc + num_segundos == 55) {
        cout << "Filho 1 continua louco! Matou seu filho (PID " << pid_filho << ")" << endl;
        kill(pid_filho, SIGKILL);
    }
    if (dtnasc + num_segundos == 57) {
        cout << "Não aguentando a pressão, filho 1 se suicida." << endl;
        kill(getpid(), SIGKILL);
    }
}
}

void
proc_filho2(int dtnasc) {
int num_segundos = 0;

while (true) {
    sleep(1);
    num_segundos ++;
    cout << "Sou o processo filho 2 (PID " << getpid() << "), estou rodando há " << num_segundos << " segundos." << endl;
    if (num_segundos == 15 && ! fork()) proc_neto2(dtnasc + num_segundos);
}
}

int
main(int argc, char ** argv) {
int num_segundos = 0;
int pidPai = getpid();
system("pstree -s pidPai");

while (true) {
    sleep(1);
    num_segundos ++;
    //int pidPai = getpid();
    cout << "Sou o processo pai (PID " << pidPai << "), estou rodando há " << num_segundos << " segundos." << endl;

    // aos 10 segundos, gera o filho 1.
    if (num_segundos == 10 && ! fork()) proc_filho1(num_segundos);
    // aos 20 segundos, gera o filho 2.
    if (num_segundos == 20 && ! fork()) proc_filho2(num_segundos);
}

return 0;
}
    
asked by anonymous 23.06.2017 / 21:38

1 answer

0

What happens is that you are literally running the pstree -s pidPai command while the right thing is to replace the word "pidPai" with the PID number of the current process. Here's the fix:

int pidPai = getpid();
system(string("pstree -s " + to_string(pidPai)).c_str());

Do not forget to include the header string at the beginning of the code.

    
26.07.2017 / 01:27