Issue
I am having a program with 1 parent process and 3 children process. And I want to calculate run time of all children processes.
int run_time[3]; // Variable to save the running time of the children process
time_t start[3]; // Variable to help measure the actual time
for ( i = 0; i < 3; i++ )
{
if( fork() == 0 ) // 3 Children process running
{
start[i] = time(NULL); // Start time of child process
usleep(1000000);
run_time[i] = time(NULL) - start[i]; // Calculate run time
printf("Running time: %d from child\n",run_time[i]);
exit(0);
}
}
for ( i2 = 0; i2 < 3; i2++ ) // Waiting all 3 children process finish
waitpid(-1, NULL, 0);
for ( i3 = 0; i3 < 3; i3++ ) // Printing out run time of children from parent process
printf("Running time: %d from parent\n",run_time[i3]);
As I know that I can not save the calculate data from child process (run_time[]
in my code) to the parent process, even with global variable and pointer (I tried). There is only one way which is using pipe()
. Something like this: int fd[2]
and then pipe(fd)
. But then I can not use pipe()
for more than 1 child process. So I would like to as if there is another way to calculate run time of child process without using pipe()
? And how can I use pipe()
for multiple children process.
Solution
If second-level granularity is sufficient for you and the run-time is expected to be on the order of seconds, you can encapsulate the runtime in the return code of the child process, like this:
// return the runtime as an 8-bit integer to the parent
exit(run_time[i] & 0xff);
Then, in parent process, use the macro WEXITSTATUS
to get the exit code. See the documentation of the wait()
system call:
If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().
Since the return code is an 8-bit integer, this will only work for values in up to 255 seconds.
As for pipes, if you already know how to communicate with a single child and want to communicate with multiple children, simply use an array of pipes.
A modified version of your program follows:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char **argv) {
int run_time[3]; // Variable to save the running time of the children process
time_t start[3]; // Variable to help measure the actual time
pid_t children[3];
int status;
int i;
for ( i = 0; i < 3; i++ ) {
children[i] = fork();
if( children[i] == 0 ) {
start[i] = time(NULL); // Start time of child process
usleep(1000000);
run_time[i] = time(NULL) - start[i]; // Calculate run time
printf("Running time: %d from child\n",run_time[i]);
// return the runtime as an 8-bit integer
exit(run_time[i] & 0xff);
}
}
for ( i = 0; i < 3; i++ ) { // Waiting all 3 children process finish
waitpid(children[i], &status, 0);
if (WIFEXITED(status)) {
run_time[i] = WEXITSTATUS(status); // use the low-order 8 bits from the exit code
} else {
run_time[i] = -1; // unknown run time
}
}
for ( i = 0; i < 3; i++ ) // Printing out run time of children from parent process
printf("Running time: %d from parent\n", run_time[i]);
}
Answered By - kfx Answer Checked By - Mary Flores (WPSolving Volunteer)