Answer the question
In order to leave comments, you need to log in
How to start several processes in sequence, and pass the exit code of the last child to the first process?
Good evening. Tell me how to complete the task correctly?
It is necessary to create a chain of 5 running processes in sequence, with each child process becoming a parent for the next child. Pass the exit code of the last child to the first process. In the first process, display the given number on the screen.
Created 5 processes in a cycle:
pid_t return_value;
for(int i = 0; i < 5; i++)
{
return_value = fork();
printf("%s %d %s %d\n", "Процесс: ", getpid(), "PID: ", return_value);
}
return 0;
Answer the question
In order to leave comments, you need to log in
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
pid_t root_pid = getpid();
for (int i = 0; i < 5; ++i) {
if (fork() != 0) {
int status;
wait(&status);
int exit_status = WEXITSTATUS(status);
if (root_pid == getpid()) {
printf("%d\n", exit_status);
return 0;
} else {
return exit_status;
}
}
}
return 28;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question