Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <string.h>
- enum
- {
- NUM_CNT = 3,
- };
- void
- outpt(int id, int num)
- {
- char *buf;
- sprintf(buf, "%d %d\n", id, num * num);
- write(STDOUT_FILENO, buf, strlen(buf) * sizeof(buf[0]));
- fflush(stdout);
- }
- int
- main(int argc, char *argv[])
- {
- int arr[NUM_CNT];
- for (int i = 0; i < NUM_CNT; ++i) {
- scanf("%d", &arr[i]);
- }
- int pid;
- if ((pid = fork()) < 0) {
- return 1;
- } else if (!pid) {
- outpt(1, arr[0]);
- return 0;
- } else {
- if ((pid = fork()) < 0) {
- return 1;
- } else if (!pid) {
- outpt(2, arr[1]);
- return 0;
- } else {
- if ((pid = fork()) < 0) {
- return 1;
- } else if (!pid) {
- outpt(3, arr[2]);
- return 0;
- } else {
- wait(NULL);
- wait(NULL);
- wait(NULL);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment