Я нашел кого-то на другом сайте с подобной проблемой, и я обновил его решение в соответствии с моим делом. Это может быть решением:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
int status = 0;
printf("I am: %d\n\n", (int)getpid());
pid_t pid = fork(); // fork a child
if(pid == 0)
{
printf("Hi I'm process %d and my parent is %d\n",getpid(),getppid());
exit(1);
}
else
{
pid_t childPid = wait(&status);
pid_t pid = fork(); // fork a child
if (pid == 0)
{
printf("Hi I'm process %d and my parent is %d.\n", getpid(), getppid());
pid = fork(); // fork a child
if (pid == 0)
{
printf("Hi I'm process %d and my parent is %d.\n",getpid(),getppid());
pid = fork();
if(pid == 0)
{
printf("Hi I m process %d and my parent is %d\n", getpid(), getppid());
exit(4);
}
else
{
pid = fork();
if(pid==0)
{
printf("Hi i m process %d and my parent is %d\n", getpid(), getppid());
exit(5);
}
else
{
pid = fork();
if(pid == 0)
{
printf("Hi i m process %d and my parent is %d\n", getpid(), getppid());
exit(6);
}
}
}
exit(3);
}
exit(2);
}
}
return 0;
}