ridjis

FaktorijelPipe

May 19th, 2016
591
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MODULE demo;
  2.  
  3. FROM SYSTEM IMPORT
  4.   ADR;
  5. FROM StrIO IMPORT
  6.   WriteString, WriteLn;
  7. FROM NumberIO IMPORT
  8.   WriteInt, ReadInt;
  9. FROM lib IMPORT
  10.   fork, pipe, dup, read, write, close;
  11. FROM errno IMPORT
  12.   geterrno;
  13.  
  14. VAR
  15.   pid: INTEGER;
  16.   n: INTEGER;
  17.   i, rezultat: LONGINT;
  18.   pfd: ARRAY [0..1] OF INTEGER;
  19.   ulaz, izlaz: INTEGER;
  20.  
  21. BEGIN
  22.  
  23.   WriteString("Unesite broj za koji zelite da izracunam faktorijel: ");
  24.   ReadInt(n);
  25.  
  26.   i:= n;
  27.   pid:= 0;
  28.   izlaz := -1;
  29.   WHILE (pid = 0) AND (i > 0) DO
  30.  
  31.     pipe(pfd);
  32.     pid:= fork();
  33.  
  34.     IF (pid = 0) THEN
  35.       DEC(i);
  36.       IF izlaz # -1 THEN
  37.         close(izlaz);
  38.       END;
  39.       izlaz := dup(pfd[1]);
  40.     ELSIF (pid > 0) THEN
  41.       ulaz := dup(pfd[0]);
  42.     ELSE
  43.       WriteString("Greska."); WriteLn;
  44.     END;
  45.     close(pfd[0]);
  46.     close(pfd[1]);
  47.   END;
  48.  
  49.   IF (pid # 0) THEN
  50.     read(ulaz, ADR(rezultat), SIZE(rezultat));
  51.     rezultat := rezultat * i;
  52.     close(ulaz);
  53.   ELSE
  54.     rezultat := 1;
  55.   END;
  56.  
  57.   IF (i = n) THEN
  58.     WriteInt(n, 0); WriteString("! = "); WriteInt(rezultat, 0); WriteLn;
  59.   ELSE
  60.     write(izlaz, ADR(rezultat), SIZE(rezultat));
  61.     close(izlaz);
  62.   END;
  63.  
  64. END demo.
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×