AAE-CNAS-Labs/Topic-4-Forking-2/task_3_2.c

40 lines
739 B
C
Raw Permalink Normal View History

//
// Written for Computer Networks and Systems lab classes
// AUTHOR : Sergiusz Warga
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/wait.h>
int child_task(int a, int b) {
return a + b;
}
int main() {
srandom(time(NULL));
int a = random() % 11;
int b = random() % 11 + 20;
int sum = 0;
pid_t pid;
pid = fork();
if (pid == -1) { // If something went wrong.
perror("Fork: ");
exit(EXIT_FAILURE);
}
if (pid == 0) { // If this is a child process.
sum = child_task(a, b);
exit(sum);
}
wait(&sum);
while (!(WIFEXITED(sum)));
printf("%d\n", WEXITSTATUS(sum));
return 0;
}