Загрузка данных


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

void print_status(int pid, int status)
{
    if (WIFEXITED(status))
        printf("%d: exit %d\n", pid, WEXITSTATUS(status));
    else if (WIFSIGNALED(status))
        printf("%d: signal %d\n", pid, WTERMSIG(status));
}

int main(int argc, char *argv[])
{
    if (argc != 6) {
        printf("%s prog1 arg1 prog2 arg2 p|s\n", argv[0]);
        return 1;
    }

    int status, pid;

    if (argv[5][0] == 'p') {
        for (int i = 0; i < 2; i++) {
            if (fork() == 0) {
                char *args[] = { argv[1 + 2 * i], argv[2 + 2 * i], NULL };
                execv(args[0], args);
                perror("execv");
                exit(127);
            }
        }
        for (int i = 0; i < 2; i++) {
            pid = wait(&status);
            print_status(pid, status);
        }
    } else {
        for (int i = 0; i < 2; i++) {
            pid = fork();
            if (pid == 0) {
                execl(argv[1 + 2 * i], argv[1 + 2 * i], argv[2 + 2 * i], NULL);
                perror("execl");
                exit(127);
            }
            waitpid(pid, &status, 0);
            print_status(pid, status);
        }
    }

    return 0;
}