У меня есть этот пример fork () Мне нужно сделать трассировку!
#include <unistd.h>
int main(void) {
int i;
for (i=0; i<3; i++)
if (fork())
wait(NULL);
return 0;
}
Мое решение для выполнения трассировки, что-то не так, я не знаю, как сделать трассировку:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
int i,pid;
for(i=0;i<3;i++) {
pid=fork();
if(pid>0) ///if is father (true)
printf("PID After the fork child === %d & father === %d (i = %d) \n\n",(int)getpid,(int)getppid,i);
wait(NULL);
printf(" After the wait child=== %d & father=== %d (i = %d)|||\n",(int)getpid,(int)getppid,i);
}
}
Это то, что я получаю в Терминале.Я ясно вижу, что это не правильно, но я не знаю, как решить эту проблему:
PID After the fork child === 134513568 & father === 134513632 (i = 0)
After the wait child=== 134513568 & father=== 134513632 (i = 0)|||
PID After the fork child === 134513568 & father === 134513632 (i = 1)
After the wait child=== 134513568 & father=== 134513632 (i = 2)|||
After the wait child=== 134513568 & father=== 134513632 (i = 2)|||
After the wait child=== 134513568 & father=== 134513632 (i = 1)|||
PID After the fork child === 134513568 & father === 134513632 (i = 2)
After the wait child=== 134513568 & father=== 134513632 (i = 2)|||
After the wait child=== 134513568 & father=== 134513632 (i = 2)|||
After the wait child=== 134513568 & father=== 134513632 (i = 0)|||
PID After the fork child === 134513568 & father === 134513632 (i = 1)
After the wait child=== 134513568 & father=== 134513632 (i = 1)|||
PID After the fork child === 134513568 & father === 134513632 (i = 2)
After the wait child=== 134513568 & father=== 134513632 (i = 2)|||
After the wait child=== 134513568 & father=== 134513632 (i = 2)|||
After the wait child=== 134513568 & father=== 134513632 (i = 1)|||
PID After the fork child === 134513568 & father === 134513632 (i = 2)
After the wait child=== 134513568 & father=== 134513632 (i = 2)|||
After the wait child=== 134513568 & father=== 134513632 (i = 2)|||
Спасибо.