У меня есть три процесса в цепочке: P1 → P2 → P3. Я хотел бы иметь возможность распечатать идентификатор всех трех из процесса дочернего процесса (P3).
Итак, мой вопрос: как получить pid великого родителя (P1) великого ребенка (P3) с помощью getppid() и т.д.?
Или мне придётся прибегнуть к хранению pid каждого из своих переменных для последующего использования (не желаемого)?
Спасибо за любую помощь, которую вы можете мне дать. Кроме того, только потому, что вот мой код:
//test_wait.cpp
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
using namespace std;
int main()
{
pid_t pid; //process id
const char *message;
int n;
int exit_code;
std::stack<int> proc_stack;
cout << "Fork program starting\n";
proc_stack.push(getpid());
pid = fork();
switch ( pid ) {
case -1:
cout << "Fork failure!\n";
return 1;
case 0:
pid = fork();
switch ( pid ) {
case -1:
cout << "Fork Failure!\n";
return 1;
case 0:
cout << "Grandchild PID: " << getpid() << endl;
cout << "Parent PID: " << getppid() << endl;
cout << "Grand Parent PID: " << proc_stack.top() << endl;
exit_code = 9;
break;
default:
exit_code = 0;
break;
}
default:
exit_code = 0;
break;
}
//waiting for child to finish
if ( pid != 0 ) { //parent
int stat_val;
pid_t child_pid;
child_pid = wait ( &stat_val ); //wait for child
}
exit ( exit_code );
}
Итак, я закончил использование стека для хранения информации. Как видно из приведенного выше кода.
Вам нужно напечатать pid и ppid, когда в case 0:
ребенком case 0:
см. Этот код.
//test_wait.cpp
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
pid_t pid; //process id
const char *message;
int n;
int exit_code;
cout << "fork program starting\n";
pid = fork();
switch ( pid ) {
case -1:
cout << "Fork failure!\n";
return 1;
case 0:
cout << "Child1 finished: PID = " << getpid() << endl;
cout << "Parent PID = " << getppid() << endl;
pid = fork();
switch ( pid ) {
case -1:
cout << "Fork Failure!\n";
return 1;
case 0:
cout << "Child2 finished: PID = " << getpid() << endl;
cout << "Parent PID = " << getppid() << endl;
message = "This is the child\n";
n = 5;
exit_code = 9;
break;
default:
message = "This is the parent\n";
n = 3;
exit_code = 0;
break;
}
default:
message = "This is the grand parent\n";
n = 3;
exit_code = 0;
break;
}
for ( int i = 0; i < n; ++i ) {
cout << message;
sleep ( 1 );
}
//waiting for child to finish
if ( pid != 0 ) { //parent
int stat_val;
pid_t child_pid;
child_pid = wait ( &stat_val ); //wait for child
}
exit ( exit_code );
}
Также вы можете получить ppid из /proc
cat/proc/pid/stat, например, для PID # 20467:
cat /proc/20467/stat
20467 (abc) S 20137 20467 20125 34818 20467 4202496 1930 5196 22 0 113 162 32 25 20 0 1 0 1701252 14548992 1606 4294967295 4194304 6492552 2076847264 2076842300 694388884 0 0 0 134889479 2151083916 0 0 17 0 0 0 221 0 0
20137 в вышеупомянутом (4-ом поле) является PPID.
/proc
(какpstree
). Нет API типа POSIXy для получения родительского родительского элемента.cout << fflush
перед каждымfork