Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODULE demo;
- FROM SYSTEM IMPORT
- ADR;
- FROM StrIO IMPORT
- WriteString, WriteLn;
- FROM NumberIO IMPORT
- WriteInt, ReadInt;
- FROM lib IMPORT
- fork, pipe, dup, read, write, close;
- FROM errno IMPORT
- geterrno;
- VAR
- pid: INTEGER;
- n: INTEGER;
- i, rezultat: LONGINT;
- pfd: ARRAY [0..1] OF INTEGER;
- ulaz, izlaz: INTEGER;
- BEGIN
- WriteString("Unesite broj za koji zelite da izracunam faktorijel: ");
- ReadInt(n);
- i:= n;
- pid:= 0;
- izlaz := -1;
- WHILE (pid = 0) AND (i > 0) DO
- pipe(pfd);
- pid:= fork();
- IF (pid = 0) THEN
- DEC(i);
- IF izlaz # -1 THEN
- close(izlaz);
- END;
- izlaz := dup(pfd[1]);
- ELSIF (pid > 0) THEN
- ulaz := dup(pfd[0]);
- ELSE
- WriteString("Greska."); WriteLn;
- END;
- close(pfd[0]);
- close(pfd[1]);
- END;
- IF (pid # 0) THEN
- read(ulaz, ADR(rezultat), SIZE(rezultat));
- rezultat := rezultat * i;
- close(ulaz);
- ELSE
- rezultat := 1;
- END;
- IF (i = n) THEN
- WriteInt(n, 0); WriteString("! = "); WriteInt(rezultat, 0); WriteLn;
- ELSE
- write(izlaz, ADR(rezultat), SIZE(rezultat));
- close(izlaz);
- END;
- END demo.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement