Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- enum
- {
- AMOUNT_OF_CHILDREN = 3,
- STRING_SIZE = 8,
- INTEGER_BASE = 10
- };
- void
- support(int cond)
- {
- char string[STRING_SIZE];
- if (read(0, string, sizeof(string)) !=sizeof(string)) {
- _exit(1);
- }
- int num = strtol(string, 0, INTEGER_BASE);
- printf("%d %d\n", cond, num * num);
- fflush(stdout);
- }
- int
- main(void)
- {
- pid_t ppid, pid;
- pid_t arr[AMOUNT_OF_CHILDREN] = {0};
- ppid = getpid();
- for (int i = 0; i < AMOUNT_OF_CHILDREN; ++i) {
- if (getpid() == ppid) {
- pid = fork();
- if (pid == 0) {
- arr[i] = getpid();
- }
- }
- }
- if (getpid() == ppid) {
- while (wait(NULL) > 0) {}
- _exit(0);
- } else {
- for (int i = 1; i <= AMOUNT_OF_CHILDREN; ++i) {
- if (getpid() == arr[i-1]) {
- support(i);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement