Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <zconf.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <memory.h>
- #include <sys/wait.h>
- #include <ctype.h>
- int main(int argc, char *argv[]) {
- enum { BUF_SIZE = 16 };
- int fd0[2];
- pipe(fd0);
- pid_t p1 = fork();
- if (p1 < 0) {
- return 1;
- } else if (p1 == 0) {
- int fd1[2];
- pipe(fd1);
- pid_t p2 = fork();
- if (p2 < 0) {
- return 1;
- } else if (p2 == 0) {
- close(fd0[0]);
- close(fd1[1]);
- if (dup2(fd1[0], 0) < 0) {
- return 1;
- }
- if (dup2(fd0[1], 1) < 0) {
- return 1;
- }
- close(fd1[0]);
- close(fd0[1]);
- execlp(argv[3], argv[3], NULL);
- return 1;
- } else {
- close(fd0[0]);
- close(fd0[1]);
- close(fd1[0]);
- int count = atoi(argv[1]);
- int mod = atoi(argv[2]);
- for (int i = 1; i <= count; i++) {
- dprintf(fd1[1], "%llu\n", (unsigned long long) (i * i) % mod);
- }
- close((fd1[1]));
- wait(NULL);
- return 0;
- }
- } else {
- close(fd0[1]);
- }
- pid_t p3 = fork();
- if (p3 < 0) {
- return 1;
- } else if (p3 == 0) {
- pid_t p4 = fork();
- if (p4 < 0) {
- return 1;
- } else if (p4 == 0) {
- close(fd0[1]);
- dup2(fd0[0], 0);
- close(fd0[0]);
- char buf[BUF_SIZE] = {0};
- while (fgets(buf, BUF_SIZE, stdin) != NULL) {
- for (int i = 0; i < BUF_SIZE && buf[i] != '\0'; i++) {
- if (isspace(buf[i])) {
- buf[i] = '\n';
- }
- }
- printf("%s", buf);
- fflush(stdout);
- }
- exit(0);
- } else {
- close(fd0[0]);
- close(fd0[1]);
- wait(NULL);
- }
- } else {
- close(fd0[0]);
- wait(NULL);
- wait(NULL);
- printf("0\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement